Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
['StorageUnit', 'p_store', 'bus', -1], ['Line', 's', 'bus0', -1],
['Line', 's', 'bus1', 1], ['Transformer', 's', 'bus0', -1],
['Transformer', 's', 'bus1', 1], ['Link', 'p', 'bus0', -1],
['Link', 'p', 'bus1', get_as_dense(n, 'Link', 'efficiency', sns)]]
args = [arg for arg in args if not n.df(arg[0]).empty]
for i in additional_linkports(n):
eff = get_as_dense(n, 'Link', f'efficiency{i}', sns)
args.append(['Link', 'p', f'bus{i}', eff])
lhs = (pd.concat([bus_injection(*arg) for arg in args], axis=1)
.groupby(axis=1, level=0)
.agg(lambda x: ''.join(x.values))
.reindex(columns=n.buses.index, fill_value=''))
sense = '='
rhs = ((- get_as_dense(n, 'Load', 'p_set', sns) * n.loads.sign)
.groupby(n.loads.bus, axis=1).sum()
.reindex(columns=n.buses.index, fill_value=0))
define_constraints(n, lhs, sense, rhs, 'Bus', 'marginal_price')
def bus_injection(c, attr, groupcol='bus', sign=1):
# additional sign only necessary for branches in reverse direction
if 'sign' in n.df(c):
sign = sign * n.df(c).sign
expr = linexpr((sign, get_var(n, c, attr))).rename(columns=n.df(c)[groupcol])
# drop empty bus2, bus3 if multiline link
if c == 'Link':
expr.drop(columns='', errors='ignore', inplace=True)
return expr
# one might reduce this a bit by using n.branches and lookup
args = [['Generator', 'p'], ['Store', 'p'], ['StorageUnit', 'p_dispatch'],
['StorageUnit', 'p_store', 'bus', -1], ['Line', 's', 'bus0', -1],
['Line', 's', 'bus1', 1], ['Transformer', 's', 'bus0', -1],
['Transformer', 's', 'bus1', 1], ['Link', 'p', 'bus0', -1],
['Link', 'p', 'bus1', get_as_dense(n, 'Link', 'efficiency', sns)]]
args = [arg for arg in args if not n.df(arg[0]).empty]
for i in additional_linkports(n):
eff = get_as_dense(n, 'Link', f'efficiency{i}', sns)
args.append(['Link', 'p', f'bus{i}', eff])
lhs = (pd.concat([bus_injection(*arg) for arg in args], axis=1)
.groupby(axis=1, level=0)
.agg(lambda x: ''.join(x.values))
.reindex(columns=n.buses.index, fill_value=''))
sense = '='
rhs = ((- get_as_dense(n, 'Load', 'p_set', sns) * n.loads.sign)
.groupby(n.loads.bus, axis=1).sum()
.reindex(columns=n.buses.index, fill_value=0))
define_constraints(n, lhs, sense, rhs, 'Bus', 'marginal_price')
def define_objective(n, sns):
"""
Defines and writes out the objective function
"""
# constant for already done investment
nom_attr = nominal_attrs.items()
constant = 0
for c, attr in nom_attr:
ext_i = get_extendable_i(n, c)
constant += n.df(c)[attr][ext_i] @ n.df(c).capital_cost[ext_i]
object_const = write_bound(n, constant, constant)
n.objective_f.write(linexpr((-1, object_const), as_pandas=False)[0])
for c, attr in lookup.query('marginal_cost').index:
cost = (get_as_dense(n, c, 'marginal_cost', sns)
.loc[:, lambda ds: (ds != 0).all()]
.mul(n.snapshot_weightings[sns], axis=0))
if cost.empty: continue
terms = linexpr((cost, get_var(n, c, attr).loc[sns, cost.columns]))
n.objective_f.write(join_exprs(terms))
# investment
for c, attr in nominal_attrs.items():
cost = n.df(c)['capital_cost'][get_extendable_i(n, c)]
if cost.empty: continue
terms = linexpr((cost, get_var(n, c, attr)[cost.index]))
n.objective_f.write(join_exprs(terms))
coeff_var = [(-1, soc),
(-1/eff_dispatch * eh, get_var(n, c, 'p_dispatch')),
(eff_store * eh, get_var(n, c, 'p_store'))]
lhs, *axes = linexpr(*coeff_var, return_axes=True)
def masked_term(coeff, var, cols):
return linexpr((coeff[cols], var[cols]))\
.reindex(index=axes[0], columns=axes[1], fill_value='').values
if ('StorageUnit', 'spill') in n.variables.index:
lhs += masked_term(-eh, get_var(n, c, 'spill'), spill.columns)
lhs += masked_term(eff_stand, prev_soc_cyclic, cyclic_i)
lhs += masked_term(eff_stand.loc[sns[1:]], soc.shift().loc[sns[1:]], noncyclic_i)
rhs = -get_as_dense(n, c, 'inflow', sns).mul(eh)
rhs.loc[sns[0], noncyclic_i] -= n.df(c).state_of_charge_initial[noncyclic_i]
define_constraints(n, lhs, '==', rhs, c, 'mu_state_of_charge')
expr = linexpr((sign, get_var(n, c, attr))).rename(columns=n.df(c)[groupcol])
# drop empty bus2, bus3 if multiline link
if c == 'Link':
expr.drop(columns='', errors='ignore', inplace=True)
return expr
# one might reduce this a bit by using n.branches and lookup
args = [['Generator', 'p'], ['Store', 'p'], ['StorageUnit', 'p_dispatch'],
['StorageUnit', 'p_store', 'bus', -1], ['Line', 's', 'bus0', -1],
['Line', 's', 'bus1', 1], ['Transformer', 's', 'bus0', -1],
['Transformer', 's', 'bus1', 1], ['Link', 'p', 'bus0', -1],
['Link', 'p', 'bus1', get_as_dense(n, 'Link', 'efficiency', sns)]]
args = [arg for arg in args if not n.df(arg[0]).empty]
for i in additional_linkports(n):
eff = get_as_dense(n, 'Link', f'efficiency{i}', sns)
args.append(['Link', 'p', f'bus{i}', eff])
lhs = (pd.concat([bus_injection(*arg) for arg in args], axis=1)
.groupby(axis=1, level=0)
.agg(lambda x: ''.join(x.values))
.reindex(columns=n.buses.index, fill_value=''))
sense = '='
rhs = ((- get_as_dense(n, 'Load', 'p_set', sns) * n.loads.sign)
.groupby(n.loads.bus, axis=1).sum()
.reindex(columns=n.buses.index, fill_value=0))
define_constraints(n, lhs, sense, rhs, 'Bus', 'marginal_price')
df[attr] = duals
n.duals = Dict()
n.dualvalues = pd.DataFrame(index=sp, columns=['in_comp', 'pnl'])
# extract shadow prices attached to components
for c, attr in sp:
map_dual(c, attr)
# discard remaining if wanted
if not keep_references:
for c, attr in n.constraints.index.difference(sp):
get_con(n, c, attr, pop)
#load
if len(n.loads):
set_from_frame(n.pnl('Load'), 'p', get_as_dense(n, 'Load', 'p_set', sns))
#clean up vars and cons
for c in list(n.vars):
if n.vars[c].df.empty and n.vars[c].pnl == {}: n.vars.pop(c)
for c in list(n.cons):
if n.cons[c].df.empty and n.cons[c].pnl == {}: n.cons.pop(c)
# recalculate injection
ca = [('Generator', 'p', 'bus' ), ('Store', 'p', 'bus'),
('Load', 'p', 'bus'), ('StorageUnit', 'p', 'bus'),
('Link', 'p0', 'bus0'), ('Link', 'p1', 'bus1')]
for i in additional_linkports(n):
ca.append(('Link', f'p{i}', f'bus{i}'))
sign = lambda c: n.df(c).sign if 'sign' in n.df(c) else -1 #sign for 'Link'
n.buses_t.p = pd.concat(
predefined = False
n.sols[c] = n.sols[c] if c in n.sols else Dict(df=pd.DataFrame(), pnl={})
n.solutions.at[(c, attr), 'in_comp'] = predefined
if isinstance(variables, pd.DataFrame):
# case that variables are timedependent
n.solutions.at[(c, attr), 'pnl'] = True
pnl = n.pnl(c) if predefined else n.sols[c].pnl
values = variables.stack().map(variables_sol).unstack()
if c in n.passive_branch_components:
set_from_frame(pnl, 'p0', values)
set_from_frame(pnl, 'p1', - values)
elif c == 'Link':
set_from_frame(pnl, 'p0', values)
for i in ['1'] + additional_linkports(n):
i_eff = '' if i == '1' else i
eff = get_as_dense(n, 'Link', f'efficiency{i_eff}', sns)
set_from_frame(pnl, f'p{i}', - values * eff)
else:
set_from_frame(pnl, attr, values)
else:
# case that variables are static
n.solutions.at[(c, attr), 'pnl'] = False
sol = variables.map(variables_sol)
if predefined:
non_ext = n.df(c)[attr]
n.df(c)[attr + '_opt'] = sol.reindex(non_ext.index).fillna(non_ext)
else:
n.sols[c].df[attr] = sol