distgridforge 0.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.
- distgridforge-0.3.0.dist-info/METADATA +266 -0
- distgridforge-0.3.0.dist-info/RECORD +38 -0
- distgridforge-0.3.0.dist-info/WHEEL +5 -0
- distgridforge-0.3.0.dist-info/licenses/LICENCE.txt +21 -0
- distgridforge-0.3.0.dist-info/top_level.txt +1 -0
- gridforge/__init__.py +32 -0
- gridforge/case_study/config.py +158 -0
- gridforge/case_study/dickert_lv_network_script.py +109 -0
- gridforge/case_study/mv_lv_feeder.py +314 -0
- gridforge/case_study/simbench_network_test_case.py +168 -0
- gridforge/core/__init__.py +0 -0
- gridforge/core/constraints.py +421 -0
- gridforge/core/cost_model.py +363 -0
- gridforge/core/grid_enhancement.py +1932 -0
- gridforge/core/power_flow.py +1146 -0
- gridforge/core/strategy.py +297 -0
- gridforge/engines/__init__.py +0 -0
- gridforge/engines/gridlabd_engine.py +527 -0
- gridforge/engines/opendss_engine.py +885 -0
- gridforge/engines/pandapower_engine.py +3766 -0
- gridforge/engines/roseau_engine.py +1234 -0
- gridforge/examples/__init__.py +0 -0
- gridforge/examples/quick_start.py +85 -0
- gridforge/main.py +0 -0
- gridforge/strategies/__init__.py +0 -0
- gridforge/strategies/flexibility.py +1476 -0
- gridforge/strategies/line_upgrade.py +1846 -0
- gridforge/strategies/path_to_source.py +596 -0
- gridforge/strategies/sizing.py +210 -0
- gridforge/strategies/step_check.py +183 -0
- gridforge/strategies/tap_change.py +267 -0
- gridforge/strategies/transformer_upgrade.py +622 -0
- gridforge/utils/__init__.py +0 -0
- gridforge/utils/config.py +55 -0
- gridforge/utils/interactive_plot.py +700 -0
- gridforge/utils/logging.py +86 -0
- gridforge/utils/plot.py +1078 -0
- gridforge/utils/reporting.py +0 -0
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Cost modeling for network changes.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
import math
|
|
6
|
+
from collections.abc import Iterable, Mapping
|
|
7
|
+
from decimal import Decimal
|
|
8
|
+
from numbers import Real
|
|
9
|
+
|
|
10
|
+
from gridforge.utils.logging import logger
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class MissingUnitCostError(LookupError):
|
|
14
|
+
"""
|
|
15
|
+
A price is missing from `unit_costs`.
|
|
16
|
+
|
|
17
|
+
Pricing the action at 0 instead would understate the reported total cost. An action
|
|
18
|
+
that really is free must say so with an explicit price of 0.0.
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class InvalidUnitCostError(ValueError):
|
|
23
|
+
"""
|
|
24
|
+
A price of `unit_costs` is not a price: not a number, not finite, or negative.
|
|
25
|
+
|
|
26
|
+
A price is a real number (int, float, Decimal, numpy scalar...), 0 or more. A string
|
|
27
|
+
is not converted, even if it reads as a number: a price must not depend on how a text
|
|
28
|
+
is parsed.
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _format_key(key: tuple[str, ...]) -> str:
|
|
33
|
+
return "unit_costs" + "".join(f"[{part!r}]" for part in key)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _invalid_reason(value: object) -> str | None:
|
|
37
|
+
"""Why `value` cannot be a price, None if it can."""
|
|
38
|
+
# bool is an int for Python, but True is no price
|
|
39
|
+
if isinstance(value, bool) or not isinstance(value, Real | Decimal):
|
|
40
|
+
return "is not a number"
|
|
41
|
+
try:
|
|
42
|
+
price = float(value)
|
|
43
|
+
except (TypeError, ValueError, ArithmeticError):
|
|
44
|
+
return "is not a finite number"
|
|
45
|
+
if not math.isfinite(price):
|
|
46
|
+
return "is not a finite number"
|
|
47
|
+
if price < 0:
|
|
48
|
+
return "is negative"
|
|
49
|
+
return None
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class CostModel:
|
|
53
|
+
"""
|
|
54
|
+
Model for calculating costs of network changes.
|
|
55
|
+
|
|
56
|
+
This class provides methods to calculate the financial impact of various
|
|
57
|
+
grid enhancement strategies, such as line upgrades, transformer upgrades,
|
|
58
|
+
and operational actions like tap changes.
|
|
59
|
+
|
|
60
|
+
Every price an action needs must be in `unit_costs`, otherwise pricing the action
|
|
61
|
+
raises `MissingUnitCostError`. A price is a real number, 0 or more: pricing an action
|
|
62
|
+
with anything else raises `InvalidUnitCostError`. See `__init__` for the schema.
|
|
63
|
+
|
|
64
|
+
Attributes:
|
|
65
|
+
unit_costs (Dict[str, Dict[str, float] | float]): A dictionary containing unit costs for different actions and equipment types.
|
|
66
|
+
logger (logging.Logger): Logger instance.
|
|
67
|
+
"""
|
|
68
|
+
|
|
69
|
+
def __init__(self, unit_costs: dict[str, dict[str, float] | float]):
|
|
70
|
+
"""
|
|
71
|
+
Initialize with unit costs.
|
|
72
|
+
|
|
73
|
+
Args:
|
|
74
|
+
unit_costs (Dict[str, Dict[str, float] | float]): Dict mapping cost categories to unit costs.
|
|
75
|
+
|
|
76
|
+
Expected structure, with the keys that the configured strategies need::
|
|
77
|
+
|
|
78
|
+
{
|
|
79
|
+
"line_upgrade": { # LineUpgradeStrategy
|
|
80
|
+
"NAYY 4x120 SE": 70000.0, # € per km, by conductor type
|
|
81
|
+
},
|
|
82
|
+
"new_line": { # Optional, a parallel line otherwise costs its line_upgrade price
|
|
83
|
+
"NAYY 4x120 SE": 90000.0, # € per km, by conductor type
|
|
84
|
+
},
|
|
85
|
+
"transformer_upgrade": { # TransformerUpgradeStrategy
|
|
86
|
+
"0.4 MVA 20/0.4 kV": 15000.0, # € per unit, by transformer type
|
|
87
|
+
},
|
|
88
|
+
"tap_change": 10.0, # € per tap step (TapChangeStrategy)
|
|
89
|
+
"load_shedding": 10000.0, # € per MW shed (flexible loads)
|
|
90
|
+
"generation_curtailment": 5000.0, # € per MW curtailed (flexible generators)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
`load_shedding` and `generation_curtailment` are prices per MW, not per MWh:
|
|
94
|
+
the algorithm works on one operating point and has no duration. Whatever the
|
|
95
|
+
duration of the activations is worth must be included in the price, on the
|
|
96
|
+
same horizon as the investments it is added to.
|
|
97
|
+
"""
|
|
98
|
+
self.unit_costs = unit_costs
|
|
99
|
+
self.logger = logger
|
|
100
|
+
self.logger.info("Initialized cost model with unit costs")
|
|
101
|
+
self.logger.debug(f"Unit costs: {unit_costs}")
|
|
102
|
+
|
|
103
|
+
def _lookup(self, key: tuple[str, ...]) -> object:
|
|
104
|
+
"""Get what is at `key` of `unit_costs`, None if there is no value there."""
|
|
105
|
+
value: object = self.unit_costs
|
|
106
|
+
for part in key:
|
|
107
|
+
value = value.get(part) if isinstance(value, Mapping) else None
|
|
108
|
+
# A table of prices where a price is expected is a misplaced price
|
|
109
|
+
return None if isinstance(value, Mapping) else value
|
|
110
|
+
|
|
111
|
+
def find_price(self, *key: str) -> float | None:
|
|
112
|
+
"""
|
|
113
|
+
Get the price at `key` of `unit_costs`, None if there is nothing there: the way
|
|
114
|
+
to read a price that may be absent, where the `calculate_*` methods raise
|
|
115
|
+
`MissingUnitCostError` for one that must be there. A `new_line` price is not
|
|
116
|
+
replaced by the `line_upgrade` price of its type here.
|
|
117
|
+
|
|
118
|
+
Args:
|
|
119
|
+
*key (str): The path to the price in `unit_costs`, like `("tap_change",)` or
|
|
120
|
+
`("line_upgrade", "NAYY 4x120 SE")`.
|
|
121
|
+
|
|
122
|
+
Returns:
|
|
123
|
+
Optional[float]: The price, None if `unit_costs` has nothing at `key` (or a
|
|
124
|
+
table of prices, which is a misplaced price).
|
|
125
|
+
|
|
126
|
+
Raises:
|
|
127
|
+
InvalidUnitCostError: If what is there is not a price.
|
|
128
|
+
"""
|
|
129
|
+
value = self._lookup(key)
|
|
130
|
+
if value is None:
|
|
131
|
+
return None
|
|
132
|
+
reason = _invalid_reason(value)
|
|
133
|
+
if reason:
|
|
134
|
+
raise InvalidUnitCostError(
|
|
135
|
+
f"Invalid unit cost: {_format_key(key)} {reason}: {value!r}. "
|
|
136
|
+
"A price is a real number, 0 or more."
|
|
137
|
+
)
|
|
138
|
+
return float(value) # type: ignore[arg-type]
|
|
139
|
+
|
|
140
|
+
def _price(self, *key: str) -> float:
|
|
141
|
+
"""Get the price at `key` of `unit_costs`, which must be there."""
|
|
142
|
+
price = self.find_price(*key)
|
|
143
|
+
if price is None:
|
|
144
|
+
raise MissingUnitCostError(
|
|
145
|
+
f"Missing unit cost {_format_key(key)}: add it, with 0.0 if it is really free."
|
|
146
|
+
)
|
|
147
|
+
return price
|
|
148
|
+
|
|
149
|
+
def check_prices(self, required: Iterable[tuple[str, ...]]) -> None:
|
|
150
|
+
"""
|
|
151
|
+
Check that `unit_costs` holds the given prices.
|
|
152
|
+
|
|
153
|
+
Args:
|
|
154
|
+
required (Iterable[Tuple[str, ...]]): Keys of `unit_costs`, like `("tap_change",)`
|
|
155
|
+
or `("line_upgrade", "NAYY 4x120 SE")`.
|
|
156
|
+
|
|
157
|
+
Raises:
|
|
158
|
+
InvalidUnitCostError: If some of them are not prices (not a number, not
|
|
159
|
+
finite, or negative), listing all of them and the missing ones.
|
|
160
|
+
MissingUnitCostError: If prices are missing, listing all of them.
|
|
161
|
+
"""
|
|
162
|
+
missing = []
|
|
163
|
+
invalid = []
|
|
164
|
+
for key in sorted(set(required), key=str):
|
|
165
|
+
value = self._lookup(key)
|
|
166
|
+
if value is None:
|
|
167
|
+
missing.append(_format_key(key))
|
|
168
|
+
elif reason := _invalid_reason(value):
|
|
169
|
+
invalid.append(f"{_format_key(key)} {reason}: {value!r}")
|
|
170
|
+
missing_message = (
|
|
171
|
+
"Missing unit costs for the configured strategies: " + ", ".join(missing)
|
|
172
|
+
)
|
|
173
|
+
if invalid:
|
|
174
|
+
raise InvalidUnitCostError(
|
|
175
|
+
"Invalid unit costs for the configured strategies, a price is a real "
|
|
176
|
+
"number, 0 or more: "
|
|
177
|
+
+ "; ".join(invalid)
|
|
178
|
+
+ (f". {missing_message}" if missing else "")
|
|
179
|
+
)
|
|
180
|
+
if missing:
|
|
181
|
+
raise MissingUnitCostError(missing_message)
|
|
182
|
+
|
|
183
|
+
def missing_prices(self, keys: Iterable[tuple[str, ...]]) -> list[str]:
|
|
184
|
+
"""
|
|
185
|
+
List those of the given prices that `unit_costs` does not hold, as
|
|
186
|
+
`check_prices` names them.
|
|
187
|
+
|
|
188
|
+
A `new_line` price falls back on the `line_upgrade` price of the type, as
|
|
189
|
+
`calculate_new_line_cost` does.
|
|
190
|
+
|
|
191
|
+
Args:
|
|
192
|
+
keys (Iterable[Tuple[str, ...]]): Keys of `unit_costs`.
|
|
193
|
+
|
|
194
|
+
Returns:
|
|
195
|
+
List[str]: The missing prices, empty if there is none.
|
|
196
|
+
|
|
197
|
+
Raises:
|
|
198
|
+
InvalidUnitCostError: If some of those that are there are not prices, as
|
|
199
|
+
`check_prices` would, listing all of them.
|
|
200
|
+
"""
|
|
201
|
+
missing = []
|
|
202
|
+
invalid = []
|
|
203
|
+
for key in sorted(set(keys), key=str):
|
|
204
|
+
found = key
|
|
205
|
+
value = self._lookup(key)
|
|
206
|
+
if value is None and key[0] == "new_line":
|
|
207
|
+
found = ("line_upgrade", *key[1:])
|
|
208
|
+
value = self._lookup(found)
|
|
209
|
+
if value is None:
|
|
210
|
+
missing.append(_format_key(key))
|
|
211
|
+
elif reason := _invalid_reason(value):
|
|
212
|
+
invalid.append(f"{_format_key(found)} {reason}: {value!r}")
|
|
213
|
+
if invalid:
|
|
214
|
+
raise InvalidUnitCostError(
|
|
215
|
+
"Invalid unit costs, a price is a real number, 0 or more: "
|
|
216
|
+
+ "; ".join(invalid)
|
|
217
|
+
)
|
|
218
|
+
return missing
|
|
219
|
+
|
|
220
|
+
def calculate_line_upgrade_cost(
|
|
221
|
+
self, line_id: str, new_type: str, length: float
|
|
222
|
+
) -> float:
|
|
223
|
+
"""
|
|
224
|
+
Calculate the cost of upgrading a line.
|
|
225
|
+
|
|
226
|
+
Args:
|
|
227
|
+
line_id (str): ID of the line being upgraded.
|
|
228
|
+
new_type (str): Type of the new line conductor/cable.
|
|
229
|
+
length (float): Length of the line in kilometers.
|
|
230
|
+
|
|
231
|
+
Returns:
|
|
232
|
+
float: Total cost in euros.
|
|
233
|
+
|
|
234
|
+
Raises:
|
|
235
|
+
MissingUnitCostError: If `new_type` has no `line_upgrade` price.
|
|
236
|
+
"""
|
|
237
|
+
unit_cost = self._price("line_upgrade", new_type)
|
|
238
|
+
total_cost = unit_cost * length
|
|
239
|
+
self.logger.info(
|
|
240
|
+
f"Line upgrade cost for {line_id} to {new_type}: {total_cost:.2f} € "
|
|
241
|
+
f"({unit_cost:.2f} €/km × {length} km)"
|
|
242
|
+
)
|
|
243
|
+
return total_cost
|
|
244
|
+
|
|
245
|
+
def calculate_transformer_upgrade_cost(
|
|
246
|
+
self, transformer_id: str, new_type: str
|
|
247
|
+
) -> float:
|
|
248
|
+
"""
|
|
249
|
+
Calculate the cost of upgrading a transformer.
|
|
250
|
+
|
|
251
|
+
Args:
|
|
252
|
+
transformer_id (str): ID of the transformer being upgraded.
|
|
253
|
+
new_type (str): Type of the new transformer.
|
|
254
|
+
|
|
255
|
+
Returns:
|
|
256
|
+
float: Total cost in euros.
|
|
257
|
+
|
|
258
|
+
Raises:
|
|
259
|
+
MissingUnitCostError: If `new_type` has no `transformer_upgrade` price.
|
|
260
|
+
"""
|
|
261
|
+
total_cost = self._price("transformer_upgrade", new_type)
|
|
262
|
+
self.logger.info(
|
|
263
|
+
f"Transformer upgrade cost for {transformer_id} to {new_type}: {total_cost:.2f} €"
|
|
264
|
+
)
|
|
265
|
+
return total_cost
|
|
266
|
+
|
|
267
|
+
def calculate_tap_change_cost(self, transformer_id: str, tap_change: int) -> float:
|
|
268
|
+
"""
|
|
269
|
+
Calculate the cost of changing transformer tap position.
|
|
270
|
+
|
|
271
|
+
Args:
|
|
272
|
+
transformer_id (str): ID of the transformer.
|
|
273
|
+
tap_change (int): Number of tap positions changed (absolute value is used).
|
|
274
|
+
|
|
275
|
+
Returns:
|
|
276
|
+
float: Total cost in euros.
|
|
277
|
+
|
|
278
|
+
Raises:
|
|
279
|
+
MissingUnitCostError: If `tap_change` has no price.
|
|
280
|
+
"""
|
|
281
|
+
unit_cost = self._price("tap_change")
|
|
282
|
+
total_cost = unit_cost * abs(tap_change)
|
|
283
|
+
self.logger.info(
|
|
284
|
+
f"Tap change cost for {transformer_id}: {total_cost:.2f} € "
|
|
285
|
+
f"({unit_cost:.2f} € × {abs(tap_change)} steps)"
|
|
286
|
+
)
|
|
287
|
+
return total_cost
|
|
288
|
+
|
|
289
|
+
def calculate_new_line_cost(self, line_type: str, length: float) -> float:
|
|
290
|
+
"""
|
|
291
|
+
Calculate the cost of adding a new line.
|
|
292
|
+
|
|
293
|
+
Args:
|
|
294
|
+
line_type (str): Type of the new line.
|
|
295
|
+
length (float): Length of the line in kilometers.
|
|
296
|
+
|
|
297
|
+
Returns:
|
|
298
|
+
float: Total cost in euros.
|
|
299
|
+
|
|
300
|
+
Raises:
|
|
301
|
+
MissingUnitCostError: If `line_type` has neither a `new_line` nor a
|
|
302
|
+
`line_upgrade` price.
|
|
303
|
+
"""
|
|
304
|
+
# New lines might have different costs than upgrades
|
|
305
|
+
unit_cost = self.find_price("new_line", line_type)
|
|
306
|
+
# If not defined, fall back to line upgrade costs
|
|
307
|
+
if unit_cost is None:
|
|
308
|
+
unit_cost = self.find_price("line_upgrade", line_type)
|
|
309
|
+
if unit_cost is None:
|
|
310
|
+
raise MissingUnitCostError(
|
|
311
|
+
f"Missing unit cost {_format_key(('new_line', line_type))}, or "
|
|
312
|
+
f"{_format_key(('line_upgrade', line_type))} which it falls back on: "
|
|
313
|
+
"add one, with 0.0 if it is really free."
|
|
314
|
+
)
|
|
315
|
+
|
|
316
|
+
total_cost = unit_cost * length
|
|
317
|
+
self.logger.info(
|
|
318
|
+
f"New line cost for {line_type}: {total_cost:.2f} € "
|
|
319
|
+
f"({unit_cost:.2f} €/km × {length} km)"
|
|
320
|
+
)
|
|
321
|
+
return total_cost
|
|
322
|
+
|
|
323
|
+
def calculate_load_shedding_cost(self, amount_shed: float) -> float:
|
|
324
|
+
"""
|
|
325
|
+
Calculate the cost of load shedding.
|
|
326
|
+
|
|
327
|
+
Args:
|
|
328
|
+
amount_shed (float): Amount of load shed in MW.
|
|
329
|
+
|
|
330
|
+
Returns:
|
|
331
|
+
float: Total cost in euros.
|
|
332
|
+
|
|
333
|
+
Raises:
|
|
334
|
+
MissingUnitCostError: If `load_shedding` has no price.
|
|
335
|
+
"""
|
|
336
|
+
unit_cost = self._price("load_shedding")
|
|
337
|
+
total_cost = unit_cost * amount_shed
|
|
338
|
+
self.logger.info(
|
|
339
|
+
f"Load shedding cost: {total_cost:.2f} € "
|
|
340
|
+
f"({unit_cost:.2f} €/MW × {amount_shed} MW)"
|
|
341
|
+
)
|
|
342
|
+
return total_cost
|
|
343
|
+
|
|
344
|
+
def calculate_generation_curtailment_cost(self, amount_curtailed: float) -> float:
|
|
345
|
+
"""
|
|
346
|
+
Calculate the cost of generation curtailment.
|
|
347
|
+
|
|
348
|
+
Args:
|
|
349
|
+
amount_curtailed (float): Amount of generation curtailed in MW.
|
|
350
|
+
|
|
351
|
+
Returns:
|
|
352
|
+
float: Total cost in euros.
|
|
353
|
+
|
|
354
|
+
Raises:
|
|
355
|
+
MissingUnitCostError: If `generation_curtailment` has no price.
|
|
356
|
+
"""
|
|
357
|
+
unit_cost = self._price("generation_curtailment")
|
|
358
|
+
total_cost = unit_cost * amount_curtailed
|
|
359
|
+
self.logger.info(
|
|
360
|
+
f"Generation curtailment cost: {total_cost:.2f} € "
|
|
361
|
+
f"({unit_cost:.2f} €/MW × {amount_curtailed} MW)"
|
|
362
|
+
)
|
|
363
|
+
return total_cost
|