mirror of
https://github.com/Akkudoktor-EOS/EOS.git
synced 2026-11-25 23:16:18 +01:00
fix(devices): constrain the physics port and validate export levels
Defer inactive EV deadline fields to the optimizer port, reject nonfinite export rates, and document the hourly Optimize boundary. Verify converter IDs, rates and LCOS, separate GENETIC0 interpolation, physical boundary flows and independent GENETIC repricing.
This commit is contained in:
@@ -1,11 +1,10 @@
|
||||
from typing import Any, Iterator, Optional
|
||||
|
||||
import numpy as np
|
||||
from pydantic import Field, field_validator
|
||||
from pydantic import Field
|
||||
|
||||
from akkudoktoreos.devices.settings.batterysettings import BATTERY_DEFAULT_CHARGE_RATES
|
||||
from akkudoktoreos.optimization.genetic.geneticdevices import DeviceParameters
|
||||
from akkudoktoreos.utils.datetimeutil import DateTime, to_datetime
|
||||
|
||||
|
||||
def max_charging_power_field(description: Optional[str] = None) -> float:
|
||||
@@ -86,9 +85,9 @@ class BaseBatteryParameters(DeviceParameters):
|
||||
json_schema_extra={
|
||||
"description": (
|
||||
"Battery-to-grid export rates as factor of maximum discharge "
|
||||
"power ]0.00 ... 1.00]. Only used with direct marketing. None "
|
||||
"falls back to the configured devices.batteries[0]."
|
||||
"grid_export_rates."
|
||||
"power ]0.00 ... 1.00]. These levels are available to algorithms "
|
||||
"that explicitly enable battery-to-grid export. None leaves the "
|
||||
"choice of export levels to the caller."
|
||||
),
|
||||
"examples": [[0.25, 0.5, 0.75, 1.0], [1.0], None],
|
||||
},
|
||||
@@ -113,13 +112,7 @@ class SolarPanelBatteryParameters(BaseBatteryParameters):
|
||||
|
||||
|
||||
class ElectricVehicleParameters(BaseBatteryParameters):
|
||||
"""Battery Electric Vehicle Device Simulation Configuration.
|
||||
|
||||
``min_soc_percentage`` is the charging target. By default it only has to be
|
||||
reached by the end of the optimization horizon; a deadline
|
||||
(``min_soc_deadline_datetime`` and/or ``min_soc_max_duration_h``) moves that
|
||||
requirement forward, for example to the next departure.
|
||||
"""
|
||||
"""Battery Electric Vehicle Device Simulation Configuration."""
|
||||
|
||||
device_id: str = Field(
|
||||
json_schema_extra={"description": "ID of electric vehicle", "examples": ["ev1"]}
|
||||
@@ -128,37 +121,6 @@ class ElectricVehicleParameters(BaseBatteryParameters):
|
||||
initial_soc_percentage: int = initial_soc_percentage_field(
|
||||
"An integer representing the current state of charge (SOC) of the battery in percentage."
|
||||
)
|
||||
min_soc_deadline_datetime: Optional[DateTime] = Field(
|
||||
default=None,
|
||||
json_schema_extra={
|
||||
"description": (
|
||||
"Absolute moment by which 'min_soc_percentage' has to be "
|
||||
"reached (departure time). A date time without timezone is read "
|
||||
"as local time. None means end of the optimization horizon."
|
||||
),
|
||||
"examples": [None, "2026-07-16T07:00:00+02:00"],
|
||||
},
|
||||
)
|
||||
min_soc_max_duration_h: Optional[float] = Field(
|
||||
default=None,
|
||||
gt=0,
|
||||
json_schema_extra={
|
||||
"description": (
|
||||
"Maximum time from the start of the optimization until "
|
||||
"'min_soc_percentage' has to be reached [h]. Combined with "
|
||||
"'min_soc_deadline_datetime' the earlier of the two applies."
|
||||
),
|
||||
"examples": [None, 6.0],
|
||||
},
|
||||
)
|
||||
|
||||
@field_validator("min_soc_deadline_datetime", mode="before")
|
||||
@classmethod
|
||||
def transform_deadline_to_datetime(cls, value: Any) -> Optional[DateTime]:
|
||||
"""Accept the usual date time representations, naive input is local time."""
|
||||
if value is None:
|
||||
return None
|
||||
return to_datetime(value)
|
||||
|
||||
|
||||
class Battery:
|
||||
|
||||
@@ -128,19 +128,18 @@ class BatteriesCommonSettings(DevicesBaseSettings):
|
||||
json_schema_extra={
|
||||
"description": (
|
||||
"Battery-to-grid export rates as factor of maximum discharge "
|
||||
"power ]0.00 ... 1.00]. Only used with direct marketing "
|
||||
"(feedintariff.direct_marketing_enabled). Each rate is one "
|
||||
"additional optimizer state; [1.0] restores all-or-nothing "
|
||||
"export. None triggers fallback to default export-rates."
|
||||
"power ]0.00 ... 1.00]. Available to algorithms that explicitly "
|
||||
"enable battery-to-grid export; configuring rates alone does not "
|
||||
"enable export. [1.0] selects full-power export. None uses the "
|
||||
"default export rates."
|
||||
),
|
||||
"examples": [[0.25, 0.5, 0.75, 1.0], [1.0], None],
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@field_validator("grid_export_rates", mode="before")
|
||||
def validate_and_sort_grid_export_rates(cls, v: Any) -> NDArray[Shape["*"], float]:
|
||||
"""Normalize the export rates to a sorted, duplicate-free array in ]0, 1]."""
|
||||
def validate_and_sort_grid_export_rates(cls, v: Any) -> list[float]:
|
||||
"""Normalize export rates to a finite, sorted, duplicate-free list in ]0, 1]."""
|
||||
# None means fallback to default values
|
||||
if v is None:
|
||||
return BATTERY_DEFAULT_GRID_EXPORT_RATES.copy()
|
||||
@@ -151,8 +150,10 @@ class BatteriesCommonSettings(DevicesBaseSettings):
|
||||
else:
|
||||
arr = np.array(v, dtype=float)
|
||||
|
||||
if arr.size == 0:
|
||||
raise ValueError("grid_export_rates must contain at least one value.")
|
||||
if arr.ndim != 1 or arr.size == 0:
|
||||
raise ValueError("grid_export_rates must be a nonempty one-dimensional list.")
|
||||
if not np.isfinite(arr).all():
|
||||
raise ValueError("grid_export_rates must contain finite values.")
|
||||
|
||||
# A rate of 0.0 is not an export level - "no export" is expressed by the
|
||||
# other battery states - so the lower bound is exclusive.
|
||||
@@ -162,8 +163,7 @@ class BatteriesCommonSettings(DevicesBaseSettings):
|
||||
arr = np.unique(arr)
|
||||
arr.sort()
|
||||
|
||||
return arr
|
||||
|
||||
return arr.tolist()
|
||||
|
||||
def to_genetic_pv_bat_param(self) -> "SolarPanelBatteryParameters":
|
||||
"""Return SolarPanelBatteryParameters for the GENETIC optimizer."""
|
||||
|
||||
Reference in New Issue
Block a user