eris-echo 2.3.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- echo/__init__.py +1 -0
- echo/configuration.py +178 -0
- echo/constants.py +8 -0
- echo/exceptions.py +16 -0
- echo/functional.py +83 -0
- echo/models/__init__.py +0 -0
- echo/models/agnostic/__init__.py +50 -0
- echo/models/agnostic/aggregation.py +64 -0
- echo/models/agnostic/base.py +84 -0
- echo/models/agnostic/bounded.py +43 -0
- echo/models/agnostic/controlled.py +97 -0
- echo/models/agnostic/flex.py +61 -0
- echo/models/agnostic/input_output.py +30 -0
- echo/models/agnostic/storage.py +307 -0
- echo/models/agnostic/tellegen/__init__.py +6 -0
- echo/models/agnostic/tellegen/base.py +62 -0
- echo/models/agnostic/tellegen/multi_commodity.py +50 -0
- echo/models/agnostic/tellegen/partitioned_multi_commodity.py +88 -0
- echo/models/agnostic/tellegen/three_way_valve.py +122 -0
- echo/models/agnostic/time_delay.py +34 -0
- echo/models/agnostic/time_varying.py +134 -0
- echo/models/base/__init__.py +21 -0
- echo/models/base/edge.py +128 -0
- echo/models/base/node.py +160 -0
- echo/models/base/optimisation_graph.py +546 -0
- echo/models/base/path.py +66 -0
- echo/models/base/port.py +564 -0
- echo/models/base/transform.py +186 -0
- echo/models/base/types.py +7 -0
- echo/models/carbon/__init__.py +4 -0
- echo/models/carbon/aggregation.py +33 -0
- echo/models/carbon/base.py +25 -0
- echo/models/electrical/__init__.py +35 -0
- echo/models/electrical/base.py +37 -0
- echo/models/electrical/ev/__init__.py +14 -0
- echo/models/electrical/ev/base.py +340 -0
- echo/models/electrical/ev/deprecated.py +273 -0
- echo/models/electrical/ev/ev_with_profile.py +137 -0
- echo/models/electrical/ev/v0g.py +264 -0
- echo/models/electrical/ev/v1g.py +85 -0
- echo/models/electrical/ev/v2g.py +84 -0
- echo/models/electrical/generation.py +49 -0
- echo/models/electrical/inverter.py +97 -0
- echo/models/gas/__init__.py +5 -0
- echo/models/gas/base.py +18 -0
- echo/models/gas/boiler_fixedcop.py +65 -0
- echo/models/gas/boiler_tempcontrolled.py +96 -0
- echo/models/prebuilt/__init__.py +17 -0
- echo/models/prebuilt/battery.py +36 -0
- echo/models/prebuilt/diesel_generator.py +57 -0
- echo/models/prebuilt/flex.py +36 -0
- echo/models/prebuilt/load.py +14 -0
- echo/models/prebuilt/scaled_solar.py +19 -0
- echo/models/prebuilt/solar.py +13 -0
- echo/models/scenario.py +63 -0
- echo/models/thermal/__init__.py +15 -0
- echo/models/thermal/base.py +117 -0
- echo/models/thermal/parameterised_chiller.py +228 -0
- echo/models/thermal/parameterised_heatpump.py +496 -0
- echo/models/thermal/simple_chiller.py +139 -0
- echo/models/thermal/simple_heatpump.py +291 -0
- echo/models/thermal/simple_heatpump_dualoutput.py +195 -0
- echo/models/thermal/storage.py +307 -0
- echo/objectives/__init__.py +0 -0
- echo/objectives/base.py +99 -0
- echo/objectives/contingency.py +158 -0
- echo/objectives/power.py +132 -0
- echo/objectives/tariff.py +694 -0
- echo/optimiser.py +460 -0
- echo/py.typed +0 -0
- echo/tracker.py +74 -0
- echo/utils.py +506 -0
- echo/validators.py +290 -0
- echo/visualization.py +229 -0
- eris_echo-2.3.0.dist-info/METADATA +127 -0
- eris_echo-2.3.0.dist-info/RECORD +79 -0
- eris_echo-2.3.0.dist-info/WHEEL +5 -0
- eris_echo-2.3.0.dist-info/licenses/LICENSE.txt +22 -0
- eris_echo-2.3.0.dist-info/top_level.txt +1 -0
echo/models/base/port.py
ADDED
|
@@ -0,0 +1,564 @@
|
|
|
1
|
+
from __future__ import annotations # Deprecating in python 3.15 in favour of lazy annotations (PEP 649 and 749)
|
|
2
|
+
|
|
3
|
+
from collections.abc import Callable
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
import numpy as np
|
|
7
|
+
import pandas as pd
|
|
8
|
+
import pyomo.environ as en
|
|
9
|
+
import shortuuid
|
|
10
|
+
from attrs import define, field, validators
|
|
11
|
+
from pyomo.core import RangeSet
|
|
12
|
+
from pyomo.core.expr.relational_expr import EqualityExpression, InequalityExpression
|
|
13
|
+
|
|
14
|
+
from echo.configuration import FlowConstraint, Flows, OptimisationType, Units
|
|
15
|
+
from echo.constants import negative_variable_component, positive_variable_component
|
|
16
|
+
from echo.exceptions import ConfigurationError
|
|
17
|
+
from echo.models.base.types import ConstraintValueType, InitialValue, InitialValueInput
|
|
18
|
+
from echo.models.scenario import EchoConcreteModel
|
|
19
|
+
from echo.utils import (
|
|
20
|
+
TimeSeriesData,
|
|
21
|
+
domain_from_flow,
|
|
22
|
+
expand_as_dict,
|
|
23
|
+
generate_array_constraint,
|
|
24
|
+
set_var_bounds_from_dict,
|
|
25
|
+
to_initial_values,
|
|
26
|
+
)
|
|
27
|
+
from echo.validators import (
|
|
28
|
+
export_constraint_validator,
|
|
29
|
+
import_constraint_validator,
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@define
|
|
34
|
+
class Port:
|
|
35
|
+
uid: str = field(factory=shortuuid.uuid)
|
|
36
|
+
port_name: str = field()
|
|
37
|
+
|
|
38
|
+
@port_name.default
|
|
39
|
+
def _set_port_name_default(self) -> str:
|
|
40
|
+
return "port_" + str(self.uid)
|
|
41
|
+
|
|
42
|
+
units: Units = Units.NA # Used to ensure that common units are being optimised over at points of interconnection
|
|
43
|
+
|
|
44
|
+
initial_value: dict[tuple[int, int], float] | float = field(default=0)
|
|
45
|
+
|
|
46
|
+
initial_value_ref: str | None = None # string ref to df column
|
|
47
|
+
initial_value_scaling: int | None = None # scaling factor for initial values
|
|
48
|
+
flow_type: OptimisationType = OptimisationType.NA
|
|
49
|
+
|
|
50
|
+
flows: Flows = Flows.NA # What flow directions are possible (import, export, both)
|
|
51
|
+
# Used to define the nature of import / export directions and constraints
|
|
52
|
+
import_constraint: FlowConstraint = FlowConstraint.NA
|
|
53
|
+
import_constraint_value: ConstraintValueType | None = field(
|
|
54
|
+
default=None, validator=[validators.instance_of(ConstraintValueType | None), import_constraint_validator]
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
export_constraint: FlowConstraint = FlowConstraint.NA
|
|
58
|
+
export_constraint_value: ConstraintValueType | None = field(
|
|
59
|
+
default=None, validator=[validators.instance_of(ConstraintValueType | None), export_constraint_validator]
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
active_periods: dict[tuple[int, int], Any] | None = None
|
|
63
|
+
slack: bool = False
|
|
64
|
+
objective: en.numeric_expr.NumericExpression | float = 0 # this will eventually be a pyomo expression
|
|
65
|
+
allow_dangling_port: bool = False
|
|
66
|
+
|
|
67
|
+
@property
|
|
68
|
+
def pos(self) -> str:
|
|
69
|
+
return positive_variable_component + self.port_name
|
|
70
|
+
|
|
71
|
+
@property
|
|
72
|
+
def neg(self) -> str:
|
|
73
|
+
return negative_variable_component + self.port_name
|
|
74
|
+
|
|
75
|
+
@property
|
|
76
|
+
def is_pos(self) -> str:
|
|
77
|
+
return f"is_pos_{self.port_name}"
|
|
78
|
+
|
|
79
|
+
@property
|
|
80
|
+
def import_con_val(self) -> str:
|
|
81
|
+
return f"import_con_val_{self.port_name}"
|
|
82
|
+
|
|
83
|
+
@property
|
|
84
|
+
def export_con_val(self) -> str:
|
|
85
|
+
return f"export_con_val_{self.port_name}"
|
|
86
|
+
|
|
87
|
+
@property
|
|
88
|
+
def import_slack(self) -> str:
|
|
89
|
+
return f"import_slack_{self.port_name}"
|
|
90
|
+
|
|
91
|
+
@property
|
|
92
|
+
def import_slack_max(self) -> str:
|
|
93
|
+
return f"import_slack_max_{self.port_name}"
|
|
94
|
+
|
|
95
|
+
@property
|
|
96
|
+
def export_slack(self) -> str:
|
|
97
|
+
return f"export_slack_{self.port_name}"
|
|
98
|
+
|
|
99
|
+
@property
|
|
100
|
+
def export_slack_max(self) -> str:
|
|
101
|
+
return f"export_slack_max_{self.port_name}"
|
|
102
|
+
|
|
103
|
+
def set_flow_constraints(
|
|
104
|
+
self,
|
|
105
|
+
max_import: ConstraintValueType | None,
|
|
106
|
+
max_export: ConstraintValueType | None,
|
|
107
|
+
slack: bool | None = False,
|
|
108
|
+
) -> None:
|
|
109
|
+
"""Sets the values of port flow constraints.
|
|
110
|
+
|
|
111
|
+
Args:
|
|
112
|
+
max_import: max allowable import into port (float, array, or None)
|
|
113
|
+
max_export: max allowable export out of port (float, array, or None)
|
|
114
|
+
slack: bool, whether we want to allow slack in the constraint
|
|
115
|
+
"""
|
|
116
|
+
if max_import is not None:
|
|
117
|
+
self.import_constraint = FlowConstraint.Fixed
|
|
118
|
+
self.import_constraint_value = max_import
|
|
119
|
+
|
|
120
|
+
if max_export is not None:
|
|
121
|
+
self.export_constraint = FlowConstraint.Fixed
|
|
122
|
+
self.export_constraint_value = max_export
|
|
123
|
+
|
|
124
|
+
if slack is not None:
|
|
125
|
+
self.slack = slack
|
|
126
|
+
|
|
127
|
+
def process_initial_value(
|
|
128
|
+
self,
|
|
129
|
+
initial_val: InitialValueInput | str,
|
|
130
|
+
expansion_periods: int = 1,
|
|
131
|
+
time_periods: int | None = None,
|
|
132
|
+
) -> None:
|
|
133
|
+
if isinstance(initial_val, dict):
|
|
134
|
+
self.set_initial_value(initial_val)
|
|
135
|
+
|
|
136
|
+
elif isinstance(initial_val, str):
|
|
137
|
+
self.initial_value_ref = initial_val
|
|
138
|
+
|
|
139
|
+
elif isinstance(initial_val, list | np.ndarray):
|
|
140
|
+
self.set_initial_value_from_array(
|
|
141
|
+
array=initial_val,
|
|
142
|
+
expansion_periods=expansion_periods,
|
|
143
|
+
time_periods=time_periods,
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
def verify_port(self) -> None:
|
|
147
|
+
"""Used to verify that a port has been set up appropriately"""
|
|
148
|
+
if self.flows is Flows.NA:
|
|
149
|
+
raise ConfigurationError("The flows value cannot be set to a value of NA.")
|
|
150
|
+
|
|
151
|
+
if (self.flows is Flows.Import) or (self.flows is Flows.Both):
|
|
152
|
+
if self.import_constraint is FlowConstraint.NA:
|
|
153
|
+
raise ConfigurationError("The Import FlowConstraint cannot be set to a value of NA.")
|
|
154
|
+
if self.import_constraint is FlowConstraint.Fixed and self.import_constraint_value is None:
|
|
155
|
+
raise ConfigurationError(
|
|
156
|
+
"The Import flow constraint value cannot be set to None when an Import constraint exists."
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
if (self.flows is Flows.Export) or (self.flows is Flows.Both):
|
|
160
|
+
if self.export_constraint is FlowConstraint.NA:
|
|
161
|
+
raise ConfigurationError("The Export FlowConstraint cannot be set to a value of NA.")
|
|
162
|
+
if self.export_constraint is FlowConstraint.Fixed and self.export_constraint_value is None:
|
|
163
|
+
raise ConfigurationError(
|
|
164
|
+
"The Export flow constraint value cannot be set to None when an Export constraint exists."
|
|
165
|
+
)
|
|
166
|
+
|
|
167
|
+
if self.flow_type is OptimisationType.NA:
|
|
168
|
+
raise ConfigurationError("The Optimisation Type has to be configured before instantiation.")
|
|
169
|
+
|
|
170
|
+
if self.units is Units.NA:
|
|
171
|
+
raise ConfigurationError("The Units parameter has to be configured before instantiation.")
|
|
172
|
+
|
|
173
|
+
def _add_flow_variable_to_model(
|
|
174
|
+
self,
|
|
175
|
+
model: EchoConcreteModel,
|
|
176
|
+
initial_value: float | Callable | None,
|
|
177
|
+
domain: RangeSet | Callable | None,
|
|
178
|
+
) -> None:
|
|
179
|
+
"""Adds the flow variable to the echo concrete model.
|
|
180
|
+
|
|
181
|
+
Args:
|
|
182
|
+
model: The echo concrete model for the flow variable to be added to.
|
|
183
|
+
initial_value: The initial value for the variable, or a rule that returns initial values.
|
|
184
|
+
domain : A Set that defines valid values for the variable (e.g., Reals, NonNegativeReals, Binary), or a
|
|
185
|
+
rule that returns Sets.
|
|
186
|
+
|
|
187
|
+
Returns:
|
|
188
|
+
None
|
|
189
|
+
"""
|
|
190
|
+
setattr(
|
|
191
|
+
model,
|
|
192
|
+
self.port_name,
|
|
193
|
+
en.Var(model.Expansion, model.Time, initialize=initial_value, domain=domain),
|
|
194
|
+
)
|
|
195
|
+
|
|
196
|
+
def _add_active_period_constraints_to_model(self, model: EchoConcreteModel) -> None:
|
|
197
|
+
if self.active_periods is None:
|
|
198
|
+
raise ValueError("self.active_periods cannot be None.")
|
|
199
|
+
|
|
200
|
+
port_active_periods = self.active_periods
|
|
201
|
+
|
|
202
|
+
def on_off_rule1(model: EchoConcreteModel, p: int, t: int) -> InequalityExpression:
|
|
203
|
+
return getattr(model, self.port_name)[p, t] <= port_active_periods[p, t] * model.big_m
|
|
204
|
+
|
|
205
|
+
def on_off_rule2(model: EchoConcreteModel, p: int, t: int) -> InequalityExpression:
|
|
206
|
+
return getattr(model, self.port_name)[p, t] >= -port_active_periods[p, t] * model.big_m
|
|
207
|
+
|
|
208
|
+
setattr(
|
|
209
|
+
model,
|
|
210
|
+
f"active_con1_{self.port_name}",
|
|
211
|
+
en.Constraint(model.Expansion, model.Time, rule=on_off_rule1),
|
|
212
|
+
)
|
|
213
|
+
setattr(
|
|
214
|
+
model,
|
|
215
|
+
f"active_con2_{self.port_name}",
|
|
216
|
+
en.Constraint(model.Expansion, model.Time, rule=on_off_rule2),
|
|
217
|
+
)
|
|
218
|
+
|
|
219
|
+
def _add_import_constraints_to_model(self, model: EchoConcreteModel) -> None:
|
|
220
|
+
# Add import constraint parameter
|
|
221
|
+
time_periods = len(model.Time)
|
|
222
|
+
exp_periods = len(model.Expansion)
|
|
223
|
+
# Generate an array of constraints (ie indexed by time and expansion period)
|
|
224
|
+
import_constraint_dict = generate_array_constraint(self.import_constraint_value, time_periods, exp_periods)
|
|
225
|
+
setattr(
|
|
226
|
+
model,
|
|
227
|
+
self.import_con_val,
|
|
228
|
+
en.Param(
|
|
229
|
+
model.Expansion,
|
|
230
|
+
model.Time,
|
|
231
|
+
initialize=import_constraint_dict,
|
|
232
|
+
domain=en.NonNegativeReals,
|
|
233
|
+
),
|
|
234
|
+
)
|
|
235
|
+
|
|
236
|
+
if self.slack:
|
|
237
|
+
self._add_slack_import_constraints_to_model(model=model)
|
|
238
|
+
else:
|
|
239
|
+
set_var_bounds_from_dict(model=model, var_name=self.port_name, ub=import_constraint_dict, lb=None)
|
|
240
|
+
|
|
241
|
+
def _add_slack_import_constraints_to_model(self, model: EchoConcreteModel) -> None:
|
|
242
|
+
"""Adds import capacity constraint with slack rules"""
|
|
243
|
+
|
|
244
|
+
# Add export capacity slack constraint
|
|
245
|
+
def import_cap_rule_slack(model: EchoConcreteModel, p: int, t: int) -> InequalityExpression:
|
|
246
|
+
return (
|
|
247
|
+
getattr(model, self.port_name)[p, t] + getattr(model, self.import_slack)[p, t]
|
|
248
|
+
<= getattr(model, self.import_con_val)[p, t]
|
|
249
|
+
)
|
|
250
|
+
|
|
251
|
+
con_name = "import_con_" + self.port_name
|
|
252
|
+
setattr(
|
|
253
|
+
model,
|
|
254
|
+
self.import_slack,
|
|
255
|
+
en.Var(
|
|
256
|
+
model.Expansion,
|
|
257
|
+
model.Time,
|
|
258
|
+
initialize=0,
|
|
259
|
+
domain=en.NonPositiveReals,
|
|
260
|
+
),
|
|
261
|
+
)
|
|
262
|
+
setattr(
|
|
263
|
+
model,
|
|
264
|
+
con_name,
|
|
265
|
+
en.Constraint(model.Expansion, model.Time, rule=import_cap_rule_slack),
|
|
266
|
+
)
|
|
267
|
+
|
|
268
|
+
# Add import capacity slack max constraint
|
|
269
|
+
def import_cap_slack_max_rule(model: EchoConcreteModel, p: int, t: int) -> InequalityExpression:
|
|
270
|
+
return getattr(model, self.import_slack)[p, t] >= getattr(model, self.import_slack_max)
|
|
271
|
+
|
|
272
|
+
con_name = "import_con_max_" + self.port_name
|
|
273
|
+
setattr(
|
|
274
|
+
model,
|
|
275
|
+
self.import_slack_max,
|
|
276
|
+
en.Var(initialize=0, domain=en.NonPositiveReals),
|
|
277
|
+
)
|
|
278
|
+
setattr(
|
|
279
|
+
model,
|
|
280
|
+
con_name,
|
|
281
|
+
en.Constraint(model.Expansion, model.Time, rule=import_cap_slack_max_rule),
|
|
282
|
+
)
|
|
283
|
+
|
|
284
|
+
def _add_export_constraints_to_model(self, model: EchoConcreteModel) -> None:
|
|
285
|
+
"""Add export constraints to the model."""
|
|
286
|
+
|
|
287
|
+
# Add export constraint parameter
|
|
288
|
+
time_periods = len(model.Time)
|
|
289
|
+
exp_periods = len(model.Expansion)
|
|
290
|
+
|
|
291
|
+
# Generate an array of constraints (ie indexed by time and expansion period)
|
|
292
|
+
export_constraint_dict = generate_array_constraint(self.export_constraint_value, time_periods, exp_periods)
|
|
293
|
+
setattr(
|
|
294
|
+
model,
|
|
295
|
+
self.export_con_val,
|
|
296
|
+
en.Param(
|
|
297
|
+
model.Expansion,
|
|
298
|
+
model.Time,
|
|
299
|
+
initialize=export_constraint_dict,
|
|
300
|
+
domain=en.NonPositiveReals,
|
|
301
|
+
),
|
|
302
|
+
)
|
|
303
|
+
|
|
304
|
+
if self.slack:
|
|
305
|
+
self._add_slack_export_constraints_to_model(model=model)
|
|
306
|
+
else:
|
|
307
|
+
set_var_bounds_from_dict(model=model, var_name=self.port_name, ub=None, lb=export_constraint_dict)
|
|
308
|
+
|
|
309
|
+
def _add_slack_export_constraints_to_model(self, model: EchoConcreteModel) -> None:
|
|
310
|
+
"""Adds import capacity constraint with slack rules"""
|
|
311
|
+
|
|
312
|
+
# Add export capacity slack constraint
|
|
313
|
+
def export_cap_rule_slack(model: EchoConcreteModel, p: int, t: int) -> InequalityExpression:
|
|
314
|
+
return (
|
|
315
|
+
getattr(model, self.port_name)[p, t] + getattr(model, self.export_slack)[p, t]
|
|
316
|
+
>= getattr(model, self.export_con_val)[p, t]
|
|
317
|
+
)
|
|
318
|
+
|
|
319
|
+
con_name = "export_con_" + self.port_name
|
|
320
|
+
setattr(
|
|
321
|
+
model,
|
|
322
|
+
self.export_slack,
|
|
323
|
+
en.Var(
|
|
324
|
+
model.Expansion,
|
|
325
|
+
model.Time,
|
|
326
|
+
initialize=0,
|
|
327
|
+
domain=en.NonNegativeReals,
|
|
328
|
+
),
|
|
329
|
+
)
|
|
330
|
+
setattr(
|
|
331
|
+
model,
|
|
332
|
+
con_name,
|
|
333
|
+
en.Constraint(model.Expansion, model.Time, rule=export_cap_rule_slack),
|
|
334
|
+
)
|
|
335
|
+
|
|
336
|
+
def export_cap_slack_max_rule(model: EchoConcreteModel, p: int, t: int) -> InequalityExpression:
|
|
337
|
+
"""Add export capacity slack max constraint"""
|
|
338
|
+
return getattr(model, self.export_slack)[p, t] <= getattr(model, self.export_slack_max)
|
|
339
|
+
|
|
340
|
+
con_name = "export_con_max_" + self.port_name
|
|
341
|
+
setattr(
|
|
342
|
+
model,
|
|
343
|
+
self.export_slack_max,
|
|
344
|
+
en.Var(initialize=0, domain=en.NonNegativeReals),
|
|
345
|
+
)
|
|
346
|
+
setattr(
|
|
347
|
+
model,
|
|
348
|
+
con_name,
|
|
349
|
+
en.Constraint(model.Expansion, model.Time, rule=export_cap_slack_max_rule),
|
|
350
|
+
)
|
|
351
|
+
|
|
352
|
+
def _determine_initial_value(
|
|
353
|
+
self,
|
|
354
|
+
time_periods: int,
|
|
355
|
+
expansion_periods: int,
|
|
356
|
+
profile: pd.DataFrame | None,
|
|
357
|
+
) -> InitialValue:
|
|
358
|
+
|
|
359
|
+
initial_value_scaling = self.initial_value_scaling or 1
|
|
360
|
+
|
|
361
|
+
if self.initial_value_ref is not None:
|
|
362
|
+
initial_val = to_initial_values(
|
|
363
|
+
profile,
|
|
364
|
+
self.initial_value_ref,
|
|
365
|
+
time_periods,
|
|
366
|
+
expansion_periods,
|
|
367
|
+
scaling=initial_value_scaling,
|
|
368
|
+
)
|
|
369
|
+
else:
|
|
370
|
+
# TODO: add scaling for explicit initial value
|
|
371
|
+
initial_val = self.initial_value
|
|
372
|
+
|
|
373
|
+
return initial_val
|
|
374
|
+
|
|
375
|
+
def add_port_to_model(self, model: EchoConcreteModel, profile: pd.DataFrame | None) -> None:
|
|
376
|
+
"""Creates pyomo vars, params, and constraints for the port."""
|
|
377
|
+
initial_value = self._determine_initial_value(
|
|
378
|
+
time_periods=len(model.Time),
|
|
379
|
+
expansion_periods=len(model.Expansion),
|
|
380
|
+
profile=profile,
|
|
381
|
+
)
|
|
382
|
+
|
|
383
|
+
domain = domain_from_flow(self.flows)
|
|
384
|
+
|
|
385
|
+
# Flow is always represented with a pyomo variable.
|
|
386
|
+
# This gives us flexibility for converting between a variable and parameter with fix/unfix
|
|
387
|
+
self._add_flow_variable_to_model(model=model, initial_value=initial_value, domain=domain)
|
|
388
|
+
|
|
389
|
+
# Convert flow variable to parameter if requested.
|
|
390
|
+
if self.flow_type is OptimisationType.Parameter:
|
|
391
|
+
getattr(model, self.port_name).fix() # Fix the variable - equivalent to setting it as an 'en.Param'
|
|
392
|
+
|
|
393
|
+
if self.import_constraint is FlowConstraint.Fixed: # only apply import/export constraints to variables
|
|
394
|
+
self._add_import_constraints_to_model(model=model)
|
|
395
|
+
|
|
396
|
+
if self.import_constraint in [FlowConstraint.Series, FlowConstraint.InRange]:
|
|
397
|
+
raise NotImplementedError("Series and InRange import flow constraints are not implemented")
|
|
398
|
+
|
|
399
|
+
if self.export_constraint is FlowConstraint.Fixed: # only apply these constraints to variables
|
|
400
|
+
self._add_export_constraints_to_model(model=model)
|
|
401
|
+
|
|
402
|
+
if self.export_constraint in [FlowConstraint.Series, FlowConstraint.InRange]:
|
|
403
|
+
raise NotImplementedError("Series and InRange export flow constraints are not implemented")
|
|
404
|
+
|
|
405
|
+
if self.active_periods is not None:
|
|
406
|
+
self._add_active_period_constraints_to_model(model=model)
|
|
407
|
+
|
|
408
|
+
def constrain_pos_neg(self, model: EchoConcreteModel) -> None:
|
|
409
|
+
"""Applies a mixed integer constraint that splits a port var into positive and negative components"""
|
|
410
|
+
if hasattr(model, self.pos) is False:
|
|
411
|
+
setattr(
|
|
412
|
+
model,
|
|
413
|
+
self.pos,
|
|
414
|
+
en.Var(
|
|
415
|
+
model.Expansion,
|
|
416
|
+
model.Time,
|
|
417
|
+
initialize=0,
|
|
418
|
+
domain=en.NonNegativeReals,
|
|
419
|
+
),
|
|
420
|
+
)
|
|
421
|
+
setattr(
|
|
422
|
+
model,
|
|
423
|
+
self.neg,
|
|
424
|
+
en.Var(
|
|
425
|
+
model.Expansion,
|
|
426
|
+
model.Time,
|
|
427
|
+
initialize=0,
|
|
428
|
+
domain=en.NonPositiveReals,
|
|
429
|
+
),
|
|
430
|
+
)
|
|
431
|
+
setattr(
|
|
432
|
+
model,
|
|
433
|
+
self.is_pos,
|
|
434
|
+
en.Var(model.Expansion, model.Time, initialize=0, domain=en.Binary),
|
|
435
|
+
)
|
|
436
|
+
|
|
437
|
+
con_rule = self.factory_pos_neg_flows(self.port_name, self.pos, self.neg)
|
|
438
|
+
con_name = positive_variable_component + negative_variable_component + self.port_name
|
|
439
|
+
setattr(
|
|
440
|
+
model,
|
|
441
|
+
con_name,
|
|
442
|
+
en.Constraint(model.Expansion, model.Time, rule=con_rule),
|
|
443
|
+
)
|
|
444
|
+
|
|
445
|
+
def only_pos_or_neg_one(model: EchoConcreteModel, p: int, t: int) -> InequalityExpression:
|
|
446
|
+
return getattr(model, self.pos)[p, t] <= getattr(model, self.is_pos)[p, t] * model.big_m
|
|
447
|
+
|
|
448
|
+
setattr(
|
|
449
|
+
model,
|
|
450
|
+
f"pos_neg_con1_{self.port_name}",
|
|
451
|
+
en.Constraint(model.Expansion, model.Time, rule=only_pos_or_neg_one),
|
|
452
|
+
)
|
|
453
|
+
|
|
454
|
+
def only_pos_or_neg_two(model: EchoConcreteModel, p: int, t: int) -> InequalityExpression:
|
|
455
|
+
return getattr(model, self.neg)[p, t] >= (getattr(model, self.is_pos)[p, t] - 1) * model.big_m
|
|
456
|
+
|
|
457
|
+
setattr(
|
|
458
|
+
model,
|
|
459
|
+
f"pos_neg_con2_{self.port_name}",
|
|
460
|
+
en.Constraint(model.Expansion, model.Time, rule=only_pos_or_neg_two),
|
|
461
|
+
)
|
|
462
|
+
|
|
463
|
+
@staticmethod
|
|
464
|
+
def factory_pos_neg_flows(var_name: str, pos_name: str, neg_name: str) -> Callable:
|
|
465
|
+
def constraint(model: EchoConcreteModel, expansion_interval: int, time_interval: int) -> EqualityExpression:
|
|
466
|
+
return getattr(model, var_name)[expansion_interval, time_interval] == (
|
|
467
|
+
getattr(model, pos_name)[expansion_interval, time_interval]
|
|
468
|
+
+ getattr(model, neg_name)[expansion_interval, time_interval]
|
|
469
|
+
)
|
|
470
|
+
|
|
471
|
+
return constraint
|
|
472
|
+
|
|
473
|
+
def set_initial_value(self, initial_value: dict) -> None:
|
|
474
|
+
"""Sets initial port value which will be used to initialise the pyomo var/param
|
|
475
|
+
|
|
476
|
+
Args:
|
|
477
|
+
initial_value: dict of initial values
|
|
478
|
+
|
|
479
|
+
Returns:
|
|
480
|
+
None
|
|
481
|
+
"""
|
|
482
|
+
|
|
483
|
+
self.initial_value = initial_value
|
|
484
|
+
|
|
485
|
+
def set_initial_value_from_timeseriesdata(self, time_series_data: TimeSeriesData) -> None:
|
|
486
|
+
self.set_initial_value(expand_as_dict(time_series_data))
|
|
487
|
+
|
|
488
|
+
def set_initial_value_from_array(
|
|
489
|
+
self,
|
|
490
|
+
array: list[float] | np.ndarray,
|
|
491
|
+
expansion_periods: int = 1,
|
|
492
|
+
time_periods: int | None = None,
|
|
493
|
+
) -> None:
|
|
494
|
+
"""Sets initial port value which is used to initialise the pyomo var/param
|
|
495
|
+
|
|
496
|
+
Args:
|
|
497
|
+
array: array, list of initial values. Should have either: length = time_periods,
|
|
498
|
+
or length = time_periods*expansion_periods
|
|
499
|
+
time_periods: int, optional number of time periods. If=None, assume that time_periods = len(array)
|
|
500
|
+
expansion_periods: number of expansion periods
|
|
501
|
+
"""
|
|
502
|
+
if time_periods is None:
|
|
503
|
+
time_periods = len(array)
|
|
504
|
+
|
|
505
|
+
time_series_data = TimeSeriesData(
|
|
506
|
+
value=array,
|
|
507
|
+
num_time_intervals=time_periods,
|
|
508
|
+
num_expansion_intervals=expansion_periods,
|
|
509
|
+
)
|
|
510
|
+
self.set_initial_value_from_timeseriesdata(time_series_data=time_series_data)
|
|
511
|
+
|
|
512
|
+
def set_active_periods_from_array(
|
|
513
|
+
self, array: list[bool] | list[int], expansion_periods: int = 1, time_periods: int | None = None
|
|
514
|
+
) -> None:
|
|
515
|
+
"""Sets port active periods
|
|
516
|
+
|
|
517
|
+
Args:
|
|
518
|
+
array: array, list of active periods as bool values
|
|
519
|
+
expansion_periods: number of expansion periods (int)
|
|
520
|
+
"""
|
|
521
|
+
|
|
522
|
+
if time_periods is None:
|
|
523
|
+
time_periods = len(array)
|
|
524
|
+
|
|
525
|
+
# We need an array which only contains 0 or 1 representing inactive (flow fixed to 0)
|
|
526
|
+
# or active (flow can be optimised)
|
|
527
|
+
# Convert bools to ints
|
|
528
|
+
active_periods_as_ints = [int(i) for i in array]
|
|
529
|
+
set_of_active_periods = set(active_periods_as_ints)
|
|
530
|
+
|
|
531
|
+
if set_of_active_periods not in [{0}, {1}, {0, 1}]:
|
|
532
|
+
raise ValueError("Active periods must be a list of booleans or a list only containing 0's or 1's")
|
|
533
|
+
|
|
534
|
+
time_series_data = TimeSeriesData(
|
|
535
|
+
value=active_periods_as_ints,
|
|
536
|
+
num_time_intervals=time_periods,
|
|
537
|
+
num_expansion_intervals=expansion_periods,
|
|
538
|
+
)
|
|
539
|
+
self.active_periods = expand_as_dict(time_series_data)
|
|
540
|
+
|
|
541
|
+
def add_objective(self, model: EchoConcreteModel) -> None:
|
|
542
|
+
"""Populates the port attribute 'objectives' with any pyomo expressions that are needed
|
|
543
|
+
Args:
|
|
544
|
+
model: pyomo concrete model
|
|
545
|
+
"""
|
|
546
|
+
total = 0
|
|
547
|
+
if self.slack is True:
|
|
548
|
+
if hasattr(model, self.import_slack) is True:
|
|
549
|
+
total += -1 * getattr(model, self.import_slack_max) * model.big_m
|
|
550
|
+
total += (
|
|
551
|
+
-1
|
|
552
|
+
* sum(getattr(model, self.import_slack)[p, t] for p in model.Expansion for t in model.Time)
|
|
553
|
+
* model.big_m
|
|
554
|
+
* 0.1
|
|
555
|
+
)
|
|
556
|
+
if hasattr(model, self.export_slack) is True:
|
|
557
|
+
total += getattr(model, self.export_slack_max) * model.big_m
|
|
558
|
+
total += (
|
|
559
|
+
sum(getattr(model, self.export_slack)[p, t] for p in model.Expansion for t in model.Time)
|
|
560
|
+
* model.big_m
|
|
561
|
+
* 0.1
|
|
562
|
+
)
|
|
563
|
+
|
|
564
|
+
self.objective += total
|