refactor(genetic0): refactor GENETIC0 to consolidated optimizer architecture

Pin the conversion, plan and visualization of golden runs.
Drive GENETIC0 through the optimizer registry.
Move GENETIC0 into one directory with role-named modules.
Prepare every run input once, run from the parameters.
Fall back on bad inputs instead of cancelling the run.
Simulate from frozen device dataclasses, not settings.
Convert and visualize from the parameters and result only.
Serve GENETIC0 from its own router, return a frozen result.

Additional changes
- Fix the run start of the golden runs and add a run across the DST switch.
- Configure a home appliance so the conversion covers it.
- Pass the warm start to the legacy preparation instead of reading the EMS.
- Replace EnergyManagement.genetic0_solution() by optimizer_result(name).
- Cover the warm start of the parameter preparation.
- Rename Genetic0OptimizationParameters to Genetic0Request, Genetic0CommonSettings to Genetic0Settings.
- Say in ADR 0007 and the skill that a move relabels baseline entries one to one.
- Record the renamed OpenAPI schemas in CHANGELOG.md and the upgrade guide.
- Add the observation time to the optimizer interface.
- Cache the load interpolator by its backend instead of in the EMS run cache.
- Add replay files that reproduce each golden result from its parameters.
- Stop writing demo providers, demo devices and defaults into the configuration.
- Count a NaN state of charge as invalid.
- Remove the unused electricity fee check.
- Record the fallbacks in CHANGELOG.md instead of the former demo providers.
- Leave out a battery that is not linked to the inverter.
- Count infinite forecast values as missing.
- Extend a prediction.hours that leaves fewer than two hours after the start hour.
- Do not charge from the grid without any known price.
- Without an inverter, buy the whole load from the grid instead of planning it at no cost.
- Keep the zone of the run start in the replay files.
- Correct the DVhub documentation that said a missing tariff cancels the run.
- Rename the request models to <Name><Thing>Request.
- Move the settings' to_genetic0_*() converters into the preparation.
- Remove the solution's unreachable configuration fallbacks.
- Record the renamed OpenAPI schemas in CHANGELOG.md and the upgrade guide.
- Add EnergyManagement.optimizer_run() and render the PDF from the run's own start.
- Read the presentation series from the earlier of the run's start hours.
- Render the PDF report for runs of any length, not only 48 hours.
- Add the generic OptimizerProblem, rendered like EOSProblem.
- Move Genetic0ParametersBaseModel into genetic0request.py.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Assisted-by: Claude Code (claude-opus-5-5)
This commit is contained in:
Bobby Noelte
2026-11-27 13:55:41 +01:00
co-authored by Claude Opus 5.5
parent 9adf7b8be2
commit 40d0fab9d2
89 changed files with 44476 additions and 3933 deletions
+46 -40
View File
@@ -17,18 +17,16 @@ from unittest.mock import Mock, patch
import numpy as np
import pytest
from akkudoktoreos.devices.genetic0.genetic0battery import (
Genetic0Battery,
Genetic0SolarPanelBatteryParameters,
)
from akkudoktoreos.devices.genetic0.genetic0inverter import (
from akkudoktoreos.optimization.genetic0.devices.genetic0battery import Genetic0Battery
from akkudoktoreos.optimization.genetic0.devices.genetic0inverter import (
Genetic0Inverter,
Genetic0InverterParameters,
)
from akkudoktoreos.optimization.genetic0.genetic0 import (
Genetic0EnergyManagementParameters,
Genetic0Simulation,
from akkudoktoreos.optimization.genetic0.genetic0request import (
Genetic0EnergyManagementRequest,
Genetic0InverterRequest,
Genetic0SolarPanelBatteryRequest,
)
from akkudoktoreos.optimization.genetic0.genetic0simulation import Genetic0Simulation
# ---------------------------------------------------------------------------
# Helpers / Fixtures
@@ -46,7 +44,7 @@ def _make_inverter(
mock_self_consumption_predictor = Mock()
mock_self_consumption_predictor.calculate_self_consumption.return_value = 1.0
params = Genetic0InverterParameters(
params = Genetic0InverterRequest(
device_id="inv1",
max_power_wh=max_power_wh,
battery_id=mock_battery.parameters.device_id if mock_battery else None,
@@ -55,10 +53,10 @@ def _make_inverter(
max_ac_charge_power_w=max_ac_charge_power_w,
)
with patch(
"akkudoktoreos.devices.genetic0.genetic0inverter.get_genetic0_load_interpolator",
"akkudoktoreos.optimization.genetic0.devices.genetic0inverter.get_genetic0_load_interpolator",
return_value=mock_self_consumption_predictor,
):
return Genetic0Inverter(params, battery=mock_battery)
return Genetic0Inverter(params.to_parameters(), battery=mock_battery)
@pytest.fixture
@@ -79,13 +77,13 @@ class TestInverterParametersDefaults:
"""Verify backward-compatible defaults for new parameters."""
def test_defaults(self):
params = Genetic0InverterParameters(device_id="inv1", max_power_wh=5000)
params = Genetic0InverterRequest(device_id="inv1", max_power_wh=5000)
assert params.dc_to_ac_efficiency == 1.0
assert params.ac_to_dc_efficiency == 1.0
assert params.max_ac_charge_power_w is None
def test_custom_values(self):
params = Genetic0InverterParameters(
params = Genetic0InverterRequest(
device_id="inv1",
max_power_wh=5000,
dc_to_ac_efficiency=0.95,
@@ -97,17 +95,17 @@ class TestInverterParametersDefaults:
assert params.max_ac_charge_power_w == 3000
def test_ac_to_dc_zero_disables_ac_charging(self):
params = Genetic0InverterParameters(
params = Genetic0InverterRequest(
device_id="inv1", max_power_wh=5000, ac_to_dc_efficiency=0.0
)
assert params.ac_to_dc_efficiency == 0.0
def test_dc_to_ac_must_be_positive(self):
with pytest.raises(Exception):
Genetic0InverterParameters(device_id="inv1", max_power_wh=5000, dc_to_ac_efficiency=0.0)
Genetic0InverterRequest(device_id="inv1", max_power_wh=5000, dc_to_ac_efficiency=0.0)
def test_max_ac_charge_power_zero(self):
params = Genetic0InverterParameters(
params = Genetic0InverterRequest(
device_id="inv1", max_power_wh=5000, max_ac_charge_power_w=0
)
assert params.max_ac_charge_power_w == 0
@@ -215,9 +213,11 @@ class TestAcChargingInSimulation:
@pytest.fixture
def simulation_setup(self, config_eos):
"""Set up a minimal GeneticSimulation with battery and inverter."""
from akkudoktoreos.optimization.genetic0.genetic0 import Genetic0Simulation
from akkudoktoreos.optimization.genetic0.genetic0params import (
Genetic0EnergyManagementParameters,
from akkudoktoreos.optimization.genetic0.genetic0request import (
Genetic0EnergyManagementRequest,
)
from akkudoktoreos.optimization.genetic0.genetic0simulation import (
Genetic0Simulation,
)
config_eos.merge_settings_from_dict(
@@ -237,7 +237,7 @@ class TestAcChargingInSimulation:
battery_max_charge_power_w: int = 5000,
):
akku = Genetic0Battery(
Genetic0SolarPanelBatteryParameters(
Genetic0SolarPanelBatteryRequest(
device_id="battery1",
capacity_wh=battery_capacity_wh,
initial_soc_percentage=battery_initial_soc_pct,
@@ -246,32 +246,32 @@ class TestAcChargingInSimulation:
min_soc_percentage=0,
max_soc_percentage=100,
max_charge_power_w=battery_max_charge_power_w,
),
).to_parameters(),
prediction_hours=prediction_hours,
)
akku.reset()
inverter = Genetic0Inverter(
Genetic0InverterParameters(
Genetic0InverterRequest(
device_id="inverter1",
max_power_wh=10000,
battery_id="battery1",
ac_to_dc_efficiency=ac_to_dc_efficiency,
dc_to_ac_efficiency=dc_to_ac_efficiency,
max_ac_charge_power_w=max_ac_charge_power_w,
),
).to_parameters(),
battery=akku,
)
simulation = Genetic0Simulation()
simulation.prepare(
Genetic0EnergyManagementParameters.model_validate(dict(
Genetic0EnergyManagementRequest.model_validate(dict(
pv_prognose_wh=[0.0] * prediction_hours, # No PV
strompreis_euro_pro_wh=[0.0003] * prediction_hours, # ~30ct/kWh
einspeiseverguetung_euro_pro_wh=0.00008,
preis_euro_pro_wh_akku=0.0001,
gesamtlast=[1000.0] * prediction_hours, # 1 kW constant load
)),
)).to_parameters(),
optimization_hours=config_eos.optimization.genetic0.horizon_hours,
prediction_hours=prediction_hours,
inverter=inverter,
@@ -466,13 +466,13 @@ class TestResidualBatteryValue:
def test_current_energy_content_unaffected(self):
"""Battery.current_energy_content() is DC-only; inverter eff applied in optimizer."""
akku = Genetic0Battery(
Genetic0SolarPanelBatteryParameters(
Genetic0SolarPanelBatteryRequest(
device_id="bat1",
capacity_wh=10000,
initial_soc_percentage=50,
discharging_efficiency=0.90,
min_soc_percentage=0,
),
).to_parameters(),
prediction_hours=24,
)
akku.reset()
@@ -550,22 +550,28 @@ def _run_evaluate_with_mocked_sim(
- self.simulation is replaced by mock_sim
Then call evaluate() and return the fitness tuple.
"""
from akkudoktoreos.optimization.genetic0.genetic0 import Genetic0Optimization
config_eos.merge_settings_from_dict(
{
"prediction": {"hours": 48},
"optimization": {"hours": 24},
}
from akkudoktoreos.optimization.genetic0.genetic0algorithm import (
Genetic0Optimization,
)
config_eos.optimization.genetic0.penalties = {
"ev_soc_miss": 10,
"ac_charge_break_even": ac_charge_break_even,
}
from akkudoktoreos.optimization.genetic0.genetic0parameters import (
Genetic0RunSettings,
)
from akkudoktoreos.utils.gridinterpolator import GridInterpolatorBackend
optim = Genetic0Optimization.__new__(Genetic0Optimization)
# Minimal __init__ state expected by evaluate()
optim.config = config_eos
optim.settings = Genetic0RunSettings(
prediction_hours=48,
horizon_hours=24,
individuals=None,
generations=None,
seed=None,
penalties={"ev_soc_miss": 10, "ac_charge_break_even": ac_charge_break_even},
ev_charge_rates=None,
battery_charge_rates=None,
self_consumption_interpolator=GridInterpolatorBackend.SCIPY,
verbose=False,
)
optim.optimize_ev = False
optim.verbose = False
optim.opti_param = {"home_appliance": 0}