From d9a5ac0b0be176e677b80c85ed784231270ff23a Mon Sep 17 00:00:00 2001 From: SchaafAlexander <86607129+SchaafAlexander@users.noreply.github.com> Date: Sun, 15 Nov 2026 13:58:48 +0100 Subject: [PATCH] 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 Assisted-by: Claude Code (claude-opus-5-5) --- .../optimization/genetic/genetic.py | 16 +++++--- tests/test_geneticcompletetail.py | 37 +++++++++++++++++++ 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/src/akkudoktoreos/optimization/genetic/genetic.py b/src/akkudoktoreos/optimization/genetic/genetic.py index 95a8c175..8c4ac081 100644 --- a/src/akkudoktoreos/optimization/genetic/genetic.py +++ b/src/akkudoktoreos/optimization/genetic/genetic.py @@ -1358,7 +1358,7 @@ class GeneticOptimization(OptimizationBase): end_slot: Exclusive end slot of the estimated range. skip_planned_days: Leave out local days whose run is already scheduled in the control horizon, so a day that straddles the - control end is not counted twice. + control end is not counted twice. Returns: 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 []: if appliance.schedule_mode != ConsumerScheduleMode.DAILY: 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 # window logic cannot see the tail. Query a throwaway copy sized to # the estimated range instead; it shares the same parameters, so the @@ -1392,7 +1394,7 @@ class GeneticOptimization(OptimizationBase): ) for slot in allowed: 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 by_date.setdefault(date, {}).setdefault(cycle, []).append(slot) 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( 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(): - earliest = start_slot + earliest = first_start for cycle in sorted(cycle_slots, key=lambda c: cycle_slots[c][0]): starts = [slot for slot in cycle_slots[cycle] if slot >= earliest] if not starts: @@ -1522,9 +1526,9 @@ class GeneticOptimization(OptimizationBase): inverter=inverter, prices_euro_per_wh=prices[tail], load_wh=loads[tail] - + self._estimated_daily_appliance_load( - self.control_end_slot, end_slot, skip_planned_days=True - ), + + self._estimated_daily_appliance_load( + self.control_end_slot, end_slot, skip_planned_days=True + ), pv_wh=pv[tail], feed_in_euro_per_wh=tariffs[tail], continuation=curve, diff --git a/tests/test_geneticcompletetail.py b/tests/test_geneticcompletetail.py index c8364df5..cf707aab 100644 --- a/tests/test_geneticcompletetail.py +++ b/tests/test_geneticcompletetail.py @@ -560,3 +560,40 @@ def test_daily_appliance_day_planned_in_control_is_not_counted_again_in_tail( assert not planned_days & tail_days # 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)) + + +@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)