mirror of
https://github.com/Akkudoktor-EOS/EOS.git
synced 2026-11-25 23:16:18 +01:00
fix(genetic): plan the running slot only for the remaining time
A run within a slot (e.g. 10:45 with hourly slots) planned slot 0 as a full slot, although the SoC is measured at the run time. Slot 0 now moves only the remaining part of the slot energy: battery, EV and inverter energy caps, SoC power limit tables and the slot's PV and load forecast are scaled by first_slot_fraction; efficiency curves are read at the power of that part. The plan's SoC headroom clamp of the AC charge factor uses the same part, and the GENETIC result reports first_slot_fraction. A run on a slot boundary is unchanged. Additional changes Pin GENETIC test start times to the full hour; their results now depend on seconds within the slot Document first_slot_fraction for consumers of GENETIC results Refs: #1543 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:
committed by
Bobby Noelte
co-authored by
Claude Opus 5.5
parent
7e10f64048
commit
0bf9f50b97
@@ -1056,6 +1056,37 @@ class GeneticOptimization(OptimizationBase):
|
||||
"""Genomes and device arrays start at now, independently of wall-clock hour."""
|
||||
return 0
|
||||
|
||||
def _remaining_first_slot_fraction(self) -> float:
|
||||
"""Return the part of slot 0 that is still ahead of the run.
|
||||
|
||||
The run start is rounded down to the slot (``ems.start_datetime``),
|
||||
while the measurements and the plan apply from the unrounded run time
|
||||
(``ems.observation_datetime``). A run at 10:45 with hourly slots has
|
||||
a quarter of slot 0 left. A run on a slot boundary, or without a
|
||||
usable run time, keeps the full slot (1.0).
|
||||
"""
|
||||
slot_seconds = self.slot_duration_h * 3600
|
||||
slot0 = self.ems.start_datetime
|
||||
observed = self.ems.observation_datetime
|
||||
elapsed_s = (observed - slot0).total_seconds()
|
||||
if elapsed_s <= 0.0:
|
||||
return 1.0
|
||||
if elapsed_s >= slot_seconds:
|
||||
logger.debug(
|
||||
"Run time {} is not within slot 0 starting {}; planning a full first slot.",
|
||||
observed,
|
||||
slot0,
|
||||
)
|
||||
return 1.0
|
||||
fraction = (slot_seconds - elapsed_s) / slot_seconds
|
||||
logger.debug(
|
||||
"Run at {} within slot 0 starting {}: {:.3f} of slot 0 left.",
|
||||
observed,
|
||||
slot0,
|
||||
fraction,
|
||||
)
|
||||
return fraction
|
||||
|
||||
def _start_day_slot(self) -> int:
|
||||
"""Offset used only to trim legacy midnight-indexed forecast inputs."""
|
||||
sd = self.ems.start_datetime
|
||||
@@ -1085,6 +1116,9 @@ class GeneticOptimization(OptimizationBase):
|
||||
# Populated from parameters.pv_battery.grid_export_rates in optimize_ems;
|
||||
# the single full-power default keeps the all-or-nothing export.
|
||||
self.bat_possible_grid_export_values: list[float] = [1.0]
|
||||
# Part of slot 0 still ahead of the run (1.0 on a slot boundary), set
|
||||
# per run by optimize_ems().
|
||||
self.first_slot_fraction: float = 1.0
|
||||
# Slot by which the EV has to reach its target SoC. None means the SoC is
|
||||
# only required at the end of the horizon (the behaviour without a deadline).
|
||||
self._ev_soc_deadline_slot: Optional[int] = None
|
||||
@@ -2524,15 +2558,18 @@ class GeneticOptimization(OptimizationBase):
|
||||
return schedule
|
||||
|
||||
max_stored_wh = ev.max_charge_power_w * self.slot_duration_h * ev.charging_efficiency
|
||||
first_slot_fraction = getattr(self, "first_slot_fraction", 1.0)
|
||||
remaining_wh = required_stored_wh
|
||||
for slot in candidates:
|
||||
required_rate = remaining_wh / max(max_stored_wh, 1e-9)
|
||||
# Slot 0 only has its remaining part when the run starts within it.
|
||||
slot_stored_wh = max_stored_wh * (first_slot_fraction if slot == 0 else 1.0)
|
||||
required_rate = remaining_wh / max(slot_stored_wh, 1e-9)
|
||||
rate, rate_index = next(
|
||||
(item for item in positive_rates if item[0] >= required_rate),
|
||||
positive_rates[-1],
|
||||
)
|
||||
schedule[slot] = rate_index
|
||||
remaining_wh -= max_stored_wh * rate
|
||||
remaining_wh -= slot_stored_wh * rate
|
||||
if remaining_wh <= 1e-9:
|
||||
break
|
||||
return schedule
|
||||
@@ -3856,8 +3893,12 @@ class GeneticOptimization(OptimizationBase):
|
||||
# AC charging at this hour is economically unjustified.
|
||||
# Penalty = excess cost per Wh × DC energy requested this slot.
|
||||
# max_charge_power_w is a power [W]; the energy movable in
|
||||
# one slot is power × slot_duration_h (¼ at 15 min).
|
||||
dc_wh = bat.max_charge_power_w * self.slot_duration_h * ac_factor
|
||||
# one slot is power × slot_duration_h (¼ at 15 min), and
|
||||
# only the remaining part of slot 0 within that slot.
|
||||
slot_scale = getattr(self, "first_slot_fraction", 1.0) if hour == 0 else 1.0
|
||||
dc_wh = (
|
||||
bat.max_charge_power_w * self.slot_duration_h * slot_scale * ac_factor
|
||||
)
|
||||
ac_wh = dc_wh / max(inv.ac_to_dc_efficiency, 1e-9)
|
||||
excess_cost_per_wh = break_even_price - best_uncovered_price
|
||||
gesamtbilanz += ac_wh * excess_cost_per_wh * ac_penalty_factor
|
||||
@@ -4215,6 +4256,9 @@ class GeneticOptimization(OptimizationBase):
|
||||
# break-even lookup must be rebuilt lazily on first evaluation.
|
||||
self._ac_break_even_best_prices = None
|
||||
|
||||
# A run within a slot plans slot 0 for the remaining time only.
|
||||
self.first_slot_fraction = self._remaining_first_slot_fraction()
|
||||
|
||||
# Initialize PV and EV batteries. slot_duration_h lets the Battery scale
|
||||
# its power caps (max_charge_power_w) to a per-slot energy cap.
|
||||
akku: Optional[Battery] = None
|
||||
@@ -4223,6 +4267,7 @@ class GeneticOptimization(OptimizationBase):
|
||||
parameters.pv_battery,
|
||||
prediction_hours=self.control_end_slot,
|
||||
slot_duration_h=self.slot_duration_h,
|
||||
first_slot_fraction=self.first_slot_fraction,
|
||||
)
|
||||
akku.set_charge_per_hour(np.full(self.control_end_slot, 0))
|
||||
|
||||
@@ -4232,6 +4277,7 @@ class GeneticOptimization(OptimizationBase):
|
||||
parameters.ev,
|
||||
prediction_hours=self.control_end_slot,
|
||||
slot_duration_h=self.slot_duration_h,
|
||||
first_slot_fraction=self.first_slot_fraction,
|
||||
)
|
||||
eauto.set_charge_per_hour(np.full(self.control_end_slot, 1))
|
||||
self.optimize_ev = (
|
||||
@@ -4356,6 +4402,7 @@ class GeneticOptimization(OptimizationBase):
|
||||
parameters.inverter,
|
||||
battery=akku,
|
||||
slot_duration_h=self.slot_duration_h,
|
||||
first_slot_fraction=self.first_slot_fraction,
|
||||
)
|
||||
|
||||
# Prepare device simulation
|
||||
@@ -4386,6 +4433,16 @@ class GeneticOptimization(OptimizationBase):
|
||||
"elect_revenue_per_hour_arr",
|
||||
):
|
||||
setattr(self.simulation, name, getattr(self.simulation, name)[: self.control_slots])
|
||||
# Only the remaining part of slot 0 is ahead: its PV and load forecast
|
||||
# shrink to that part (spread evenly over the slot). Prices are per Wh
|
||||
# and stay. The terminal value curve above keeps the full forecasts.
|
||||
if self.first_slot_fraction < 1.0:
|
||||
for name in ("load_energy_array", "pv_prediction_wh"):
|
||||
values = getattr(self.simulation, name)
|
||||
if values is not None and len(values) > 0:
|
||||
values = np.array(values, dtype=float)
|
||||
values[0] *= self.first_slot_fraction
|
||||
setattr(self.simulation, name, values)
|
||||
|
||||
# Setup the DEAP environment and optimization process. The appliance
|
||||
# genome layout (built above) drives the appliance gene block; evaluate
|
||||
@@ -4481,6 +4538,7 @@ class GeneticOptimization(OptimizationBase):
|
||||
**{
|
||||
"parameters": parameters,
|
||||
"interval_seconds": self._genetic_cfg.interval_sec,
|
||||
"first_slot_fraction": self.first_slot_fraction,
|
||||
"start_hour": start_hour,
|
||||
"extra_data": extra_data,
|
||||
"fitness_history": self.fitness_history,
|
||||
|
||||
@@ -474,6 +474,18 @@ class GeneticSolution(ConfigMixin, GeneticParametersBaseModel):
|
||||
default=3600, description="Duration of one result/control slot in seconds."
|
||||
)
|
||||
|
||||
first_slot_fraction: float = Field(
|
||||
default=1.0,
|
||||
gt=0.0,
|
||||
le=1.0,
|
||||
description=(
|
||||
"Part of the first control slot that was still ahead when the "
|
||||
"optimization ran (0.25 for a run at 10:45 with hourly slots). The "
|
||||
"results of the first slot cover only that part, and its factors are "
|
||||
"chosen for it. 1.0 for a run on a slot boundary."
|
||||
),
|
||||
)
|
||||
|
||||
@field_validator("start_solution_datetime", mode="before")
|
||||
@classmethod
|
||||
def transform_start_solution_datetime(cls, value: Any) -> Optional[DateTime]:
|
||||
@@ -621,6 +633,7 @@ class GeneticSolution(ConfigMixin, GeneticParametersBaseModel):
|
||||
discharge_allowed: bool,
|
||||
soc_pct: float,
|
||||
battery_grid_export_allowed: bool = False,
|
||||
slot_fraction: float = 1.0,
|
||||
) -> tuple[float, float, bool, bool]:
|
||||
"""Clamp raw genetic gene values by the battery's actual SOC at that hour.
|
||||
|
||||
@@ -638,6 +651,10 @@ class GeneticSolution(ConfigMixin, GeneticParametersBaseModel):
|
||||
(the inverter curtails automatically, but this makes intent clear).
|
||||
- Discharge: blocked when SOC is at or below min SOC.
|
||||
- Battery grid export: blocked when SOC is at or below min SOC.
|
||||
|
||||
``slot_fraction`` is the part of the slot the factors apply to:
|
||||
``first_slot_fraction`` for the running first slot, else 1.0. The
|
||||
headroom check of the AC charge factor uses only that part of the slot.
|
||||
"""
|
||||
parameters = getattr(self, "parameters", None)
|
||||
# Without parameters, convert the battery settings: they hold no
|
||||
@@ -679,7 +696,7 @@ class GeneticSolution(ConfigMixin, GeneticParametersBaseModel):
|
||||
)
|
||||
# Energy storable in one optimization slot, not per hour: scale the
|
||||
# power [W] by the slot duration (1.0 hourly, 0.25 at 15 min).
|
||||
slot_duration_h = float(self.interval_seconds) / 3600.0
|
||||
slot_duration_h = float(self.interval_seconds) / 3600.0 * slot_fraction
|
||||
max_dc_per_slot_wh = (
|
||||
effective_ac * max_ac_cp_w * slot_duration_h * ac_to_dc_eff * ch_eff
|
||||
)
|
||||
@@ -821,6 +838,7 @@ class GeneticSolution(ConfigMixin, GeneticParametersBaseModel):
|
||||
discharge_allowed_hour,
|
||||
soc_h_pct,
|
||||
battery_grid_export_allowed_hour,
|
||||
self.first_slot_fraction if result_idx == 0 else 1.0,
|
||||
)
|
||||
operation_mode, operation_mode_factor = self._battery_operation_from_solution(
|
||||
eff_ac, eff_dc, eff_dis, eff_grid_export, battery_grid_export_factor_hour
|
||||
@@ -1097,6 +1115,7 @@ class GeneticSolution(ConfigMixin, GeneticParametersBaseModel):
|
||||
bool(self.discharge_allowed[hour_idx]),
|
||||
soc_h_pct,
|
||||
battery_grid_export_allowed_hour,
|
||||
self.first_slot_fraction if result_idx == 0 else 1.0,
|
||||
)
|
||||
operation_mode, operation_mode_factor = self._battery_operation_from_solution(
|
||||
eff_ac,
|
||||
|
||||
Reference in New Issue
Block a user