Files
EOS/tests/test_genetic0translations.py
T
Bobby NoelteandClaude Opus 5.5 40d0fab9d2 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)
2026-11-27 13:55:41 +01:00

173 lines
6.6 KiB
Python

#!/usr/bin/env python3
"""Quick test to verify API field name translations work correctly."""
import json
from akkudoktoreos.optimization.genetic0.genetic0api import (
Genetic0SimulationResult,
)
from akkudoktoreos.optimization.genetic0.genetic0request import (
Genetic0EnergyManagementRequest,
)
def test_genetic0_params_german_input():
"""Test that German field names are accepted as input."""
data_de = {
"pv_prognose_wh": [100.0, 200.0],
"strompreis_euro_pro_wh": [0.0003, 0.0003],
"einspeiseverguetung_euro_pro_wh": 0.00007,
"preis_euro_pro_wh_akku": 0.0001,
"gesamtlast": [500.0, 600.0],
}
params = Genetic0EnergyManagementRequest.model_validate(data_de)
assert params.pv_forecast_wh == [100.0, 200.0]
print("✅ German input accepted")
def test_genetic0_params_english_input():
"""Test that English field names are accepted as input."""
data_en = {
"pv_forecast_wh": [100.0, 200.0],
"electricity_price_per_wh": [0.0003, 0.0003],
"feed_in_tariff_per_wh": 0.00007,
"price_per_wh_battery": 0.0001,
"total_load": [500.0, 600.0],
}
params = Genetic0EnergyManagementRequest.model_validate(data_en)
assert params.pv_forecast_wh == [100.0, 200.0]
print("✅ English input accepted")
def test_genetic0_params_english_output():
"""Test that both English and German field names are in JSON output (backward compatibility)."""
data_de = {
"pv_prognose_wh": [100.0, 200.0],
"strompreis_euro_pro_wh": [0.0003, 0.0003],
"einspeiseverguetung_euro_pro_wh": 0.00007,
"preis_euro_pro_wh_akku": 0.0001,
"gesamtlast": [500.0, 600.0],
}
params = Genetic0EnergyManagementRequest.model_validate(data_de)
json_output = json.loads(params.model_dump_json(by_alias=True))
# English names should be in output
assert "pv_forecast_wh" in json_output
assert "electricity_price_per_wh" in json_output
assert "feed_in_tariff_per_wh" in json_output
assert "price_per_wh_battery" in json_output
assert "total_load" in json_output
# German names should ALSO be in output (backward compatibility)
assert "pv_prognose_wh" in json_output
assert "strompreis_euro_pro_wh" in json_output
assert "einspeiseverguetung_euro_pro_wh" in json_output
assert "preis_euro_pro_wh_akku" in json_output
assert "gesamtlast" in json_output
# Both should have same values
assert json_output["pv_forecast_wh"] == json_output["pv_prognose_wh"]
assert json_output["electricity_price_per_wh"] == json_output["strompreis_euro_pro_wh"]
print("✅ Both English and German output generated")
def test_genetic0_simulation_result_translations():
"""Test simulation result field translations."""
data_de = {
"Last_Wh_pro_Stunde": [100.0, 200.0],
"EAuto_SoC_pro_Stunde": [50.0, 60.0],
"Einnahmen_Euro_pro_Stunde": [0.1, 0.2],
"Gesamt_Verluste": 50.0,
"Gesamtbilanz_Euro": -10.0,
"Gesamteinnahmen_Euro": 5.0,
"Gesamtkosten_Euro": 15.0,
"Home_appliance_wh_per_hour": [0.0, 100.0],
"Kosten_Euro_pro_Stunde": [0.2, 0.3],
"Netzbezug_Wh_pro_Stunde": [50.0, 60.0],
"Netzeinspeisung_Wh_pro_Stunde": [10.0, 20.0],
"Verluste_Pro_Stunde": [5.0, 10.0],
"akku_soc_pro_stunde": [80.0, 90.0],
"Electricity_price": [0.0003, 0.0003],
}
result = Genetic0SimulationResult.model_validate(data_de)
json_output = json.loads(result.model_dump_json(by_alias=True))
# Check English field names in output
english_names = [
"load_wh_per_hour",
"ev_soc_per_hour",
"revenue_per_hour",
"total_losses",
"total_balance",
"total_revenue",
"total_costs",
"home_appliance_wh_per_hour",
"costs_per_hour",
"grid_consumption_wh_per_hour",
"grid_feed_in_wh_per_hour",
"losses_per_hour",
"battery_soc_per_hour",
"electricity_price",
]
for name in english_names:
assert name in json_output, f"English field {name} missing in output"
# Check ALL deprecated German field names are still in output (backward compatibility)
for name in data_de:
assert name in json_output, f"Deprecated field {name} missing in output"
# Deprecated fields must mirror the values of the renamed fields
assert json_output["Last_Wh_pro_Stunde"] == json_output["load_wh_per_hour"]
assert json_output["Home_appliance_wh_per_hour"] == json_output["home_appliance_wh_per_hour"]
assert json_output["Gesamtbilanz_Euro"] == json_output["total_balance"]
assert json_output["akku_soc_pro_stunde"] == json_output["battery_soc_per_hour"]
assert json_output["Electricity_price"] == json_output["electricity_price"]
print("✅ Simulation result translations work")
if __name__ == "__main__":
test_genetic0_params_german_input()
test_genetic0_params_english_input()
test_genetic0_params_english_output()
test_genetic0_simulation_result_translations()
print("\n✅✅✅ All translation tests passed! ✅✅✅")
def test_genetic0_optimization_parameters_device_translations():
"""Test that German device field names are accepted and re-emitted."""
from akkudoktoreos.optimization.genetic0.genetic0request import (
Genetic0Request,
)
ems_de = {
"pv_prognose_wh": [100.0, 200.0],
"strompreis_euro_pro_wh": [0.0003, 0.0003],
"einspeiseverguetung_euro_pro_wh": 0.00007,
"preis_euro_pro_wh_akku": 0.0001,
"gesamtlast": [500.0, 600.0],
}
params = Genetic0Request.model_validate(dict(
ems=ems_de,
pv_akku={"device_id": "battery1", "capacity_wh": 8000},
inverter=None,
eauto={"device_id": "ev1", "capacity_wh": 60000},
))
# English attributes are populated from the German input names
assert params.pv_battery is not None
assert params.pv_battery.capacity_wh == 8000
assert params.ev is not None
assert params.ev.capacity_wh == 60000
# English input names work as well
params_en = Genetic0Request.model_validate(dict(
ems=ems_de,
pv_battery={"device_id": "battery1", "capacity_wh": 8000},
inverter=None,
ev={"device_id": "ev1", "capacity_wh": 60000},
))
assert params_en.pv_battery is not None
assert params_en.ev is not None
# Both names are present in the output (backward compatibility)
dumped = params.model_dump()
for name in ("pv_battery", "pv_akku", "ev", "eauto"):
assert name in dumped, f"{name} missing in output"
assert dumped["pv_akku"] == dumped["pv_battery"]
assert dumped["eauto"] == dumped["ev"]