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
@@ -406,17 +406,33 @@ class Battery:
|
||||
parameters: BaseBatteryParameters,
|
||||
prediction_hours: int,
|
||||
slot_duration_h: float = 1.0,
|
||||
first_slot_fraction: float = 1.0,
|
||||
):
|
||||
# `prediction_hours` is the number of optimization slots, not hours. At
|
||||
# the default optimization interval of 3600 s, slot_duration_h is 1.0 and
|
||||
# the slot count equals the hour count, so existing callers are
|
||||
# unaffected. At 900 s (15 min) slot_duration_h is 0.25 and there are 4x
|
||||
# as many slots, each able to move a quarter of the hourly energy.
|
||||
#
|
||||
# `first_slot_fraction` is the part of slot 0 still ahead when the
|
||||
# optimization runs within that slot (0.25 at 10:45 with hourly slots).
|
||||
# Slot 0 then moves only that fraction of the slot energy; the slot's
|
||||
# power limits and power dependent efficiencies stay the same.
|
||||
if not 0.0 < first_slot_fraction <= 1.0:
|
||||
raise ValueError(f"first_slot_fraction must be in (0, 1], got {first_slot_fraction}.")
|
||||
self.parameters = parameters
|
||||
self.prediction_hours = prediction_hours
|
||||
self.slot_duration_h = slot_duration_h
|
||||
self.first_slot_fraction = first_slot_fraction
|
||||
self._setup()
|
||||
|
||||
def slot_scale(self, hour: int) -> float:
|
||||
"""Return the part of the slot duration that is simulated for slot ``hour``.
|
||||
|
||||
``first_slot_fraction`` for slot 0, 1.0 for every later slot.
|
||||
"""
|
||||
return self.first_slot_fraction if hour == 0 else 1.0
|
||||
|
||||
def _setup(self) -> None:
|
||||
"""Sets up the battery parameters based on provided parameters."""
|
||||
self.capacity_wh = self.parameters.capacity_wh
|
||||
@@ -548,24 +564,30 @@ class Battery:
|
||||
self._curves.reset(self.prediction_hours)
|
||||
self._charge_limit_raw_wh_per_slot = [float("inf")] * self.prediction_hours
|
||||
|
||||
def _discharge_slot_cap_wh(self) -> float:
|
||||
"""Return the raw discharge energy of one slot [Wh] at the current SoC."""
|
||||
def _discharge_slot_cap_wh(self, hour: int) -> float:
|
||||
"""Return the raw discharge energy of slot ``hour`` [Wh] at the current SoC."""
|
||||
curves = self._curves
|
||||
if curves is None or curves.discharge_limit_wh_by_soc is None:
|
||||
return self.max_discharge_power_w * self.slot_duration_h
|
||||
return self.max_discharge_power_w * self.slot_duration_h * self.slot_scale(hour)
|
||||
return _table_entry(
|
||||
curves.discharge_limit_wh_by_soc,
|
||||
int(self.soc_wh * curves.soc_index_per_wh + 0.5),
|
||||
)
|
||||
) * self.slot_scale(hour)
|
||||
|
||||
def _discharging_efficiency_at(self, slot_raw_wh: float) -> float:
|
||||
"""Return the discharging efficiency at the slot power of ``slot_raw_wh``."""
|
||||
def _discharging_efficiency_at(self, slot_raw_wh: float, scale: float = 1.0) -> float:
|
||||
"""Return the discharging efficiency at the slot power of ``slot_raw_wh``.
|
||||
|
||||
Args:
|
||||
slot_raw_wh: Raw discharge energy of the slot [Wh].
|
||||
scale: Simulated part of the slot duration (see ``slot_scale``); the
|
||||
power is the energy divided by that part of the slot.
|
||||
"""
|
||||
curves = self._curves
|
||||
if curves is None or curves.discharging_efficiency_by_power is None:
|
||||
return self.discharging_efficiency
|
||||
return _table_entry(
|
||||
curves.discharging_efficiency_by_power,
|
||||
int(slot_raw_wh * curves.discharge_index_per_wh + 0.5),
|
||||
int(slot_raw_wh / scale * curves.discharge_index_per_wh + 0.5),
|
||||
)
|
||||
|
||||
def limit_slot_charge(self, hour: int, raw_wh: float) -> None:
|
||||
@@ -582,24 +604,29 @@ class Battery:
|
||||
self._charge_limit_raw_wh_per_slot[hour], max(float(raw_wh), 0.0)
|
||||
)
|
||||
|
||||
def rated_discharge_energy_wh(self) -> float:
|
||||
"""Return the DC energy one full-power discharge slot delivers.
|
||||
def rated_discharge_energy_wh(self, hour: Optional[int] = None) -> float:
|
||||
"""Return the DC energy a full-power discharge delivers in one slot.
|
||||
|
||||
This is the reference a grid-export rate is applied to: a rate of 0.5
|
||||
exports at most half the battery's rated discharge power, independent of
|
||||
how much of the slot budget self-consumption already used.
|
||||
|
||||
Args:
|
||||
hour: Slot index. Slot 0 delivers only ``first_slot_fraction`` of
|
||||
the slot energy. None gives a full slot.
|
||||
"""
|
||||
rated_slot_wh = self.max_discharge_power_w * self.slot_duration_h
|
||||
return rated_slot_wh * self._discharging_efficiency_at(rated_slot_wh)
|
||||
scale = 1.0 if hour is None else self.slot_scale(hour)
|
||||
rated_slot_wh = self.max_discharge_power_w * self.slot_duration_h * scale
|
||||
return rated_slot_wh * self._discharging_efficiency_at(rated_slot_wh, scale)
|
||||
|
||||
def remaining_discharge_energy_wh(self, hour: int) -> float:
|
||||
"""Return DC energy still deliverable within one optimization slot."""
|
||||
discharged_raw_wh = self._discharged_raw_wh_per_slot[hour]
|
||||
raw_power_remaining_wh = max(self._discharge_slot_cap_wh() - discharged_raw_wh, 0.0)
|
||||
raw_power_remaining_wh = max(self._discharge_slot_cap_wh(hour) - discharged_raw_wh, 0.0)
|
||||
raw_soc_available_wh = max(self.soc_wh - self.min_soc_wh, 0.0)
|
||||
raw_remaining_wh = min(raw_power_remaining_wh, raw_soc_available_wh)
|
||||
return raw_remaining_wh * self._discharging_efficiency_at(
|
||||
discharged_raw_wh + raw_remaining_wh
|
||||
discharged_raw_wh + raw_remaining_wh, self.slot_scale(hour)
|
||||
)
|
||||
|
||||
def discharge_released(self, hour: int) -> bool:
|
||||
@@ -672,22 +699,26 @@ class Battery:
|
||||
|
||||
# Maximum raw discharge due to power limit, scaled to the slot duration.
|
||||
# The power limit is a power [W]; energy movable in one slot is
|
||||
# power x slot_duration_h. With a battery profile the limit and the
|
||||
# efficiency are looked up inline (no calls): this runs for every slot
|
||||
# of every candidate plan.
|
||||
# power x slot_duration_h, and only the remaining part of slot 0 when
|
||||
# the optimization runs within it. With a battery profile the limit and
|
||||
# the efficiency are looked up inline (no calls): this runs for every
|
||||
# slot of every candidate plan.
|
||||
discharged_raw_wh = self._discharged_raw_wh_per_slot[hour]
|
||||
discharging_efficiency = self.discharging_efficiency
|
||||
scale = self.first_slot_fraction if hour == 0 else 1.0
|
||||
curves = self._curves
|
||||
if curves is None:
|
||||
slot_cap_wh = self.max_discharge_power_w * self.slot_duration_h
|
||||
slot_cap_wh = self.max_discharge_power_w * self.slot_duration_h * scale
|
||||
raw_withdrawal_wh = min(raw_available_wh, max(slot_cap_wh - discharged_raw_wh, 0.0))
|
||||
else:
|
||||
limits = curves.discharge_limit_wh_by_soc
|
||||
if limits is None:
|
||||
slot_cap_wh = self.max_discharge_power_w * self.slot_duration_h
|
||||
slot_cap_wh = self.max_discharge_power_w * self.slot_duration_h * scale
|
||||
else:
|
||||
index = int(self.soc_wh * curves.soc_index_per_wh + 0.5)
|
||||
slot_cap_wh = limits[index] if 0 <= index <= 100 else _table_entry(limits, index)
|
||||
slot_cap_wh = (
|
||||
limits[index] if 0 <= index <= 100 else _table_entry(limits, index)
|
||||
) * scale
|
||||
raw_withdrawal_wh = slot_cap_wh - discharged_raw_wh
|
||||
if raw_withdrawal_wh < 0.0:
|
||||
raw_withdrawal_wh = 0.0
|
||||
@@ -702,7 +733,8 @@ class Battery:
|
||||
if raw_estimate_wh > raw_withdrawal_wh:
|
||||
raw_estimate_wh = raw_withdrawal_wh
|
||||
index = int(
|
||||
(discharged_raw_wh + raw_estimate_wh) * curves.discharge_index_per_wh + 0.5
|
||||
(discharged_raw_wh + raw_estimate_wh) / scale * curves.discharge_index_per_wh
|
||||
+ 0.5
|
||||
)
|
||||
discharging_efficiency = efficiencies[index] if index <= 100 else efficiencies[-1]
|
||||
|
||||
@@ -804,9 +836,11 @@ class Battery:
|
||||
charged_raw_wh = self._charged_raw_wh_per_slot[hour]
|
||||
# Scale the power cap [W] to a per-slot energy cap [Wh] (W x slot hours).
|
||||
# At slot_duration_h=1.0 (hourly) this equals the legacy power value.
|
||||
# Slot 0 only has its remaining part when the run starts within it.
|
||||
scale = self.first_slot_fraction if hour == 0 else 1.0
|
||||
max_charge_per_slot_wh_fast = max(
|
||||
min(
|
||||
self.max_charge_power_w * self.slot_duration_h,
|
||||
self.max_charge_power_w * self.slot_duration_h * scale,
|
||||
self._charge_limit_raw_wh_per_slot[hour],
|
||||
)
|
||||
- charged_raw_wh,
|
||||
@@ -824,7 +858,7 @@ class Battery:
|
||||
index = int(soc_wh_fast * curves.soc_index_per_wh + 0.5)
|
||||
soc_limit_raw_wh = (
|
||||
limits[index] if 0 <= index <= 100 else _table_entry(limits, index)
|
||||
) - charged_raw_wh
|
||||
) * scale - charged_raw_wh
|
||||
if soc_limit_raw_wh < 0.0:
|
||||
soc_limit_raw_wh = 0.0
|
||||
efficiencies = curves.charging_efficiency_by_power
|
||||
@@ -840,7 +874,9 @@ class Battery:
|
||||
intended_raw_wh = soc_limit_raw_wh
|
||||
if intended_raw_wh < 0.0:
|
||||
intended_raw_wh = 0.0
|
||||
index = int((charged_raw_wh + intended_raw_wh) * curves.charge_index_per_wh + 0.5)
|
||||
index = int(
|
||||
(charged_raw_wh + intended_raw_wh) / scale * curves.charge_index_per_wh + 0.5
|
||||
)
|
||||
charging_efficiency_fast = efficiencies[index] if index <= 100 else efficiencies[-1]
|
||||
|
||||
# Decide mode & determine raw_request_wh and raw_charge_wh
|
||||
|
||||
@@ -137,12 +137,26 @@ class Inverter:
|
||||
parameters: InverterParameters,
|
||||
battery: Optional[Battery] = None,
|
||||
slot_duration_h: float = 1.0,
|
||||
first_slot_fraction: float = 1.0,
|
||||
):
|
||||
# `first_slot_fraction` is the part of slot 0 still ahead when the
|
||||
# optimization runs within that slot; slot 0 then moves only that part
|
||||
# of the slot energy (see Battery).
|
||||
if not 0.0 < first_slot_fraction <= 1.0:
|
||||
raise ValueError(f"first_slot_fraction must be in (0, 1], got {first_slot_fraction}.")
|
||||
self.parameters: InverterParameters = parameters
|
||||
self.battery: Optional[Battery] = battery
|
||||
self.slot_duration_h: float = slot_duration_h
|
||||
self.first_slot_fraction: float = first_slot_fraction
|
||||
self._setup()
|
||||
|
||||
def slot_scale(self, hour: int) -> float:
|
||||
"""Return the part of the slot duration that is simulated for slot ``hour``.
|
||||
|
||||
``first_slot_fraction`` for slot 0, 1.0 for every later slot.
|
||||
"""
|
||||
return self.first_slot_fraction if hour == 0 else 1.0
|
||||
|
||||
def _setup(self) -> None:
|
||||
if self.battery and self.parameters.battery_id != self.battery.parameters.device_id:
|
||||
error_msg = f"Battery ID mismatch - {self.parameters.battery_id} is configured; got {self.battery.parameters.device_id}."
|
||||
@@ -167,19 +181,24 @@ class Inverter:
|
||||
self.max_ac_charge_power_w = self.parameters.max_ac_charge_power_w
|
||||
self.ac_charge_limits_total_charge = self.parameters.ac_charge_limits_total_charge
|
||||
|
||||
def dc_to_ac_efficiency_at(self, ac_wh: float) -> float:
|
||||
def dc_to_ac_efficiency_at(self, ac_wh: float, scale: float = 1.0) -> float:
|
||||
"""Return the DC-to-AC efficiency of a conversion delivering ``ac_wh`` in one slot.
|
||||
|
||||
Without ``dc_to_ac_efficiency_curve`` this is the constant
|
||||
``dc_to_ac_efficiency``. With a curve, the efficiency is interpolated at
|
||||
the load fraction ``ac_wh / max_power_wh`` of this slot. ``max_power_wh``
|
||||
is already scaled to the slot duration, so the fraction equals the mean
|
||||
AC power relative to the rated power.
|
||||
the load fraction ``ac_wh / (max_power_wh * scale)`` of this slot.
|
||||
``max_power_wh`` is already scaled to the slot duration, so the fraction
|
||||
equals the mean AC power relative to the rated power.
|
||||
|
||||
Args:
|
||||
ac_wh: AC energy of the conversion in this slot [Wh].
|
||||
scale: Simulated part of the slot duration (see ``slot_scale``).
|
||||
"""
|
||||
if self.dc_to_ac_efficiency_curve is None:
|
||||
return self.dc_to_ac_efficiency
|
||||
curve: list[tuple[float, float]] = self.dc_to_ac_efficiency_curve
|
||||
load_fraction = ac_wh / self.max_power_wh if self.max_power_wh > 0 else 0.0
|
||||
slot_max_power_wh = self.max_power_wh * scale
|
||||
load_fraction = ac_wh / slot_max_power_wh if slot_max_power_wh > 0 else 0.0
|
||||
# This runs for every battery discharge of every candidate plan. Find
|
||||
# the curve segment by bisection on the load fractions, split off once
|
||||
# per curve, instead of walking the points. The segment and the
|
||||
@@ -234,7 +253,11 @@ class Inverter:
|
||||
"""
|
||||
if self.ac_charge_limits_total_charge and self.battery and factor > 0.0:
|
||||
self.battery.limit_slot_charge(
|
||||
hour, self.battery.max_charge_power_w * self.slot_duration_h * factor
|
||||
hour,
|
||||
self.battery.max_charge_power_w
|
||||
* self.slot_duration_h
|
||||
* self.slot_scale(hour)
|
||||
* factor,
|
||||
)
|
||||
|
||||
def charge_battery_from_grid(self, hour: int, factor: float) -> tuple[float, float]:
|
||||
@@ -250,9 +273,10 @@ class Inverter:
|
||||
"""
|
||||
if not self.battery or factor <= 0.0:
|
||||
return 0.0, 0.0
|
||||
scale = self.slot_scale(hour)
|
||||
if self.ac_charge_limits_total_charge:
|
||||
stored, battery_losses = self.battery.charge_energy(
|
||||
self.battery.max_charge_power_w * self.slot_duration_h * factor, hour
|
||||
self.battery.max_charge_power_w * self.slot_duration_h * scale * factor, hour
|
||||
)
|
||||
else:
|
||||
stored, battery_losses = self.battery.charge_energy(None, hour, charge_factor=factor)
|
||||
@@ -262,7 +286,7 @@ class Inverter:
|
||||
# curve the efficiency is the one at the AC load actually drawn.
|
||||
curve = self.ac_to_dc_efficiency_curve
|
||||
if curve is not None:
|
||||
ac_energy = solve_ac_for_charge_dc_energy(curve, dc_energy, self.max_power_wh)
|
||||
ac_energy = solve_ac_for_charge_dc_energy(curve, dc_energy, self.max_power_wh * scale)
|
||||
else:
|
||||
ac_energy = dc_energy / self.ac_to_dc_efficiency
|
||||
return ac_energy, battery_losses + (ac_energy - dc_energy)
|
||||
@@ -273,18 +297,20 @@ class Inverter:
|
||||
dc_request: float,
|
||||
requested_ac_wh: float,
|
||||
dc_to_ac_efficiency: float,
|
||||
scale: float = 1.0,
|
||||
) -> float:
|
||||
"""Return the AC energy of a discharge that delivered ``battery_discharge_dc``.
|
||||
|
||||
If the battery delivered less than requested (low state of charge or its
|
||||
power limit), the conversion runs at a lower AC load than requested. With
|
||||
an efficiency curve the efficiency must then be the one at the AC energy
|
||||
that is actually delivered, not at the request.
|
||||
that is actually delivered, not at the request. ``scale`` is the
|
||||
simulated part of the slot duration (see ``slot_scale``).
|
||||
"""
|
||||
curve = self.dc_to_ac_efficiency_curve
|
||||
if curve is not None and battery_discharge_dc < dc_request * (1.0 - 1e-12):
|
||||
return solve_ac_for_dc_energy(
|
||||
curve, battery_discharge_dc, self.max_power_wh, requested_ac_wh
|
||||
curve, battery_discharge_dc, self.max_power_wh * scale, requested_ac_wh
|
||||
)
|
||||
return battery_discharge_dc * dc_to_ac_efficiency
|
||||
|
||||
@@ -293,15 +319,16 @@ class Inverter:
|
||||
battery = self.battery
|
||||
if not battery or requested_ac_wh <= 0.0:
|
||||
return 0.0, 0.0
|
||||
scale = self.first_slot_fraction if hour == 0 else 1.0
|
||||
if type(battery) is not Battery:
|
||||
# Another battery implementation: ask it, step by step.
|
||||
if not battery.discharge_released(hour):
|
||||
return 0.0, 0.0
|
||||
dc_to_ac_efficiency = self.dc_to_ac_efficiency_at(requested_ac_wh)
|
||||
dc_to_ac_efficiency = self.dc_to_ac_efficiency_at(requested_ac_wh, scale)
|
||||
dc_request = requested_ac_wh / dc_to_ac_efficiency
|
||||
battery_discharge_dc, discharge_losses = battery.discharge_energy(dc_request, hour)
|
||||
battery_discharge_ac = self._delivered_ac_wh(
|
||||
battery_discharge_dc, dc_request, requested_ac_wh, dc_to_ac_efficiency
|
||||
battery_discharge_dc, dc_request, requested_ac_wh, dc_to_ac_efficiency, scale
|
||||
)
|
||||
inverter_discharge_losses = battery_discharge_dc - battery_discharge_ac
|
||||
return battery_discharge_ac, discharge_losses + inverter_discharge_losses
|
||||
@@ -319,7 +346,7 @@ class Inverter:
|
||||
if self.dc_to_ac_efficiency_curve is None:
|
||||
dc_to_ac_efficiency = self.dc_to_ac_efficiency
|
||||
else:
|
||||
dc_to_ac_efficiency = self.dc_to_ac_efficiency_at(requested_ac_wh)
|
||||
dc_to_ac_efficiency = self.dc_to_ac_efficiency_at(requested_ac_wh, scale)
|
||||
dc_request = requested_ac_wh / dc_to_ac_efficiency
|
||||
|
||||
if battery._curves is not None:
|
||||
@@ -335,7 +362,10 @@ class Inverter:
|
||||
# Match Battery.discharge_energy(): preserve a SoC below the reserve.
|
||||
return 0.0, 0.0
|
||||
max_raw_wh = max(
|
||||
battery.max_discharge_power_w * battery.slot_duration_h - discharged_raw_wh[hour],
|
||||
battery.max_discharge_power_w
|
||||
* battery.slot_duration_h
|
||||
* (battery.first_slot_fraction if hour == 0 else 1.0)
|
||||
- discharged_raw_wh[hour],
|
||||
0.0,
|
||||
)
|
||||
max_deliverable_wh = min(raw_available_wh, max_raw_wh) * discharging_efficiency
|
||||
@@ -354,7 +384,7 @@ class Inverter:
|
||||
battery_discharge_ac = solve_ac_for_dc_energy(
|
||||
self.dc_to_ac_efficiency_curve,
|
||||
battery_discharge_dc,
|
||||
self.max_power_wh,
|
||||
self.max_power_wh * scale,
|
||||
requested_ac_wh,
|
||||
)
|
||||
else:
|
||||
@@ -402,12 +432,16 @@ class Inverter:
|
||||
if consumption < 0.0:
|
||||
consumption = 0.0
|
||||
battery = self.battery
|
||||
max_power_wh = self.max_power_wh
|
||||
# Slot 0 only has its remaining part when the run starts within it:
|
||||
# the inverter moves that part of the slot energy, and the slot's
|
||||
# energies are converted to power over that part of the slot.
|
||||
scale = self.first_slot_fraction if hour == 0 else 1.0
|
||||
max_power_wh = self.max_power_wh * scale
|
||||
|
||||
# Convert interval energy [Wh] to mean power [W] for the probability
|
||||
# lookup, then convert its expected direct power back to slot energy.
|
||||
if generation > 0.0 and consumption > 0.0:
|
||||
slot_duration_h = self.slot_duration_h
|
||||
slot_duration_h = self.slot_duration_h * scale
|
||||
expected_direct_power_w = (
|
||||
self.self_consumption_predictor.calculate_expected_direct_consumption(
|
||||
consumption / slot_duration_h,
|
||||
@@ -504,14 +538,14 @@ class Inverter:
|
||||
# limited by the energy the battery can actually deliver.
|
||||
remaining_battery_dc = battery.remaining_discharge_energy_wh(hour)
|
||||
remaining_battery_ac = remaining_battery_dc * self.dc_to_ac_efficiency_at(
|
||||
remaining_battery_dc
|
||||
remaining_battery_dc, scale
|
||||
)
|
||||
# The rate caps the export against the battery's *rated* discharge
|
||||
# power, so it stays a plain power setpoint ("export at 50 %") that
|
||||
# does not silently grow when self-consumption used less of the slot.
|
||||
# At factor 1.0 this bound never binds; behaviour is unchanged.
|
||||
rated_export_dc = battery.rated_discharge_energy_wh() * export_factor
|
||||
rated_export_ac = rated_export_dc * self.dc_to_ac_efficiency_at(rated_export_dc)
|
||||
rated_export_dc = battery.rated_discharge_energy_wh(hour) * export_factor
|
||||
rated_export_ac = rated_export_dc * self.dc_to_ac_efficiency_at(rated_export_dc, scale)
|
||||
export_capacity = remaining_inverter_ac_capacity
|
||||
if remaining_battery_ac < export_capacity:
|
||||
export_capacity = remaining_battery_ac
|
||||
|
||||
@@ -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