2026-09-16 18:20:49 +02:00
|
|
|
"""Offline regression tests for per-run optimization results and atomic publication."""
|
|
|
|
|
|
|
|
|
|
from asyncio import Lock
|
|
|
|
|
from types import SimpleNamespace
|
2026-09-16 18:45:19 +02:00
|
|
|
from typing import Any
|
2026-11-23 20:23:26 +00:00
|
|
|
from unittest.mock import ANY, AsyncMock, Mock
|
2026-09-16 18:20:49 +02:00
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
from akkudoktoreos.core import ems as ems_module
|
|
|
|
|
from akkudoktoreos.core.emsettings import EnergyManagementMode
|
2026-11-23 20:23:26 +00:00
|
|
|
from akkudoktoreos.optimization.genetic import geneticalgorithm as genetic_module
|
|
|
|
|
from akkudoktoreos.optimization.genetic import geneticconvert, geneticprepare
|
2026-11-23 20:23:25 +00:00
|
|
|
from akkudoktoreos.optimization.genetic0 import genetic0algorithm as genetic0_module
|
|
|
|
|
from akkudoktoreos.optimization.genetic0 import genetic0convert, genetic0prepare
|
|
|
|
|
from akkudoktoreos.optimization.genetic0.genetic0request import (
|
|
|
|
|
Genetic0Request,
|
2026-09-16 18:20:49 +02:00
|
|
|
)
|
2026-11-23 20:23:26 +00:00
|
|
|
from akkudoktoreos.optimization.genetic.geneticrequest import GeneticRequest
|
2026-09-16 18:20:49 +02:00
|
|
|
from akkudoktoreos.optimization.optimization import OptimizationAlgorithm
|
2026-11-23 20:23:25 +00:00
|
|
|
from akkudoktoreos.optimization.optimizer import OptimizerRun
|
2026-09-16 18:20:49 +02:00
|
|
|
from akkudoktoreos.utils.datetimeutil import to_datetime
|
2026-11-23 20:23:25 +00:00
|
|
|
from akkudoktoreos.utils.gridinterpolator import GridInterpolatorBackend
|
2026-09-16 18:20:49 +02:00
|
|
|
|
|
|
|
|
|
2026-10-11 08:40:43 +02:00
|
|
|
def _patch_optimizer(monkeypatch, prefix: str, constructor: Any) -> None:
|
2026-11-23 20:23:26 +00:00
|
|
|
"""Replace the optimizer class that the optimizer's ``run()`` instantiates.
|
2026-10-11 08:40:43 +02:00
|
|
|
|
2026-11-23 20:23:26 +00:00
|
|
|
Both are imported on first use inside their optimizer, so they are patched at their source.
|
2026-10-11 08:40:43 +02:00
|
|
|
"""
|
2026-11-23 20:23:26 +00:00
|
|
|
module = genetic0_module if prefix == "Genetic0" else genetic_module
|
2026-10-11 08:40:43 +02:00
|
|
|
monkeypatch.setattr(module, prefix + "Optimization", constructor)
|
|
|
|
|
|
|
|
|
|
|
2026-11-23 20:23:25 +00:00
|
|
|
def _patch_prepare(monkeypatch, prefix: str, prepare: Any) -> None:
|
2026-11-23 20:23:26 +00:00
|
|
|
"""Replace the forecast and device preparation of GENETIC or GENETIC0.
|
|
|
|
|
|
|
|
|
|
GENETIC resolves every request, the configured default one included, through
|
|
|
|
|
``resolve_request()``; GENETIC0's legacy preparation serves runs without a request only.
|
|
|
|
|
"""
|
2026-11-23 20:23:25 +00:00
|
|
|
if prefix == "Genetic0":
|
|
|
|
|
monkeypatch.setattr(genetic0prepare, "prepare_request", prepare)
|
|
|
|
|
else:
|
2026-11-23 20:23:26 +00:00
|
|
|
monkeypatch.setattr(geneticprepare, "resolve_request", prepare)
|
2026-11-23 20:23:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def _warm_start_of(call: Any) -> Any:
|
|
|
|
|
"""The start solution a call of GENETIC0's legacy preparation got."""
|
|
|
|
|
start, observation, start_solution = call.args
|
|
|
|
|
return start_solution
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _assert_prepared_without_request(prefix: str, prepare: Any) -> None:
|
2026-11-23 20:23:26 +00:00
|
|
|
"""Both pass the warm start (none here) into their preparation; GENETIC a default request."""
|
|
|
|
|
prepare.assert_awaited_once()
|
2026-11-23 20:23:25 +00:00
|
|
|
if prefix == "Genetic0":
|
|
|
|
|
assert _warm_start_of(prepare.await_args) is None
|
|
|
|
|
else:
|
2026-11-23 20:23:26 +00:00
|
|
|
request, start, observation, warm_start = prepare.await_args.args
|
|
|
|
|
assert request.model_dump() == GeneticRequest().model_dump()
|
|
|
|
|
assert warm_start is None
|
2026-11-23 20:23:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def _conversion_arguments(prefix: str) -> dict[str, Any]:
|
|
|
|
|
"""Arguments of the stand-in results' conversion methods (none for both optimizers)."""
|
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _native_solution(prefix: str) -> Any:
|
|
|
|
|
"""The native solution the latest run published for GENETIC or GENETIC0."""
|
2026-11-23 20:23:26 +00:00
|
|
|
return ems_module.EnergyManagement.optimizer_result(prefix.upper())
|
2026-11-23 20:23:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def _set_native_solution(monkeypatch, prefix: str, solution: Any) -> None:
|
|
|
|
|
"""Publish ``solution`` as the result of a previous run of GENETIC or GENETIC0."""
|
2026-11-23 20:23:26 +00:00
|
|
|
runs = {prefix.upper(): OptimizerRun(parameters=object(), result=solution)}
|
|
|
|
|
monkeypatch.setattr(ems_module.EnergyManagement, "_optimizer_runs", runs)
|
2026-11-23 20:23:25 +00:00
|
|
|
|
|
|
|
|
|
2026-11-23 20:23:26 +00:00
|
|
|
def _stand_in_request() -> SimpleNamespace:
|
|
|
|
|
"""A request both preparations accept.
|
|
|
|
|
|
|
|
|
|
GENETIC0's preparation looks at its home appliance, GENETIC's at its start solution and its
|
|
|
|
|
home appliances.
|
|
|
|
|
"""
|
|
|
|
|
return SimpleNamespace(
|
|
|
|
|
dishwasher=None,
|
|
|
|
|
start_solution=None,
|
|
|
|
|
start_solution_datetime=None,
|
|
|
|
|
resolved_home_appliances=lambda: [],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _supplied(parameters: Any, generations: int, seed: int) -> dict[str, Any]:
|
2026-11-23 20:23:25 +00:00
|
|
|
"""Run arguments that supply the parameters, generations and seed of a run."""
|
2026-11-23 20:23:26 +00:00
|
|
|
return {"request": parameters, "overrides": {"generations": generations, "seed": seed}}
|
2026-11-23 20:23:25 +00:00
|
|
|
|
|
|
|
|
|
2026-09-16 18:20:49 +02:00
|
|
|
@pytest.fixture
|
|
|
|
|
def offline_ems(monkeypatch):
|
|
|
|
|
"""Run the actual EMS orchestration with no real adapters or prediction IO."""
|
|
|
|
|
cls = ems_module.EnergyManagement
|
|
|
|
|
for name in (
|
|
|
|
|
"_start_datetime",
|
2026-09-17 20:14:24 +02:00
|
|
|
"_observation_datetime",
|
2026-09-16 18:20:49 +02:00
|
|
|
"_last_run_datetime",
|
|
|
|
|
"_plan",
|
|
|
|
|
"_optimization_solution",
|
|
|
|
|
):
|
|
|
|
|
monkeypatch.setattr(cls, name, None)
|
2026-11-23 20:23:25 +00:00
|
|
|
monkeypatch.setattr(cls, "_optimizer_runs", {})
|
2026-09-16 18:20:49 +02:00
|
|
|
monkeypatch.setattr(cls, "_stage", ems_module.EnergyManagementStage.IDLE)
|
|
|
|
|
monkeypatch.setattr(cls, "_run_lock", Lock())
|
|
|
|
|
monkeypatch.setattr(ems_module, "CacheEnergyManagementStore", Mock())
|
2026-11-23 20:23:25 +00:00
|
|
|
ems = SimpleNamespace(
|
2026-09-16 18:20:49 +02:00
|
|
|
config=SimpleNamespace(
|
2026-09-17 20:14:24 +02:00
|
|
|
general=SimpleNamespace(timezone="Europe/Berlin"),
|
2026-09-16 18:20:49 +02:00
|
|
|
ems=SimpleNamespace(mode=EnergyManagementMode.OPTIMIZATION),
|
|
|
|
|
optimization=SimpleNamespace(
|
|
|
|
|
algorithm=OptimizationAlgorithm.GENETIC,
|
2026-11-23 20:23:26 +00:00
|
|
|
self_consumption_interpolator="scipy",
|
|
|
|
|
genetic=SimpleNamespace(
|
|
|
|
|
generations=3,
|
|
|
|
|
seed=17,
|
|
|
|
|
interval_sec=3600,
|
|
|
|
|
individuals=31,
|
|
|
|
|
horizon_hours=24,
|
|
|
|
|
tail_horizon_hours=0,
|
|
|
|
|
penalties={},
|
|
|
|
|
workers=None,
|
|
|
|
|
pin_workers=True,
|
|
|
|
|
fitness_cache_max_entries=None,
|
|
|
|
|
terminal_value_mode="AUTO",
|
|
|
|
|
terminal_value_window_hours=24,
|
|
|
|
|
),
|
2026-11-23 20:23:25 +00:00
|
|
|
genetic0=SimpleNamespace(
|
|
|
|
|
generations=5, seed=29, horizon_hours=24, individuals=13, penalties={}
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
prediction=SimpleNamespace(hours=48),
|
2026-11-23 20:23:26 +00:00
|
|
|
feedintariff=SimpleNamespace(provider=None, direct_marketing_enabled=False),
|
2026-11-23 20:23:25 +00:00
|
|
|
devices=SimpleNamespace(
|
|
|
|
|
batteries=None, electric_vehicles=None, max_home_appliances=0
|
2026-09-16 18:20:49 +02:00
|
|
|
),
|
|
|
|
|
server=SimpleNamespace(verbose=False),
|
|
|
|
|
),
|
|
|
|
|
prediction=SimpleNamespace(update_data=AsyncMock()),
|
|
|
|
|
adapter=SimpleNamespace(update_data=AsyncMock()),
|
|
|
|
|
set_start_datetime=cls.set_start_datetime,
|
|
|
|
|
)
|
2026-11-23 20:23:26 +00:00
|
|
|
# The parameter preparations read the settings (and GENETIC0's the predictions) themselves.
|
|
|
|
|
monkeypatch.setattr(geneticprepare, "get_config", lambda: ems.config)
|
|
|
|
|
# Stand-in requests are already resolved.
|
|
|
|
|
monkeypatch.setattr(
|
|
|
|
|
geneticprepare, "resolve_request", AsyncMock(side_effect=lambda request, *args: request)
|
|
|
|
|
)
|
2026-11-23 20:23:25 +00:00
|
|
|
monkeypatch.setattr(genetic0prepare, "get_config", lambda: ems.config)
|
|
|
|
|
monkeypatch.setattr(
|
|
|
|
|
genetic0prepare, "get_prediction", lambda: SimpleNamespace(record_keys=[])
|
|
|
|
|
)
|
|
|
|
|
monkeypatch.setattr(
|
|
|
|
|
genetic0prepare,
|
|
|
|
|
"configured_grid_interpolator_backend",
|
|
|
|
|
lambda: GridInterpolatorBackend.SCIPY,
|
|
|
|
|
)
|
|
|
|
|
# Stand-in results carry their own conversion methods.
|
|
|
|
|
async def optimization_solution(parameters: Any, result: Any) -> Any:
|
|
|
|
|
return await result.optimization_solution()
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(genetic0convert, "optimization_solution", optimization_solution)
|
2026-11-23 20:23:26 +00:00
|
|
|
monkeypatch.setattr(geneticconvert, "optimization_solution", optimization_solution)
|
|
|
|
|
monkeypatch.setattr(
|
|
|
|
|
geneticconvert,
|
|
|
|
|
"energy_management_plan",
|
|
|
|
|
lambda parameters, result: result.energy_management_plan(),
|
|
|
|
|
)
|
2026-11-23 20:23:25 +00:00
|
|
|
monkeypatch.setattr(
|
|
|
|
|
genetic0convert,
|
|
|
|
|
"energy_management_plan",
|
|
|
|
|
lambda parameters, result: result.energy_management_plan(),
|
|
|
|
|
)
|
|
|
|
|
# Stand-in requests have no forecasts to derive the simulation's dataclasses from.
|
|
|
|
|
monkeypatch.setattr(
|
|
|
|
|
genetic0prepare.Genetic0Parameters,
|
|
|
|
|
"from_request",
|
|
|
|
|
classmethod(lambda cls, **fields: SimpleNamespace(**fields)),
|
|
|
|
|
)
|
|
|
|
|
return ems
|
2026-09-16 18:20:49 +02:00
|
|
|
|
|
|
|
|
|
2026-09-17 20:14:24 +02:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
@pytest.mark.parametrize("algorithm", list(OptimizationAlgorithm))
|
|
|
|
|
@pytest.mark.parametrize("explicit_start", [False, True])
|
|
|
|
|
async def test_implicit_genetic_start_uses_site_timezone(
|
|
|
|
|
offline_ems, monkeypatch, set_other_timezone, algorithm, explicit_start
|
|
|
|
|
):
|
|
|
|
|
set_other_timezone("UTC")
|
|
|
|
|
now = to_datetime("2026-09-16T22:47:23Z", in_timezone="UTC")
|
|
|
|
|
|
|
|
|
|
def frozen_datetime(value=None, **kwargs):
|
|
|
|
|
return to_datetime(now if value is None else value, **kwargs)
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(ems_module, "to_datetime", frozen_datetime)
|
|
|
|
|
offline_ems.config.optimization.genetic.interval_sec = 900
|
|
|
|
|
await ems_module.EnergyManagement.run(
|
|
|
|
|
offline_ems,
|
|
|
|
|
mode=EnergyManagementMode.PREDICTION,
|
|
|
|
|
algorithm=algorithm,
|
|
|
|
|
start_datetime=now if explicit_start else None,
|
|
|
|
|
)
|
|
|
|
|
start = ems_module.EnergyManagement._start_datetime
|
|
|
|
|
observed = ems_module.EnergyManagement._observation_datetime
|
|
|
|
|
assert start is not None and observed is not None
|
|
|
|
|
assert observed.timestamp() == now.timestamp()
|
|
|
|
|
if algorithm == OptimizationAlgorithm.GENETIC:
|
|
|
|
|
assert start.minute == 45
|
|
|
|
|
assert start.hour == (22 if explicit_start else 0)
|
|
|
|
|
assert start.day == (16 if explicit_start else 17)
|
|
|
|
|
assert start.timezone_name == ("UTC" if explicit_start else "Europe/Berlin")
|
|
|
|
|
else:
|
|
|
|
|
assert start.minute == 0
|
|
|
|
|
assert start.hour == 22
|
|
|
|
|
assert start.day == 16
|
|
|
|
|
assert start.timezone_name == "UTC"
|
|
|
|
|
|
|
|
|
|
|
2026-09-16 18:20:49 +02:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
@pytest.mark.parametrize("algorithm", list(OptimizationAlgorithm))
|
|
|
|
|
@pytest.mark.parametrize("selection", ["configured", "explicit"])
|
|
|
|
|
@pytest.mark.parametrize("supplied", [False, True])
|
2026-09-16 19:14:02 +02:00
|
|
|
@pytest.mark.parametrize("timezone, expected_hour", [("UTC", 8), ("Europe/Berlin", 10)])
|
2026-09-16 18:20:49 +02:00
|
|
|
async def test_optimization_routes_only_selected_algorithm(
|
2026-09-16 19:14:02 +02:00
|
|
|
monkeypatch, offline_ems, algorithm, selection, supplied, timezone, expected_hour
|
2026-09-16 18:20:49 +02:00
|
|
|
):
|
|
|
|
|
"""Configuration selection and explicit overrides retain isolated async paths."""
|
|
|
|
|
selected_name = "Genetic" if algorithm == OptimizationAlgorithm.GENETIC else "Genetic0"
|
2026-11-23 20:23:25 +00:00
|
|
|
other_name = "Genetic0" if selected_name == "Genetic" else "Genetic"
|
2026-09-16 18:20:49 +02:00
|
|
|
suffix = "genetic" if algorithm == OptimizationAlgorithm.GENETIC else "genetic0"
|
|
|
|
|
other_suffix = "genetic0" if suffix == "genetic" else "genetic"
|
2026-11-23 20:23:26 +00:00
|
|
|
sentinel_parameters = _stand_in_request()
|
2026-09-16 18:20:49 +02:00
|
|
|
sentinel_result, sentinel_plan = object(), object()
|
|
|
|
|
solution = SimpleNamespace(
|
|
|
|
|
optimization_solution=AsyncMock(return_value=sentinel_result),
|
|
|
|
|
energy_management_plan=Mock(return_value=sentinel_plan),
|
|
|
|
|
)
|
|
|
|
|
constructors = {}
|
|
|
|
|
preparers = {}
|
|
|
|
|
for prefix in ("Genetic", "Genetic0"):
|
|
|
|
|
constructor = Mock()
|
|
|
|
|
constructor.return_value.optimize_ems.return_value = solution
|
|
|
|
|
constructors[prefix] = constructor
|
2026-10-11 08:40:43 +02:00
|
|
|
_patch_optimizer(monkeypatch, prefix, constructor)
|
2026-09-16 18:20:49 +02:00
|
|
|
prepare = AsyncMock(return_value=sentinel_parameters)
|
|
|
|
|
preparers[prefix] = prepare
|
2026-11-23 20:23:25 +00:00
|
|
|
_patch_prepare(monkeypatch, prefix, prepare)
|
2026-09-16 19:14:02 +02:00
|
|
|
kwargs: dict[str, Any] = {
|
|
|
|
|
"start_datetime": to_datetime("2026-09-16T10:00:00+02:00", in_timezone=timezone)
|
|
|
|
|
}
|
2026-09-16 18:20:49 +02:00
|
|
|
if selection == "configured":
|
|
|
|
|
offline_ems.config.optimization.algorithm = algorithm
|
|
|
|
|
else:
|
|
|
|
|
offline_ems.config.optimization.algorithm = OptimizationAlgorithm(other_suffix.upper())
|
|
|
|
|
kwargs["algorithm"] = algorithm
|
|
|
|
|
if supplied:
|
2026-11-23 20:23:26 +00:00
|
|
|
kwargs.update(_supplied(sentinel_parameters, 7, 43))
|
2026-09-17 20:14:24 +02:00
|
|
|
if algorithm == OptimizationAlgorithm.GENETIC:
|
2026-11-23 20:23:26 +00:00
|
|
|
kwargs["overrides"]["individuals"] = 11
|
2026-09-16 18:20:49 +02:00
|
|
|
run_result = await ems_module.EnergyManagement.run(offline_ems, **kwargs)
|
|
|
|
|
assert run_result is solution
|
|
|
|
|
selected = constructors[selected_name]
|
|
|
|
|
expected_config = getattr(offline_ems.config.optimization, suffix)
|
2026-11-23 20:23:25 +00:00
|
|
|
expected_seed = 43 if supplied else expected_config.seed
|
|
|
|
|
expected_generations = 7 if supplied else expected_config.generations
|
2026-09-17 20:14:24 +02:00
|
|
|
expected_arguments: dict[str, Any] = {
|
|
|
|
|
"start_hour": expected_hour,
|
|
|
|
|
"parameters": sentinel_parameters,
|
|
|
|
|
}
|
2026-11-23 20:23:26 +00:00
|
|
|
# Both get their algorithm settings from the parameters (step 3).
|
|
|
|
|
(settings,) = selected.call_args.args
|
|
|
|
|
assert (settings.seed, settings.generations, settings.verbose) == (
|
|
|
|
|
expected_seed,
|
|
|
|
|
expected_generations,
|
|
|
|
|
False,
|
|
|
|
|
)
|
2026-11-23 20:23:25 +00:00
|
|
|
if algorithm == OptimizationAlgorithm.GENETIC:
|
|
|
|
|
expected_arguments["ngen"] = expected_generations
|
2026-11-23 20:23:26 +00:00
|
|
|
expected_arguments["individuals"] = 11 if supplied else expected_config.individuals
|
|
|
|
|
# The start of the start solution comes from the parameters, not their request.
|
|
|
|
|
expected_arguments["start_solution_datetime"] = None
|
2026-11-23 20:23:25 +00:00
|
|
|
selected.return_value.optimize_ems.assert_called_once_with(**expected_arguments)
|
|
|
|
|
else:
|
|
|
|
|
# GENETIC0 runs from its parameters, which carry the request and the start hour.
|
|
|
|
|
(parameters,) = selected.return_value.optimize_ems.call_args.args
|
|
|
|
|
assert parameters.request is sentinel_parameters
|
|
|
|
|
assert parameters.start_hour == expected_hour
|
2026-09-16 18:20:49 +02:00
|
|
|
constructors[other_name].assert_not_called()
|
|
|
|
|
preparers[other_name].assert_not_awaited()
|
2026-11-23 20:23:26 +00:00
|
|
|
if supplied and algorithm == OptimizationAlgorithm.GENETIC:
|
|
|
|
|
# GENETIC completes the supplied request.
|
|
|
|
|
preparers[selected_name].assert_awaited_once_with(
|
|
|
|
|
sentinel_parameters, ANY, ANY, None
|
|
|
|
|
)
|
|
|
|
|
elif supplied:
|
2026-09-16 18:20:49 +02:00
|
|
|
preparers[selected_name].assert_not_awaited()
|
|
|
|
|
else:
|
2026-11-23 20:23:25 +00:00
|
|
|
_assert_prepared_without_request(selected_name, preparers[selected_name])
|
|
|
|
|
conversion_arguments = _conversion_arguments(selected_name)
|
|
|
|
|
solution.optimization_solution.assert_awaited_once_with(**conversion_arguments)
|
|
|
|
|
solution.energy_management_plan.assert_called_once_with(**conversion_arguments)
|
2026-09-16 18:20:49 +02:00
|
|
|
cls = ems_module.EnergyManagement
|
2026-11-23 20:23:25 +00:00
|
|
|
assert _native_solution(selected_name) is solution
|
|
|
|
|
assert _native_solution(other_name) is None
|
2026-09-16 18:20:49 +02:00
|
|
|
assert cls.optimization_solution() is sentinel_result
|
|
|
|
|
assert cls.plan() is sentinel_plan
|
|
|
|
|
assert cls.stage() == ems_module.EnergyManagementStage.IDLE
|
|
|
|
|
offline_ems.prediction.update_data.assert_awaited_once_with(
|
|
|
|
|
force_enable=False, force_update=False
|
|
|
|
|
)
|
|
|
|
|
assert offline_ems.adapter.update_data.await_count == 2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
@pytest.mark.parametrize("mode", [EnergyManagementMode.DISABLED, EnergyManagementMode.PREDICTION])
|
|
|
|
|
async def test_non_optimization_modes_never_optimize(monkeypatch, offline_ems, mode):
|
|
|
|
|
constructors = [Mock(), Mock()]
|
2026-10-11 08:40:43 +02:00
|
|
|
_patch_optimizer(monkeypatch, "Genetic", constructors[0])
|
|
|
|
|
_patch_optimizer(monkeypatch, "Genetic0", constructors[1])
|
2026-09-16 18:20:49 +02:00
|
|
|
offline_ems.config.ems.mode = mode
|
|
|
|
|
await ems_module.EnergyManagement.run(offline_ems)
|
|
|
|
|
for constructor in constructors:
|
|
|
|
|
constructor.assert_not_called()
|
|
|
|
|
assert offline_ems.prediction.update_data.await_count == (
|
|
|
|
|
mode == EnergyManagementMode.PREDICTION
|
|
|
|
|
)
|
|
|
|
|
assert offline_ems.adapter.update_data.await_count == (mode == EnergyManagementMode.PREDICTION)
|
|
|
|
|
assert ems_module.EnergyManagement.plan() is None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
@pytest.mark.parametrize("start_hour", [None, 11])
|
|
|
|
|
async def test_legacy_optimize_explicitly_uses_genetic0(monkeypatch, start_hour):
|
2026-11-23 20:23:25 +00:00
|
|
|
"""Even with GENETIC configured, /optimize must never become the new optimizer.
|
|
|
|
|
|
|
|
|
|
The GENETIC0 router gets its run trigger from the server's placeholder override.
|
|
|
|
|
"""
|
|
|
|
|
from httpx import ASGITransport, AsyncClient
|
|
|
|
|
|
2026-09-16 18:20:49 +02:00
|
|
|
from akkudoktoreos.server import eos
|
|
|
|
|
|
|
|
|
|
fake = SimpleNamespace(
|
2026-11-23 20:23:25 +00:00
|
|
|
run=AsyncMock(return_value=None), optimizer_run=Mock(return_value=None)
|
2026-09-16 18:20:49 +02:00
|
|
|
)
|
|
|
|
|
monkeypatch.setattr(eos, "get_ems", lambda: fake)
|
2026-11-23 20:23:25 +00:00
|
|
|
body = {
|
|
|
|
|
"ems": {
|
|
|
|
|
"pv_prognose_wh": [0.0, 100.0],
|
|
|
|
|
"gesamtlast": [100.0, 100.0],
|
|
|
|
|
"strompreis_euro_pro_wh": [0.0003, 0.0003],
|
|
|
|
|
"einspeiseverguetung_euro_pro_wh": 0.00008,
|
|
|
|
|
"preis_euro_pro_wh_akku": 0.0,
|
|
|
|
|
},
|
|
|
|
|
"pv_akku": None,
|
|
|
|
|
"eauto": None,
|
|
|
|
|
"inverter": None,
|
|
|
|
|
}
|
|
|
|
|
query = "ngen=2" + ("" if start_hour is None else f"&start_hour={start_hour}")
|
|
|
|
|
async with AsyncClient(
|
|
|
|
|
transport=ASGITransport(app=eos.app, raise_app_exceptions=False), base_url="http://test"
|
|
|
|
|
) as client:
|
|
|
|
|
response = await client.post(f"/optimize?{query}", json=body)
|
|
|
|
|
assert response.status_code == 404
|
|
|
|
|
assert response.json()["title"] == "Optimization solution retrieval failed"
|
2026-09-16 18:20:49 +02:00
|
|
|
kwargs = fake.run.await_args.kwargs
|
|
|
|
|
assert kwargs["mode"] == EnergyManagementMode.OPTIMIZATION
|
|
|
|
|
assert kwargs["algorithm"] == OptimizationAlgorithm.GENETIC0
|
2026-11-23 20:23:25 +00:00
|
|
|
assert kwargs["request"].model_dump() == Genetic0Request.model_validate(body).model_dump()
|
|
|
|
|
assert kwargs["overrides"] == {"generations": 2}
|
2026-09-16 18:20:49 +02:00
|
|
|
assert "genetic_parameters" not in kwargs
|
|
|
|
|
assert (
|
|
|
|
|
kwargs["start_datetime"] is None
|
|
|
|
|
if start_hour is None
|
|
|
|
|
else kwargs["start_datetime"].hour == 11
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
@pytest.mark.parametrize("phase", ["optimizer", "conversion", "plan"])
|
|
|
|
|
async def test_failed_legacy_http_run_never_reports_previous_solution(
|
|
|
|
|
monkeypatch, offline_ems, phase
|
|
|
|
|
):
|
|
|
|
|
import json
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
from types import MethodType
|
|
|
|
|
|
|
|
|
|
from httpx import ASGITransport, AsyncClient
|
|
|
|
|
|
2026-11-23 20:23:25 +00:00
|
|
|
from akkudoktoreos.optimization.genetic0.genetic0api import Genetic0Solution
|
2026-09-16 18:20:49 +02:00
|
|
|
from akkudoktoreos.server import eos
|
|
|
|
|
|
|
|
|
|
cls = ems_module.EnergyManagement
|
|
|
|
|
data = json.loads(
|
|
|
|
|
(Path(__file__).parent / "testdata/genetic0/optimize_result_1.json").read_text()
|
|
|
|
|
)
|
|
|
|
|
previous = Genetic0Solution.model_validate(data)
|
2026-11-23 20:23:25 +00:00
|
|
|
_set_native_solution(monkeypatch, "Genetic0", previous)
|
2026-09-16 18:20:49 +02:00
|
|
|
constructor = Mock()
|
|
|
|
|
error = RuntimeError("synthetic " + phase + " failure")
|
|
|
|
|
native = SimpleNamespace(
|
|
|
|
|
optimization_solution=AsyncMock(return_value=object()),
|
|
|
|
|
energy_management_plan=Mock(return_value=object()),
|
|
|
|
|
)
|
|
|
|
|
constructor.return_value.optimize_ems.return_value = native
|
|
|
|
|
if phase == "optimizer":
|
|
|
|
|
constructor.return_value.optimize_ems.side_effect = error
|
|
|
|
|
elif phase == "conversion":
|
|
|
|
|
native.optimization_solution.side_effect = error
|
|
|
|
|
else:
|
|
|
|
|
native.energy_management_plan.side_effect = error
|
2026-10-11 08:40:43 +02:00
|
|
|
_patch_optimizer(monkeypatch, "Genetic0", constructor)
|
2026-09-16 18:20:49 +02:00
|
|
|
offline_ems.run = MethodType(cls.run, offline_ems)
|
2026-11-23 20:23:25 +00:00
|
|
|
offline_ems.optimizer_result = cls.optimizer_result
|
2026-09-16 18:20:49 +02:00
|
|
|
monkeypatch.setattr(eos, "get_ems", lambda: offline_ems)
|
|
|
|
|
async with AsyncClient(
|
|
|
|
|
transport=ASGITransport(app=eos.app, raise_app_exceptions=False), base_url="http://test"
|
|
|
|
|
) as client:
|
|
|
|
|
response = await client.post("/optimize?ngen=1", json=data["parameters"])
|
|
|
|
|
constructor.return_value.optimize_ems.assert_called_once()
|
|
|
|
|
# This diagnostic proves a failure is the stale-success bug, not invalid input.
|
|
|
|
|
if response.status_code == 200:
|
|
|
|
|
assert response.json()["start_solution"] == previous.start_solution
|
|
|
|
|
assert response.json()["result"]["total_balance"] == previous.result.total_balance
|
|
|
|
|
assert response.status_code >= 400, (
|
|
|
|
|
"The failing optimizer returned HTTP 200 with the previous solution"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
@pytest.mark.parametrize("prefix", ["Genetic", "Genetic0"])
|
|
|
|
|
@pytest.mark.parametrize("phase", ["conversion", "plan"])
|
|
|
|
|
async def test_conversion_failure_preserves_consistent_previous_results(
|
|
|
|
|
monkeypatch, offline_ems, prefix, phase
|
|
|
|
|
):
|
|
|
|
|
cls = ems_module.EnergyManagement
|
|
|
|
|
previous_specific, previous_generic, previous_plan = object(), object(), object()
|
2026-11-23 20:23:25 +00:00
|
|
|
_set_native_solution(monkeypatch, prefix, previous_specific)
|
2026-09-16 18:20:49 +02:00
|
|
|
monkeypatch.setattr(cls, "_optimization_solution", previous_generic)
|
|
|
|
|
monkeypatch.setattr(cls, "_plan", previous_plan)
|
|
|
|
|
conversion = AsyncMock(return_value=object())
|
|
|
|
|
solution = SimpleNamespace(optimization_solution=conversion, energy_management_plan=Mock())
|
|
|
|
|
if phase == "conversion":
|
|
|
|
|
conversion.side_effect = RuntimeError("synthetic conversion failure")
|
|
|
|
|
else:
|
|
|
|
|
solution.energy_management_plan.side_effect = RuntimeError("synthetic plan failure")
|
|
|
|
|
constructor = Mock()
|
|
|
|
|
constructor.return_value.optimize_ems.return_value = solution
|
2026-10-11 08:40:43 +02:00
|
|
|
_patch_optimizer(monkeypatch, prefix, constructor)
|
2026-11-23 20:23:26 +00:00
|
|
|
supplied_parameters = _supplied(_stand_in_request(), 3, 17)
|
2026-09-16 18:20:49 +02:00
|
|
|
result = await cls.run(
|
|
|
|
|
offline_ems,
|
|
|
|
|
algorithm=OptimizationAlgorithm(prefix.upper()),
|
2026-09-16 18:45:19 +02:00
|
|
|
**supplied_parameters,
|
2026-09-16 18:20:49 +02:00
|
|
|
)
|
|
|
|
|
assert result is None
|
2026-11-23 20:23:25 +00:00
|
|
|
conversion.assert_awaited_once_with(**_conversion_arguments(prefix))
|
2026-09-16 18:20:49 +02:00
|
|
|
assert solution.energy_management_plan.call_count == (phase == "plan")
|
|
|
|
|
assert offline_ems.adapter.update_data.await_count == 1
|
|
|
|
|
assert (
|
2026-11-23 20:23:25 +00:00
|
|
|
_native_solution(prefix),
|
2026-09-16 18:20:49 +02:00
|
|
|
cls.optimization_solution(),
|
|
|
|
|
cls.plan(),
|
|
|
|
|
cls.stage(),
|
|
|
|
|
) == (
|
|
|
|
|
previous_specific,
|
|
|
|
|
previous_generic,
|
|
|
|
|
previous_plan,
|
|
|
|
|
ems_module.EnergyManagementStage.IDLE,
|
|
|
|
|
), (
|
|
|
|
|
"Failed conversion published a new algorithm result beside the old plan and left EMS in OPTIMIZATION"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_legacy_endpoint_uses_run_return_value_not_last_cached_solution(monkeypatch):
|
|
|
|
|
import json
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
2026-11-23 20:23:25 +00:00
|
|
|
from httpx import ASGITransport, AsyncClient
|
|
|
|
|
from test_genetic0optimize import result_of
|
|
|
|
|
|
|
|
|
|
from akkudoktoreos.optimization.genetic0.genetic0api import Genetic0Solution
|
2026-09-16 18:20:49 +02:00
|
|
|
from akkudoktoreos.server import eos
|
|
|
|
|
|
|
|
|
|
data = json.loads(
|
|
|
|
|
(Path(__file__).parent / "testdata/genetic0/optimize_result_1.json").read_text()
|
|
|
|
|
)
|
|
|
|
|
produced = Genetic0Solution.model_validate(data)
|
|
|
|
|
fake = SimpleNamespace(
|
2026-11-23 20:23:25 +00:00
|
|
|
run=AsyncMock(return_value=result_of(produced)),
|
|
|
|
|
optimizer_run=Mock(side_effect=AssertionError("must use this run's result")),
|
2026-09-16 18:20:49 +02:00
|
|
|
)
|
|
|
|
|
monkeypatch.setattr(eos, "get_ems", lambda: fake)
|
2026-11-23 20:23:25 +00:00
|
|
|
async with AsyncClient(
|
|
|
|
|
transport=ASGITransport(app=eos.app), base_url="http://test"
|
|
|
|
|
) as client:
|
|
|
|
|
response = await client.post("/optimize?ngen=1", json=data["parameters"])
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
assert response.json()["start_solution"] == produced.start_solution
|
|
|
|
|
assert response.json()["result"]["total_balance"] == produced.result.total_balance
|
|
|
|
|
fake.optimizer_run.assert_not_called()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
@pytest.mark.parametrize("prefix", ["Genetic", "Genetic0"])
|
|
|
|
|
async def test_raising_preparation_cancels_the_run(monkeypatch, offline_ems, prefix):
|
|
|
|
|
"""A preparation that raises cancels the run like one that returns None."""
|
|
|
|
|
cls = ems_module.EnergyManagement
|
|
|
|
|
previous_specific, previous_generic, previous_plan = object(), object(), object()
|
|
|
|
|
_set_native_solution(monkeypatch, prefix, previous_specific)
|
|
|
|
|
monkeypatch.setattr(cls, "_optimization_solution", previous_generic)
|
|
|
|
|
monkeypatch.setattr(cls, "_plan", previous_plan)
|
|
|
|
|
constructor = Mock()
|
|
|
|
|
_patch_optimizer(monkeypatch, prefix, constructor)
|
|
|
|
|
_patch_prepare(monkeypatch, prefix, AsyncMock(side_effect=RuntimeError("synthetic")))
|
|
|
|
|
result = await cls.run(offline_ems, algorithm=OptimizationAlgorithm(prefix.upper()))
|
|
|
|
|
assert result is None
|
|
|
|
|
constructor.assert_not_called()
|
|
|
|
|
assert offline_ems.adapter.update_data.await_count == 1 # acquisition only
|
|
|
|
|
assert (
|
|
|
|
|
_native_solution(prefix),
|
|
|
|
|
cls.optimization_solution(),
|
|
|
|
|
cls.plan(),
|
|
|
|
|
cls.stage(),
|
|
|
|
|
) == (
|
|
|
|
|
previous_specific,
|
|
|
|
|
previous_generic,
|
|
|
|
|
previous_plan,
|
|
|
|
|
ems_module.EnergyManagementStage.IDLE,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_previous_genetic0_result_is_the_warm_start_of_the_next_run(
|
|
|
|
|
monkeypatch, offline_ems
|
|
|
|
|
):
|
|
|
|
|
"""The EMS passes the result of the last successful GENETIC0 run into the next preparation.
|
|
|
|
|
|
|
|
|
|
The optimizer holds no state: the warm start comes from the EMS slot, and a failed run keeps
|
|
|
|
|
the previous one.
|
|
|
|
|
"""
|
|
|
|
|
cls = ems_module.EnergyManagement
|
|
|
|
|
first = SimpleNamespace(
|
|
|
|
|
start_solution=[1.0, 2.0],
|
|
|
|
|
optimization_solution=AsyncMock(return_value=object()),
|
|
|
|
|
energy_management_plan=Mock(return_value=object()),
|
|
|
|
|
)
|
|
|
|
|
second = SimpleNamespace(
|
|
|
|
|
start_solution=[3.0, 4.0],
|
|
|
|
|
optimization_solution=AsyncMock(return_value=object()),
|
|
|
|
|
energy_management_plan=Mock(return_value=object()),
|
|
|
|
|
)
|
|
|
|
|
constructor = Mock()
|
|
|
|
|
constructor.return_value.optimize_ems.side_effect = [
|
|
|
|
|
first,
|
|
|
|
|
RuntimeError("synthetic optimizer failure"),
|
|
|
|
|
second,
|
|
|
|
|
second,
|
|
|
|
|
]
|
|
|
|
|
_patch_optimizer(monkeypatch, "Genetic0", constructor)
|
|
|
|
|
prepare = AsyncMock(return_value=SimpleNamespace(dishwasher=None))
|
|
|
|
|
_patch_prepare(monkeypatch, "Genetic0", prepare)
|
|
|
|
|
for _ in range(3):
|
|
|
|
|
await cls.run(offline_ems, algorithm=OptimizationAlgorithm.GENETIC0)
|
|
|
|
|
assert cls.optimizer_result("GENETIC0") is second
|
|
|
|
|
# Without a stored run there is no warm start.
|
|
|
|
|
monkeypatch.setattr(cls, "_optimizer_runs", {})
|
|
|
|
|
await cls.run(offline_ems, algorithm=OptimizationAlgorithm.GENETIC0)
|
|
|
|
|
assert [_warm_start_of(call) for call in prepare.await_args_list] == [
|
|
|
|
|
None,
|
|
|
|
|
[1.0, 2.0],
|
|
|
|
|
[1.0, 2.0], # the failed second run kept the first as warm start
|
|
|
|
|
None,
|
|
|
|
|
]
|