Files
EOS/tests/test_geneticcompletedevices.py
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

331 lines
14 KiB
Python

"""Profiles, cycle windows and deadlines on the GENETIC slot grid."""
from typing import Any
import numpy as np
import pytest
from pydantic import ValidationError
from akkudoktoreos.devices.devicesabc import (
ConsumerDeadlinePolicy,
ConsumerScheduleMode,
)
from akkudoktoreos.devices.settings.batterysettings import BatteriesCommonSettings
from akkudoktoreos.devices.settings.homeappliancesettings import (
HomeApplianceCommonSettings,
)
from akkudoktoreos.optimization.genetic import geneticprepare
from akkudoktoreos.optimization.genetic0 import genetic0prepare
from akkudoktoreos.optimization.genetic.devices.geneticbattery import (
GeneticBattery,
)
from akkudoktoreos.optimization.genetic.devices.genetichomeappliance import (
GeneticDeadlinePolicy,
GeneticHomeAppliance,
resample_power_to_slot_energy,
)
from akkudoktoreos.optimization.genetic.geneticrequest import (
GeneticElectricVehicleRequest,
GeneticHomeApplianceRequest,
)
from akkudoktoreos.utils.datetimeutil import to_datetime
def appliance(*, slots: int = 192, slot_h: float = 0.25, **kwargs: Any) -> GeneticHomeAppliance:
return GeneticHomeAppliance(
GeneticHomeApplianceRequest.model_validate(
{
"device_id": "washer",
"load_profile_power_w": [1200.0, 600.0, 300.0],
"load_profile_interval_seconds": 600,
**kwargs,
}
).to_parameters(),
optimization_hours=48,
prediction_hours=slots,
slot_duration_h=slot_h,
)
def test_noninteger_resampling_conserves_real_energy() -> None:
# 10-minute 1.2kW/0.6kW/0.3kW phases: 350Wh, delivered in two 15min slots.
device = appliance()
np.testing.assert_allclose(device.run_energy_wh, [250.0, 100.0])
device.build_load_curve([3, 12])
assert device.get_load_curve().sum() == pytest.approx(700)
np.testing.assert_allclose(device.get_load_curve()[3:5], [250.0, 100.0])
assert device.run_slots == 2
@pytest.mark.parametrize("input_s,slot_s", [(600, 900), (1200, 900), (900, 3600), (3600, 900)])
def test_profile_energy_independent_of_slot_grid(input_s: int, slot_s: int) -> None:
result = resample_power_to_slot_energy([400.0, 1600.0, 200.0], input_s, slot_s)
assert result.sum() == pytest.approx(2200 * input_s / 3600)
@pytest.mark.parametrize("profile", [[], [-1], [float("nan")], [float("inf")]])
def test_invalid_profiles_rejected_in_parameters_and_settings(profile: list[float]) -> None:
for model in (GeneticHomeApplianceRequest, HomeApplianceCommonSettings):
with pytest.raises(ValidationError):
model.model_validate({"device_id": "bad", "load_profile_power_w": profile})
@pytest.mark.parametrize(
"fields", [{"duration_h": 2}, {"consumption_wh": 500}, {"duration_h": 2, "consumption_wh": 500}]
)
def test_profile_cannot_silently_override_explicit_flat_definition(fields: dict[str, int]) -> None:
for model in (GeneticHomeApplianceRequest, HomeApplianceCommonSettings):
with pytest.raises(ValidationError, match="Conflicting"):
model.model_validate({"device_id": "bad", "load_profile_power_w": [100.0], **fields})
def test_settings_profile_roundtrip_and_legacy_defaults() -> None:
defaults = HomeApplianceCommonSettings(device_id="legacy")
assert geneticprepare.home_appliance_request(defaults).consumption_wh == 3000
assert genetic0prepare.home_appliance_request(defaults).duration_h == 3
settings = HomeApplianceCommonSettings.model_validate(
{
"device_id": "washer",
"load_profile_power_w": [1200.0, 600.0],
"load_profile_interval_seconds": 600,
"schedule_mode": "DAILY",
"num_cycles": 2,
"min_cycle_gap_h": 1,
"time_windows": {"windows": [{"start_time": "07:00", "duration": "12 hours"}]},
}
)
restored = HomeApplianceCommonSettings.model_validate_json(settings.model_dump_json())
params = geneticprepare.home_appliance_request(restored)
assert params.load_profile_power_w == [1200.0, 600.0]
assert params.consumption_wh is None and params.duration_h is None
assert params.num_cycles == 2 and params.min_cycle_gap_h == 1
assert params.schedule_mode == ConsumerScheduleMode.DAILY
assert params.shared_time_windows is not None
with pytest.raises(ValueError, match="GENETIC"):
genetic0prepare.home_appliance_request(restored)
def test_cycle_and_shared_windows_intersect_after_completed_cycle() -> None:
settings = HomeApplianceCommonSettings.model_validate(
{
"device_id": "washer",
"load_profile_power_w": [1200.0],
"load_profile_interval_seconds": 1800,
"schedule_mode": "DAILY",
"cycle_time_windows": {
"windows": [
{"start_time": "06:00", "duration": "2 hours", "value": 0},
{"start_time": "18:00", "duration": "2 hours", "value": 1},
]
},
"time_windows": {"windows": [{"start_time": "18:30", "duration": "1 hour"}]},
}
)
device = GeneticHomeAppliance(geneticprepare.home_appliance_request(settings).to_parameters(), 48, 192, 0.25)
device.set_completed_cycles(1)
zero = to_datetime("2026-09-16T00:00:00+02:00", in_timezone="Europe/Berlin")
assert device.num_remaining_cycles == 1
assert device.allowed_start_slots(
slot0_datetime=zero, earliest_slot=0, horizon_end_slot=96, cycle_index=1
) == [74, 75, 76]
assert (
device.allowed_start_slots(
slot0_datetime=zero, earliest_slot=0, horizon_end_slot=96, cycle_index=0
)
== []
)
# Next-day DAILY lookup remains possible using absolute configured cycle IDs.
assert device.allowed_start_slots(
slot0_datetime=zero, earliest_slot=96, horizon_end_slot=192, cycle_index=1
) == [170, 171, 172]
def test_touching_cycle_windows_preserve_union() -> None:
device = appliance(
time_windows={
"windows": [
{"start_time": "08:00", "duration": "15 minutes", "value": 0},
{"start_time": "08:15", "duration": "15 minutes", "value": 0},
]
}
)
assert device.allowed_start_slots(
slot0_datetime=to_datetime("2026-09-16T00:00:00+02:00", in_timezone="Europe/Berlin"),
earliest_slot=0,
horizon_end_slot=96,
cycle_index=0,
) == [32]
def test_absolute_bounds_round_inward_on_quarter_hour_grid() -> None:
device = appliance(
earliest_start_datetime="2026-09-16T08:01:00+02:00",
deadline_datetime="2026-09-16T09:01:00+02:00",
deadline_policy="STRICT",
)
zero = to_datetime("2026-09-16T00:00:00+02:00", in_timezone="Europe/Berlin")
assert device.allowed_start_slots(
slot0_datetime=zero, earliest_slot=0, horizon_end_slot=96
) == [33, 34]
assert not device.deadline_missed([34], zero)
assert device.deadline_missed([35], zero)
def test_best_effort_relaxes_only_deadline_not_window_or_earliest() -> None:
device = appliance(
deadline_datetime="2026-09-16T08:00:00+02:00",
shared_time_windows={"windows": [{"start_time": "09:00", "duration": "1 hour"}]},
)
zero = to_datetime("2026-09-16T00:00:00+02:00", in_timezone="Europe/Berlin")
assert device.allowed_start_slots(
slot0_datetime=zero, earliest_slot=0, horizon_end_slot=96
) == [36]
assert device.deadline_relaxed and device.deadline_missed([36], zero)
device.deadline_policy = GeneticDeadlinePolicy.STRICT
assert (
device.allowed_start_slots(slot0_datetime=zero, earliest_slot=0, horizon_end_slot=96) == []
)
assert not device.deadline_relaxed
def test_overnight_window_uses_opening_date_and_weekday() -> None:
device = appliance(
shared_time_windows={
"windows": [
{
"start_time": "23:00",
"duration": "3 hours",
"date": "2026-09-15",
"day_of_week": 1,
}
]
}
)
zero = to_datetime("2026-09-16T00:00:00+02:00", in_timezone="Europe/Berlin")
assert device.allowed_start_slots(
slot0_datetime=zero, earliest_slot=0, horizon_end_slot=96
) == list(range(7))
@pytest.mark.parametrize("date,expected", [("2026-03-29", 8), ("2026-10-25", 16)])
def test_dst_elapsed_slots_and_local_window(date: str, expected: int) -> None:
zero = to_datetime(f"{date}T00:00:00", in_timezone="Europe/Berlin")
device = appliance(
shared_time_windows={"windows": [{"start_time": "03:00", "duration": "30 minutes"}]}
)
assert device.allowed_start_slots(
slot0_datetime=zero, earliest_slot=0, horizon_end_slot=100
) == [expected]
assert device.run_end_datetime(expected, zero).hour == 3
@pytest.mark.parametrize("starts", [[-1], [191], [192]])
def test_complete_curve_does_not_silently_truncate_runs(starts: list[int]) -> None:
with pytest.raises(ValueError, match="complete"):
appliance().build_load_curve(starts)
def test_ev_deadlines_roundtrip_without_changing_slot_physics() -> None:
settings = BatteriesCommonSettings.model_validate(
{
"device_id": "car",
"profile_adjustment": {"nominal_capacity_wh": 10000, "max_charge_power_w": 4000},
"min_soc_deadline_datetime": "2026-09-16T07:30:00+02:00",
"min_soc_max_duration_h": 2.5,
}
)
params = geneticprepare.electric_vehicle_request(settings)
assert params.min_soc_max_duration_h == 2.5
assert params.min_soc_deadline_datetime is not None
assert params.min_soc_deadline_datetime.in_timezone("Europe/Berlin").hour == 7
assert params.capacity_wh == 10000
assert params.max_charge_power_w == pytest.approx(4000)
battery = GeneticBattery(params.to_parameters(), prediction_hours=16, slot_duration_h=0.25)
battery.charge_array[0] = 1
# 4000 W for a 0.25 h slot caps the 2000 Wh request at 1000 Wh; the charging
# efficiency comes from the battery profile.
charged, losses = battery.charge_energy(2000, hour=0)
assert charged == pytest.approx(1000 * params.charging_efficiency)
assert losses == pytest.approx(1000 * (1 - params.charging_efficiency))
assert "min_soc_deadline_datetime" not in genetic0prepare.electric_vehicle_request(settings).model_dump()
@pytest.mark.parametrize("duration", [0.0, -1.0, float("inf"), float("nan")])
def test_ev_invalid_departure_durations_rejected(duration: float) -> None:
# Optimizer parameters take "capacity_wh", settings a profile adjustment.
for model, capacity in (
(GeneticElectricVehicleRequest, {"capacity_wh": 10000}),
(BatteriesCommonSettings, {"profile_adjustment": {"nominal_capacity_wh": 10000}}),
):
with pytest.raises(ValidationError):
model.model_validate({"device_id": "car", **capacity, "min_soc_max_duration_h": duration})
def test_completed_cycles_parameter_initializes_remaining_cycles() -> None:
device = appliance(num_cycles=3, completed_cycles=2)
assert device.completed_cycles == 2
assert device.num_remaining_cycles == 1
done = appliance(num_cycles=3, completed_cycles=3)
assert done.num_remaining_cycles == 0
with pytest.raises(ValidationError, match="completed_cycles"):
appliance(num_cycles=2, completed_cycles=3)
def test_best_effort_multiple_cycles_retain_room_for_joint_gap_repair() -> None:
device = appliance(
num_cycles=2,
min_cycle_gap_h=1,
deadline_datetime="2026-09-16T08:00:00+02:00",
shared_time_windows={"windows": [{"start_time": "09:00", "duration": "3 hours"}]},
)
zero = to_datetime("2026-09-16T00:00:00+02:00", in_timezone="Europe/Berlin")
assert device.allowed_start_slots(
slot0_datetime=zero, earliest_slot=0, horizon_end_slot=96, cycle_index=0
) == list(range(36, 47))
assert device.deadline_relaxed
device.build_load_curve([36, 42])
assert device.get_load_curve().sum() == pytest.approx(700)
@pytest.mark.parametrize("deadline", ["2026-09-16T07:00:00Z", "2026-09-16T09:00:00+02:00"])
def test_absolute_deadline_offsets_describe_same_instant(deadline: str) -> None:
device = appliance(deadline_datetime=deadline, deadline_policy="STRICT")
zero = to_datetime("2026-09-16T08:00:00+02:00", in_timezone="Europe/Berlin")
assert device.allowed_start_slots(
slot0_datetime=zero, earliest_slot=0, horizon_end_slot=16
) == [0, 1, 2]
assert not device.deadline_missed([2], zero)
assert device.deadline_missed([3], zero)
APPLIANCE_REQUEST: dict[str, Any] = {
"device_id": "dishwasher",
"consumption_wh": 2000,
"duration_h": 2,
"num_cycles": 1,
}
@pytest.mark.parametrize("host_timezone", ["Europe/Berlin", "America/New_York"])
@pytest.mark.parametrize(
"model, field, request_data",
[
(GeneticElectricVehicleRequest, "min_soc_deadline_datetime", {"device_id": "car", "capacity_wh": 10000}),
(GeneticHomeApplianceRequest, "earliest_start_datetime", APPLIANCE_REQUEST),
(GeneticHomeApplianceRequest, "deadline_datetime", APPLIANCE_REQUEST),
(GeneticHomeApplianceRequest, "run_started_datetime", APPLIANCE_REQUEST),
],
)
def test_request_date_times_are_held_in_utc(
set_other_timezone, host_timezone, model, field, request_data
) -> None:
"""Request date-times render the same on every host and read back the same."""
set_other_timezone(host_timezone)
moment = to_datetime("2026-09-16T09:30:00", in_timezone="Asia/Tokyo")
request = model.model_validate({**request_data, field: moment})
assert getattr(request, field) == moment
assert getattr(request, field).timezone_name == "UTC"
dumped = request.model_dump(mode="json")
assert dumped[field] == "2026-09-16T00:30:00Z"
assert model.model_validate(dumped).model_dump(mode="json") == dumped