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
+17
-18
@@ -6,11 +6,13 @@ from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
from conftest import genetic_result_of, genetic_run_parameters
|
||||
from fastapi.testclient import TestClient
|
||||
from pypdf import PdfReader
|
||||
|
||||
from akkudoktoreos.optimization.genetic import geneticvisualize
|
||||
from akkudoktoreos.optimization.genetic.geneticsolution import GeneticSolution
|
||||
from akkudoktoreos.optimization.genetic import geneticapi, geneticvisualize
|
||||
from akkudoktoreos.optimization.genetic.geneticapi import GeneticSolution
|
||||
from akkudoktoreos.optimization.optimizer import OptimizerRun
|
||||
from akkudoktoreos.server import eos
|
||||
from akkudoktoreos.utils.datetimeutil import to_datetime
|
||||
|
||||
@@ -20,11 +22,11 @@ def retained_result():
|
||||
path = Path(__file__).parent / "testdata/genetic/optimize_result_1.json"
|
||||
solution = GeneticSolution.model_validate_json(path.read_text())
|
||||
solution.start_solution_datetime = to_datetime("2026-09-16T10:00:00+02:00")
|
||||
return solution
|
||||
return genetic_result_of(solution)
|
||||
|
||||
|
||||
def test_missing_algorithm_report_returns_404(config_eos, monkeypatch):
|
||||
monkeypatch.setattr(eos, "get_ems", lambda: SimpleNamespace(genetic_solution=lambda: None))
|
||||
monkeypatch.setattr(eos, "get_ems", lambda: SimpleNamespace(optimizer_run=lambda name: None))
|
||||
response = TestClient(eos.app).get("/v1/energy-management/optimization/solution/GENETIC/pdf")
|
||||
assert response.status_code == 404
|
||||
assert response.headers["content-type"].startswith("application/problem+json")
|
||||
@@ -32,24 +34,21 @@ def test_missing_algorithm_report_returns_404(config_eos, monkeypatch):
|
||||
|
||||
def test_retained_algorithm_result_renders_real_pdf(config_eos, monkeypatch, retained_result):
|
||||
solution = retained_result
|
||||
monkeypatch.setattr(eos, "get_ems", lambda: SimpleNamespace(genetic_solution=lambda: solution))
|
||||
run = OptimizerRun(parameters=genetic_run_parameters(solution.request), result=solution)
|
||||
monkeypatch.setattr(eos, "get_ems", lambda: SimpleNamespace(optimizer_run={"GENETIC": run}.get))
|
||||
# A later run and changed configuration must not retime the retained report.
|
||||
from akkudoktoreos.core.coreabc import get_ems
|
||||
from akkudoktoreos.optimization.genetic import geneticvisualize
|
||||
|
||||
get_ems().set_start_datetime(to_datetime("2027-01-01T00:00:00+01:00"))
|
||||
config_eos.optimization.genetic.interval_sec = 900
|
||||
monkeypatch.setattr(
|
||||
geneticvisualize, "get_ems", lambda: pytest.fail("Report must retain its own time grid")
|
||||
)
|
||||
before = solution.model_dump_json()
|
||||
before = geneticapi.solution_response(solution).model_dump_json()
|
||||
response = TestClient(eos.app).get("/v1/energy-management/optimization/solution/GENETIC/pdf")
|
||||
assert response.status_code == 200
|
||||
assert response.headers["content-type"] == "application/pdf"
|
||||
assert "genetic" in response.headers["content-disposition"]
|
||||
assert response.content.startswith(b"%PDF-")
|
||||
assert len(PdfReader(BytesIO(response.content)).pages) >= 4
|
||||
assert solution.model_dump_json() == before
|
||||
assert geneticapi.solution_response(solution).model_dump_json() == before
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -57,21 +56,21 @@ async def test_render_is_offloaded_and_uses_an_owned_copy(config_eos, monkeypatc
|
||||
solution = retained_result
|
||||
caller_thread = threading.get_ident()
|
||||
captured = []
|
||||
monkeypatch.setattr(
|
||||
eos,
|
||||
"get_ems",
|
||||
lambda: SimpleNamespace(genetic_solution=lambda: solution),
|
||||
)
|
||||
run = OptimizerRun(parameters=genetic_run_parameters(solution.request), result=solution)
|
||||
monkeypatch.setattr(eos, "get_ems", lambda: SimpleNamespace(optimizer_run={"GENETIC": run}.get))
|
||||
original_controls = list(solution.ac_charge)
|
||||
|
||||
def render(*, solution):
|
||||
def render(*, parameters, result):
|
||||
solution = result
|
||||
captured.append((threading.get_ident(), solution))
|
||||
solution.ac_charge[0] = 0.123
|
||||
return b"%PDF-isolated-render"
|
||||
|
||||
# The PDF stack is imported on first use, so patch it at its source.
|
||||
monkeypatch.setattr(geneticvisualize, "genetic_prepare_visualize", render)
|
||||
response = await eos.fastapi_energy_management_optimization_solution_genetic_pdf_get()
|
||||
response = await geneticapi.fastapi_energy_management_optimization_solution_genetic_pdf_get(
|
||||
latest=run
|
||||
)
|
||||
assert response.body == b"%PDF-isolated-render"
|
||||
assert captured[0][0] != caller_thread
|
||||
assert captured[0][1] is not solution
|
||||
|
||||
Reference in New Issue
Block a user