mirror of
https://github.com/Akkudoktor-EOS/EOS.git
synced 2026-12-02 18:36:28 +01:00
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)
261 lines
16 KiB
Markdown
261 lines
16 KiB
Markdown
---
|
|
name: optimizer-architecture
|
|
description: Use when creating, changing, refactoring or removing an optimizer (GENETIC, GENETIC0 or a new one), its parameter preparation, simulation, simulation devices, solution conversion, or the code that runs optimizers.
|
|
---
|
|
|
|
# Optimizer Architecture
|
|
|
|
Terms as in `GLOSSARY.md`: **optimizer**, **parameter preparation**, **optimization
|
|
parameters**, **presentation data**, **solution conversion**, **warm start**. The decisions and
|
|
their reasons are in `docs/adr/` 0003 to 0011; read the ADR before deviating from a rule here,
|
|
and flag any conflict.
|
|
|
|
## One optimizer, four parts
|
|
|
|
| Part | May use |
|
|
|---|---|
|
|
| Parameter preparation (`<name>prepare.py` or `prepare/`) | configuration, predictions, measurements, and the run start, observation time and warm start passed in as arguments |
|
|
| Algorithm (`run()`) | the optimization parameters only |
|
|
| Simulation and simulation devices | the optimization parameters only |
|
|
| Solution conversion (`convert()`) | the optimization parameters and the result of the run only |
|
|
|
|
- **Hard rule:** outside parameter preparation, an optimizer never reads
|
|
configuration, predictions, measurements or EMS state, directly or through a
|
|
helper that does (e.g. `configured_grid_interpolator_backend()`, the EMS run
|
|
cache in `core.cache`). This includes data only needed to present the
|
|
solution: time zone, device IDs, run start and the prediction series the run
|
|
used travel in the optimization parameters. The modules and names this
|
|
covers are listed once, in `WORLD_MODULES`, `WORLD_NAMES` and `PREPARATION_BANNED_*` of
|
|
`tests/test_optimizerarchitecture.py`; that list is authoritative. Among
|
|
them: `config`, `prediction`, `measurement`, `adapter`, `server`,
|
|
`core.ems`, `core.cache`, the `core.*settings` and database modules, the
|
|
`core.coreabc` mixins and getters, `OptimizationBase` and the shared device
|
|
configuration (`devices.settings`, `devices.profile`, `devices.devices`,
|
|
`devices.devicesabc`). From `optimization.optimization` only
|
|
`OptimizationSolution` may be imported. The other contract names of
|
|
`CONTRACT_NAMES` (`PydanticDateTimeDataFrame` of the solution, the plan's
|
|
`BatteryOperationMode` and `ApplianceOperationMode`, the request schema's
|
|
time windows (`TimeWindow`, `TimeWindowSequence`, `ValueTimeWindow`,
|
|
`CycleTimeWindowSequence`) and consumer enums (`ConsumerScheduleMode`,
|
|
`ConsumerDeadlinePolicy`)) are allowed only in the request,
|
|
conversion, visualization and API modules (`CONTRACT_ROLES`), never in the
|
|
parameters or the simulation.
|
|
- Parameter preparation runs once per run, never fails, never returns `None` and follows the
|
|
"Optimization parameters are always valid" rules of the `energy-domain` skill.
|
|
- Parameter preparation does not read EMS state either (`core.ems`,
|
|
`get_ems()`, `EnergyManagementSystemMixin`, `StartMixin`); the energy
|
|
management passes the run start and the warm start as arguments. No part of
|
|
an optimizer, its preparation included, imports `akkudoktoreos.server`.
|
|
- Only `<name>optimizer.py`, the package `__init__.py` and the parameter
|
|
preparation itself import the parameter preparation. Any other module would
|
|
reach the configuration through it. The package and `<name>optimizer.py`
|
|
may re-export the preparation, so the other modules import neither; only
|
|
the package imports `<name>optimizer.py`.
|
|
|
|
## The interface
|
|
|
|
This is the one maintained copy of the interface; ADR 0006 records the decision and links here.
|
|
|
|
```python
|
|
class Optimizer(Protocol[P, Q, R]):
|
|
name: str
|
|
def run_clock(self) -> RunClock # slot_seconds, timezone (None: server time)
|
|
async def prepare(
|
|
self, start_datetime: DateTime, observation_datetime: DateTime,
|
|
warm_start: R | None, overrides: Mapping[str, Any] | None = None,
|
|
) -> P
|
|
async def prepare_from_request(
|
|
self, request: Q, start_datetime: DateTime, observation_datetime: DateTime,
|
|
warm_start: R | None, overrides: Mapping[str, Any] | None = None,
|
|
) -> P
|
|
def run(self, parameters: P) -> R
|
|
def api(self) -> OptimizerApi | None
|
|
async def convert(
|
|
self, parameters: P, result: R
|
|
) -> tuple[OptimizationSolution, EnergyManagementPlan]
|
|
```
|
|
|
|
The code is `akkudoktoreos.optimization.optimizer.Optimizer`; the energy management finds an
|
|
optimizer through `akkudoktoreos.optimization.optimizerregistry.get_optimizer(name)` and keeps
|
|
the last parameters and result per optimizer (`OptimizerRun`); the result is the next warm start.
|
|
`start_datetime` is the start of the run's first slot; `observation_datetime` is the unrounded
|
|
moment of the run that measurements (e.g. a measured state of charge) refer to.
|
|
`run_clock()` is how the energy management learns the optimizer's slot length and the time zone
|
|
of a run without an explicit start; the optimizer reads them from its settings in its parameter
|
|
preparation, and the energy management reads no optimizer's settings.
|
|
|
|
- `run()` is deterministic: the same optimization parameters give the same result. Generations,
|
|
individuals, seed and penalties are part of the optimization parameters. A run-time override
|
|
(e.g. `?ngen=`) reaches the preparation as `overrides` (`{"generations": 10}`), never beside the
|
|
parameters; the preparation applies the keys it knows and logs an error for any other key.
|
|
- `convert()` is `async`, so that an optimizer can migrate step by step while its conversion
|
|
still awaits reads; once it uses only the parameters and the result, it awaits nothing.
|
|
- **Optimization parameters (`P`), including the simulation's device
|
|
parameters, are plain dataclasses, never Pydantic models** (ADR 0009). They
|
|
are valid because parameter preparation builds them valid, not because a
|
|
model validates them. Optimization requests (`Q`) and settings models are
|
|
Pydantic models: validation and public schemas belong where external input
|
|
arrives.
|
|
- **Optimization requests never go straight into `run()`.** A REST endpoint
|
|
passes the client's optimization request (`Q`) to the optimizer's
|
|
`prepare_from_request()`, which
|
|
completes it from the configuration with the same fallback rules as
|
|
`prepare()`. Keep the request's public schema unchanged; add fields the run
|
|
needs to the optimization parameters, not to the request.
|
|
- **Presentation data cover every entry point.** A client's request may start in another time
|
|
zone than the configured one or carry longer arrays than an automatic run; prepare the series
|
|
for the widest window the conversion can need, and let `convert()` take its own share.
|
|
Building them follows the fallback rules too: a series that cannot be read is left out with
|
|
an error log.
|
|
- Derive the simulation's device dataclasses from the request with pure functions
|
|
(`<Name><Thing>Request.to_parameters()`): no configuration, no clock (a date comes from the
|
|
run start in the parameters), no change to the request. A replay file can then store the
|
|
request and derive them again. GENETIC0's home appliance breaks this (today's date, writes a
|
|
default time window into the request); it is frozen, so do not copy it.
|
|
- A run must be reproducible from its stored optimization parameters alone. Add a test that runs
|
|
from a stored parameter file, under a deliberately different configuration, when you change
|
|
what goes into the parameters, and one that asserts the replay (de)serialization round trip is
|
|
lossless.
|
|
- The result (`R`) is a frozen dataclass. Where `<name>api.py` builds public response models
|
|
from it, normalise its values as those models validate them (NumPy arrays to lists, NaN to
|
|
`None`, the fields' number types), so the responses stay identical.
|
|
|
|
## Layout and names (ADR 0010)
|
|
|
|
Everything of an optimizer lives in `src/akkudoktoreos/optimization/<name>/`,
|
|
`<name>` being the optimizer's name in lowercase (`genetic`, `genetic0`); its
|
|
simulation devices in `devices/` below it. Modules are `<name><role>.py`,
|
|
classes `<Name><Role>`. This table is the one maintained copy; ADR 0010 links here.
|
|
|
|
| Role | Module | Class | Kind |
|
|
|---|---|---|---|
|
|
| settings | `<name>settings.py` | `<Name>Settings` | Pydantic, `optimization.<name>` |
|
|
| request | `<name>request.py` | `<Name>Request` | Pydantic, public API |
|
|
| parameters | `<name>parameters.py` | `<Name>Parameters` | dataclass, input of `run()` |
|
|
| preparation | `<name>prepare.py` | `prepare()`, `prepare_from_request()` | |
|
|
| optimizer | `<name>optimizer.py` | `<Name>Optimizer` | implements the interface |
|
|
| algorithm | `<name>algorithm.py` | | the search |
|
|
| result | `<name>result.py` | `<Name>Result` | dataclass, output of `run()` |
|
|
| simulation | `<name>simulation.py`, `devices/<name><device>.py` | `<Name><Device>`, `<Name><Device>Parameters` | dataclass parameters |
|
|
| conversion | `<name>convert.py` | `convert()` | |
|
|
| visualization | `<name>visualize.py` | | renders parameters and result |
|
|
| API | `<name>api.py` | REST router | fed by FastAPI dependencies |
|
|
|
|
- A new module that fits no role still gets the `<name>` prefix. Do not
|
|
invent a second name for an existing role.
|
|
- Nested request models are `<Name><Thing>Request`; `<Name><Device>Parameters`
|
|
names the simulation's device dataclass.
|
|
- Renaming a Pydantic model changes its schema name in OpenAPI and the
|
|
configuration documentation, and moving a route into an optimizer's router
|
|
changes the OpenAPI paths: regenerate the docs and declare it as
|
|
**Breaking:** in the pull request.
|
|
- Dataclasses are `@dataclass(frozen=True, slots=True)`. A run never changes
|
|
its inputs; parameters hold sequences as tuples.
|
|
- The parameters may keep the Pydantic optimization request they were derived
|
|
from (GENETIC0 does, for replay files, conversion, visualization and API),
|
|
an exception to the tuple rule; the algorithm and the simulation read the
|
|
derived dataclasses, never the request.
|
|
- Only `<name>settings.py`, `<name>request.py`, parameter preparation and
|
|
`<name>api.py` may import `pydantic` or `akkudoktoreos.core.pydantic`.
|
|
|
|
## Independence
|
|
|
|
- An optimizer never imports another optimizer.
|
|
- **Duplicate code instead of sharing it.** Simulation physics (efficiency curves, AC/DC energy
|
|
solvers, device models) is copied into each optimizer that needs it. Never move simulation code
|
|
into a shared module, and never merge copies across optimizers to remove duplication. A fix in
|
|
one copy reaches another optimizer only through a deliberate change to that optimizer.
|
|
- An optimizer may import: generic utilities (such as `datetimeutil`; Pydantic only in the roles allowed above), the
|
|
output contract (`OptimizationSolution`, the energy management plan, and in the roles above the contract names) and, in parameter
|
|
preparation only, the configuration settings, including the shared operation-mode enums and
|
|
settings-side resolution such as battery profiles. Translate those into the optimizer's own
|
|
types during parameter preparation; do not pass settings objects or enums from the
|
|
configuration into the simulation.
|
|
- Code outside an optimizer imports nothing from it but its package
|
|
(`optimization/<name>/__init__.py`), which exports what the registry and the
|
|
settings field need. Converting device
|
|
settings into an optimizer's device parameters belongs to that optimizer's parameter
|
|
preparation, not to `devices/settings/`: the shared device configuration
|
|
(`devices.settings`, `devices.profile`, `devices.devices`, `devices.devicesabc`)
|
|
never imports an optimizer, not even its package.
|
|
|
|
## What an optimizer owns
|
|
|
|
Its package holds everything specific to it: the settings model `optimization.<name>` and the
|
|
validation of its horizons, parameter preparation, run, simulation, simulation devices, solution
|
|
conversion, visualization, its REST endpoints (contributed as a router) and its EOSdash wizard
|
|
steps. The energy management, the server and EOSdash use only the registry and the interface;
|
|
never add a branch per optimizer there.
|
|
|
|
None of these gets an exception from the hard rule (ADR 0011):
|
|
|
|
- `<name>visualize.py` renders from the optimization parameters and result
|
|
only, like `convert()`.
|
|
- `<name>api.py` fetches nothing itself. It declares placeholder dependency
|
|
functions for the last parameters and result and the run trigger and hands
|
|
them out with its router as `OptimizerApi(router, last_run, run)` through
|
|
`api()`; the generic server includes every registered router (before its own
|
|
routes, so a specific path wins) and fills the placeholders through
|
|
`app.dependency_overrides`. Errors are `OptimizerProblem`s, which the server
|
|
renders like its own RFC 7807 problems. It imports FastAPI, Pydantic and its
|
|
request, never configuration, EMS state or `akkudoktoreos.server`. The public
|
|
response models live there and are built from the parameters and the result.
|
|
- Wizard steps are field metadata in `<name>settings.py` that EOSdash reads;
|
|
no EOSdash code inside an optimizer.
|
|
|
|
Removing an optimizer touches exactly four places in the production code: its directory, its
|
|
registry entry, its static settings field in `OptimizationCommonSettings`, and a configuration
|
|
migration step that drops `optimization.<name>` and switches `optimization.algorithm` to a
|
|
remaining optimizer. If a change makes removal need anything else, it breaks this rule. What
|
|
belongs only to the optimizer goes with it: its tests, its test data and, while the architecture
|
|
test's `BASELINE` lists violations of the optimizer, those entries. The remaining optimizers'
|
|
tests pass unchanged.
|
|
|
|
## Enforcement
|
|
|
|
`tests/test_optimizerarchitecture.py` (ADR 0007) checks the import rules above
|
|
against `BASELINE`, the violations that existed when it was introduced.
|
|
|
|
- Parameter preparation is recognised by file name:
|
|
`optimization/<name>/<name>prepare.py` or a subpackage
|
|
`optimization/<name>/prepare/` (a package; a plain `prepare.py` does not
|
|
count). Only there may an optimizer import the modules and names of the hard
|
|
rule, except EMS state and the server.
|
|
- When you find another module or helper that reads configuration,
|
|
predictions, measurements or EMS state, add it to the list in the test.
|
|
That is the only case in which `BASELINE` may grow: by the existing uses the
|
|
new list entry reveals, in the same commit, and named in the pull request.
|
|
- `optimization/<name>/<name>settings.py` may import `config.configabc` to define
|
|
`optimization.<name>`, nothing else from the configuration.
|
|
- Otherwise never add an entry to `BASELINE`, and never rename a module to
|
|
`*prepare.py` or `*settings.py` just to pass the test. Remove the entries
|
|
your change fixes; the test fails on stale entries.
|
|
- Moving or splitting a module relabels its entries to the new module one to
|
|
one, in the same commit and named in the pull request; it adds none.
|
|
- The test discovers optimizers (every package under `optimization/`); do not
|
|
hard-code optimizer names in it.
|
|
- It cannot detect simulation physics moved into a new shared module, nor a
|
|
helper that reads the configuration but is missing from the list. Both are
|
|
enforced by review: treat them as hard violations.
|
|
- A failing architecture test is a hard violation.
|
|
|
|
## Refactoring versus behaviour change (ADR 0008)
|
|
|
|
- **Refactoring an optimizer never changes its results.** With a fixed seed,
|
|
every golden input (`tests/testdata/<optimizer>/optimize_result_*.json`,
|
|
replay files) gives the same solution as before. Never regenerate or edit
|
|
golden data in a refactoring; if a golden test fails, the refactoring is
|
|
wrong.
|
|
- A change that alters results is a behaviour change: its own pull request,
|
|
declared as such, never mixed with refactoring.
|
|
- **GENETIC0 is frozen.** No bug fixes, no behaviour changes, no tuning. Only
|
|
structural changes needed to follow this architecture, each proven
|
|
result-identical by its golden tests. Fix a GENETIC0 bug in GENETIC instead,
|
|
and report it to the user.
|
|
- Before refactoring an optimizer, pin its whole output per golden input
|
|
(solution, plan, visualization); add replay files (complete optimization
|
|
parameters plus the expected result) as soon as the parameter type exists.
|
|
- **Migrating an optimizer** to this architecture: follow the steps,
|
|
characterization technique and move-then-fix rule in `MIGRATION.md` next to
|
|
this file.
|