Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Features/refine investments #753

Draft
wants to merge 5 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/oemof/solph/constraints/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@
from .flow_count_limit import limit_active_flow_count_by_keyword # noqa: F401
from .integral_limit import emission_limit # noqa: F401
from .integral_limit import generic_integral_limit # noqa: F401
from .investment_limit import additional_investment_flow_limit # noqa: F401
from .investment_limit import investment_limit # noqa: F401
from .shared_limit import shared_limit # noqa: F401
125 changes: 55 additions & 70 deletions src/oemof/solph/constraints/investment_limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,7 @@
from pyomo import environ as po


def investment_limit(model, limit=None):
r"""Set an absolute limit for the total investment costs of an investment
optimization problem:

.. math:: \sum_{investment\_costs} \leq limit

Parameters
----------
model : oemof.solph.Model
Model to which the constraint is added
limit : float
Absolute limit of the investment (i.e. RHS of constraint)
"""

def investment_rule(m):
expr = 0

if hasattr(m, "InvestmentFlow"):
expr += m.InvestmentFlow.investment_costs

if hasattr(m, "GenericInvestmentStorageBlock"):
expr += m.GenericInvestmentStorageBlock.investment_costs
return expr <= limit

model.investment_limit = po.Constraint(rule=investment_rule)

return model


def additional_investment_flow_limit(model, keyword, limit=None):
def investment_limit(model, keyword=None, limit=None):
r"""
Global limit for investment flows weighted by an attribute keyword.

Expand All @@ -59,31 +30,32 @@ def additional_investment_flow_limit(model, keyword, limit=None):
----------
model : oemof.solph.Model
Model to which constraints are added.
keyword : attribute to consider
keyword : need to consider (None: ep_costs)
All flows with Investment attribute containing the keyword will be
used.
limit : numeric
Global limit of keyword attribute for the energy system.

**Constraint**

.. math:: \sum_{i \in IF} P_i \cdot w_i \leq limit
.. math:: \sum_{i} P_i \cdot w_i \leq limit

With `IF` being the set of InvestmentFlows considered for the integral
limit.
Where :math:`i` being all investments (for keyword=None)
or being the set of InvestmentFlows considered.

The symbols used are defined as follows
(with Variables (V) and Parameters (P)):

+---------------+---------------------------------------+------+--------------------------------------------------------------+
| symbol | attribute | type | explanation |
+===============+=======================================+======+==============================================================+
| :math:`P_{i}` | :py:obj:`InvestmentFlow.invest[i, o]` | V | installed capacity of investment flow |
+---------------+---------------------------------------+------+--------------------------------------------------------------+
| :math:`w_i` | :py:obj:`keyword` | P | weight given to investment flow named according to `keyword` |
+---------------+---------------------------------------+------+--------------------------------------------------------------+
| :math:`limit` | :py:obj:`limit` | P | global limit given by keyword `limit` |
+---------------+---------------------------------------+------+--------------------------------------------------------------+
+---------------+---------------------------------------+------+--------------------------------------------------+
| symbol | attribute | type | explanation |
+===============+=======================================+======+==================================================+
| :math:`P_{i}` | :py:obj:`InvestmentFlow.invest[i, o]` | V | installed capacity of investment flow |
+---------------+---------------------------------------+------+--------------------------------------------------+
| :math:`w_i` | :py:obj:`keyword` | P | weight given to investment flow named according |
| | | | to `keyword`, :math:`w_i = 1` if keyword is None |
+---------------+---------------------------------------+------+--------------------------------------------------+
| :math:`limit` | :py:obj:`limit` | P | global limit given by keyword `limit` |
+---------------+---------------------------------------+------+--------------------------------------------------+

Note
----
Expand All @@ -105,36 +77,49 @@ def additional_investment_flow_limit(model, keyword, limit=None):
... investment=solph.Investment(ep_costs=100, space=1))})
>>> es.add(bus, sink, src1, src2)
>>> model = solph.Model(es)
>>> model = solph.constraints.additional_investment_flow_limit(
... model, "space", limit=1500)
>>> model = solph.constraints.investment_limit(model, "space", limit=1500)
>>> a = model.solve(solver="cbc")
>>> int(round(model.invest_limit_space()))
1500
""" # noqa: E501
invest_flows = {}

for (i, o) in model.flows:
if hasattr(model.flows[i, o].investment, keyword):
invest_flows[(i, o)] = model.flows[i, o].investment

limit_name = "invest_limit_" + keyword

setattr(
model,
limit_name,
po.Expression(
expr=sum(
model.InvestmentFlow.invest[inflow, outflow]
* getattr(invest_flows[inflow, outflow], keyword)
for (inflow, outflow) in invest_flows
)
),
)

setattr(
model,
limit_name + "_constraint",
po.Constraint(expr=(getattr(model, limit_name) <= limit)),
)
if keyword is None:
def investment_rule(m):
expr = 0

if hasattr(m, "InvestmentFlow"):
expr += m.InvestmentFlow.investment_costs

if hasattr(m, "GenericInvestmentStorageBlock"):
expr += m.GenericInvestmentStorageBlock.investment_costs
return expr <= limit

model.investment_limit = po.Constraint(rule=investment_rule)

else:
invest_flows = {}

for (i, o) in model.flows:
if keyword in model.flows[i, o].investment.other_needs:
invest_flows[(i, o)] = model.flows[i, o].investment

limit_name = "invest_limit_" + keyword

setattr(
model,
limit_name,
po.Expression(
expr=sum(
model.InvestmentFlow.invest[inflow, outflow]
* invest_flows[inflow, outflow].other_needs[keyword]
for (inflow, outflow) in invest_flows
)
),
)

setattr(
model,
limit_name + "_constraint",
po.Constraint(expr=(getattr(model, limit_name) <= limit)),
)

return model
6 changes: 6 additions & 0 deletions src/oemof/solph/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Investment:
`minimum` defines the threshold for the invested capacity.
ep_costs : float, :math:`c_{invest,var}`
Equivalent periodical costs for the investment per flow capacity.
other_needs : dict
existing : float, :math:`P_{exist}` or :math:`E_{exist}`
Existing / installed capacity. The invested capacity is added on top
of this value. Not applicable if `nonconvex` is set to `True`.
Expand Down Expand Up @@ -54,6 +55,7 @@ def __init__(
existing=0,
nonconvex=False,
offset=0,
other_needs=None,
**kwargs,
):

Expand All @@ -63,6 +65,10 @@ def __init__(
self.existing = existing
self.nonconvex = nonconvex
self.offset = offset
if other_needs is not None:
self.other_needs = other_needs
else:
self.other_needs = {}

for attribute in kwargs.keys():
value = kwargs.get(attribute)
Expand Down
10 changes: 5 additions & 5 deletions tests/constraint_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,8 @@ def test_generic_invest_limit(self):
label="source_0",
outputs={
bus: solph.Flow(
investment=solph.Investment(ep_costs=50, space=4)
investment=solph.Investment(ep_costs=50,
other_needs={"space": 4})
)
},
)
Expand All @@ -633,7 +634,8 @@ def test_generic_invest_limit(self):
label="source_1",
outputs={
bus: solph.Flow(
investment=solph.Investment(ep_costs=100, space=1)
investment=solph.Investment(ep_costs=100,
other_needs={"space": 1})
)
},
)
Expand All @@ -647,9 +649,7 @@ def test_generic_invest_limit(self):

om = self.get_om()

om = solph.constraints.additional_investment_flow_limit(
om, "space", limit=20
)
om = solph.constraints.investment_limit(om, "space", limit=20)

self.compare_lp_files("generic_invest_limit.lp", my_om=om)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ def test_optimise_storage_size(
invest_relation_output_capacity=1 / 6,
inflow_conversion_factor=1,
outflow_conversion_factor=0.8,
investment=solph.Investment(ep_costs=epc, existing=6851),
investment=solph.Investment(ep_costs=epc,
existing=6851),
)

# Solve model
Expand Down