/
test_solver.py
409 lines (378 loc) · 15.8 KB
/
test_solver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
from datetime import datetime, timedelta
import pytest
import pytz
import numpy as np
import pandas as pd
from flexmeasures.data.models.time_series import Sensor
from flexmeasures.data.models.planning.storage import StorageScheduler
from flexmeasures.data.models.planning.utils import (
ensure_storage_specs,
initialize_series,
)
from flexmeasures.utils.calculations import integrate_time_series
TOLERANCE = 0.00001
@pytest.mark.parametrize("use_inflexible_device", [False, True])
def test_battery_solver_day_1(
add_battery_assets, add_inflexible_device_forecasts, use_inflexible_device
):
epex_da = Sensor.query.filter(Sensor.name == "epex_da").one_or_none()
battery = Sensor.query.filter(Sensor.name == "Test battery").one_or_none()
assert battery.get_attribute("market_id") == epex_da.id
tz = pytz.timezone("Europe/Amsterdam")
start = tz.localize(datetime(2015, 1, 1))
end = tz.localize(datetime(2015, 1, 2))
resolution = timedelta(minutes=15)
soc_at_start = battery.get_attribute("soc_in_mwh")
storage_specs = ensure_storage_specs(
dict(soc_at_start=soc_at_start), battery, start, end, resolution
)
schedule = StorageScheduler().schedule(
battery,
start,
end,
resolution,
storage_specs=storage_specs,
inflexible_device_sensors=add_inflexible_device_forecasts.keys()
if use_inflexible_device
else None,
)
soc_schedule = integrate_time_series(schedule, soc_at_start, decimal_precision=6)
with pd.option_context("display.max_rows", None, "display.max_columns", 3):
print(soc_schedule)
# Check if constraints were met
assert (
min(schedule.values) >= battery.get_attribute("capacity_in_mw") * -1 - TOLERANCE
)
assert max(schedule.values) <= battery.get_attribute("capacity_in_mw")
for soc in soc_schedule.values:
assert soc >= battery.get_attribute("min_soc_in_mwh")
assert soc <= battery.get_attribute("max_soc_in_mwh")
@pytest.mark.parametrize(
"roundtrip_efficiency",
[
1,
0.99,
0.01,
],
)
def test_battery_solver_day_2(add_battery_assets, roundtrip_efficiency: float):
"""Check battery scheduling results for day 2, which is set up with
8 expensive, then 8 cheap, then again 8 expensive hours.
If efficiency losses aren't too bad, we expect the scheduler to:
- completely discharge within the first 8 hours
- completely charge within the next 8 hours
- completely discharge within the last 8 hours
If efficiency losses are bad, the price difference is not worth cycling the battery,
and so we expect the scheduler to only:
- completely discharge within the last 8 hours
"""
epex_da = Sensor.query.filter(Sensor.name == "epex_da").one_or_none()
battery = Sensor.query.filter(Sensor.name == "Test battery").one_or_none()
assert battery.get_attribute("market_id") == epex_da.id
tz = pytz.timezone("Europe/Amsterdam")
start = tz.localize(datetime(2015, 1, 2))
end = tz.localize(datetime(2015, 1, 3))
resolution = timedelta(minutes=15)
soc_at_start = battery.get_attribute("soc_in_mwh")
soc_min = 0.5
soc_max = 4.5
storage_specs = ensure_storage_specs(
dict(
soc_at_start=soc_at_start,
soc_min=soc_min,
soc_max=soc_max,
roundtrip_efficiency=roundtrip_efficiency,
),
battery,
start,
end,
resolution,
)
schedule = StorageScheduler().schedule(
battery,
start,
end,
resolution,
storage_specs=storage_specs,
)
soc_schedule = integrate_time_series(
schedule,
soc_at_start,
up_efficiency=roundtrip_efficiency**0.5,
down_efficiency=roundtrip_efficiency**0.5,
decimal_precision=6,
)
with pd.option_context("display.max_rows", None, "display.max_columns", 3):
print(soc_schedule)
# Check if constraints were met
assert min(schedule.values) >= battery.get_attribute("capacity_in_mw") * -1
assert max(schedule.values) <= battery.get_attribute("capacity_in_mw") + TOLERANCE
for soc in soc_schedule.values:
assert soc >= max(soc_min, battery.get_attribute("min_soc_in_mwh"))
assert soc <= battery.get_attribute("max_soc_in_mwh")
# Check whether the resulting soc schedule follows our expectations for 8 expensive, 8 cheap and 8 expensive hours
assert soc_schedule.iloc[-1] == max(
soc_min, battery.get_attribute("min_soc_in_mwh")
) # Battery sold out at the end of its planning horizon
# As long as the roundtrip efficiency isn't too bad (I haven't computed the actual switch point)
if roundtrip_efficiency > 0.9:
assert soc_schedule.loc[start + timedelta(hours=8)] == max(
soc_min, battery.get_attribute("min_soc_in_mwh")
) # Sell what you begin with
assert soc_schedule.loc[start + timedelta(hours=16)] == min(
soc_max, battery.get_attribute("max_soc_in_mwh")
) # Buy what you can to sell later
else:
# If the roundtrip efficiency is poor, best to stand idle
assert soc_schedule.loc[start + timedelta(hours=8)] == battery.get_attribute(
"soc_in_mwh"
)
assert soc_schedule.loc[start + timedelta(hours=16)] == battery.get_attribute(
"soc_in_mwh"
)
@pytest.mark.parametrize(
"target_soc, charging_station_name",
[
(1, "Test charging station"),
(5, "Test charging station"),
(0, "Test charging station (bidirectional)"),
(5, "Test charging station (bidirectional)"),
],
)
def test_charging_station_solver_day_2(target_soc, charging_station_name):
"""Starting with a state of charge 1 kWh, within 2 hours we should be able to reach
any state of charge in the range [1, 5] kWh for a unidirectional station,
or [0, 5] for a bidirectional station, given a charging capacity of 2 kW.
"""
soc_at_start = 1
duration_until_target = timedelta(hours=2)
epex_da = Sensor.query.filter(Sensor.name == "epex_da").one_or_none()
charging_station = Sensor.query.filter(
Sensor.name == charging_station_name
).one_or_none()
assert charging_station.get_attribute("capacity_in_mw") == 2
assert charging_station.get_attribute("market_id") == epex_da.id
tz = pytz.timezone("Europe/Amsterdam")
start = tz.localize(datetime(2015, 1, 2))
end = tz.localize(datetime(2015, 1, 3))
resolution = timedelta(minutes=15)
target_soc_datetime = start + duration_until_target
soc_targets = initialize_series(np.nan, start, end, resolution, inclusive="right")
soc_targets.loc[target_soc_datetime] = target_soc
storage_specs = ensure_storage_specs(
dict(soc_at_start=soc_at_start, soc_targets=soc_targets),
charging_station,
start,
end,
resolution,
)
consumption_schedule = StorageScheduler().schedule(
charging_station, start, end, resolution, storage_specs=storage_specs
)
soc_schedule = integrate_time_series(
consumption_schedule, soc_at_start, decimal_precision=6
)
# Check if constraints were met
assert (
min(consumption_schedule.values)
>= charging_station.get_attribute("capacity_in_mw") * -1
)
assert (
max(consumption_schedule.values)
<= charging_station.get_attribute("capacity_in_mw") + TOLERANCE
)
print(consumption_schedule.head(12))
print(soc_schedule.head(12))
assert abs(soc_schedule.loc[target_soc_datetime] - target_soc) < TOLERANCE
@pytest.mark.parametrize(
"target_soc, charging_station_name",
[
(9, "Test charging station"),
(15, "Test charging station"),
(5, "Test charging station (bidirectional)"),
(15, "Test charging station (bidirectional)"),
],
)
def test_fallback_to_unsolvable_problem(target_soc, charging_station_name):
"""Starting with a state of charge 10 kWh, within 2 hours we should be able to reach
any state of charge in the range [10, 14] kWh for a unidirectional station,
or [6, 14] for a bidirectional station, given a charging capacity of 2 kW.
Here we test target states of charge outside that range, ones that we should be able
to get as close to as 1 kWh difference.
We want our scheduler to handle unsolvable problems like these with a sensible fallback policy.
"""
soc_at_start = 10
duration_until_target = timedelta(hours=2)
expected_gap = 1
epex_da = Sensor.query.filter(Sensor.name == "epex_da").one_or_none()
charging_station = Sensor.query.filter(
Sensor.name == charging_station_name
).one_or_none()
assert charging_station.get_attribute("capacity_in_mw") == 2
assert charging_station.get_attribute("market_id") == epex_da.id
tz = pytz.timezone("Europe/Amsterdam")
start = tz.localize(datetime(2015, 1, 2))
end = tz.localize(datetime(2015, 1, 3))
resolution = timedelta(minutes=15)
target_soc_datetime = start + duration_until_target
soc_targets = initialize_series(np.nan, start, end, resolution, inclusive="right")
soc_targets.loc[target_soc_datetime] = target_soc
storage_specs = ensure_storage_specs(
dict(soc_at_start=soc_at_start, soc_targets=soc_targets),
charging_station,
start,
end,
resolution,
)
consumption_schedule = StorageScheduler().schedule(
charging_station,
start,
end,
resolution,
storage_specs=storage_specs,
)
soc_schedule = integrate_time_series(
consumption_schedule, soc_at_start, decimal_precision=6
)
# Check if constraints were met
assert (
min(consumption_schedule.values)
>= charging_station.get_attribute("capacity_in_mw") * -1
)
assert max(consumption_schedule.values) <= charging_station.get_attribute(
"capacity_in_mw"
)
print(consumption_schedule.head(12))
print(soc_schedule.head(12))
assert (
abs(abs(soc_schedule.loc[target_soc_datetime] - target_soc) - expected_gap)
< TOLERANCE
)
@pytest.mark.parametrize(
"market_scenario",
[
"dynamic contract",
"fixed contract",
],
)
def test_building_solver_day_2(
db,
add_battery_assets,
add_market_prices,
create_test_tariffs,
add_inflexible_device_forecasts,
inflexible_devices,
flexible_devices,
market_scenario: str,
):
"""Check battery scheduling results within the context of a building with PV, for day 2, against the following market scenarios:
1) a dynamic tariff with equal consumption and feed-in tariffs, that is set up with 8 expensive, then 8 cheap, then again 8 expensive hours.
2) a fixed consumption tariff and a fixed feed-in tariff that is lower, which incentives to maximize self-consumption of PV power into the battery.
In the test data:
- Hours with net production coincide with low dynamic market prices.
- Hours with net consumption coincide with high dynamic market prices.
So when the prices are low (in scenario 1), we have net production, and when they are high, net consumption.
That means we have first net consumption, then net production, and then net consumption again.
In either scenario, we expect the scheduler to:
- completely discharge within the first 8 hours (either due to 1) high prices, or 2) net consumption)
- completely charge within the next 8 hours (either due to 1) low prices, or 2) net production)
- completely discharge within the last 8 hours (either due to 1) high prices, or 2) net consumption)
"""
battery = flexible_devices["battery power sensor"]
building = battery.generic_asset
default_consumption_price_sensor = Sensor.query.filter(
Sensor.name == "epex_da"
).one_or_none()
assert battery.get_attribute("market_id") == default_consumption_price_sensor.id
if market_scenario == "dynamic contract":
consumption_price_sensor = default_consumption_price_sensor
production_price_sensor = consumption_price_sensor
elif market_scenario == "fixed contract":
consumption_price_sensor = create_test_tariffs["consumption_price_sensor"]
production_price_sensor = create_test_tariffs["production_price_sensor"]
else:
raise NotImplementedError(
f"Missing test case for market conditions '{market_scenario}'"
)
tz = pytz.timezone("Europe/Amsterdam")
start = tz.localize(datetime(2015, 1, 2))
end = tz.localize(datetime(2015, 1, 3))
resolution = timedelta(minutes=15)
soc_at_start = 2.5
soc_min = 0.5
soc_max = 4.5
storage_specs = ensure_storage_specs(
dict(
soc_at_start=soc_at_start,
soc_min=soc_min,
soc_max=soc_max,
),
battery,
start,
end,
resolution,
)
schedule = StorageScheduler().schedule(
battery,
start,
end,
resolution,
storage_specs=storage_specs,
consumption_price_sensor=consumption_price_sensor,
production_price_sensor=production_price_sensor,
inflexible_device_sensors=inflexible_devices.values(),
)
soc_schedule = integrate_time_series(schedule, soc_at_start, decimal_precision=6)
with pd.option_context("display.max_rows", None, "display.max_columns", 3):
print(soc_schedule)
# Check if constraints were met
capacity = pd.DataFrame(
data=np.sum(np.array(list(add_inflexible_device_forecasts.values())), axis=0),
columns=["inflexible"],
).tail(
-4 * 24
) # remove first 96 quarter-hours (the schedule is about the 2nd day)
capacity["max"] = building.get_attribute("capacity_in_mw")
capacity["min"] = -building.get_attribute("capacity_in_mw")
capacity["production headroom"] = capacity["max"] - capacity["inflexible"]
capacity["consumption headroom"] = capacity["inflexible"] - capacity["min"]
capacity["battery production headroom"] = capacity["production headroom"].clip(
upper=battery.get_attribute("capacity_in_mw")
)
capacity["battery consumption headroom"] = capacity["consumption headroom"].clip(
upper=battery.get_attribute("capacity_in_mw")
)
capacity[
"schedule"
] = schedule.values # consumption is positive, production is negative
with pd.option_context(
"display.max_rows", None, "display.max_columns", None, "display.width", 2000
):
print(capacity)
assert (capacity["schedule"] >= -capacity["battery production headroom"]).all()
assert (capacity["schedule"] <= capacity["battery consumption headroom"]).all()
for soc in soc_schedule.values:
assert soc >= max(soc_min, battery.get_attribute("min_soc_in_mwh"))
assert soc <= battery.get_attribute("max_soc_in_mwh")
# Check whether the resulting soc schedule follows our expectations for.
# To recap, in scenario 1 and 2, the schedule should mainly be influenced by:
# 1) 8 expensive, 8 cheap and 8 expensive hours
# 2) 8 net-consumption, 8 net-production and 8 net-consumption hours
# Result after 8 hours
# 1) Sell what you begin with
# 2) The battery discharged as far as it could during the first 8 net-consumption hours
assert soc_schedule.loc[start + timedelta(hours=8)] == max(
soc_min, battery.get_attribute("min_soc_in_mwh")
)
# Result after second 8 hour-interval
# 1) Buy what you can to sell later, when prices will be high again
# 2) The battery charged with PV power as far as it could during the middle 8 net-production hours
assert soc_schedule.loc[start + timedelta(hours=16)] == min(
soc_max, battery.get_attribute("max_soc_in_mwh")
)
# Result at end of day
# 1) The battery sold out at the end of its planning horizon
# 2) The battery discharged as far as it could during the last 8 net-consumption hours
assert soc_schedule.iloc[-1] == max(
soc_min, battery.get_attribute("min_soc_in_mwh")
)