Files
EOS/src/akkudoktoreos/prediction/elecpricefixed.py
T
Bobby NoelteandGitHub e23bb7b497
Bump Version / Bump Version Workflow (push) Has been cancelled
CodeQL Advanced / Analyze (actions) (push) Has been cancelled
CodeQL Advanced / Analyze (python) (push) Has been cancelled
docker-build / platform-excludes (push) Has been cancelled
docker-build / build (push) Has been cancelled
docker-build / merge (push) Has been cancelled
pre-commit / pre-commit (push) Has been cancelled
Run Pytest on Pull Request / test (push) Has been cancelled
chore: prepare for update of genetic algorithm (#1190)
Andreas will update the genetic algorithm for 15-minutes optimization
intervals.

Copy the current GENETIC optimization algorithm to GENETIC0 to enable
to keep the algorithm with the current functionality. Also copy resources
like the load interpolator to the GENETIC0 algorithm to keep them despite
possible later changes to the interpolator.

Make the deprecated legacy /optimize endpoint use the GENETIC0 optimization
algorithm to in-fact behave the same way even if there will later be changes
to the GENETIC algorithm by Andreas. Add a new REST endpoint to provide
the unprocessed optimisation results of the GENETIC and GENETIC0 algorithm
in case one wants to use them as done with the deprecated /optimize endpoint.

Adapt the optimization configuration to have distinct configurations for the
GENETIC and the GENETIC0 algorithm.

Create a copy of the current tests for the GENETIC algorithm to be used
for the GENETIC0 algorithm. This avoids the tests for the GENETIC0
algorithm to be influenced by later changes by Andreas.

Signed-off-by: Bobby Noelte <b0661n0e17e@gmail.com>
2026-07-29 12:56:08 +02:00

112 lines
4.1 KiB
Python

"""Provides fixed price electricity price data."""
from typing import Optional
from loguru import logger
from pydantic import Field
from akkudoktoreos.config.configabc import (
SettingsBaseModel,
ValueTimeWindowSequence,
)
from akkudoktoreos.prediction.elecpriceabc import ElecPriceProvider
from akkudoktoreos.utils.datetimeutil import to_duration
class ElecPriceFixedCommonSettings(SettingsBaseModel):
"""Common configuration settings for fixed electricity pricing.
This model defines a fixed electricity price schedule using a sequence
of time windows. Each window specifies a time interval and the electricity
price applicable during that interval.
"""
time_windows: ValueTimeWindowSequence = Field(
default_factory=ValueTimeWindowSequence,
json_schema_extra={
"description": (
"Sequence of time windows defining the fixed "
"price schedule. If not provided, no fixed pricing is applied."
),
"examples": [
{
"windows": [
{"start_time": "00:00", "duration": "8 hours", "value": 0.288},
{"start_time": "08:00", "duration": "16 hours", "value": 0.34},
],
}
],
},
)
class ElecPriceFixed(ElecPriceProvider):
"""Fixed price electricity price data.
ElecPriceFixed is a singleton-based class that retrieves electricity price data
from a fixed schedule defined by time windows.
The provider generates hourly electricity prices based on the configured time windows.
For each hour in the forecast period, it determines which time window applies and
assigns the corresponding price.
Attributes:
time_windows: Sequence of time windows with associated electricity prices.
"""
@classmethod
def provider_id(cls) -> str:
"""Return the unique identifier for the ElecPriceFixed provider."""
return "ElecPriceFixed"
async def _update_data(self, force_update: Optional[bool] = False) -> None:
"""Update electricity price data from fixed schedule.
Generates electricity prices based on the configured time windows
at the optimization interval granularity. The price sequence starts
synchronized to the wall clock at the next full interval boundary.
Args:
force_update: If True, forces update even if data exists.
Raises:
ValueError: If no time windows are configured.
"""
time_windows_seq = self.config.elecprice.elecpricefixed.time_windows
if time_windows_seq is None or not time_windows_seq.windows:
error_msg = "No time windows configured for fixed electricity price"
logger.error(error_msg)
raise ValueError(error_msg)
start_datetime = self.ems_start_datetime
interval_seconds = 900 # Usual smallest time interval (15 min) used in electricty prices
total_hours = self.config.prediction.hours
interval = to_duration(interval_seconds)
end_datetime = start_datetime.add(hours=total_hours)
logger.debug(
f"Generating fixed electricity prices for {total_hours} hours "
f"starting at {start_datetime}"
)
# Build the full price array in one call — kWh values aligned to the
# optimization grid. to_array mirrors the key_to_array signature so
# the grid is constructed identically to how prediction data is read.
prices_kwh = time_windows_seq.to_array(
start_datetime=start_datetime,
end_datetime=end_datetime,
interval=interval,
dropna=True,
boundary="context",
align_to_interval=True,
)
# Convert kWh → Wh and store one entry per interval step.
for idx, price_kwh in enumerate(prices_kwh):
current_dt = start_datetime.add(seconds=idx * interval_seconds)
await self.update_value(current_dt, "elecprice_marketprice_wh", price_kwh / 1000.0)
logger.debug(f"Successfully generated {len(prices_kwh)} fixed electricity price entries")