Skip to content
Merged
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
2 changes: 2 additions & 0 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -384,13 +384,15 @@ components:
items:
type: number
minimum: 0
maximum: 100000000
description: Household energy demand at each time step (Wh)
example: [3000, 4000, 5000, 4500, 3500, 3000]
ft:
type: array
items:
type: number
minimum: 0
maximum: 100000000
description: Forecasted energy generation (e.g., solar PV) at each time step (Wh)
example: [2000, 6000, 8000, 7000, 4000, 1000]
p_N:
Expand Down
11 changes: 9 additions & 2 deletions src/optimizer/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,19 @@ def handle_validation_error(error):
'c_priority': fields.Integer(required=False, description='Charging and discharging priority compared to other batteries. 2 = highest priority.')
})

# 100 MWh per step, far above any site evcc serves
MAX_STEP_ENERGY = 1e8

time_series_model = api.model('TimeSeries', {
# the example is what the Swagger UI on / prefills, one step like its other series. A zero length step divides by zero
'dt': fields.List(fields.Float(exclusiveMin=0), required=True, example=[3600],
description='duration in seconds for each time step (s)'),
'gt': fields.List(fields.Float, required=True, description='Required energy for home consumption at each time step (Wh)'),
'ft': fields.List(fields.Float, required=True, description='Forecasted solar generation at each time step (Wh)'),
# a broken household profile sent 1e9 to 1e16 Wh per step: HiGHS returns no solution for such
# coefficients and the request ended Not Solved instead of naming the bad step
'gt': fields.List(fields.Float(max=MAX_STEP_ENERGY), required=True,
description='Required energy for home consumption at each time step (Wh)'),
'ft': fields.List(fields.Float(max=MAX_STEP_ENERGY), required=True,
description='Forecasted solar generation at each time step (Wh)'),
'p_N': fields.List(fields.Float, required=True, description='Price per Wh taken from grid at each time step'),
'p_E': fields.List(fields.Float, required=True, description='Remuneration per Wh fed into grid at each time step'),
})
Expand Down
11 changes: 8 additions & 3 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,14 @@ def infeasible(self, *args, **kwargs):


def test_an_infeasible_probe_is_reported_without_the_split(monkeypatch):
# production: a demand forecast of 1e36 Wh in a few slots. CBC proves it infeasible in the probe,
# the split handed it to HiGHS, which refuses bounds past 1e20 and returned no solution at all
# a probe CBC proves infeasible went on to the split, which solved the same model again on HiGHS.
# The production case, 1e36 Wh of demand, is now a 400, so the infeasibility is simulated
original = pulp.LpProblem.solve

def infeasible(self, *args, **kwargs):
return dataclasses.replace(original(self, *args, **kwargs), status=pulp.LpSolveStatus.Infeasible, has_solution=False)

request = json.loads(pathlib.Path('test_cases/009-discharge-before-import.json').read_text())["request"]
request["time_series"]["gt"][3] = 1e36
stages = []
solve = Optimizer._solve

Expand All @@ -202,6 +206,7 @@ def record(self, solver):
return solve(self, solver)

monkeypatch.setattr(settings, "solver", "hybrid")
monkeypatch.setattr(pulp.LpProblem, "solve", infeasible)
monkeypatch.setattr(Optimizer, "_solve", record)
body = app.test_client().post("/optimize/charge-schedule", json=request).json

Expand Down
11 changes: 11 additions & 0 deletions tests/test_bad_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,17 @@ def test_non_positive_step_names_the_field(payload, dt):
assert list(response.json['details']) == ['time_series.dt.1']


@pytest.mark.parametrize('series', ['gt', 'ft'])
def test_absurd_step_energy_names_the_step(payload, series):
# a broken household profile sent 1e16 Wh, which HiGHS cannot solve: it ended Not Solved
payload['time_series'][series][1] = 1.47e16

response = app.test_client().post('/optimize/charge-schedule', json=payload)

assert response.status_code == 400
assert list(response.json['details']) == [f'time_series.{series}.1']


def test_client_version_is_logged(payload, capsys):
payload['time_series']['p_N'] = [0.0003]

Expand Down
Loading