mirror of
https://github.com/Akkudoktor-EOS/EOS.git
synced 2026-12-02 18:36:28 +01:00
refactor(genetic): refactor GENETIC to the consolidated optimizer architecture
Pin result, conversion, plan and visualization of golden runs. Drive GENETIC through the optimizer interface and registry. Move GENETIC into one directory with role-named modules. Prepare every run input once, run from the parameters. Fall back instead of cancelling the optimization run. Simulate battery, EV and inverter from frozen dataclasses. Simulate the home appliance from frozen dataclasses. Convert and visualize from the parameters and result only. Serve GENETIC from its own router. Return a frozen result, build responses in the API. Echo the request as sent, not with an invented time window. Additional changes - Pin that the result does not depend on the number of evaluation workers. - Run the golden module in UTC: until the request date-times are held in UTC they render in the host time zone. - Add Optimizer.run_clock(), so the EMS gets slot length and time zone without GENETIC settings. - Rename ConfigOptimizationRequest, RuntimeForecasts and GeneticCommonSettings as ADR 0010 asks. - Correct the request example in ADR 0010. - Record the renamed OpenAPI schemas in CHANGELOG.md and the upgrade guide. - Add replay files that reproduce each golden result from its parameters. - Drop OptimizationBase from the algorithm. - Give the replay and worker tests own review files against xdist races. - Handle a failing preparation in the EMS like a failing run, no separate cancel path. - Record the fallbacks in CHANGELOG.md and the upgrade guide. - Without an inverter, buy the whole load from the grid instead of planning it at no cost. - Do not charge from the grid without any known price. - Treat an infinite value after the control horizon as a gap. - Correct the documentation that said missing forecasts cancel the run. - Copy the efficiency helpers and the self-consumption table evaluation into GENETIC. - Add CycleTimeWindowSequence, ConsumerScheduleMode and ConsumerDeadlinePolicy as contract names. - Hold the date-times of battery, EV and home appliance requests in UTC, so they render the same on every host. - Drop the UTC pin of the GENETIC golden module; the request date-times no longer depend on the host. - Copy the time window logic GENETIC needs into genetictimewindow.py. - Add ValueTimeWindow as a contract name. - Move the settings' to_genetic_*() converters into the preparation. - Drop the conversion's prediction reads and configuration fallbacks, which runs never reached. - Make the moved docstrings valid reST. - Remove the generic optimization solution route; each optimizer serves its own path. - Import the GENETIC settings from the package. - Record the removed route in CHANGELOG.md and the upgrade guide. - Make the terminal and tail value models frozen dataclasses. - Move the response models into geneticapi; remove geneticsolution and geneticabc. - Remove the legacy appliance start arrays and API, which read the wall clock. - Remove the time window helpers only they used. - Echo a start_solution without start_solution_datetime as sent. - Carry the previous solution's start in the run parameters. - Plan the home appliance cycles that fit, or leave the appliance out, instead of failing. - Move the appliance layout rules into geneticappliancelayout for the preparation. - Leave out a DAILY home appliance whose open windows leave no room for a run, with an error log, instead of silently. - Name the reason of the SoC fallback instead of Python's exception text. - Answer a solution request for an unknown algorithm with a 404 problem response. - Record the fixes in CHANGELOG.md and the upgrade guide. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Assisted-by: Claude Code (claude-opus-5-5)
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
b108335ee4
commit
b3d5334d3e
@@ -6,17 +6,18 @@ from unittest.mock import Mock
|
||||
|
||||
import pytest
|
||||
|
||||
from akkudoktoreos.optimization.genetic import geneticprepare
|
||||
from akkudoktoreos.optimization.genetic0.genetic0request import (
|
||||
Genetic0EnergyManagementRequest,
|
||||
)
|
||||
from akkudoktoreos.optimization.genetic.geneticparams import (
|
||||
GeneticEnergyManagementParameters,
|
||||
from akkudoktoreos.optimization.genetic.geneticrequest import (
|
||||
GeneticEnergyManagementRequest,
|
||||
)
|
||||
from akkudoktoreos.optimization.optimization import OptimizationAlgorithm
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"model", [GeneticEnergyManagementParameters, Genetic0EnergyManagementRequest]
|
||||
"model", [GeneticEnergyManagementRequest, Genetic0EnergyManagementRequest]
|
||||
)
|
||||
@pytest.mark.parametrize("tariff", [0.00008, [0.00008, -0.00002]])
|
||||
def test_energy_parameter_aliases_preserve_wh_prices(model, tariff):
|
||||
@@ -46,15 +47,12 @@ def test_energy_parameter_aliases_preserve_wh_prices(model, tariff):
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("algorithm", list(OptimizationAlgorithm))
|
||||
async def test_algorithm_result_endpoint_keeps_solution_types_separate(monkeypatch, algorithm):
|
||||
"""GENETIC is served by the generic route, GENETIC0 by its own router (ADR 0005)."""
|
||||
"""Each optimizer serves its own solution from its router (ADR 0005, 0011)."""
|
||||
from httpx import ASGITransport, AsyncClient
|
||||
|
||||
from akkudoktoreos.server import eos
|
||||
|
||||
fake = SimpleNamespace(
|
||||
genetic_solution=Mock(return_value=None),
|
||||
optimizer_run=Mock(return_value=None),
|
||||
)
|
||||
fake = SimpleNamespace(optimizer_run=Mock(return_value=None))
|
||||
monkeypatch.setattr(eos, "get_ems", lambda: fake)
|
||||
monkeypatch.setattr(
|
||||
eos,
|
||||
@@ -67,12 +65,27 @@ async def test_algorithm_result_endpoint_keeps_solution_types_separate(monkeypat
|
||||
response = await client.get(f"/v1/energy-management/optimization/solution/{algorithm}")
|
||||
assert response.status_code == 404
|
||||
assert response.json()["title"] == "Algorithm specific optimization solution retrieval failed"
|
||||
if algorithm == OptimizationAlgorithm.GENETIC:
|
||||
fake.genetic_solution.assert_called_once_with()
|
||||
fake.optimizer_run.assert_not_called()
|
||||
else:
|
||||
fake.optimizer_run.assert_called_once_with("GENETIC0")
|
||||
fake.genetic_solution.assert_not_called()
|
||||
fake.optimizer_run.assert_called_once_with(str(algorithm))
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("algorithm", ["genetic", "FOO"])
|
||||
async def test_unknown_algorithm_solution_is_a_problem_response(algorithm):
|
||||
"""An algorithm without its own solution endpoint gets a 404 problem response, not HTML."""
|
||||
from httpx import ASGITransport, AsyncClient
|
||||
|
||||
from akkudoktoreos.server import eos
|
||||
|
||||
async with AsyncClient(
|
||||
transport=ASGITransport(app=eos.app, raise_app_exceptions=False), base_url="http://test"
|
||||
) as client:
|
||||
response = await client.get(f"/v1/energy-management/optimization/solution/{algorithm}")
|
||||
assert response.status_code == 404
|
||||
assert response.headers["content-type"].startswith("application/problem+json")
|
||||
problem = response.json()
|
||||
assert problem["title"] == "Unknown optimization algorithm"
|
||||
assert f"'{algorithm}' unknown" in problem["detail"]
|
||||
assert "GENETIC0" in problem["detail"]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("algorithm", ["genetic", "genetic0"])
|
||||
@@ -118,10 +131,10 @@ def test_device_maps_feed_algorithm_specific_converters(algorithm):
|
||||
inverter = genetic0prepare.inverter_request(hybrid)
|
||||
appliance = genetic0prepare.home_appliance_request(washer)
|
||||
else:
|
||||
battery = storage.to_genetic_pv_bat_param()
|
||||
ev = car.to_genetic_ev_bat_param()
|
||||
inverter = hybrid.to_genetic_param()
|
||||
appliance = washer.to_genetic_param()
|
||||
battery = geneticprepare.battery_request(storage)
|
||||
ev = geneticprepare.electric_vehicle_request(car)
|
||||
inverter = geneticprepare.inverter_request(hybrid)
|
||||
appliance = geneticprepare.home_appliance_request(washer)
|
||||
assert (battery.device_id, ev.device_id, inverter.device_id, appliance.device_id) == (
|
||||
"storage",
|
||||
"car",
|
||||
|
||||
Reference in New Issue
Block a user