diff --git a/AGENTS.md b/AGENTS.md index a40c12046..97c9cab80 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -56,7 +56,8 @@ The model is a maximization problem. Read these before changing it: - The suite runs with `uv run pytest`. - `test_cases/*.json` are loaded by `tests/test_app.py`. Each file holds a `request` and an optional `expected_response`, and the test asserts the optimizer status and, with `numpy.isclose`, the objective value. Add a scenario by dropping in a JSON file. -- For behavior the data driven harness cannot assert, such as a specific charge schedule, construct an `Optimizer` directly in a dedicated test module and assert on the returned dict. +- A test case with `"strict": true` additionally compares grid import, grid export, and the battery schedules with `numpy.allclose`. Use it for features that only pick between cost neutral alternatives, where the objective value barely moves. Do not set it on scenarios with several equally optimal schedules, the comparison would be arbitrary. +- Prefer a data driven case over a dedicated test module, so a failure points at the scenario rather than at a feature specific script. - Cover both the success and the limit violation paths. ## Writing Style diff --git a/test_cases/024-attenuate-demand-peaks.json b/test_cases/024-attenuate-demand-peaks.json new file mode 100644 index 000000000..1cd8e3d51 --- /dev/null +++ b/test_cases/024-attenuate-demand-peaks.json @@ -0,0 +1,112 @@ +{ + "strict": true, + "request": { + "strategy": { + "charging_strategy": "attenuate_demand_peaks" + }, + "batteries": [ + { + "charge_from_grid": true, + "discharge_to_grid": false, + "s_capacity": 20000, + "s_min": 0, + "s_max": 20000, + "s_initial": 0, + "s_goal": [ + 0, + 0, + 0, + 4000 + ], + "c_min": 0, + "c_max": 8000, + "d_max": 0, + "p_a": 0 + } + ], + "time_series": { + "dt": [ + 3600, + 3600, + 3600, + 3600 + ], + "gt": [ + 0, + 0, + 0, + 0 + ], + "ft": [ + 0, + 0, + 0, + 0 + ], + "p_N": [ + 0.3, + 0.3, + 0.3, + 0.3 + ], + "p_E": [ + 0.0, + 0.0, + 0.0, + 0.0 + ] + }, + "eta_c": 0.95, + "eta_d": 0.95 + }, + "expected_response": { + "status": "Optimal", + "objective_value": -1263.1579199999999, + "limit_violations": { + "grid_import_limit_exceeded": false, + "grid_export_limit_hit": false + }, + "batteries": [ + { + "charging_power": [ + 1052.6316, + 1052.6316, + 1052.6316, + 1052.6316 + ], + "discharging_power": [ + 0.0, + 0.0, + 0.0, + 0.0 + ], + "state_of_charge": [ + 1000.0, + 2000.0, + 3000.0, + 4000.0 + ] + } + ], + "grid_import": [ + 1052.6316, + 1052.6316, + 1052.6316, + 1052.6316 + ], + "grid_export": [ + 0.0, + 0.0, + 0.0, + 0.0 + ], + "flow_direction": [ + 0, + 0, + 0, + 0 + ], + "grid_import_overshoot": [], + "grid_export_overshoot": [] + } +} \ No newline at end of file diff --git a/test_cases/025-attenuate-feedin-peaks.json b/test_cases/025-attenuate-feedin-peaks.json new file mode 100644 index 000000000..252f336aa --- /dev/null +++ b/test_cases/025-attenuate-feedin-peaks.json @@ -0,0 +1,112 @@ +{ + "strict": true, + "request": { + "strategy": { + "charging_strategy": "attenuate_feedin_peaks" + }, + "batteries": [ + { + "charge_from_grid": false, + "discharge_to_grid": false, + "s_capacity": 20000, + "s_min": 0, + "s_max": 20000, + "s_initial": 0, + "s_goal": [ + 0, + 0, + 0, + 4000 + ], + "c_min": 0, + "c_max": 8000, + "d_max": 0, + "p_a": 0 + } + ], + "time_series": { + "dt": [ + 3600, + 3600, + 3600, + 3600 + ], + "gt": [ + 0, + 0, + 0, + 0 + ], + "ft": [ + 2000, + 6000, + 6000, + 2000 + ], + "p_N": [ + 0.3, + 0.3, + 0.3, + 0.3 + ], + "p_E": [ + 0.1, + 0.1, + 0.1, + 0.1 + ] + }, + "eta_c": 0.95, + "eta_d": 0.95 + }, + "expected_response": { + "status": "Optimal", + "objective_value": 1178.9473600000001, + "limit_violations": { + "grid_import_limit_exceeded": false, + "grid_export_limit_hit": false + }, + "batteries": [ + { + "charging_power": [ + 0.0, + 2105.2632, + 2105.2632, + 0.0 + ], + "discharging_power": [ + 0.0, + 0.0, + 0.0, + 0.0 + ], + "state_of_charge": [ + 0.0, + 2000.0, + 4000.0, + 4000.0 + ] + } + ], + "grid_import": [ + 0.0, + 0.0, + 0.0, + 0.0 + ], + "grid_export": [ + 2000.0, + 3894.7368, + 3894.7368, + 2000.0 + ], + "flow_direction": [ + 1, + 1, + 1, + 1 + ], + "grid_import_overshoot": [], + "grid_export_overshoot": [] + } +} \ No newline at end of file diff --git a/test_cases/026-attenuate-grid-peaks.json b/test_cases/026-attenuate-grid-peaks.json new file mode 100644 index 000000000..df6eb6f3c --- /dev/null +++ b/test_cases/026-attenuate-grid-peaks.json @@ -0,0 +1,160 @@ +{ + "strict": true, + "request": { + "strategy": { + "charging_strategy": "attenuate_grid_peaks" + }, + "batteries": [ + { + "charge_from_grid": true, + "discharge_to_grid": false, + "s_capacity": 20000, + "s_min": 0, + "s_max": 20000, + "s_initial": 0, + "s_goal": [ + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 12000 + ], + "c_min": 0, + "c_max": 2000, + "d_max": 0, + "p_a": 0 + } + ], + "time_series": { + "dt": [ + 3600, + 3600, + 3600, + 3600, + 3600, + 3600, + 3600, + 3600 + ], + "gt": [ + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0 + ], + "ft": [ + 0, + 0, + 0, + 3000, + 6000, + 6000, + 3000, + 0 + ], + "p_N": [ + 0.3, + 0.3, + 0.3, + 0.3, + 0.3, + 0.3, + 0.3, + 0.3 + ], + "p_E": [ + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1 + ] + }, + "eta_c": 0.95, + "eta_d": 0.95 + }, + "expected_response": { + "status": "Optimal", + "objective_value": -389.47363999999993, + "limit_violations": { + "grid_import_limit_exceeded": false, + "grid_export_limit_hit": false + }, + "batteries": [ + { + "charging_power": [ + 1157.8947, + 1157.8947, + 1157.8947, + 2000.0, + 2000.0, + 2000.0, + 2000.0, + 1157.8947 + ], + "discharging_power": [ + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0 + ], + "state_of_charge": [ + 1100.0, + 2200.0, + 3300.0, + 5200.0, + 7100.0, + 9000.0, + 10900.0, + 12000.0 + ] + } + ], + "grid_import": [ + 1157.8947, + 1157.8947, + 1157.8947, + 0.0, + 0.0, + 0.0, + 0.0, + 1157.8947 + ], + "grid_export": [ + 0.0, + 0.0, + 0.0, + 1000.0, + 4000.0, + 4000.0, + 1000.0, + 0.0 + ], + "flow_direction": [ + 0, + 0, + 0, + 1, + 1, + 1, + 1, + 0 + ], + "grid_import_overshoot": [], + "grid_export_overshoot": [] + } +} \ No newline at end of file diff --git a/test_cases/027-attenuate-feedin-peaks-below-cap.json b/test_cases/027-attenuate-feedin-peaks-below-cap.json new file mode 100644 index 000000000..b3e297c97 --- /dev/null +++ b/test_cases/027-attenuate-feedin-peaks-below-cap.json @@ -0,0 +1,117 @@ +{ + "strict": true, + "request": { + "strategy": { + "charging_strategy": "attenuate_feedin_peaks" + }, + "batteries": [ + { + "charge_from_grid": false, + "discharge_to_grid": false, + "s_capacity": 220, + "s_min": 0, + "s_max": 220, + "s_initial": 0, + "c_min": 0, + "c_max": 50, + "d_max": 0, + "p_a": 0.25 + } + ], + "time_series": { + "dt": [ + 3600, + 3600, + 3600, + 3600, + 3600 + ], + "gt": [ + 0, + 0, + 0, + 0, + 0 + ], + "ft": [ + 1000, + 100, + 100, + 100, + 100 + ], + "p_N": [ + 0.3, + 0.3, + 0.3, + 0.3, + 0.3 + ], + "p_E": [ + 0.08, + 0.08, + 0.08, + 0.08, + 0.08 + ] + }, + "eta_c": 1.0, + "eta_d": 1.0 + }, + "expected_response": { + "status": "Optimal", + "objective_value": 136.89999999999998, + "limit_violations": { + "grid_import_limit_exceeded": false, + "grid_export_limit_hit": false + }, + "batteries": [ + { + "charging_power": [ + 50.0, + 42.5, + 42.5, + 42.5, + 42.5 + ], + "discharging_power": [ + 0.0, + 0.0, + 0.0, + 0.0, + 0.0 + ], + "state_of_charge": [ + 50.0, + 92.5, + 135.0, + 177.5, + 220.0 + ] + } + ], + "grid_import": [ + 0.0, + 0.0, + 0.0, + 0.0, + 0.0 + ], + "grid_export": [ + 950.0, + 57.5, + 57.5, + 57.5, + 57.5 + ], + "flow_direction": [ + 1, + 1, + 1, + 1, + 1 + ], + "grid_import_overshoot": [], + "grid_export_overshoot": [] + } +} \ No newline at end of file diff --git a/tests/test_app.py b/tests/test_app.py index eb3488628..371359c07 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -34,6 +34,35 @@ def test_optimizer(test_case: pathlib.Path): expected_objective_value, rtol=1e-05, atol=1e-08, equal_nan=False), \ f"objective value: {actual_objective_value}, expected was: {expected_objective_value}" + # cases marked strict also compare the schedule itself. needed where the feature under + # test only picks between cost neutral alternatives, which the objective value hides + if test_data.get("strict"): + for key in ("grid_import", "grid_export"): + assert numpy.allclose(response.json[key], expected_response[key], atol=1), \ + f"{key}: {response.json[key]}, expected was: {expected_response[key]}" + for i, battery in enumerate(expected_response["batteries"]): + for key in ("charging_power", "discharging_power"): + actual = response.json["batteries"][i][key] + assert numpy.allclose(actual, battery[key], atol=1), \ + f"battery {i} {key}: {actual}, expected was: {battery[key]}" + + # independent invariant, not a comparison against stored output: with c_min set every step + # charges either nothing or at least c_min, except when the battery is essentially full and + # only a sub-c_min top-off remains. guards against regressions like #19, where p_demand let + # charging slip below c_min far from s_max. + if response.json["status"] == "Optimal": + dt = request["time_series"]["dt"] + for i, battery in enumerate(request["batteries"]): + c_min = battery.get("c_min", 0) + if not c_min: + continue + charging = response.json["batteries"][i]["charging_power"] + soc = response.json["batteries"][i]["state_of_charge"] + for t, charge in enumerate(charging): + step = c_min * dt[t] / 3600 + room = battery["s_max"] - soc[t] + assert charge <= 1 or charge >= step - 1 or room <= step + 1, \ + f"battery {i} t={t}: charges {charge} below c_min step {step} with {room} Wh room to s_max" def test_abort_returns_json_message(): diff --git a/tests/testbench.py b/tests/testbench.py index 8307629f2..e460d577c 100644 --- a/tests/testbench.py +++ b/tests/testbench.py @@ -72,7 +72,10 @@ if response.status_code != 200: print(f"Request to optimizer returned with status {response.status_code}") sys.exit(1) + strict = test_case.get('strict', False) test_case = {} + if strict: + test_case['strict'] = strict test_case['request'] = request test_case['expected_response'] = response.get_json() json.dump(test_case, indent=4, fp=open(file_in, "w"))