fix(genetic): count a run in progress in the DAILY appliance estimate

Since #1549 a run in progress counts as the run of its day and delays
the next cycle until after its end and the minimum gap. The DAILY
appliance estimate of the tail and terminal value now does the same
(completed_cycles_on, next_cycle_earliest_slot), so a running appliance
is not expected to run a second time on that day.

Additional changes
Format the tail load expression and the estimate docstring (ruff format)
Test the estimate with and without a reported run in progress

Refs: #1495
Signed-off-by: SchaafAlexander <86607129+SchaafAlexander@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Assisted-by: Claude Code (claude-opus-5-5)
This commit is contained in:
SchaafAlexander
2026-11-15 15:10:57 +01:00
committed by Bobby Noelte
co-authored by Claude Opus 5.5
parent a344e04ca5
commit d9a5ac0b0b
2 changed files with 47 additions and 6 deletions
@@ -1358,7 +1358,7 @@ class GeneticOptimization(OptimizationBase):
end_slot: Exclusive end slot of the estimated range. end_slot: Exclusive end slot of the estimated range.
skip_planned_days: Leave out local days whose run is already skip_planned_days: Leave out local days whose run is already
scheduled in the control horizon, so a day that straddles the scheduled in the control horizon, so a day that straddles the
control end is not counted twice. control end is not counted twice.
Returns: Returns:
Per-slot appliance load [Wh] over ``[start_slot, end_slot)``. Per-slot appliance load [Wh] over ``[start_slot, end_slot)``.
@@ -1372,6 +1372,8 @@ class GeneticOptimization(OptimizationBase):
for appliance in self.simulation.home_appliances or []: for appliance in self.simulation.home_appliances or []:
if appliance.schedule_mode != ConsumerScheduleMode.DAILY: if appliance.schedule_mode != ConsumerScheduleMode.DAILY:
continue continue
# A run in progress counts as done for its day, as in the layout.
completed_cycles = appliance.completed_cycles_on(first_date)
# The configured appliance only spans the control horizon, so its # The configured appliance only spans the control horizon, so its
# window logic cannot see the tail. Query a throwaway copy sized to # window logic cannot see the tail. Query a throwaway copy sized to
# the estimated range instead; it shares the same parameters, so the # the estimated range instead; it shares the same parameters, so the
@@ -1392,7 +1394,7 @@ class GeneticOptimization(OptimizationBase):
) )
for slot in allowed: for slot in allowed:
date = slot0.add(seconds=slot * slot_seconds).date() date = slot0.add(seconds=slot * slot_seconds).date()
if date == first_date and cycle < appliance.completed_cycles: if date == first_date and cycle < completed_cycles:
continue continue
by_date.setdefault(date, {}).setdefault(cycle, []).append(slot) by_date.setdefault(date, {}).setdefault(cycle, []).append(slot)
if skip_planned_days and self.appliance_layout is not None: if skip_planned_days and self.appliance_layout is not None:
@@ -1405,8 +1407,10 @@ class GeneticOptimization(OptimizationBase):
gap_slots = appliance.run_slots + math.ceil( gap_slots = appliance.run_slots + math.ceil(
appliance.min_cycle_gap_h / self.slot_duration_h appliance.min_cycle_gap_h / self.slot_duration_h
) )
# The next run starts after a run in progress and its minimum gap.
first_start = max(start_slot, appliance.next_cycle_earliest_slot())
for cycle_slots in by_date.values(): for cycle_slots in by_date.values():
earliest = start_slot earliest = first_start
for cycle in sorted(cycle_slots, key=lambda c: cycle_slots[c][0]): for cycle in sorted(cycle_slots, key=lambda c: cycle_slots[c][0]):
starts = [slot for slot in cycle_slots[cycle] if slot >= earliest] starts = [slot for slot in cycle_slots[cycle] if slot >= earliest]
if not starts: if not starts:
@@ -1522,9 +1526,9 @@ class GeneticOptimization(OptimizationBase):
inverter=inverter, inverter=inverter,
prices_euro_per_wh=prices[tail], prices_euro_per_wh=prices[tail],
load_wh=loads[tail] load_wh=loads[tail]
+ self._estimated_daily_appliance_load( + self._estimated_daily_appliance_load(
self.control_end_slot, end_slot, skip_planned_days=True self.control_end_slot, end_slot, skip_planned_days=True
), ),
pv_wh=pv[tail], pv_wh=pv[tail],
feed_in_euro_per_wh=tariffs[tail], feed_in_euro_per_wh=tariffs[tail],
continuation=curve, continuation=curve,
+37
View File
@@ -560,3 +560,40 @@ def test_daily_appliance_day_planned_in_control_is_not_counted_again_in_tail(
assert not planned_days & tail_days assert not planned_days & tail_days
# Base load is zero: the tail holds exactly one 3 kWh run per remaining day. # Base load is zero: the tail holds exactly one 3 kWh run per remaining day.
assert float(np.sum(tail_load)) == pytest.approx(3000 * len(tail_days)) assert float(np.sum(tail_load)) == pytest.approx(3000 * len(tail_days))
@pytest.mark.parametrize("running", [False, True])
def test_daily_appliance_run_in_progress_counts_for_its_day_in_estimate(config_eos, running):
"""#1495 with #1548: a run in progress is the day's run, not an extra one.
A water heater started at 01:30 and still runs at 02:00. Its day is done:
the appliance estimate adds no further run on that day, only the run of
the next day. Without the report, the day's run is still expected.
"""
opt, params = setup_run(config_eos, start_hour=2, hours=72 + 2)
appliance = dict(
device_id="water_heater",
consumption_wh=3000,
duration_h=2,
num_cycles=1,
schedule_mode="DAILY",
time_windows={"windows": [{"start_time": "00:00", "duration": "5 hours", "value": 0}]},
)
if running:
appliance["run_started_datetime"] = to_datetime("2026-09-05T01:30:00")
params.home_appliances = [HomeApplianceParameters.model_validate(appliance)]
def choose(*args, **kwargs):
genome = opt.create_individual()
genome[:] = [0] * len(genome)
return genome, {}
with patch.object(opt, "optimize", side_effect=choose):
opt.optimize_ems(params)
# Slot 0 is 02:00; slots 0-21 are the rest of 2026-09-05, 22-45 the next day.
first_day = opt._estimated_daily_appliance_load(0, 22)
two_days = opt._estimated_daily_appliance_load(0, 46)
expected_first_day = 0.0 if running else 3000.0
assert float(np.sum(first_day)) == pytest.approx(expected_first_day)
assert float(np.sum(two_days)) == pytest.approx(expected_first_day + 3000.0)