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
@@ -1,3 +1,4 @@
|
||||
from dataclasses import replace
|
||||
from io import BytesIO
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
@@ -7,20 +8,21 @@ import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
import pendulum
|
||||
import pytest
|
||||
from conftest import genetic_result_of, genetic_run_parameters
|
||||
from matplotlib.testing.compare import compare_images
|
||||
from pypdf import PdfReader
|
||||
|
||||
from akkudoktoreos.optimization.genetic import geneticvisualize as visualize
|
||||
from akkudoktoreos.optimization.genetic.geneticsolution import GeneticSolution
|
||||
from akkudoktoreos.optimization.genetic.geneticvisualize import (
|
||||
genetic_generate_example_report,
|
||||
)
|
||||
from akkudoktoreos.optimization.genetic.terminalvalue import (
|
||||
from akkudoktoreos.optimization.genetic.geneticapi import GeneticSolution
|
||||
from akkudoktoreos.optimization.genetic.geneticterminalvalue import (
|
||||
TailDiagnostics,
|
||||
TailPlanSlot,
|
||||
TerminalValueCurve,
|
||||
TerminalValueResult,
|
||||
)
|
||||
from akkudoktoreos.optimization.genetic.geneticvisualize import (
|
||||
genetic_generate_example_report,
|
||||
)
|
||||
|
||||
filename = "example_report.pdf"
|
||||
|
||||
@@ -73,7 +75,7 @@ class TestGeneticVisualizationIntervals:
|
||||
home_appliance_energy_wh={"water-heater": energy},
|
||||
)
|
||||
return SimpleNamespace(
|
||||
parameters=SimpleNamespace(
|
||||
request=SimpleNamespace(
|
||||
ems=SimpleNamespace(
|
||||
total_load=energy,
|
||||
pv_forecast_wh=energy,
|
||||
@@ -131,8 +133,7 @@ class TestGeneticVisualizationIntervals:
|
||||
return original(self, start_date, y_list, **kwargs)
|
||||
|
||||
monkeypatch.setattr(visualize.GeneticVisualizationReport, "create_line_chart_date", capture)
|
||||
monkeypatch.setattr(visualize, "get_ems", lambda: pytest.fail("Snapshot must own report timing"))
|
||||
pdf = visualize.genetic_prepare_visualize(report_solution)
|
||||
pdf = visualize.genetic_prepare_visualize(genetic_run_parameters(report_solution.request), report_solution)
|
||||
assert pdf.startswith(b"%PDF-")
|
||||
assert all(row[0] == report_solution.start_solution_datetime for row in captured)
|
||||
assert all(row[3] == interval_seconds for row in captured)
|
||||
@@ -178,8 +179,12 @@ class TestGeneticVisualizationIntervals:
|
||||
assert not solution.controls_start_at_now
|
||||
assert solution.terminal_value is None
|
||||
start = pendulum.datetime(2026, 9, 16, solution.start_hour, tz="UTC")
|
||||
monkeypatch.setattr(visualize, "get_ems", lambda: SimpleNamespace(start_datetime=start))
|
||||
pdf = visualize.genetic_prepare_visualize(solution)
|
||||
# A historical solution has no start; the run start of the parameters is the fallback.
|
||||
parameters = genetic_run_parameters(solution.parameters)
|
||||
parameters = replace(
|
||||
parameters, settings=replace(parameters.settings, start_datetime=start)
|
||||
)
|
||||
pdf = visualize.genetic_prepare_visualize(parameters, genetic_result_of(solution))
|
||||
assert len(PdfReader(BytesIO(pdf)).pages) >= 4
|
||||
|
||||
def test_fixed_fallback_does_not_invent_tail_charts(self, config_eos, report_solution):
|
||||
@@ -188,7 +193,7 @@ class TestGeneticVisualizationIntervals:
|
||||
battery_energy_wh=500, requested_tail_hours=24,
|
||||
)
|
||||
report_solution.result.home_appliance_energy_wh = {}
|
||||
pdf = visualize.genetic_prepare_visualize(report_solution)
|
||||
pdf = visualize.genetic_prepare_visualize(genetic_run_parameters(report_solution.request), report_solution)
|
||||
text = " ".join(page.extract_text() for page in PdfReader(BytesIO(pdf)).pages)
|
||||
assert "No complete tail forecast" in text
|
||||
assert "effective tail: 0 h" in text
|
||||
@@ -219,9 +224,9 @@ class TestGeneticVisualizationIntervals:
|
||||
def test_pdf_clips_forecast_tail_and_accepts_historical_diagnostics(self,
|
||||
config_eos, monkeypatch, report_solution, tariff
|
||||
):
|
||||
report_solution.parameters.ems.total_load = [100.0] * 6
|
||||
report_solution.parameters.ems.pv_forecast_wh = [200.0] * 6
|
||||
report_solution.parameters.ems.feed_in_tariff_per_wh = tariff
|
||||
report_solution.request.ems.total_load = [100.0] * 6
|
||||
report_solution.request.ems.pv_forecast_wh = [200.0] * 6
|
||||
report_solution.request.ems.feed_in_tariff_per_wh = tariff
|
||||
report_solution.extra_data = {
|
||||
"verluste": [1.0, 2.0, 3.0], "bilanz": [0.1, 0.2, 0.3],
|
||||
"nebenbedingung": [0.0, 0.0, 0.0],
|
||||
@@ -234,7 +239,7 @@ class TestGeneticVisualizationIntervals:
|
||||
return original(self, start_date, y_list, **kwargs)
|
||||
|
||||
monkeypatch.setattr(visualize.GeneticVisualizationReport, "create_line_chart_date", capture)
|
||||
pdf = visualize.genetic_prepare_visualize(report_solution)
|
||||
pdf = visualize.genetic_prepare_visualize(genetic_run_parameters(report_solution.request), report_solution)
|
||||
assert pdf.startswith(b"%PDF-")
|
||||
tariff_plot = next(series for series, kwargs in captured if kwargs.get("title") == "Remuneration")
|
||||
assert np.asarray(tariff_plot[0]).tolist() == [0.00007] * 4
|
||||
|
||||
Reference in New Issue
Block a user