feat: integrate device and runtime configuration foundation on main (#1328)

* feat: adapt configuration for multi optimization algorithms

Decouple configuration from optimization algorithm parameters. Add to_[algorithm]_param() methods
to the configuration that derive optimization algorithm specific parameters from the configuration.
Add x-scope tags to the configuration options that describe for which specific algorithms the
configuration option is for.

The whole device settings are restructured. There are now general settings for the device classes
with the afore mentioned to_[algorithm]_param() methods. The general device settings got their own
directory `devices/settings`. By this the parameter class also does not have to be a pydantic model
which can be used for future optimization/ simulations speed up.

Also the parameter class for a device is now part of the device module. This better decouples and
also is the natural place for parameters of a device.

Besides this feature there are also fixes and improvements:

* feat: extend home appliance time window settings and simulation

  Home appliance can now be configured for multiple runs with per-cycle allowed time windows. The
  number of remaining cycles to plan is determined at runtime by reading the
  ``cycles_completed_measurement_key`` from the measurement store.

* feat: specialiced CycleTimeWindowSequence for time window sequences

  Sequence of time windows associated to cycles.

  This model specializes ``ValueTimeWindowSequence`` so that the ``value``
  field of each ``ValueTimeWindow`` encodes the **cycle index** (0-based
  integer) the window belongs to.

  Typical use: an appliance that must run ``n`` times per day, each run
  constrained to a distinct time window.  Assign ``value=0`` to windows
  for the first cycle, ``value=1`` for the second, and so on.  Multiple
  windows may share the same cycle index (their allowed regions are unioned).
  Windows with ``value=None`` are silently ignored by all cycle-aware methods.

* fix: Make test_configmigrate also regard the _ANY_SENTENIEL in key values

* chore: Make devices configurations a map instead of a list

  This makes config paths stable regardless of declaration order and lets each device settings
  class build its own config path from ``self.device_id`` without needing an external index.
  Tests are adapted likewise.

  Devices configurations are automatically migrated from lists to maps.

* chore: rename levelized_cost_of_storage_kwh to levelized_cost_of_storage_amt kwh

  This better fits in the naming scheme and also makes clear the costs are money.

Signed-off-by: Bobby Noelte <b0661n0e17e@gmail.com>

* fix: runtime config update ignored by config file

Runtime settings were handed back to pydantic-settings as init settings,
which rank below the config file and the environment. Any key already
present in EOS.config.json or in the environment silently discarded the
update, so a bulk PUT /v1/config returned 200 without applying anything,
while the granular PUT /v1/config/{path} endpoint kept working.

Add a dedicated runtime settings source ranked directly below the command
line arguments and record granular updates there as well, so both
endpoints share one store that survives re-evaluation of the settings
sources. Environment variables keep precedence over the config file for
all keys that were not set at runtime.

Also repairs revert_settings() and update(), which passed their data
through the same init settings.

Closes #1303

* fix: env vars ignored on first config build

ConfigEOS.__init__ passed self as first positional argument to _setup,
which forwards it to pydantic_settings.BaseSettings.__init__. Its first
positional parameter is _case_sensitive, so the environment source
matched the upper case variable names against the lower case field names
and returned nothing. Environment settings only took effect after the
next configuration setup.

* docs: changelog for config priority fixes

* fix(config): preserve device identities and storage costs during migration

* fix(devices): preserve charge-rate typing and public import compatibility

* ruff format fix

* fix(config): satisfy typed device conversion and migration contracts

* docs(config): refresh validated configuration prerequisite schemas

---------

Signed-off-by: Bobby Noelte <b0661n0e17e@gmail.com>
Co-authored-by: Bobby Noelte <b0661n0e17e@gmail.com>
Co-authored-by: r0b2g1t <r0b2g1t@users.noreply.github.com>
This commit is contained in:
Andreas
2026-09-17 17:51:40 +02:00
committed by GitHub
co-authored by Bobby Noelte r0b2g1t
parent 431d7d57e5
commit 8224c64654
55 changed files with 5214 additions and 2170 deletions
@@ -1,100 +1,625 @@
import numpy as np
"""Simulation of a home appliance that runs one or more fixed-duration cycles.
from akkudoktoreos.config.configabc import TimeWindow, TimeWindowSequence
from akkudoktoreos.optimization.genetic.geneticdevices import HomeApplianceParameters
from akkudoktoreos.utils.datetimeutil import to_datetime, to_duration, to_time
This module models household devices such as dishwashers or washing
machines that must run for a fixed duration, possibly multiple times
(cycles), within one or more allowed time windows. Given a set of
requested start times, `HomeAppliance` repairs them into a
feasible, chronologically ordered schedule and produces the resulting
hourly load curve.
Time windows are always expressed as a `CycleTimeWindowSequence`
(see ``akkudoktoreos.config.configabc``): each contained window's
``value`` encodes the 0-based cycle index it applies to, so different
cycles of the same appliance can be constrained to different windows.
When no windows are configured, every cycle defaults to a single
window spanning the full prediction horizon (i.e. unconstrained).
Cycle start times are repaired according to the following rules:
1. Round and clip the requested start to the simulation horizon.
2. Snap each cycle's start to the nearest start allowed by that
cycle's own time window.
3. Sort all cycle starts chronologically, keeping each start paired
with the cycle (and therefore the allowed-start mask) it belongs
to.
4. Walk the sorted starts and push any cycle that starts too soon
after its predecessor to the next start allowed by its own
window, enforcing the appliance duration plus the configured
minimum idle gap.
5. Generate the combined hourly load curve from the final starts.
"""
from typing import Optional
import numpy as np
from pydantic import Field
from akkudoktoreos.config.configabc import CycleTimeWindowSequence, ValueTimeWindow
from akkudoktoreos.optimization.genetic.geneticdevices import DeviceParameters
from akkudoktoreos.utils.datetimeutil import (
DateTime,
Duration,
to_datetime,
to_duration,
to_time,
)
class HomeApplianceParameters(DeviceParameters):
"""Configuration for a simulated home appliance device."""
device_id: str = Field(
json_schema_extra={
"description": "ID of home appliance",
"examples": ["dishwasher"],
}
)
consumption_wh: int = Field(
gt=0,
json_schema_extra={
"description": (
"An integer representing the energy consumption "
"of a household device in watt-hours."
),
"examples": [2000],
},
)
duration_h: int = Field(
gt=0,
json_schema_extra={
"description": (
"An integer representing the usage duration of a household device in hours."
),
"examples": [3],
},
)
num_cycles: int = Field(
default=1,
gt=0,
json_schema_extra={
"description": "Number of cycles the appliance must run.",
"examples": [2],
},
)
min_cycle_gap_h: int = Field(
default=0,
ge=0,
json_schema_extra={
"description": (
"Minimum idle time between the end of one cycle and the start of the next cycle."
),
"examples": [1],
},
)
time_windows: Optional[CycleTimeWindowSequence] = Field(
default=None,
json_schema_extra={
"description": (
"Allowed per-cycle time windows. Each window's `value` "
"encodes the 0-based cycle index it applies to; multiple "
"windows may share a cycle index. When omitted, every "
"cycle is unconstrained across the full prediction "
"horizon."
),
"examples": [
[
{
"start_time": "10:00",
"duration": "3 hours",
"value": 0,
},
],
],
},
)
class HomeAppliance:
"""Non-vectorized simulation of a multi-cycle home appliance.
A home appliance may execute multiple fixed-duration cycles during
the simulation horizon. See the module docstring for the start-time
repair algorithm.
"""
def __init__(
self,
parameters: HomeApplianceParameters,
optimization_hours: int,
prediction_hours: int,
):
self.parameters: HomeApplianceParameters = parameters
) -> None:
"""Initializes the appliance and builds its allowed-start masks.
Args:
parameters: The appliance's configuration.
optimization_hours: Number of hours under active
optimization.
prediction_hours: Length of the simulation horizon, in
hours.
"""
self.parameters = parameters
self.optimization_hours = optimization_hours
self.prediction_hours = prediction_hours
self.duration_h = parameters.duration_h
self.consumption_wh = parameters.consumption_wh
self.num_cycles = parameters.num_cycles
self.min_cycle_gap_h = parameters.min_cycle_gap_h
self.completed_cycles = 0
self.load_curve = np.zeros(prediction_hours)
# Start times for remaining cycles, in chronological order.
self.start_hours: list[int] = []
# Absolute cycle index corresponding to each remaining cycle.
#
# Example:
# num_cycles = 4
# completed_cycles = 2
#
# remaining_cycle_indices = [2, 3]
self.remaining_cycle_indices: list[int] = []
# start_allowed[k][hour]
#
# k is the index into remaining_cycle_indices (NOT into the
# chronologically-sorted self.start_hours).
self.start_allowed: list[np.ndarray] = []
self.start_earliest: list[int] = []
self.start_latest: list[int] = []
self._setup()
# ------------------------------------------------------------------
# Setup
# ------------------------------------------------------------------
def _setup(self) -> None:
"""Sets up the home appliance parameters based provided parameters."""
self.load_curve = np.zeros(self.prediction_hours) # Initialize the load curve with zeros
self.duration_h = self.parameters.duration_h
self.consumption_wh = self.parameters.consumption_wh
# setup possible start times
"""Sets up appliance parameters and default time windows.
When no ``time_windows`` are configured, builds a single
placeholder window spanning the full prediction horizon with
``value`` left unset. ``CycleTimeWindowSequence.cycles_to_matrix``
ignores windows whose ``value`` is ``None``, so this window never
matches any cycle; every remaining cycle then falls through to
the "no window for this cycle" branch in
``_build_cycle_start_allowed``, which treats it as unconstrained.
The net effect is the same as having no windows at all, without
assigning cycles an explicit (and misleadingly meaningful)
``value``.
"""
if self.parameters.time_windows is None:
self.parameters.time_windows = TimeWindowSequence(
self.parameters.time_windows = CycleTimeWindowSequence(
windows=[
TimeWindow(
ValueTimeWindow(
start_time=to_time("00:00"),
duration=to_duration(f"{self.prediction_hours} hours"),
),
]
)
start_datetime = to_datetime().set(hour=0, minute=0, second=0)
duration = to_duration(f"{self.duration_h} hours")
self.start_allowed: list[bool] = []
for hour in range(0, self.prediction_hours):
self.start_allowed.append(
self.parameters.time_windows.contains(
start_datetime.add(hours=hour), duration=duration
)
)
start_earliest = self.parameters.time_windows.earliest_start_time(duration, start_datetime)
if start_earliest:
self.start_earliest = start_earliest.hour
else:
self.start_earliest = 0
start_latest = self.parameters.time_windows.latest_start_time(duration, start_datetime)
if start_latest:
self.start_latest = start_latest.hour
else:
self.start_latest = 23
def set_starting_time(self, start_hour: int, global_start_hour: int = 0) -> int:
"""Sets the start time of the device and generates the corresponding load curve.
self._build_start_allowed()
:param start_hour: The hour at which the device should start.
@property
def num_remaining_cycles(self) -> int:
"""int: Number of cycles which still have to be scheduled."""
return max(0, self.num_cycles - self.completed_cycles)
def set_completed_cycles(self, completed_cycles: int) -> None:
"""Sets the number of cycles already completed.
Clears any previously scheduled start times and load curve,
and rebuilds the allowed-start masks for the cycles that
remain.
Args:
completed_cycles: Number of cycles completed so far.
Clamped to ``[0, num_cycles]``.
"""
if not self.start_allowed[start_hour]:
# It is not allowed (by the time windows) to start the application at this time
if global_start_hour <= self.start_latest:
# There is a time window left to start the appliance. Use it
start_hour = self.start_latest
else:
# There is no time window left to run the application
# Set the start into tomorrow
start_hour = self.start_earliest + 24
self.completed_cycles = max(
0,
min(completed_cycles, self.num_cycles),
)
self.start_hours = []
self.reset_load_curve()
self._build_start_allowed()
# ------------------------------------------------------------------
# Time-window handling
# ------------------------------------------------------------------
def _build_start_allowed(self) -> None:
"""Builds allowed start positions for all remaining cycles."""
if self.parameters.time_windows is None:
raise ValueError("Expected time windows in parameters, got {self.parameters}.")
self.start_allowed = []
self.start_earliest = []
self.start_latest = []
self.remaining_cycle_indices = list(
range(
self.completed_cycles,
self.num_cycles,
)
)
if not self.remaining_cycle_indices:
return
start_datetime = to_datetime().set(
hour=0,
minute=0,
second=0,
microsecond=0,
)
end_datetime = start_datetime.add(
hours=self.prediction_hours,
)
interval = to_duration("1 hour")
self._build_cycle_start_allowed(
self.parameters.time_windows,
start_datetime,
end_datetime,
interval,
)
def _build_cycle_start_allowed(
self,
time_windows: CycleTimeWindowSequence,
start_datetime: DateTime,
end_datetime: DateTime,
interval: Duration,
) -> None:
"""Builds allowed starts from per-cycle windows.
Args:
time_windows: Windows associated with individual cycles.
start_datetime: Start of the simulation horizon.
end_datetime: End of the simulation horizon.
interval: Step size used to sample the cycle windows.
"""
cycle_indices, matrix = time_windows.cycles_to_matrix(
start_datetime=start_datetime,
end_datetime=end_datetime,
interval=interval,
)
cycle_to_row = {cycle: row for row, cycle in enumerate(cycle_indices)}
max_start = max(
0,
self.prediction_hours - self.duration_h,
)
# The matrix tells us which individual *steps* are inside
# the cycle window. We still have to check that the complete
# appliance duration fits inside the window.
for cycle in self.remaining_cycle_indices:
row_index = cycle_to_row.get(cycle)
if row_index is None:
# No window for this cycle -> unconstrained.
allowed = np.zeros(
self.prediction_hours,
dtype=bool,
)
allowed[: max_start + 1] = True
else:
allowed = self._build_duration_feasibility(
matrix[row_index],
)
self.start_allowed.append(allowed)
allowed_indices = np.flatnonzero(allowed)
if len(allowed_indices):
self.start_earliest.append(int(allowed_indices[0]))
self.start_latest.append(int(allowed_indices[-1]))
else:
self.start_earliest.append(0)
self.start_latest.append(max_start)
def _build_duration_feasibility(
self,
window_steps: np.ndarray,
) -> np.ndarray:
"""Returns starts where the complete appliance fits in a window.
A start ``s`` is feasible when every step in
``window_steps[s : s + duration_h]`` lies inside the cycle's
window (i.e. sums to ``duration_h``).
Args:
window_steps: Per-hour membership of the cycle's window
(1.0 inside the window, 0.0 outside), one value per
hour of the prediction horizon.
Returns:
Boolean mask, one entry per hour of the prediction
horizon, ``True`` where a cycle of length ``duration_h``
can start without leaving the window.
"""
horizon = len(window_steps)
max_start = max(
0,
horizon - self.duration_h,
)
allowed = np.zeros(
horizon,
dtype=bool,
)
if self.duration_h > horizon:
return allowed
# Rolling sum of `duration_h` consecutive steps, aligned so
# that window_sums[s] == sum(window_steps[s : s + duration_h]).
cumulative = np.concatenate(([0.0], np.cumsum(window_steps)))
window_sums = cumulative[self.duration_h :] - cumulative[: -self.duration_h]
allowed[: max_start + 1] = window_sums[: max_start + 1] == float(self.duration_h)
return allowed
# ------------------------------------------------------------------
# Scheduling / repair
# ------------------------------------------------------------------
def set_starting_times(
self,
start_hours: list[int],
) -> list[int]:
"""Sets and repairs the start times of all remaining cycles.
See the module docstring for the repair algorithm.
Args:
start_hours: Requested start hour for each remaining
cycle, in the same order as ``remaining_cycle_indices``
(i.e. matching ``self.start_allowed``).
Returns:
The repaired, chronologically ordered start hours.
Raises:
ValueError: If ``start_hours`` does not have exactly
``num_remaining_cycles`` entries.
"""
if len(start_hours) != self.num_remaining_cycles:
raise ValueError(
f"Expected {self.num_remaining_cycles} start times, got {len(start_hours)}."
)
if self.num_remaining_cycles == 0:
self.start_hours = []
self.reset_load_curve()
return []
max_start = max(
0,
self.prediction_hours - self.duration_h,
)
# 1. Round and clip.
starts = [
max(
0,
min(
int(round(start)),
max_start,
),
)
for start in start_hours
]
# 2. Snap each cycle to its nearest allowed start, keeping the
# start paired with the cycle (start_allowed index) it
# belongs to.
repaired = [
(
self._repair_start(
start,
cycle_index,
max_start,
),
cycle_index,
)
for cycle_index, start in enumerate(starts)
]
# 3. Sort by start time, keeping each cycle's own index
# attached so its allowed-start mask is still used
# correctly in step 4.
repaired.sort(key=lambda pair: pair[0])
# 4. Enforce duration + minimum idle gap. Each cycle is
# pushed forward, if needed, to the next start allowed by
# its *own* window.
min_next_start = self.duration_h + self.min_cycle_gap_h
final_starts = [repaired[0][0]]
for index in range(1, len(repaired)):
_, cycle_index = repaired[index]
earliest = final_starts[index - 1] + min_next_start
candidate = self._first_allowed_start_at_or_after(
cycle_index=cycle_index,
earliest=earliest,
)
if candidate is None:
# No valid start remains for this cycle.
final_starts.append(max_start)
else:
final_starts.append(candidate)
# 5. Reconstruct load curve from the final schedule.
self.start_hours = final_starts
self._build_load_curve()
return list(self.start_hours)
def _repair_start(
self,
start: int,
cycle_index: int,
max_start: int,
) -> int:
"""Snaps a start to the nearest allowed start.
Args:
start: Requested (already rounded and clipped) start
hour.
cycle_index: Index into ``self.start_allowed`` for the
cycle being repaired.
max_start: Latest hour at which any cycle may start
without exceeding the prediction horizon, used as a
fallback when the cycle has no allowed start at all.
Returns:
The nearest hour allowed for this cycle, or ``max_start``
if the cycle has no allowed start.
"""
allowed = self.start_allowed[cycle_index]
if not np.any(allowed):
# Same fallback as the vectorized implementation.
return max_start
if allowed[start]:
return start
allowed_indices = np.flatnonzero(allowed)
distances = np.abs(allowed_indices - start)
return int(allowed_indices[np.argmin(distances)])
def _first_allowed_start_at_or_after(
self,
cycle_index: int,
earliest: int,
) -> int | None:
"""Returns the first allowed start at or after ``earliest``.
Args:
cycle_index: Index into ``self.start_allowed`` for the
cycle being scheduled.
earliest: Earliest acceptable start hour.
Returns:
The first allowed hour ``>= earliest``, or ``None`` if no
such hour exists.
"""
allowed = self.start_allowed[cycle_index]
allowed_indices = np.flatnonzero(allowed)
if len(allowed_indices) == 0:
return None
position = np.searchsorted(
allowed_indices,
max(0, earliest),
side="left",
)
if position >= len(allowed_indices):
return None
return int(allowed_indices[position])
# ------------------------------------------------------------------
# Backwards-compatible single-cycle interface
# ------------------------------------------------------------------
def set_starting_time(
self,
start_hour: int,
global_start_hour: int = 0,
) -> int:
"""Sets the start time of the first remaining cycle.
Args:
start_hour: Requested start hour for the first remaining
cycle.
global_start_hour: Retained for API compatibility with
the old, single-cycle implementation. Unused.
Returns:
The repaired start hour of the first remaining cycle, or
``start_hour`` unchanged if there are no remaining
cycles.
"""
if self.num_remaining_cycles == 0:
self.reset_load_curve()
return start_hour
if self.start_hours:
starts = list(self.start_hours)
else:
starts = [self.start_earliest[index] for index in range(self.num_remaining_cycles)]
starts[0] = start_hour
repaired = self.set_starting_times(starts)
return repaired[0]
# ------------------------------------------------------------------
# Load curve
# ------------------------------------------------------------------
def _build_load_curve(self) -> None:
"""Builds the load curve from all scheduled cycles."""
self.reset_load_curve()
# Calculate power per hour based on total consumption and duration
power_per_hour = self.consumption_wh / self.duration_h # Convert to watt-hours
power_per_hour = self.consumption_wh / self.duration_h
# Set the power for the duration of use in the load curve array
if start_hour < len(self.load_curve):
end_hour = min(start_hour + self.duration_h, self.prediction_hours)
self.load_curve[start_hour:end_hour] = power_per_hour
for start_hour in self.start_hours:
if start_hour >= self.prediction_hours:
continue
return start_hour
end_hour = min(
start_hour + self.duration_h,
self.prediction_hours,
)
self.load_curve[start_hour:end_hour] += power_per_hour
def reset_load_curve(self) -> None:
"""Resets the load curve."""
"""Resets the load curve to all zeros."""
self.load_curve = np.zeros(self.prediction_hours)
def get_load_curve(self) -> np.ndarray:
"""Returns the current load curve."""
"""Returns the current hourly load curve, in watts."""
return self.load_curve
def get_load_for_hour(self, hour: int) -> float:
"""Returns the load for a specific hour.
:param hour: The hour for which the load is queried.
:return: The load in watts for the specified hour.
Args:
hour: Hour of the prediction horizon to look up.
Returns:
The load, in watts, at ``hour``.
Raises:
ValueError: If ``hour`` is outside
``[0, prediction_hours)``.
"""
if hour < 0 or hour >= self.prediction_hours:
raise ValueError(
f"The specified hour {hour} is outside the available time frame {self.prediction_hours}."
f"The specified hour {hour} is outside the available "
f"time frame {self.prediction_hours}."
)
return self.load_curve[hour]
return float(self.load_curve[hour])