diff --git a/openapi.yaml b/openapi.yaml index 68a927df4..7c6bf084f 100644 --- a/openapi.yaml +++ b/openapi.yaml @@ -384,6 +384,7 @@ 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: @@ -391,6 +392,7 @@ components: 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: diff --git a/src/optimizer/app.py b/src/optimizer/app.py index 2561a2854..e701680a2 100644 --- a/src/optimizer/app.py +++ b/src/optimizer/app.py @@ -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'), }) diff --git a/tests/test_app.py b/tests/test_app.py index 190a6a7ec..fd16bae50 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -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 @@ -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 diff --git a/tests/test_bad_request.py b/tests/test_bad_request.py index ecc8676b4..262d6b956 100644 --- a/tests/test_bad_request.py +++ b/tests/test_bad_request.py @@ -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]