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
@@ -3,16 +3,19 @@ from typing import Optional
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
from conftest import genetic_optimization
|
||||
|
||||
from akkudoktoreos.config.config import ConfigEOS
|
||||
from akkudoktoreos.core.cache import CacheEnergyManagementStore
|
||||
from akkudoktoreos.core.coreabc import get_ems
|
||||
from akkudoktoreos.devices.genetic.battery import Battery
|
||||
from akkudoktoreos.optimization.genetic.genetic import GeneticOptimization
|
||||
from akkudoktoreos.optimization.genetic.geneticparams import (
|
||||
from akkudoktoreos.optimization.genetic.devices.geneticbattery import GeneticBattery
|
||||
from akkudoktoreos.optimization.genetic.geneticalgorithm import (
|
||||
GeneticOptimization,
|
||||
)
|
||||
from akkudoktoreos.optimization.genetic.geneticrequest import (
|
||||
GeneticOptimizationParameters,
|
||||
)
|
||||
from akkudoktoreos.optimization.genetic.geneticsolution import GeneticSolution
|
||||
from akkudoktoreos.optimization.genetic.geneticresult import GeneticResult
|
||||
from akkudoktoreos.utils.datetimeutil import to_datetime
|
||||
|
||||
ems_eos = get_ems(init=True) # init once
|
||||
@@ -39,7 +42,7 @@ def test_direct_marketing_preserves_constant_supplied_feed_in_tariff(config_eos:
|
||||
)
|
||||
)
|
||||
|
||||
adjusted = GeneticOptimization()._parameters_for_config(parameters)
|
||||
adjusted = genetic_optimization()._parameters_for_config(parameters)
|
||||
|
||||
assert adjusted.ems.einspeiseverguetung_euro_pro_wh == [0.00007, 0.00007]
|
||||
assert parameters.ems.einspeiseverguetung_euro_pro_wh == [0.00007, 0.00007]
|
||||
@@ -62,7 +65,7 @@ def test_direct_marketing_keeps_variable_feed_in_tariff(config_eos: ConfigEOS):
|
||||
)
|
||||
)
|
||||
|
||||
adjusted = GeneticOptimization()._parameters_for_config(parameters)
|
||||
adjusted = genetic_optimization()._parameters_for_config(parameters)
|
||||
|
||||
assert adjusted.ems.einspeiseverguetung_euro_pro_wh == [0.0001, -0.00005]
|
||||
|
||||
@@ -120,7 +123,7 @@ def test_grid_export_rates_reach_the_solution(config_eos: ConfigEOS):
|
||||
)
|
||||
)
|
||||
|
||||
optimization = GeneticOptimization(fixed_seed=42)
|
||||
optimization = genetic_optimization(seed=42)
|
||||
solution = optimization.optimize_ems(parameters=parameters, start_hour=0, ngen=3)
|
||||
|
||||
# Full power first, so the full-power state keeps the lowest export index.
|
||||
@@ -173,8 +176,8 @@ def test_ev_deadline_slot_resolution(config_eos: ConfigEOS):
|
||||
}
|
||||
)
|
||||
ems_eos.set_start_datetime(to_datetime().set(hour=10, minute=0, second=0, microsecond=0))
|
||||
optimization = GeneticOptimization(fixed_seed=1)
|
||||
optimization._slot0_datetime = optimization.ems.start_datetime
|
||||
optimization = genetic_optimization(seed=1)
|
||||
optimization._slot0_datetime = optimization.settings.start_datetime
|
||||
slot0 = optimization._slot0_datetime
|
||||
|
||||
# Duration only: 6 h after the start hour 10.
|
||||
@@ -214,10 +217,10 @@ def test_ev_soc_penalty_reads_the_deadline_slot(config_eos: ConfigEOS):
|
||||
}
|
||||
)
|
||||
ems_eos.set_start_datetime(to_datetime().set(hour=10, minute=0, second=0, microsecond=0))
|
||||
optimization = GeneticOptimization(fixed_seed=1)
|
||||
optimization = genetic_optimization(seed=1)
|
||||
simulation_result = {"EAuto_SoC_pro_Stunde": [20.0, 35.0, 50.0, 80.0]}
|
||||
|
||||
optimization.simulation.ev = MagicMock(spec=Battery)
|
||||
optimization.simulation.ev = MagicMock(spec=GeneticBattery)
|
||||
optimization.simulation.ev.current_soc_percentage.return_value = 80.0
|
||||
|
||||
# Without a deadline the final SoC counts.
|
||||
@@ -254,7 +257,7 @@ def test_ev_deadline_charges_before_departure(config_eos: ConfigEOS):
|
||||
CacheEnergyManagementStore().clear()
|
||||
|
||||
parameters = _ev_deadline_parameters(hours, min_soc_max_duration_h=6)
|
||||
solution = GeneticOptimization(fixed_seed=42).optimize_ems(
|
||||
solution = genetic_optimization(seed=42).optimize_ems(
|
||||
parameters=parameters, start_hour=0, ngen=40
|
||||
)
|
||||
|
||||
@@ -266,7 +269,7 @@ def test_ev_deadline_charges_before_departure(config_eos: ConfigEOS):
|
||||
|
||||
def _terminal_value_run(
|
||||
config_eos: ConfigEOS, mode: str, prices: Optional[list[float]] = None
|
||||
) -> GeneticSolution:
|
||||
) -> GeneticResult:
|
||||
"""48 h with expensive energy and two dirt-cheap slots at the very end.
|
||||
|
||||
Charging in those last slots only pays off when the stored energy keeps a
|
||||
@@ -324,7 +327,7 @@ def _terminal_value_run(
|
||||
ev=None,
|
||||
)
|
||||
)
|
||||
return GeneticOptimization(fixed_seed=7).optimize_ems(
|
||||
return genetic_optimization(seed=7).optimize_ems(
|
||||
parameters=parameters, start_hour=0, ngen=20
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user