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

Allow for fixing investments #1007

Open
wants to merge 47 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
b001fe6
Introduce draft for fixing investments (yet untested)
jokochems Oct 31, 2023
63e449f
Reformat using black
jokochems Oct 31, 2023
ebe0fa2
Revise architecture and fix erroneous indexing
jokochems Nov 1, 2023
9ae659f
Add / refactor test(s)
jokochems Nov 1, 2023
670a534
Add fixes
jokochems Nov 1, 2023
d66e556
Fix black issues
jokochems Nov 1, 2023
11a7fdb
Add further tests
jokochems Nov 1, 2023
a0a8ea3
Add lp files
jokochems Nov 1, 2023
bdc49ae
Add minor black fix
jokochems Nov 1, 2023
d67d4cf
Fix import
jokochems Nov 1, 2023
2bfa151
Add test for newly introduced error message
jokochems Nov 1, 2023
93752f4
Rename to address line length issue
jokochems Nov 1, 2023
74ffe1c
Satisfy our picky CI
jokochems Nov 1, 2023
3dfeb76
Alter import order
jokochems Nov 1, 2023
427d461
Fix imports once more
jokochems Nov 1, 2023
ce68649
Allow for fixing investments in GenericStorage and SinkDSM
jokochems Nov 1, 2023
fac2b97
Extend test to all DSM modelling approaches
jokochems Nov 1, 2023
2c9766a
Extend docs and changelog
jokochems Nov 1, 2023
536dd91
Add constraint test for fixed storage investment
jokochems Nov 1, 2023
95f53c4
Add fix
jokochems Nov 1, 2023
944a049
Add tests for dsm units
jokochems Nov 1, 2023
21e766f
Merge branch 'dev' into features/fix-investment-results-in-repeated-s…
jokochems Dec 8, 2023
1c5b64a
Introduce draft for fixing investments (yet untested)
jokochems Oct 31, 2023
9953714
Reformat using black
jokochems Oct 31, 2023
3d9d334
Revise architecture and fix erroneous indexing
jokochems Nov 1, 2023
011471b
Add / refactor test(s)
jokochems Nov 1, 2023
8893b11
Add fixes
jokochems Nov 1, 2023
d3c78aa
Fix black issues
jokochems Nov 1, 2023
4d8a002
Add further tests
jokochems Nov 1, 2023
4401b9a
Add lp files
jokochems Nov 1, 2023
cb6b9f8
Add minor black fix
jokochems Nov 1, 2023
817d092
Fix import
jokochems Nov 1, 2023
b8b1f2e
Add test for newly introduced error message
jokochems Nov 1, 2023
296934d
Rename to address line length issue
jokochems Nov 1, 2023
71808ce
Satisfy our picky CI
jokochems Nov 1, 2023
4a6a5cf
Alter import order
jokochems Nov 1, 2023
19a98b2
Fix imports once more
jokochems Nov 1, 2023
a1f9692
Allow for fixing investments in GenericStorage and SinkDSM
jokochems Nov 1, 2023
d412725
Extend test to all DSM modelling approaches
jokochems Nov 1, 2023
a2add52
Extend docs and changelog
jokochems Nov 1, 2023
f611d86
Add constraint test for fixed storage investment
jokochems Nov 1, 2023
759a219
Add fix
jokochems Nov 1, 2023
72980a9
Add tests for dsm units
jokochems Nov 1, 2023
10a6f70
Merge remote-tracking branch 'upstream/features/fix-investment-result…
jokochems Dec 8, 2023
5b535fd
Allow for rounding in repeated solving
jokochems Dec 8, 2023
ba35e54
Add / fix tests
jokochems Dec 8, 2023
91669ff
Alter import order
jokochems Dec 8, 2023
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
18 changes: 17 additions & 1 deletion src/oemof/solph/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,9 @@ class Model(BaseModel):
InvestNonConvexFlowBlock,
]

def __init__(self, energysystem, discount_rate=None, **kwargs):
def __init__(
self, energysystem, discount_rate=None, **kwargs
):
if discount_rate is not None:
self.discount_rate = discount_rate
elif (
Expand All @@ -383,6 +385,7 @@ def __init__(self, energysystem, discount_rate=None, **kwargs):
self._set_discount_rate_with_warning()
else:
pass
self._fix_investments = False
super().__init__(energysystem, **kwargs)

def _set_discount_rate_with_warning(self):
Expand Down Expand Up @@ -521,3 +524,16 @@ def _add_parent_block_variables(self):
if (o, i) in self.UNIDIRECTIONAL_FLOWS:
for p, t in self.TIMEINDEX:
self.flow[o, i, p, t].setlb(0)

def fix_investments(self):
"""Fix investment results of an already solved model"""
if self.solver_results is None:
msg = (
"Cannot fix investments as model has not yet been solved!\n"
"You have to first solve your model and then call method "
"`fix_investments()` on your model instance."
)
raise ValueError(msg)
self._fix_investments = True
if hasattr(self, "InvestmentFlowBlock"):
self.InvestmentFlowBlock.fix_investments_results()
13 changes: 13 additions & 0 deletions src/oemof/solph/flows/_investment_flow_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
attribute investment and the associated source (s) and target (t)
of flow e.g. groups=[(s1, t1, f1), (s2, t2, f2),..]
"""
m = self.parent_block()
Fixed Show fixed Hide fixed
if group is None:
return None

Expand Down Expand Up @@ -1175,3 +1176,15 @@
self.maximum_rule_build = BuildAction(rule=_max_invest_rule)

return self.maximum_rule

def fix_investments_results(self):
"""Fix investments if `_fix_investments` is set to True for model"""
m = self.parent_block()
for (i, o) in self.INVESTFLOWS:
for p in m.PERIODS:
self.invest[i, o, p].fix()
self.total[i, o, p].fix()
if m.es.periods is not None:
self.old[i, o, p].fix()
self.old_end[i, o, p].fix()
self.old_exo[i, o, p].fix()
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from oemof.solph import processing
Fixed Show fixed Hide fixed
from test_multi_period_investment_model import (
set_up_multi_period_investment_model,
)


def test_multi_period_investment_fixed(solver="cbc"):
"""test fixing investments in a repeated solve"""
om = set_up_multi_period_investment_model(approach="DLR")
om.receive_duals()
om.solve(solver=solver)

assert om.solver_results is not None
om.fix_investments()
om.solve(solver=solver)
for (i, o) in om.InvestmentFlowBlock.INVESTFLOWS:
for p in om.PERIODS:
assert om.InvestmentFlowBlock.invest[i, o, p].fixed
assert om.InvestmentFlowBlock.total[i, o, p].fixed
assert om.InvestmentFlowBlock.old[i, o, p].fixed
assert om.InvestmentFlowBlock.old_end[i, o, p].fixed
assert om.InvestmentFlowBlock.old_exo[i, o, p].fixed
Loading
Loading