Skip to content

Commit

Permalink
Added economic indicators to variables
Browse files Browse the repository at this point in the history
  • Loading branch information
ddceruti committed Oct 28, 2024
1 parent 2b54cdb commit 5790763
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 43 deletions.
3 changes: 1 addition & 2 deletions tests/sts.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ def test_sts_forced(request):
assert result.solver.status == pyo.SolverStatus.ok
# assert that the objective value is less than 2% away from the expected
# value
assert abs(pyo.value(model.obj)) == approx(4.6259e+06, rel=0.02)
# assert (abs(pyo.value(model.obj)) - 4.6259e+06) < 0.02 * 4.6259e+06
assert abs(pyo.value(model.obj)) == approx(519676.4358105995, rel=0.02)


def test_sts_eco(request):
Expand Down
89 changes: 48 additions & 41 deletions topotherm/single_timestep.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,57 +247,64 @@ def total_energy_cons(m, t):
mdl.set_t, rule=total_energy_cons,
doc='Total energy conservation')

def objective_function(m):
fuel = sum(
sum(m.P_source[k, t]
* economics.source_price[k]
* matrices['flh_source'][k, t]
for k in m.set_n_p)
for t in mdl.set_t)

# CAREFUL HARDCODED FOR 0 TIME STEPS
def pipes_fix(k):
return ((m.P['ij', 'in', k, 0] + m.P['ji', 'in', k, 0])
* regression_inst['a'])

def pipes_var(k):
return (regression_inst['b']
* (m.lambda_['ij', k] + m.lambda_['ji', k]))

pipes = (sum(((pipes_fix(k) + pipes_var(k))
* matrices['l_i'][k])
for k in m.set_n_i)
* annuity(economics.pipes_c_irr,
economics.pipes_lifetime))

source = sum(m.P_source_inst[k]
* economics.source_c_inv[k]
* annuity(economics.source_c_irr[k],
economics.source_lifetime[k])
for k in m.set_n_p)

if optimization_mode == "economic":
revenue = (sum(
mdl.revenue = pyo.Var(doc='Revenue')
mdl.revenue_constr = pyo.Constraint(
expr=mdl.revenue == (
sum(
sum(
sum(m.lambda_['ij', sets['a_i_in'][j].item()]
sum(mdl.lambda_['ij', sets['a_i_in'][j].item()]
* matrices['flh_consumer'][k, t]
* matrices['q_c'][k, t]
for k in sets['a_c_out'][j]
if len(sets['a_i_in'][j]) > 0)
+ sum(
(m.lambda_['ji', sets['a_i_out'][j].item()])
(mdl.lambda_['ji', sets['a_i_out'][j].item()])
* matrices['flh_consumer'][k, t]
* matrices['q_c'][k, t]
for k in sets['a_c_out'][j]
if len(sets['a_i_out'][j]) > 0)
for j in mdl.set_n)
for t in mdl.set_t)
* economics.heat_price * (-1))
else:
revenue = 0

return fuel + pipes + source + revenue
for t in mdl.set_t) * economics.heat_price * (-1)),
doc='Revenue constraint')

mdl.opex_source = pyo.Var(doc='OPEX Source')
mdl.opex_source_constr = pyo.Constraint(
expr=mdl.opex_source == sum(
sum(mdl.P_source[k, t]
* economics.source_price[k]
* matrices['flh_source'][k, t]
for k in mdl.set_n_p)
for t in mdl.set_t),
doc='OPEX Source constraint')

mdl.capex_pipes = pyo.Var(doc='CAPEX Pipe')

# CAREFUL HARDCODED FOR 0 TIME STEPS
def pipes_fix(k):
return ((mdl.P['ij', 'in', k, 0] + mdl.P['ji', 'in', k, 0])
* regression_inst['a'])

def pipes_var(k):
return (regression_inst['b']
* (mdl.lambda_['ij', k] + mdl.lambda_['ji', k]))

mdl.capex_pipe_constr = pyo.Constraint(
expr=mdl.capex_pipes == (sum(
(pipes_fix(k) + pipes_var(k)) * matrices['l_i'][k]
for k in mdl.set_n_i)
* annuity(economics.pipes_c_irr, economics.pipes_lifetime)),
doc='CAPEX Pipe constraint')

mdl.capex_source = pyo.Var(doc='CAPEX Source')
mdl.capex_source_constr = pyo.Constraint(
expr=mdl.capex_source == sum(mdl.P_source_inst[k]
* economics.source_c_inv[k]
* annuity(economics.source_c_irr[k],
economics.source_lifetime[k])
for k in mdl.set_n_p),
doc='CAPEX Source constraint')

mdl.obj = pyo.Objective(rule=objective_function,
doc='Objective function')
mdl.obj = pyo.Objective(
expr=mdl.capex_source + mdl.capex_pipes + mdl.opex_source + mdl.revenue,
doc='Objective function')
return mdl

0 comments on commit 5790763

Please sign in to comment.