mirror of
https://github.com/Akkudoktor-EOS/EOS.git
synced 2026-12-02 18:36:28 +01:00
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)
143 lines
5.5 KiB
Python
143 lines
5.5 KiB
Python
"""Contracts shared by the configuration, tariff and optimizer PRs."""
|
|
|
|
from types import SimpleNamespace
|
|
from typing import Any
|
|
from unittest.mock import Mock
|
|
|
|
import pytest
|
|
|
|
from akkudoktoreos.optimization.genetic0.genetic0request import (
|
|
Genetic0EnergyManagementRequest,
|
|
)
|
|
from akkudoktoreos.optimization.genetic.geneticparams import (
|
|
GeneticEnergyManagementParameters,
|
|
)
|
|
from akkudoktoreos.optimization.optimization import OptimizationAlgorithm
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"model", [GeneticEnergyManagementParameters, Genetic0EnergyManagementRequest]
|
|
)
|
|
@pytest.mark.parametrize("tariff", [0.00008, [0.00008, -0.00002]])
|
|
def test_energy_parameter_aliases_preserve_wh_prices(model, tariff):
|
|
raw = {
|
|
"pv_prognose_wh": [250.0, 0.0],
|
|
"gesamtlast": [125.0, 125.0],
|
|
"strompreis_euro_pro_wh": [0.0003, -0.0001],
|
|
"einspeiseverguetung_euro_pro_wh": tariff,
|
|
"preis_euro_pro_wh_akku": 0.00005,
|
|
}
|
|
params = model.model_validate(raw)
|
|
data = params.model_dump(mode="json")
|
|
pairs = {
|
|
"pv_prognose_wh": "pv_forecast_wh",
|
|
"gesamtlast": "total_load",
|
|
"strompreis_euro_pro_wh": "electricity_price_per_wh",
|
|
"einspeiseverguetung_euro_pro_wh": "feed_in_tariff_per_wh",
|
|
"preis_euro_pro_wh_akku": "price_per_wh_battery",
|
|
}
|
|
for deprecated, canonical in pairs.items():
|
|
assert data[deprecated] == data[canonical] == raw[deprecated]
|
|
assert (
|
|
model.model_validate({pairs[k]: v for k, v in raw.items()}).model_dump(mode="json") == data
|
|
)
|
|
|
|
|
|
@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)."""
|
|
from httpx import ASGITransport, AsyncClient
|
|
|
|
from akkudoktoreos.server import eos
|
|
|
|
fake = SimpleNamespace(
|
|
genetic_solution=Mock(return_value=None),
|
|
optimizer_run=Mock(return_value=None),
|
|
)
|
|
monkeypatch.setattr(eos, "get_ems", lambda: fake)
|
|
monkeypatch.setattr(
|
|
eos,
|
|
"get_config",
|
|
lambda: SimpleNamespace(optimization=SimpleNamespace(algorithms=["GENETIC", "GENETIC0"])),
|
|
)
|
|
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.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()
|
|
|
|
|
|
@pytest.mark.parametrize("algorithm", ["genetic", "genetic0"])
|
|
def test_device_maps_feed_algorithm_specific_converters(algorithm):
|
|
from akkudoktoreos.devices.devices import DevicesCommonSettings
|
|
|
|
settings = DevicesCommonSettings.model_validate(
|
|
{
|
|
"batteries": {
|
|
"storage": {"profile_adjustment": {"nominal_capacity_wh": 12000, "max_charge_power_w": 3200}}
|
|
},
|
|
"electric_vehicles": {
|
|
"car": {"profile_adjustment": {"nominal_capacity_wh": 60000, "max_charge_power_w": 7000}}
|
|
},
|
|
"inverters": {"inverter": {"battery_id": "storage", "profile_adjustment": {"nominal_power_w": 5000}}},
|
|
"home_appliances": {
|
|
"washer": {
|
|
"consumption_wh": 1800,
|
|
"duration_h": 2,
|
|
"num_cycles": 2,
|
|
"min_cycle_gap_h": 1,
|
|
}
|
|
},
|
|
}
|
|
)
|
|
assert settings.batteries is not None
|
|
assert settings.electric_vehicles is not None
|
|
assert settings.inverters is not None
|
|
storage = settings.batteries["storage"]
|
|
car = settings.electric_vehicles["car"]
|
|
hybrid = settings.inverters["inverter"]
|
|
washer = settings.home_appliances["washer"]
|
|
battery: Any
|
|
ev: Any
|
|
inverter: Any
|
|
appliance: Any
|
|
if algorithm == "genetic0":
|
|
# GENETIC0 converts the device settings in its parameter preparation (ADR 0004).
|
|
from akkudoktoreos.optimization.genetic0 import genetic0prepare
|
|
|
|
battery = genetic0prepare.battery_request(storage)
|
|
ev = genetic0prepare.electric_vehicle_request(car)
|
|
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()
|
|
assert (battery.device_id, ev.device_id, inverter.device_id, appliance.device_id) == (
|
|
"storage",
|
|
"car",
|
|
"inverter",
|
|
"washer",
|
|
)
|
|
assert battery.capacity_wh == 12000
|
|
assert battery.max_charge_power_w == 3200
|
|
assert ev.capacity_wh == 60000
|
|
assert ev.max_charge_power_w == 7000
|
|
assert inverter.max_power_wh == 5000
|
|
assert inverter.battery_id == "storage"
|
|
assert appliance.consumption_wh == 1800
|
|
assert appliance.duration_h == 2
|
|
assert settings.batteries["storage"].measurement_key_soc_factor == "storage-soc-factor"
|
|
if algorithm == "genetic":
|
|
assert appliance.num_cycles == 2
|
|
assert appliance.min_cycle_gap_h == 1
|