Files
EOS/docs/adr/0006-optimizer-interface-prepare-run-convert.md
Bobby NoelteandClaude Opus 5.5 b3d5334d3e 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)
2026-11-27 13:55:41 +01:00

57 lines
3.8 KiB
Markdown

# Optimizers implement prepare, run and convert
Every optimizer follows the same three-step interface (prepare, run, convert), so the energy management can drive any of
them the same way and every run can be replayed from its optimization parameters. The interface is
a `Protocol` generic in the optimizer's optimization parameters `P`, optimization request `Q` and
optimization result `R`, with `run_clock()` (the slot length and time zone of the next run),
`prepare()` and `prepare_from_request()` (asynchronous, given the run start, the observation
time, the warm start and run-time overrides), `run()`, `convert()`
(asynchronous), which returns the optimization solution and the energy management plan, and
`api()`, which hands out the optimizer's REST router
([0011](0011-optimizer-api-visualization-and-wizard-get-no-exception.md)). Its current signature
is kept in one place, the `optimizer-architecture` skill
(`ai/skills/optimizer-architecture/SKILL.md`).
- `run_clock()` gives the energy management the optimizer's slot length and, for a run without
an explicit start, the time zone to take the current time in; the energy management rounds the
start down to a slot boundary before `prepare()`. The optimizer reads both from its settings
through its parameter preparation; the energy management knows no optimizer's settings.
- `prepare()` is the parameter preparation. It never fails and never returns `None` (see the
`energy-domain` skill); the energy management never cancels a run because of it.
- `run()` is CPU-bound and deterministic: the same optimization parameters give the same result.
All algorithm settings (generations, individuals, seed, penalties) are part of the optimization
parameters; a REST override such as `?ngen=` is passed to the preparation as an override and
so becomes part of the parameters before `run()`.
- `convert()` is the solution conversion and uses only the parameters and the result.
The energy management is the only owner of run state. It keeps the last result per optimizer and
passes the run start, the observation time (the unrounded moment measurements refer to) and that
result (the warm start) to `prepare()` as arguments, so no part of an optimizer reads EMS state;
the warm start and its start datetime travel into `run()` inside the optimization parameters.
Optimization requests from clients (the parameters accepted by `POST /optimize` and the GENETIC0
legacy endpoint, and `GeneticRequest`) never go straight into `run()`. They keep their
public schema (`Q`) and go through the optimizer's parameter preparation,
`prepare_from_request()`, which fills everything the
request does not carry (time zone, device IDs, algorithm settings) from the configuration and
applies the same fallback rules as `prepare()`. A malformed request is still rejected (422) before
preparation; a request that is valid but incomplete is completed, never rejected. So `run()` and
`convert()` only ever see complete optimization parameters, whatever the entry point.
## Considered Options
- **Preparation reads the previous result from the energy management**: rejected. It keeps the
optimizer coupled to EMS internals (as GENETIC's `get_ems().genetic_solution()` did), and a run cannot be
reproduced from its parameters alone.
- **Algorithm settings passed beside the parameters**: rejected. A stored parameter set would not
be enough to replay a run.
- **Clients send complete optimization parameters**: rejected. The presentation data required by
[0003](0003-optimizers-see-only-their-optimization-parameters.md) would break every existing
client of the public API.
## Consequences
- The energy management stores results per optimizer in one generic slot, not one attribute per
optimizer.
- An optimization run that cannot be reproduced from its stored optimization parameters is a bug.