mirror of
				https://github.com/Akkudoktor-EOS/EOS.git
				synced 2025-10-30 22:36:21 +00:00 
			
		
		
		
	fix: automatic optimization (#596)
This fix implements the long term goal to have the EOS server run optimization (or
energy management) on regular intervals automatically. Thus clients can request
the current energy management plan at any time and it is updated on regular
intervals without interaction by the client.
This fix started out to "only" make automatic optimization (or energy management)
runs working. It turned out there are several endpoints that in some way
update predictions or run the optimization. To lock against such concurrent attempts
the code had to be refactored to allow control of execution. During refactoring it
became clear that some classes and files are named without a proper reference
to their usage. Thus not only refactoring but also renaming became necessary.
The names are still not the best, but I hope they are more intuitive.
The fix includes several bug fixes that are not directly related to the automatic optimization
but are necessary to keep EOS running properly to do the automatic optimization and
to test and document the changes.
This is a breaking change as the configuration structure changed once again and
the server API was also enhanced and streamlined. The server API that is used by
Andreas and Jörg in their videos has not changed.
* fix: automatic optimization
  Allow optimization to automatically run on configured intervals gathering all
  optimization parameters from configuration and predictions. The automatic run
  can be configured to only run prediction updates skipping the optimization.
  Extend documentaion to also cover automatic optimization. Lock automatic runs
  against runs initiated by the /optimize or other endpoints. Provide new
  endpoints to retrieve the energy management plan and the genetic solution
  of the latest automatic optimization run. Offload energy management to thread
  pool executor to keep the app more responsive during the CPU heavy optimization
  run.
* fix: EOS servers recognize environment variables on startup
  Force initialisation of EOS configuration on server startup to assure
  all sources of EOS configuration are properly set up and read. Adapt
  server tests and configuration tests to also test for environment
  variable configuration.
* fix: Remove 0.0.0.0 to localhost translation under Windows
  EOS imposed a 0.0.0.0 to localhost translation under Windows for
  convenience. This caused some trouble in user configurations. Now, as the
  default IP address configuration is 127.0.0.1, the user is responsible
  for to set up the correct Windows compliant IP address.
* fix: allow names for hosts additional to IP addresses
* fix: access pydantic model fields by class
  Access by instance is deprecated.
* fix: down sampling key_to_array
* fix: make cache clear endpoint clear all cache files
  Make /v1/admin/cache/clear clear all cache files. Before it only cleared
  expired cache files by default. Add new endpoint /v1/admin/clear-expired
  to only clear expired cache files.
* fix: timezonefinder returns Europe/Paris instead of Europe/Berlin
  timezonefinder 8.10 got more inaccurate for timezones in europe as there is
  a common timezone. Use new package tzfpy instead which is still returning
  Europe/Berlin if you are in Germany. tzfpy also claims to be faster than
  timezonefinder.
* fix: provider settings configuration
  Provider configuration used to be a union holding the settings for several
  providers. Pydantic union handling does not always find the correct type
  for a provider setting. This led to exceptions in specific configurations.
  Now provider settings are explicit comfiguration items for each possible
  provider. This is a breaking change as the configuration structure was
  changed.
* fix: ClearOutside weather prediction irradiance calculation
  Pvlib needs a pandas time index. Convert time index.
* fix: test config file priority
  Do not use config_eos fixture as this fixture already creates a config file.
* fix: optimization sample request documentation
  Provide all data in documentation of optimization sample request.
* fix: gitlint blocking pip dependency resolution
  Replace gitlint by commitizen. Gitlint is not actively maintained anymore.
  Gitlint dependencies blocked pip from dependency resolution.
* fix: sync pre-commit config to actual dependency requirements
  .pre-commit-config.yaml was out of sync, also requirements-dev.txt.
* fix: missing babel in requirements.txt
  Add babel to requirements.txt
* feat: setup default device configuration for automatic optimization
  In case the parameters for automatic optimization are not fully defined a
  default configuration is setup to allow the automatic energy management
  run. The default configuration may help the user to correctly define
  the device configuration.
* feat: allow configuration of genetic algorithm parameters
  The genetic algorithm parameters for number of individuals, number of
  generations, the seed and penalty function parameters are now avaliable
  as configuration options.
* feat: allow configuration of home appliance time windows
  The time windows a home appliance is allowed to run are now configurable
  by the configuration (for /v1 API) and also by the home appliance parameters
  (for the classic /optimize API). If there is no such configuration the
  time window defaults to optimization hours, which was the standard before
  the change. Documentation on how to configure time windows is added.
* feat: standardize mesaurement keys for battery/ ev SoC measurements
  The standardized measurement keys to report battery SoC to the device
  simulations can now be retrieved from the device configuration as a
  read-only config option.
* feat: feed in tariff prediction
  Add feed in tarif predictions needed for automatic optimization. The feed in
  tariff can be retrieved as fixed feed in tarif or can be imported. Also add
  tests for the different feed in tariff providers. Extend documentation to
  cover the feed in tariff providers.
* feat: add energy management plan based on S2 standard instructions
  EOS can generate an energy management plan as a list of simple instructions.
  May be retrieved by the /v1/energy-management/plan endpoint. The instructions
  loosely follow the S2 energy management standard.
* feat: make measurement keys configurable by EOS configuration.
  The fixed measurement keys are replaced by configurable measurement keys.
* feat: make pendulum DateTime, Date, Duration types usable for pydantic models
  Use pydantic_extra_types.pendulum_dt to get pydantic pendulum types. Types are
  added to the datetimeutil utility. Remove custom made pendulum adaptations
  from EOS pydantic module. Make EOS modules use the pydantic pendulum types
  managed by the datetimeutil module instead of the core pendulum types.
* feat: Add Time, TimeWindow, TimeWindowSequence and to_time to datetimeutil.
  The time windows are are added to support home appliance time window
  configuration. All time classes are also pydantic models. Time is the base
  class for time definition derived from pendulum.Time.
* feat: Extend DataRecord by configurable field like data.
  Configurable field like data was added to support the configuration of
  measurement records.
* feat: Add additional information to health information
  Version information is added to the health endpoints of eos and eosDash.
  The start time of the last optimization and the latest run time of the energy
  management is added to the EOS health information.
* feat: add pydantic merge model tests
* feat: add plan tab to EOSdash
  The plan tab displays the current energy management instructions.
* feat: add predictions tab to EOSdash
  The predictions tab displays the current predictions.
* feat: add cache management to EOSdash admin tab
  The admin tab is extended by a section for cache management. It allows to
  clear the cache.
* feat: add about tab to EOSdash
  The about tab resembles the former hello tab and provides extra information.
* feat: Adapt changelog and prepare for release management
  Release management using commitizen is added. The changelog file is adapted and
  teh changelog and a description for release management is added in the
  documentation.
* feat(doc): Improve install and devlopment documentation
  Provide a more concise installation description in Readme.md and add extra
  installation page and development page to documentation.
* chore: Use memory cache for interpolation instead of dict in inverter
  Decorate calculate_self_consumption() with @cachemethod_until_update to cache
  results in memory during an energy management/ optimization run. Replacement
  of dict type caching in inverter is now possible because all optimization
  runs are properly locked and the memory cache CacheUntilUpdateStore is properly
  cleared at the start of any energy management/ optimization operation.
* chore: refactor genetic
  Refactor the genetic algorithm modules for enhanced module structure and better
  readability. Removed unnecessary and overcomplex devices singleton. Also
  split devices configuration from genetic algorithm parameters to allow further
  development independently from genetic algorithm parameter format. Move
  charge rates configuration for electric vehicles from optimization to devices
  configuration to allow to have different charge rates for different cars in
  the future.
* chore: Rename memory cache to CacheEnergyManagementStore
  The name better resembles the task of the cache to chache function and method
  results for an energy management run. Also the decorator functions are renamed
  accordingly: cachemethod_energy_management, cache_energy_management
* chore: use class properties for config/ems/prediction mixin classes
* chore: skip debug logs from mathplotlib
  Mathplotlib is very noisy in debug mode.
* chore: automatically sync bokeh js to bokeh python package
  bokeh was updated to 3.8.0, make JS CDN automatically follow the package version.
* chore: rename hello.py to about.py
  Make hello.py the adapted EOSdash about page.
* chore: remove demo page from EOSdash
  As no the plan and prediction pages are working without configuration, the demo
  page is no longer necessary
* chore: split test_server.py for system test
  Split test_server.py to create explicit test_system.py for system tests.
* chore: move doc utils to generate_config_md.py
  The doc utils are only used in scripts/generate_config_md.py. Move it there to
  attribute for strong cohesion.
* chore: improve pydantic merge model documentation
* chore: remove pendulum warning from readme
* chore: remove GitHub discussions from contributing documentation
  Github discussions is to be replaced by Akkudoktor.net.
* chore(release): bump version to 0.1.0+dev for development
* build(deps): bump fastapi[standard] from 0.115.14 to 0.117.1
  bump fastapi and make coverage version (for pytest-cov) explicit to avoid pip break.
* build(deps): bump uvicorn from 0.36.0 to 0.37.0
BREAKING CHANGE: EOS configuration changed. V1 API changed.
  - The available_charge_rates_percent configuration is removed from optimization.
    Use the new charge_rate configuration for the electric vehicle
  - Optimization configuration parameter hours renamed to horizon_hours
  - Device configuration now has to provide the number of devices and device
    properties per device.
  - Specific prediction provider configuration to be provided by explicit
    configuration item (no union for all providers).
  - Measurement keys to be provided as a list.
  - New feed in tariff providers have to be configured.
  - /v1/measurement/loadxxx endpoints are removed. Use generic mesaurement endpoints.
  - /v1/admin/cache/clear now clears all cache files. Use
    /v1/admin/cache/clear-expired to only clear all expired cache files.
Signed-off-by: Bobby Noelte <b0661n0e17e@gmail.com>
			
			
This commit is contained in:
		| @@ -1,49 +1,376 @@ | ||||
| from typing import Optional | ||||
| """General configuration settings for simulated devices for optimization.""" | ||||
|  | ||||
| from akkudoktoreos.core.coreabc import SingletonMixin | ||||
| from akkudoktoreos.devices.battery import Battery | ||||
| from akkudoktoreos.devices.devicesabc import DevicesBase | ||||
| from akkudoktoreos.devices.generic import HomeAppliance | ||||
| from akkudoktoreos.devices.inverter import Inverter | ||||
| from akkudoktoreos.devices.settings import DevicesCommonSettings | ||||
| import json | ||||
| from typing import Any, Optional, TextIO, cast | ||||
|  | ||||
| from loguru import logger | ||||
| from pydantic import Field, computed_field, model_validator | ||||
|  | ||||
| from akkudoktoreos.config.configabc import SettingsBaseModel | ||||
| from akkudoktoreos.core.cache import CacheFileStore | ||||
| from akkudoktoreos.core.coreabc import ConfigMixin, SingletonMixin | ||||
| from akkudoktoreos.core.emplan import ResourceStatus | ||||
| from akkudoktoreos.core.pydantic import ConfigDict, PydanticBaseModel | ||||
| from akkudoktoreos.devices.devicesabc import DevicesBaseSettings | ||||
| from akkudoktoreos.utils.datetimeutil import DateTime, TimeWindowSequence, to_datetime | ||||
|  | ||||
|  | ||||
| class Devices(SingletonMixin, DevicesBase): | ||||
|     def __init__(self, settings: Optional[DevicesCommonSettings] = None): | ||||
|         if hasattr(self, "_initialized"): | ||||
|             return | ||||
|         super().__init__() | ||||
|         if settings is None: | ||||
|             settings = self.config.devices | ||||
|             if settings is None: | ||||
|                 return | ||||
| class BatteriesCommonSettings(DevicesBaseSettings): | ||||
|     """Battery devices base settings.""" | ||||
|  | ||||
|         # initialize devices | ||||
|         if settings.batteries is not None: | ||||
|             for battery_params in settings.batteries: | ||||
|                 self.add_device(Battery(battery_params)) | ||||
|         if settings.inverters is not None: | ||||
|             for inverter_params in settings.inverters: | ||||
|                 self.add_device(Inverter(inverter_params)) | ||||
|         if settings.home_appliances is not None: | ||||
|             for home_appliance_params in settings.home_appliances: | ||||
|                 self.add_device(HomeAppliance(home_appliance_params)) | ||||
|     capacity_wh: int = Field( | ||||
|         default=8000, | ||||
|         gt=0, | ||||
|         description="Capacity [Wh].", | ||||
|         examples=[8000], | ||||
|     ) | ||||
|  | ||||
|         self.post_setup() | ||||
|     charging_efficiency: float = Field( | ||||
|         default=0.88, | ||||
|         gt=0, | ||||
|         le=1, | ||||
|         description="Charging efficiency [0.01 ... 1.00].", | ||||
|         examples=[0.88], | ||||
|     ) | ||||
|  | ||||
|     def post_setup(self) -> None: | ||||
|         for device in self.devices.values(): | ||||
|             device.post_setup() | ||||
|     discharging_efficiency: float = Field( | ||||
|         default=0.88, | ||||
|         gt=0, | ||||
|         le=1, | ||||
|         description="Discharge efficiency [0.01 ... 1.00].", | ||||
|         examples=[0.88], | ||||
|     ) | ||||
|  | ||||
|     levelized_cost_of_storage_kwh: float = Field( | ||||
|         default=0.0, | ||||
|         description="Levelized cost of storage (LCOS), the average lifetime cost of delivering one kWh [€/kWh].", | ||||
|         examples=[0.12], | ||||
|     ) | ||||
|  | ||||
|     max_charge_power_w: Optional[float] = Field( | ||||
|         default=5000, | ||||
|         gt=0, | ||||
|         description="Maximum charging power [W].", | ||||
|         examples=[5000], | ||||
|     ) | ||||
|  | ||||
|     min_charge_power_w: Optional[float] = Field( | ||||
|         default=50, | ||||
|         gt=0, | ||||
|         description="Minimum charging power [W].", | ||||
|         examples=[50], | ||||
|     ) | ||||
|  | ||||
|     charge_rates: Optional[list[float]] = Field( | ||||
|         default=None, | ||||
|         description="Charge rates as factor of maximum charging power [0.00 ... 1.00]. None denotes all charge rates are available.", | ||||
|         examples=[[0.0, 0.25, 0.5, 0.75, 1.0], None], | ||||
|     ) | ||||
|  | ||||
|     min_soc_percentage: int = Field( | ||||
|         default=0, | ||||
|         ge=0, | ||||
|         le=100, | ||||
|         description="Minimum state of charge (SOC) as percentage of capacity [%].", | ||||
|         examples=[10], | ||||
|     ) | ||||
|  | ||||
|     max_soc_percentage: int = Field( | ||||
|         default=100, | ||||
|         ge=0, | ||||
|         le=100, | ||||
|         description="Maximum state of charge (SOC) as percentage of capacity [%].", | ||||
|         examples=[100], | ||||
|     ) | ||||
|  | ||||
|     @computed_field  # type: ignore[prop-decorator] | ||||
|     @property | ||||
|     def measurement_key_soc_factor(self) -> str: | ||||
|         """Measurement key for the battery state of charge (SoC) as factor of total capacity [0.0 ... 1.0].""" | ||||
|         return f"{self.device_id}-soc-factor" | ||||
|  | ||||
|     @computed_field  # type: ignore[prop-decorator] | ||||
|     @property | ||||
|     def measurement_key_power_l1_w(self) -> str: | ||||
|         """Measurement key for the L1 power the battery is charged or discharged with [W].""" | ||||
|         return f"{self.device_id}-power-l1-w" | ||||
|  | ||||
|     @computed_field  # type: ignore[prop-decorator] | ||||
|     @property | ||||
|     def measurement_key_power_l2_w(self) -> str: | ||||
|         """Measurement key for the L2 power the battery is charged or discharged with [W].""" | ||||
|         return f"{self.device_id}-power-l2-w" | ||||
|  | ||||
|     @computed_field  # type: ignore[prop-decorator] | ||||
|     @property | ||||
|     def measurement_key_power_l3_w(self) -> str: | ||||
|         """Measurement key for the L3 power the battery is charged or discharged with [W].""" | ||||
|         return f"{self.device_id}-power-l3-w" | ||||
|  | ||||
|     @computed_field  # type: ignore[prop-decorator] | ||||
|     @property | ||||
|     def measurement_key_power_3_phase_sym_w(self) -> str: | ||||
|         """Measurement key for the symmetric 3 phase power the battery is charged or discharged with [W].""" | ||||
|         return f"{self.device_id}-power-3-phase-sym-w" | ||||
|  | ||||
|     @computed_field  # type: ignore[prop-decorator] | ||||
|     @property | ||||
|     def measurement_keys(self) -> Optional[list[str]]: | ||||
|         """Measurement keys for the battery stati that are measurements. | ||||
|  | ||||
|         Battery SoC, power. | ||||
|         """ | ||||
|         keys: list[str] = [ | ||||
|             self.measurement_key_soc_factor, | ||||
|             self.measurement_key_power_l1_w, | ||||
|             self.measurement_key_power_l2_w, | ||||
|             self.measurement_key_power_l3_w, | ||||
|             self.measurement_key_power_3_phase_sym_w, | ||||
|         ] | ||||
|         return keys | ||||
|  | ||||
|  | ||||
| # Initialize the Devices simulation, it is a singleton. | ||||
| devices: Optional[Devices] = None | ||||
| class InverterCommonSettings(DevicesBaseSettings): | ||||
|     """Inverter devices base settings.""" | ||||
|  | ||||
|     max_power_w: Optional[float] = Field( | ||||
|         default=None, | ||||
|         gt=0, | ||||
|         description="Maximum power [W].", | ||||
|         examples=[10000], | ||||
|     ) | ||||
|  | ||||
|     battery_id: Optional[str] = Field( | ||||
|         default=None, | ||||
|         description="ID of battery controlled by this inverter.", | ||||
|         examples=[None, "battery1"], | ||||
|     ) | ||||
|  | ||||
|     @computed_field  # type: ignore[prop-decorator] | ||||
|     @property | ||||
|     def measurement_keys(self) -> Optional[list[str]]: | ||||
|         """Measurement keys for the inverter stati that are measurements.""" | ||||
|         keys: list[str] = [] | ||||
|         return keys | ||||
|  | ||||
|  | ||||
| def get_devices() -> Devices: | ||||
|     global devices | ||||
|     # Fix circular import at runtime | ||||
|     if devices is None: | ||||
|         devices = Devices() | ||||
|     """Gets the EOS Devices simulation.""" | ||||
|     return devices | ||||
| class HomeApplianceCommonSettings(DevicesBaseSettings): | ||||
|     """Home Appliance devices base settings.""" | ||||
|  | ||||
|     consumption_wh: int = Field( | ||||
|         gt=0, | ||||
|         description="Energy consumption [Wh].", | ||||
|         examples=[2000], | ||||
|     ) | ||||
|  | ||||
|     duration_h: int = Field( | ||||
|         gt=0, | ||||
|         le=24, | ||||
|         description="Usage duration in hours [0 ... 24].", | ||||
|         examples=[1], | ||||
|     ) | ||||
|  | ||||
|     time_windows: Optional[TimeWindowSequence] = Field( | ||||
|         default=None, | ||||
|         description="Sequence of allowed time windows. Defaults to optimization general time window.", | ||||
|         examples=[ | ||||
|             { | ||||
|                 "windows": [ | ||||
|                     {"start_time": "10:00", "duration": "2 hours"}, | ||||
|                 ], | ||||
|             }, | ||||
|         ], | ||||
|     ) | ||||
|  | ||||
|     @computed_field  # type: ignore[prop-decorator] | ||||
|     @property | ||||
|     def measurement_keys(self) -> Optional[list[str]]: | ||||
|         """Measurement keys for the home appliance stati that are measurements.""" | ||||
|         keys: list[str] = [] | ||||
|         return keys | ||||
|  | ||||
|  | ||||
| class DevicesCommonSettings(SettingsBaseModel): | ||||
|     """Base configuration for devices simulation settings.""" | ||||
|  | ||||
|     batteries: Optional[list[BatteriesCommonSettings]] = Field( | ||||
|         default=None, | ||||
|         description="List of battery devices", | ||||
|         examples=[[{"device_id": "battery1", "capacity_wh": 8000}]], | ||||
|     ) | ||||
|  | ||||
|     max_batteries: Optional[int] = Field( | ||||
|         default=None, | ||||
|         ge=0, | ||||
|         description="Maximum number of batteries that can be set", | ||||
|         examples=[1, 2], | ||||
|     ) | ||||
|  | ||||
|     electric_vehicles: Optional[list[BatteriesCommonSettings]] = Field( | ||||
|         default=None, | ||||
|         description="List of electric vehicle devices", | ||||
|         examples=[[{"device_id": "battery1", "capacity_wh": 8000}]], | ||||
|     ) | ||||
|  | ||||
|     max_electric_vehicles: Optional[int] = Field( | ||||
|         default=None, | ||||
|         ge=0, | ||||
|         description="Maximum number of electric vehicles that can be set", | ||||
|         examples=[1, 2], | ||||
|     ) | ||||
|  | ||||
|     inverters: Optional[list[InverterCommonSettings]] = Field( | ||||
|         default=None, description="List of inverters", examples=[[]] | ||||
|     ) | ||||
|  | ||||
|     max_inverters: Optional[int] = Field( | ||||
|         default=None, | ||||
|         ge=0, | ||||
|         description="Maximum number of inverters that can be set", | ||||
|         examples=[1, 2], | ||||
|     ) | ||||
|  | ||||
|     home_appliances: Optional[list[HomeApplianceCommonSettings]] = Field( | ||||
|         default=None, description="List of home appliances", examples=[[]] | ||||
|     ) | ||||
|  | ||||
|     max_home_appliances: Optional[int] = Field( | ||||
|         default=None, | ||||
|         ge=0, | ||||
|         description="Maximum number of home_appliances that can be set", | ||||
|         examples=[1, 2], | ||||
|     ) | ||||
|  | ||||
|     @computed_field  # type: ignore[prop-decorator] | ||||
|     @property | ||||
|     def measurement_keys(self) -> Optional[list[str]]: | ||||
|         """Return the measurement keys for the resource/ device stati that are measurements.""" | ||||
|         keys: list[str] = [] | ||||
|  | ||||
|         if self.max_batteries and self.batteries: | ||||
|             for battery in self.batteries: | ||||
|                 keys.extend(battery.measurement_keys) | ||||
|         if self.max_electric_vehicles and self.electric_vehicles: | ||||
|             for electric_vehicle in self.electric_vehicles: | ||||
|                 keys.extend(electric_vehicle.measurement_keys) | ||||
|         return keys | ||||
|  | ||||
|  | ||||
| # Type used for indexing: (resource_id, optional actuator_id) | ||||
| class ResourceKey(PydanticBaseModel): | ||||
|     """Key identifying a resource and optionally an actuator.""" | ||||
|  | ||||
|     resource_id: str | ||||
|     actuator_id: Optional[str] = None | ||||
|  | ||||
|     model_config = ConfigDict(frozen=True) | ||||
|  | ||||
|     def __hash__(self) -> int: | ||||
|         """Returns a stable hash based on the resource_id and actuator_id. | ||||
|  | ||||
|         Returns: | ||||
|             int: Hash value derived from the resource_id and actuator_id. | ||||
|         """ | ||||
|         return hash(self.resource_id + self.actuator_id if self.actuator_id else "") | ||||
|  | ||||
|     def as_tuple(self) -> tuple[str, Optional[str]]: | ||||
|         """Return the key as a tuple for internal dictionary indexing.""" | ||||
|         return (self.resource_id, self.actuator_id) | ||||
|  | ||||
|     def __eq__(self, other: Any) -> bool: | ||||
|         if not isinstance(other, ResourceKey): | ||||
|             return NotImplemented | ||||
|         return self.resource_id == other.resource_id and self.actuator_id == other.actuator_id | ||||
|  | ||||
|  | ||||
| class ResourceRegistry(SingletonMixin, ConfigMixin, PydanticBaseModel): | ||||
|     """Registry for collecting and retrieving device status reports for simulations. | ||||
|  | ||||
|     Maintains the latest and optionally historical status reports for each resource. | ||||
|     """ | ||||
|  | ||||
|     keep_history: bool = False | ||||
|     history_size: int = 100 | ||||
|  | ||||
|     latest: dict[ResourceKey, ResourceStatus] = Field( | ||||
|         default_factory=dict, | ||||
|         description="Latest resource status that was reported per resource key.", | ||||
|         example=[], | ||||
|     ) | ||||
|     history: dict[ResourceKey, list[tuple[DateTime, ResourceStatus]]] = Field( | ||||
|         default_factory=dict, | ||||
|         description="History of resource stati that were reported per resource key.", | ||||
|         example=[], | ||||
|     ) | ||||
|  | ||||
|     @model_validator(mode="after") | ||||
|     def _enforce_history_limits(self) -> "ResourceRegistry": | ||||
|         """Ensure history list lengths respect the history_size limit.""" | ||||
|         if self.keep_history: | ||||
|             for key, records in self.history.items(): | ||||
|                 if len(records) > self.history_size: | ||||
|                     self.history[key] = records[-self.history_size :] | ||||
|         return self | ||||
|  | ||||
|     def update_status(self, key: ResourceKey, status: ResourceStatus) -> None: | ||||
|         """Update the latest status and optionally store in history. | ||||
|  | ||||
|         Args: | ||||
|             key (ResourceKey): Identifier for the resource. | ||||
|             status (ResourceStatus): Status report to store. | ||||
|         """ | ||||
|         self.latest[key] = status | ||||
|         if self.keep_history: | ||||
|             timestamp = getattr(status, "transition_timestamp", None) or to_datetime() | ||||
|             self.history.setdefault(key, []).append((timestamp, status)) | ||||
|             if len(self.history[key]) > self.history_size: | ||||
|                 self.history[key] = self.history[key][-self.history_size :] | ||||
|  | ||||
|     def status_latest(self, key: ResourceKey) -> Optional[ResourceStatus]: | ||||
|         """Retrieve the most recent status for a resource.""" | ||||
|         return self.latest.get(key) | ||||
|  | ||||
|     def status_history(self, key: ResourceKey) -> list[tuple[DateTime, ResourceStatus]]: | ||||
|         """Retrieve historical status reports for a resource.""" | ||||
|         if not self.keep_history: | ||||
|             raise RuntimeError("History tracking is disabled.") | ||||
|         return self.history.get(key, []) | ||||
|  | ||||
|     def status_exists(self, key: ResourceKey) -> bool: | ||||
|         """Check if a status report exists for the given resource. | ||||
|  | ||||
|         Args: | ||||
|             key (ResourceKey): Identifier for the resource. | ||||
|         """ | ||||
|         return key in self.latest | ||||
|  | ||||
|     def save(self) -> None: | ||||
|         """Save the registry to file.""" | ||||
|         # Make explicit cast to make mypy happy | ||||
|         cache_file = cast( | ||||
|             TextIO, CacheFileStore().create(key="resource_registry", mode="w+", suffix=".json") | ||||
|         ) | ||||
|         cache_file.seek(0) | ||||
|         cache_file.write(self.model_dump_json(indent=4)) | ||||
|         cache_file.truncate()  # Important to remove leftover data! | ||||
|  | ||||
|     def load(self) -> None: | ||||
|         """Load registry state from file and update the current instance.""" | ||||
|         cache_file = CacheFileStore().get(key="resource_registry") | ||||
|         if cache_file: | ||||
|             try: | ||||
|                 cache_file.seek(0) | ||||
|                 data = json.load(cache_file) | ||||
|                 loaded = self.__class__.model_validate(data) | ||||
|  | ||||
|                 self.keep_history = loaded.keep_history | ||||
|                 self.history_size = loaded.history_size | ||||
|                 self.latest = loaded.latest | ||||
|                 self.history = loaded.history | ||||
|             except Exception as e: | ||||
|                 logger.error("Can not load resource registry: {}", e) | ||||
|  | ||||
|  | ||||
| def get_resource_registry() -> ResourceRegistry: | ||||
|     """Gets the EOS resource registry.""" | ||||
|     return ResourceRegistry() | ||||
|   | ||||
| @@ -1,181 +1,131 @@ | ||||
| """Abstract and base classes for devices.""" | ||||
|  | ||||
| from enum import Enum | ||||
| from typing import Optional, Type | ||||
| from enum import StrEnum | ||||
|  | ||||
| from loguru import logger | ||||
| from pendulum import DateTime | ||||
| from pydantic import Field, computed_field | ||||
| from pydantic import Field | ||||
|  | ||||
| from akkudoktoreos.core.coreabc import ( | ||||
|     ConfigMixin, | ||||
|     DevicesMixin, | ||||
|     EnergyManagementSystemMixin, | ||||
|     PredictionMixin, | ||||
| ) | ||||
| from akkudoktoreos.core.pydantic import ParametersBaseModel | ||||
| from akkudoktoreos.utils.datetimeutil import to_duration | ||||
| from akkudoktoreos.config.configabc import SettingsBaseModel | ||||
|  | ||||
|  | ||||
| class DeviceParameters(ParametersBaseModel): | ||||
|     device_id: str = Field(description="ID of device", examples="device1") | ||||
|     hours: Optional[int] = Field( | ||||
|         default=None, | ||||
|         gt=0, | ||||
|         description="Number of prediction hours. Defaults to global config prediction hours.", | ||||
|         examples=[None], | ||||
| class DevicesBaseSettings(SettingsBaseModel): | ||||
|     """Base devices setting.""" | ||||
|  | ||||
|     device_id: str = Field( | ||||
|         default="<unknown>", | ||||
|         description="ID of device", | ||||
|         examples=["battery1", "ev1", "inverter1", "dishwasher"], | ||||
|     ) | ||||
|  | ||||
|  | ||||
| class DeviceOptimizeResult(ParametersBaseModel): | ||||
|     device_id: str = Field(description="ID of device", examples=["device1"]) | ||||
|     hours: int = Field(gt=0, description="Number of hours in the simulation.", examples=[24]) | ||||
| class BatteryOperationMode(StrEnum): | ||||
|     """Battery Operation Mode. | ||||
|  | ||||
|     Enumerates the operating modes of a battery in a home energy | ||||
|     management simulation. These modes require no direct awareness | ||||
|     of electricity prices or carbon intensity — higher-level | ||||
|     controllers or optimizers decide when to switch modes. | ||||
|  | ||||
| class DeviceState(Enum): | ||||
|     UNINITIALIZED = 0 | ||||
|     PREPARED = 1 | ||||
|     INITIALIZED = 2 | ||||
|     Modes | ||||
|     ----- | ||||
|     - IDLE: | ||||
|         No charging or discharging. | ||||
|  | ||||
|     - SELF_CONSUMPTION: | ||||
|         Charge from local surplus and discharge to meet local demand. | ||||
|  | ||||
| class DevicesStartEndMixin(ConfigMixin, EnergyManagementSystemMixin): | ||||
|     """A mixin to manage start, end datetimes for devices data. | ||||
|     - NON_EXPORT: | ||||
|         Charge from on-site or local surplus with the goal of | ||||
|         minimizing or preventing energy export to the external grid. | ||||
|         Discharging to the grid is not allowed. | ||||
|  | ||||
|     The starting datetime for devices data generation is provided by the energy management | ||||
|     system. Device data cannot be computed if this value is `None`. | ||||
|     - PEAK_SHAVING: | ||||
|         Discharge during local demand peaks to reduce grid draw. | ||||
|  | ||||
|     - GRID_SUPPORT_EXPORT: | ||||
|         Discharge to support the upstream grid when commanded. | ||||
|  | ||||
|     - GRID_SUPPORT_IMPORT: | ||||
|         Charge from the grid when instructed to absorb excess supply. | ||||
|  | ||||
|     - FREQUENCY_REGULATION: | ||||
|         Perform fast bidirectional power adjustments based on grid | ||||
|         frequency deviations. | ||||
|  | ||||
|     - RAMP_RATE_CONTROL: | ||||
|         Smooth changes in local net load or generation. | ||||
|  | ||||
|     - RESERVE_BACKUP: | ||||
|         Maintain a minimum state of charge for emergency use. | ||||
|  | ||||
|     - OUTAGE_SUPPLY: | ||||
|         Discharge to power critical loads during a grid outage. | ||||
|  | ||||
|     - FORCED_CHARGE: | ||||
|         Override all other logic and charge regardless of conditions. | ||||
|  | ||||
|     - FORCED_DISCHARGE: | ||||
|         Override all other logic and discharge regardless of conditions. | ||||
|  | ||||
|     - FAULT: | ||||
|         Battery is unavailable due to fault or error state. | ||||
|     """ | ||||
|  | ||||
|     # Computed field for end_datetime and keep_datetime | ||||
|     @computed_field  # type: ignore[prop-decorator] | ||||
|     @property | ||||
|     def end_datetime(self) -> Optional[DateTime]: | ||||
|         """Compute the end datetime based on the `start_datetime` and `hours`. | ||||
|  | ||||
|         Ajusts the calculated end time if DST transitions occur within the prediction window. | ||||
|  | ||||
|         Returns: | ||||
|             Optional[DateTime]: The calculated end datetime, or `None` if inputs are missing. | ||||
|         """ | ||||
|         if self.ems.start_datetime and self.config.prediction.hours: | ||||
|             end_datetime = self.ems.start_datetime + to_duration( | ||||
|                 f"{self.config.prediction.hours} hours" | ||||
|             ) | ||||
|             dst_change = end_datetime.offset_hours - self.ems.start_datetime.offset_hours | ||||
|             logger.debug( | ||||
|                 f"Pre: {self.ems.start_datetime}..{end_datetime}: DST change: {dst_change}" | ||||
|             ) | ||||
|             if dst_change < 0: | ||||
|                 end_datetime = end_datetime + to_duration(f"{abs(int(dst_change))} hours") | ||||
|             elif dst_change > 0: | ||||
|                 end_datetime = end_datetime - to_duration(f"{abs(int(dst_change))} hours") | ||||
|             logger.debug( | ||||
|                 f"Pst: {self.ems.start_datetime}..{end_datetime}: DST change: {dst_change}" | ||||
|             ) | ||||
|             return end_datetime | ||||
|         return None | ||||
|  | ||||
|     @computed_field  # type: ignore[prop-decorator] | ||||
|     @property | ||||
|     def total_hours(self) -> Optional[int]: | ||||
|         """Compute the hours from `start_datetime` to `end_datetime`. | ||||
|  | ||||
|         Returns: | ||||
|             Optional[pendulum.period]: The duration hours, or `None` if either datetime is unavailable. | ||||
|         """ | ||||
|         end_dt = self.end_datetime | ||||
|         if end_dt is None: | ||||
|             return None | ||||
|         duration = end_dt - self.ems.start_datetime | ||||
|         return int(duration.total_hours()) | ||||
|     IDLE = "IDLE" | ||||
|     SELF_CONSUMPTION = "SELF_CONSUMPTION" | ||||
|     NON_EXPORT = "NON_EXPORT" | ||||
|     PEAK_SHAVING = "PEAK_SHAVING" | ||||
|     GRID_SUPPORT_EXPORT = "GRID_SUPPORT_EXPORT" | ||||
|     GRID_SUPPORT_IMPORT = "GRID_SUPPORT_IMPORT" | ||||
|     FREQUENCY_REGULATION = "FREQUENCY_REGULATION" | ||||
|     RAMP_RATE_CONTROL = "RAMP_RATE_CONTROL" | ||||
|     RESERVE_BACKUP = "RESERVE_BACKUP" | ||||
|     OUTAGE_SUPPLY = "OUTAGE_SUPPLY" | ||||
|     FORCED_CHARGE = "FORCED_CHARGE" | ||||
|     FORCED_DISCHARGE = "FORCED_DISCHARGE" | ||||
|     FAULT = "FAULT" | ||||
|  | ||||
|  | ||||
| class DeviceBase(DevicesStartEndMixin, PredictionMixin, DevicesMixin): | ||||
|     """Base class for device simulations. | ||||
| class ApplianceOperationMode(StrEnum): | ||||
|     """Appliance operation modes. | ||||
|  | ||||
|     Enables access to EOS configuration data (attribute `config`), EOS prediction data (attribute | ||||
|     `prediction`) and EOS device registry (attribute `devices`). | ||||
|     Modes | ||||
|     ----- | ||||
|     - OFF: | ||||
|         Stop or prevent any active operation of the appliance. | ||||
|  | ||||
|     Behavior: | ||||
|         - Several initialization phases (setup, post_setup): | ||||
|             - setup: Initialize class attributes from DeviceParameters (pydantic input validation) | ||||
|             - post_setup: Set connections between devices | ||||
|         - NotImplemented: | ||||
|             - hooks during optimization | ||||
|     - RUN: | ||||
|         Start or continue normal operation of the appliance. | ||||
|  | ||||
|     Notes: | ||||
|         - This class is base to concrete devices like battery, inverter, etc. that are used in optimization. | ||||
|         - Not a pydantic model for a low footprint during optimization. | ||||
|     - DEFER: | ||||
|         Postpone operation to a later time window based on | ||||
|         scheduling or optimization criteria. | ||||
|  | ||||
|     - PAUSE: | ||||
|         Temporarily suspend an ongoing operation, keeping the | ||||
|         option to resume later. | ||||
|  | ||||
|     - RESUME: | ||||
|         Continue an operation that was previously paused or | ||||
|         deferred. | ||||
|  | ||||
|     - LIMIT_POWER: | ||||
|         Run the appliance under reduced power constraints, | ||||
|         for example in response to load-management or | ||||
|         demand-response signals. | ||||
|  | ||||
|     - FORCED_RUN: | ||||
|         Start or maintain operation even if constraints or | ||||
|         optimization strategies would otherwise delay or limit it. | ||||
|  | ||||
|     - FAULT: | ||||
|         Appliance is unavailable due to fault or error state. | ||||
|     """ | ||||
|  | ||||
|     def __init__(self, parameters: Optional[DeviceParameters] = None): | ||||
|         self.device_id: str = "<invalid>" | ||||
|         self.parameters: Optional[DeviceParameters] = None | ||||
|         self.hours = -1 | ||||
|         if self.total_hours is not None: | ||||
|             self.hours = self.total_hours | ||||
|  | ||||
|         self.initialized = DeviceState.UNINITIALIZED | ||||
|  | ||||
|         if parameters is not None: | ||||
|             self.setup(parameters) | ||||
|  | ||||
|     def setup(self, parameters: DeviceParameters) -> None: | ||||
|         if self.initialized != DeviceState.UNINITIALIZED: | ||||
|             return | ||||
|  | ||||
|         self.parameters = parameters | ||||
|         self.device_id = self.parameters.device_id | ||||
|  | ||||
|         if self.parameters.hours is not None: | ||||
|             self.hours = self.parameters.hours | ||||
|         if self.hours < 0: | ||||
|             raise ValueError("hours is unset") | ||||
|  | ||||
|         self._setup() | ||||
|  | ||||
|         self.initialized = DeviceState.PREPARED | ||||
|  | ||||
|     def post_setup(self) -> None: | ||||
|         if self.initialized.value >= DeviceState.INITIALIZED.value: | ||||
|             return | ||||
|  | ||||
|         self._post_setup() | ||||
|         self.initialized = DeviceState.INITIALIZED | ||||
|  | ||||
|     def _setup(self) -> None: | ||||
|         """Implement custom setup in derived device classes.""" | ||||
|         pass | ||||
|  | ||||
|     def _post_setup(self) -> None: | ||||
|         """Implement custom setup in derived device classes that is run when all devices are initialized.""" | ||||
|         pass | ||||
|  | ||||
|  | ||||
| class DevicesBase(DevicesStartEndMixin, PredictionMixin): | ||||
|     """Base class for handling device data. | ||||
|  | ||||
|     Enables access to EOS configuration data (attribute `config`) and EOS prediction data (attribute | ||||
|     `prediction`). | ||||
|     """ | ||||
|  | ||||
|     def __init__(self) -> None: | ||||
|         super().__init__() | ||||
|         self.devices: dict[str, "DeviceBase"] = dict() | ||||
|  | ||||
|     def get_device_by_id(self, device_id: str) -> Optional["DeviceBase"]: | ||||
|         return self.devices.get(device_id) | ||||
|  | ||||
|     def add_device(self, device: Optional["DeviceBase"]) -> None: | ||||
|         if device is None: | ||||
|             return | ||||
|         if device.device_id in self.devices: | ||||
|             raise ValueError(f"{device.device_id} already registered") | ||||
|         self.devices[device.device_id] = device | ||||
|  | ||||
|     def remove_device(self, device: Type["DeviceBase"] | str) -> bool: | ||||
|         if isinstance(device, DeviceBase): | ||||
|             device = device.device_id | ||||
|         return self.devices.pop(device, None) is not None  # type: ignore[arg-type] | ||||
|  | ||||
|     def reset(self) -> None: | ||||
|         self.devices = dict() | ||||
|     OFF = "OFF" | ||||
|     RUN = "RUN" | ||||
|     DEFER = "DEFER" | ||||
|     PAUSE = "PAUSE" | ||||
|     RESUME = "RESUME" | ||||
|     LIMIT_POWER = "LIMIT_POWER" | ||||
|     FORCED_RUN = "FORCED_RUN" | ||||
|     FAULT = "FAULT" | ||||
|   | ||||
| @@ -1,79 +0,0 @@ | ||||
| from typing import Optional | ||||
|  | ||||
| import numpy as np | ||||
| from pydantic import Field | ||||
|  | ||||
| from akkudoktoreos.devices.devicesabc import DeviceBase, DeviceParameters | ||||
|  | ||||
|  | ||||
| class HomeApplianceParameters(DeviceParameters): | ||||
|     """Home Appliance Device Simulation Configuration.""" | ||||
|  | ||||
|     device_id: str = Field(description="ID of home appliance", examples=["dishwasher"]) | ||||
|     consumption_wh: int = Field( | ||||
|         gt=0, | ||||
|         description="An integer representing the energy consumption of a household device in watt-hours.", | ||||
|         examples=[2000], | ||||
|     ) | ||||
|     duration_h: int = Field( | ||||
|         gt=0, | ||||
|         description="An integer representing the usage duration of a household device in hours.", | ||||
|         examples=[3], | ||||
|     ) | ||||
|  | ||||
|  | ||||
| class HomeAppliance(DeviceBase): | ||||
|     def __init__( | ||||
|         self, | ||||
|         parameters: Optional[HomeApplianceParameters] = None, | ||||
|     ): | ||||
|         self.parameters: Optional[HomeApplianceParameters] = None | ||||
|         super().__init__(parameters) | ||||
|  | ||||
|     def _setup(self) -> None: | ||||
|         if self.parameters is None: | ||||
|             raise ValueError(f"Parameters not set: {self.parameters}") | ||||
|         self.load_curve = np.zeros(self.hours)  # Initialize the load curve with zeros | ||||
|         self.duration_h = self.parameters.duration_h | ||||
|         self.consumption_wh = self.parameters.consumption_wh | ||||
|  | ||||
|     def set_starting_time(self, start_hour: int, global_start_hour: int = 0) -> None: | ||||
|         """Sets the start time of the device and generates the corresponding load curve. | ||||
|  | ||||
|         :param start_hour: The hour at which the device should start. | ||||
|         """ | ||||
|         self.reset_load_curve() | ||||
|         # Check if the duration of use is within the available time frame | ||||
|         if start_hour + self.duration_h > self.hours: | ||||
|             raise ValueError("The duration of use exceeds the available time frame.") | ||||
|         if start_hour < global_start_hour: | ||||
|             raise ValueError("The start time is earlier than the available time frame.") | ||||
|  | ||||
|         # Calculate power per hour based on total consumption and duration | ||||
|         power_per_hour = self.consumption_wh / self.duration_h  # Convert to watt-hours | ||||
|  | ||||
|         # Set the power for the duration of use in the load curve array | ||||
|         self.load_curve[start_hour : start_hour + self.duration_h] = power_per_hour | ||||
|  | ||||
|     def reset_load_curve(self) -> None: | ||||
|         """Resets the load curve.""" | ||||
|         self.load_curve = np.zeros(self.hours) | ||||
|  | ||||
|     def get_load_curve(self) -> np.ndarray: | ||||
|         """Returns the current load curve.""" | ||||
|         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. | ||||
|         """ | ||||
|         if hour < 0 or hour >= self.hours: | ||||
|             raise ValueError("The specified hour is outside the available time frame.") | ||||
|  | ||||
|         return self.load_curve[hour] | ||||
|  | ||||
|     def get_latest_starting_point(self) -> int: | ||||
|         """Returns the latest possible start time at which the device can still run completely.""" | ||||
|         return self.hours - self.duration_h | ||||
							
								
								
									
										0
									
								
								src/akkudoktoreos/devices/genetic/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								src/akkudoktoreos/devices/genetic/__init__.py
									
									
									
									
									
										Normal file
									
								
							| @@ -1,125 +1,23 @@ | ||||
| from typing import Any, Optional | ||||
| 
 | ||||
| import numpy as np | ||||
| from pydantic import Field, field_validator | ||||
| 
 | ||||
| from akkudoktoreos.devices.devicesabc import ( | ||||
|     DeviceBase, | ||||
|     DeviceOptimizeResult, | ||||
|     DeviceParameters, | ||||
| from akkudoktoreos.optimization.genetic.geneticdevices import ( | ||||
|     BaseBatteryParameters, | ||||
|     SolarPanelBatteryParameters, | ||||
| ) | ||||
| from akkudoktoreos.utils.utils import NumpyEncoder | ||||
| 
 | ||||
| 
 | ||||
| def max_charging_power_field(description: Optional[str] = None) -> float: | ||||
|     if description is None: | ||||
|         description = "Maximum charging power in watts." | ||||
|     return Field( | ||||
|         default=5000, | ||||
|         gt=0, | ||||
|         description=description, | ||||
|     ) | ||||
| 
 | ||||
| 
 | ||||
| def initial_soc_percentage_field(description: str) -> int: | ||||
|     return Field(default=0, ge=0, le=100, description=description, examples=[42]) | ||||
| 
 | ||||
| 
 | ||||
| def discharging_efficiency_field(default_value: float) -> float: | ||||
|     return Field( | ||||
|         default=default_value, | ||||
|         gt=0, | ||||
|         le=1, | ||||
|         description="A float representing the discharge efficiency of the battery.", | ||||
|     ) | ||||
| 
 | ||||
| 
 | ||||
| class BaseBatteryParameters(DeviceParameters): | ||||
|     """Battery Device Simulation Configuration.""" | ||||
| 
 | ||||
|     device_id: str = Field(description="ID of battery", examples=["battery1"]) | ||||
|     capacity_wh: int = Field( | ||||
|         gt=0, | ||||
|         description="An integer representing the capacity of the battery in watt-hours.", | ||||
|         examples=[8000], | ||||
|     ) | ||||
|     charging_efficiency: float = Field( | ||||
|         default=0.88, | ||||
|         gt=0, | ||||
|         le=1, | ||||
|         description="A float representing the charging efficiency of the battery.", | ||||
|     ) | ||||
|     discharging_efficiency: float = discharging_efficiency_field(0.88) | ||||
|     max_charge_power_w: Optional[float] = max_charging_power_field() | ||||
|     initial_soc_percentage: int = initial_soc_percentage_field( | ||||
|         "An integer representing the state of charge of the battery at the **start** of the current hour (not the current state)." | ||||
|     ) | ||||
|     min_soc_percentage: int = Field( | ||||
|         default=0, | ||||
|         ge=0, | ||||
|         le=100, | ||||
|         description="An integer representing the minimum state of charge (SOC) of the battery in percentage.", | ||||
|         examples=[10], | ||||
|     ) | ||||
|     max_soc_percentage: int = Field( | ||||
|         default=100, | ||||
|         ge=0, | ||||
|         le=100, | ||||
|         description="An integer representing the maximum state of charge (SOC) of the battery in percentage.", | ||||
|     ) | ||||
| 
 | ||||
| 
 | ||||
| class SolarPanelBatteryParameters(BaseBatteryParameters): | ||||
|     max_charge_power_w: Optional[float] = max_charging_power_field() | ||||
| 
 | ||||
| 
 | ||||
| class ElectricVehicleParameters(BaseBatteryParameters): | ||||
|     """Battery Electric Vehicle Device Simulation Configuration.""" | ||||
| 
 | ||||
|     device_id: str = Field(description="ID of electric vehicle", examples=["ev1"]) | ||||
|     discharging_efficiency: float = discharging_efficiency_field(1.0) | ||||
|     initial_soc_percentage: int = initial_soc_percentage_field( | ||||
|         "An integer representing the current state of charge (SOC) of the battery in percentage." | ||||
|     ) | ||||
| 
 | ||||
| 
 | ||||
| class ElectricVehicleResult(DeviceOptimizeResult): | ||||
|     """Result class containing information related to the electric vehicle's charging and discharging behavior.""" | ||||
| 
 | ||||
|     device_id: str = Field(description="ID of electric vehicle", examples=["ev1"]) | ||||
|     charge_array: list[float] = Field( | ||||
|         description="Hourly charging status (0 for no charging, 1 for charging)." | ||||
|     ) | ||||
|     discharge_array: list[int] = Field( | ||||
|         description="Hourly discharging status (0 for no discharging, 1 for discharging)." | ||||
|     ) | ||||
|     discharging_efficiency: float = Field(description="The discharge efficiency as a float..") | ||||
|     capacity_wh: int = Field(description="Capacity of the EV’s battery in watt-hours.") | ||||
|     charging_efficiency: float = Field(description="Charging efficiency as a float..") | ||||
|     max_charge_power_w: int = Field(description="Maximum charging power in watts.") | ||||
|     soc_wh: float = Field( | ||||
|         description="State of charge of the battery in watt-hours at the start of the simulation." | ||||
|     ) | ||||
|     initial_soc_percentage: int = Field( | ||||
|         description="State of charge at the start of the simulation in percentage." | ||||
|     ) | ||||
| 
 | ||||
|     @field_validator("discharge_array", "charge_array", mode="before") | ||||
|     def convert_numpy(cls, field: Any) -> Any: | ||||
|         return NumpyEncoder.convert_numpy(field)[0] | ||||
| 
 | ||||
| 
 | ||||
| class Battery(DeviceBase): | ||||
| class Battery: | ||||
|     """Represents a battery device with methods to simulate energy charging and discharging.""" | ||||
| 
 | ||||
|     def __init__(self, parameters: Optional[BaseBatteryParameters] = None): | ||||
|         self.parameters: Optional[BaseBatteryParameters] = None | ||||
|         super().__init__(parameters) | ||||
|     def __init__(self, parameters: BaseBatteryParameters, prediction_hours: int): | ||||
|         self.parameters = parameters | ||||
|         self.prediction_hours = prediction_hours | ||||
|         self._setup() | ||||
| 
 | ||||
|     def _setup(self) -> None: | ||||
|         """Sets up the battery parameters based on configuration or provided parameters.""" | ||||
|         if self.parameters is None: | ||||
|             raise ValueError(f"Parameters not set: {self.parameters}") | ||||
|         self.capacity_wh = self.parameters.capacity_wh | ||||
|         self.initial_soc_percentage = self.parameters.initial_soc_percentage | ||||
|         self.charging_efficiency = self.parameters.charging_efficiency | ||||
| @@ -138,8 +36,8 @@ class Battery(DeviceBase): | ||||
|             self.max_charge_power_w = self.parameters.max_charge_power_w | ||||
|         else: | ||||
|             self.max_charge_power_w = self.capacity_wh  # TODO this should not be equal capacity_wh | ||||
|         self.discharge_array = np.full(self.hours, 1) | ||||
|         self.charge_array = np.full(self.hours, 1) | ||||
|         self.discharge_array = np.full(self.prediction_hours, 1) | ||||
|         self.charge_array = np.full(self.prediction_hours, 1) | ||||
|         self.soc_wh = (self.initial_soc_percentage / 100) * self.capacity_wh | ||||
|         self.min_soc_wh = (self.min_soc_percentage / 100) * self.capacity_wh | ||||
|         self.max_soc_wh = (self.max_soc_percentage / 100) * self.capacity_wh | ||||
| @@ -147,11 +45,11 @@ class Battery(DeviceBase): | ||||
|     def to_dict(self) -> dict[str, Any]: | ||||
|         """Converts the object to a dictionary representation.""" | ||||
|         return { | ||||
|             "device_id": self.device_id, | ||||
|             "device_id": self.parameters.device_id, | ||||
|             "capacity_wh": self.capacity_wh, | ||||
|             "initial_soc_percentage": self.initial_soc_percentage, | ||||
|             "soc_wh": self.soc_wh, | ||||
|             "hours": self.hours, | ||||
|             "hours": self.prediction_hours, | ||||
|             "discharge_array": self.discharge_array, | ||||
|             "charge_array": self.charge_array, | ||||
|             "charging_efficiency": self.charging_efficiency, | ||||
| @@ -163,25 +61,31 @@ class Battery(DeviceBase): | ||||
|         """Resets the battery state to its initial values.""" | ||||
|         self.soc_wh = (self.initial_soc_percentage / 100) * self.capacity_wh | ||||
|         self.soc_wh = min(max(self.soc_wh, self.min_soc_wh), self.max_soc_wh) | ||||
|         self.discharge_array = np.full(self.hours, 1) | ||||
|         self.charge_array = np.full(self.hours, 1) | ||||
|         self.discharge_array = np.full(self.prediction_hours, 1) | ||||
|         self.charge_array = np.full(self.prediction_hours, 1) | ||||
| 
 | ||||
|     def set_discharge_per_hour(self, discharge_array: np.ndarray) -> None: | ||||
|         """Sets the discharge values for each hour.""" | ||||
|         if len(discharge_array) != self.hours: | ||||
|             raise ValueError(f"Discharge array must have exactly {self.hours} elements.") | ||||
|         if len(discharge_array) != self.prediction_hours: | ||||
|             raise ValueError( | ||||
|                 f"Discharge array must have exactly {self.prediction_hours} elements. Got {len(discharge_array)} elements." | ||||
|             ) | ||||
|         self.discharge_array = np.array(discharge_array) | ||||
| 
 | ||||
|     def set_charge_per_hour(self, charge_array: np.ndarray) -> None: | ||||
|         """Sets the charge values for each hour.""" | ||||
|         if len(charge_array) != self.hours: | ||||
|             raise ValueError(f"Charge array must have exactly {self.hours} elements.") | ||||
|         if len(charge_array) != self.prediction_hours: | ||||
|             raise ValueError( | ||||
|                 f"Charge array must have exactly {self.prediction_hours} elements. Got {len(charge_array)} elements." | ||||
|             ) | ||||
|         self.charge_array = np.array(charge_array) | ||||
| 
 | ||||
|     def set_charge_allowed_for_hour(self, charge: float, hour: int) -> None: | ||||
|         """Sets the charge for a specific hour.""" | ||||
|         if hour >= self.hours: | ||||
|             raise ValueError(f"Hour {hour} is out of range. Must be less than {self.hours}.") | ||||
|         if hour >= self.prediction_hours: | ||||
|             raise ValueError( | ||||
|                 f"Hour {hour} is out of range. Must be less than {self.prediction_hours}." | ||||
|             ) | ||||
|         self.charge_array[hour] = charge | ||||
| 
 | ||||
|     def current_soc_percentage(self) -> float: | ||||
							
								
								
									
										112
									
								
								src/akkudoktoreos/devices/genetic/homeappliance.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										112
									
								
								src/akkudoktoreos/devices/genetic/homeappliance.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,112 @@ | ||||
| from typing import Optional | ||||
|  | ||||
| import numpy as np | ||||
|  | ||||
| from akkudoktoreos.optimization.genetic.geneticdevices import HomeApplianceParameters | ||||
| from akkudoktoreos.utils.datetimeutil import ( | ||||
|     TimeWindow, | ||||
|     TimeWindowSequence, | ||||
|     to_datetime, | ||||
|     to_duration, | ||||
|     to_time, | ||||
| ) | ||||
|  | ||||
|  | ||||
| class HomeAppliance: | ||||
|     def __init__( | ||||
|         self, | ||||
|         parameters: HomeApplianceParameters, | ||||
|         optimization_hours: int, | ||||
|         prediction_hours: int, | ||||
|     ): | ||||
|         self.parameters: HomeApplianceParameters = parameters | ||||
|         self.prediction_hours = prediction_hours | ||||
|         self._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 | ||||
|         self.appliance_start: Optional[int] = None | ||||
|         # setup possible start times | ||||
|         if self.parameters.time_windows is None: | ||||
|             self.parameters.time_windows = TimeWindowSequence( | ||||
|                 windows=[ | ||||
|                     TimeWindow( | ||||
|                         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) -> None: | ||||
|         """Sets the start time of the device and generates the corresponding load curve. | ||||
|  | ||||
|         :param start_hour: The hour at which the device should start. | ||||
|         """ | ||||
|         self.reset_load_curve() | ||||
|  | ||||
|         # Check if the duration of use is within the available time windows | ||||
|         if not self.start_allowed[start_hour]: | ||||
|             # No available time window to start home appliance | ||||
|             # Use the earliest one | ||||
|             start_hour = self.start_earliest | ||||
|  | ||||
|         # Check if it is possibility to start the appliance | ||||
|         if start_hour < global_start_hour: | ||||
|             # Start is before current time | ||||
|             # Use the latest one | ||||
|             start_hour = self.start_latest | ||||
|  | ||||
|         # Calculate power per hour based on total consumption and duration | ||||
|         power_per_hour = self.consumption_wh / self.duration_h  # Convert to watt-hours | ||||
|  | ||||
|         # Set the power for the duration of use in the load curve array | ||||
|         self.load_curve[start_hour : start_hour + self.duration_h] = power_per_hour | ||||
|  | ||||
|         # Set the selected start hour | ||||
|         self.appliance_start = start_hour | ||||
|  | ||||
|     def reset_load_curve(self) -> None: | ||||
|         """Resets the load curve.""" | ||||
|         self.load_curve = np.zeros(self.prediction_hours) | ||||
|  | ||||
|     def get_load_curve(self) -> np.ndarray: | ||||
|         """Returns the current load curve.""" | ||||
|         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. | ||||
|         """ | ||||
|         if hour < 0 or hour >= self.prediction_hours: | ||||
|             raise ValueError( | ||||
|                 f"The specified hour {hour} is outside the available time frame {self.prediction_hours}." | ||||
|             ) | ||||
|  | ||||
|         return self.load_curve[hour] | ||||
|  | ||||
|     def get_appliance_start(self) -> Optional[int]: | ||||
|         return self.appliance_start | ||||
| @@ -1,65 +1,32 @@ | ||||
| from typing import Optional | ||||
| 
 | ||||
| from loguru import logger | ||||
| from pydantic import Field | ||||
| 
 | ||||
| from akkudoktoreos.devices.devicesabc import DeviceBase, DeviceParameters | ||||
| from akkudoktoreos.devices.genetic.battery import Battery | ||||
| from akkudoktoreos.optimization.genetic.geneticdevices import InverterParameters | ||||
| from akkudoktoreos.prediction.interpolator import get_eos_load_interpolator | ||||
| 
 | ||||
| 
 | ||||
| class InverterParameters(DeviceParameters): | ||||
|     """Inverter Device Simulation Configuration.""" | ||||
| 
 | ||||
|     device_id: str = Field(description="ID of inverter", examples=["inverter1"]) | ||||
|     max_power_wh: float = Field(gt=0, examples=[10000]) | ||||
|     battery_id: Optional[str] = Field( | ||||
|         default=None, description="ID of battery", examples=[None, "battery1"] | ||||
|     ) | ||||
| 
 | ||||
| 
 | ||||
| class Inverter(DeviceBase): | ||||
| class Inverter: | ||||
|     def __init__( | ||||
|         self, | ||||
|         parameters: Optional[InverterParameters] = None, | ||||
|         parameters: InverterParameters, | ||||
|         battery: Optional[Battery] = None, | ||||
|     ): | ||||
|         self.parameters: Optional[InverterParameters] = None | ||||
|         super().__init__(parameters) | ||||
| 
 | ||||
|         self.scr_lookup: dict = {} | ||||
| 
 | ||||
|     def _calculate_scr(self, consumption: float, generation: float) -> float: | ||||
|         """Check if the consumption and production is in the lookup table. If not, calculate and store the value.""" | ||||
|         if consumption not in self.scr_lookup: | ||||
|             self.scr_lookup[consumption] = {} | ||||
| 
 | ||||
|         if generation not in self.scr_lookup[consumption]: | ||||
|             scr = self.self_consumption_predictor.calculate_self_consumption( | ||||
|                 consumption, generation | ||||
|             ) | ||||
|             self.scr_lookup[consumption][generation] = scr | ||||
|             return scr | ||||
| 
 | ||||
|         return self.scr_lookup[consumption][generation] | ||||
|         self.parameters: InverterParameters = parameters | ||||
|         self.battery: Optional[Battery] = battery | ||||
|         self._setup() | ||||
| 
 | ||||
|     def _setup(self) -> None: | ||||
|         if self.parameters is None: | ||||
|             raise ValueError(f"Parameters not set: {self.parameters}") | ||||
|         if self.parameters.battery_id is None: | ||||
|             # For the moment raise exception | ||||
|             # TODO: Make battery configurable by config | ||||
|             error_msg = "Battery for PV inverter is mandatory." | ||||
|         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}." | ||||
|             logger.error(error_msg) | ||||
|             raise NotImplementedError(error_msg) | ||||
|             raise ValueError(error_msg) | ||||
|         self.self_consumption_predictor = get_eos_load_interpolator() | ||||
|         self.max_power_wh = ( | ||||
|             self.parameters.max_power_wh | ||||
|         )  # Maximum power that the inverter can handle | ||||
| 
 | ||||
|     def _post_setup(self) -> None: | ||||
|         if self.parameters is None: | ||||
|             raise ValueError(f"Parameters not set: {self.parameters}") | ||||
|         self.battery = self.devices.get_device_by_id(self.parameters.battery_id) | ||||
| 
 | ||||
|     def process_energy( | ||||
|         self, generation: float, consumption: float, hour: int | ||||
|     ) -> tuple[float, float, float, float]: | ||||
| @@ -76,8 +43,10 @@ class Inverter(DeviceBase): | ||||
|                 grid_import = -remaining_power  # Negative indicates feeding into the grid | ||||
|                 self_consumption = self.max_power_wh | ||||
|             else: | ||||
|                 # Calculate scr with lookup table | ||||
|                 scr = self._calculate_scr(consumption, generation) | ||||
|                 # Calculate scr using cached results per energy management/optimization run | ||||
|                 scr = self.self_consumption_predictor.calculate_self_consumption( | ||||
|                     consumption, generation | ||||
|                 ) | ||||
| 
 | ||||
|                 # Remaining power after consumption | ||||
|                 remaining_power = (generation - consumption) * scr  # EVQ | ||||
| @@ -86,11 +55,12 @@ class Inverter(DeviceBase): | ||||
| 
 | ||||
|                 if remaining_load_evq > 0: | ||||
|                     # Akku muss den Restverbrauch decken | ||||
|                     from_battery, discharge_losses = self.battery.discharge_energy( | ||||
|                         remaining_load_evq, hour | ||||
|                     ) | ||||
|                     remaining_load_evq -= from_battery  # Restverbrauch nach Akkuentladung | ||||
|                     losses += discharge_losses | ||||
|                     if self.battery: | ||||
|                         from_battery, discharge_losses = self.battery.discharge_energy( | ||||
|                             remaining_load_evq, hour | ||||
|                         ) | ||||
|                         remaining_load_evq -= from_battery  # Restverbrauch nach Akkuentladung | ||||
|                         losses += discharge_losses | ||||
| 
 | ||||
|                     # Wenn der Akku den Restverbrauch nicht vollständig decken kann, wird der Rest ins Netz gezogen | ||||
|                     if remaining_load_evq > 0: | ||||
| @@ -101,10 +71,13 @@ class Inverter(DeviceBase): | ||||
| 
 | ||||
|                 if remaining_power > 0: | ||||
|                     # Load battery with excess energy | ||||
|                     charged_energie, charge_losses = self.battery.charge_energy( | ||||
|                         remaining_power, hour | ||||
|                     ) | ||||
|                     remaining_surplus = remaining_power - (charged_energie + charge_losses) | ||||
|                     if self.battery: | ||||
|                         charged_energie, charge_losses = self.battery.charge_energy( | ||||
|                             remaining_power, hour | ||||
|                         ) | ||||
|                         remaining_surplus = remaining_power - (charged_energie + charge_losses) | ||||
|                     else: | ||||
|                         remaining_surplus = remaining_power | ||||
| 
 | ||||
|                     # Feed-in to the grid based on remaining capacity | ||||
|                     if remaining_surplus > self.max_power_wh - consumption: | ||||
| @@ -124,10 +97,13 @@ class Inverter(DeviceBase): | ||||
|             available_ac_power = max(self.max_power_wh - generation, 0) | ||||
| 
 | ||||
|             # Discharge battery to cover shortfall, if possible | ||||
|             battery_discharge, discharge_losses = self.battery.discharge_energy( | ||||
|                 min(shortfall, available_ac_power), hour | ||||
|             ) | ||||
|             losses += discharge_losses | ||||
|             if self.battery: | ||||
|                 battery_discharge, discharge_losses = self.battery.discharge_energy( | ||||
|                     min(shortfall, available_ac_power), hour | ||||
|                 ) | ||||
|                 losses += discharge_losses | ||||
|             else: | ||||
|                 battery_discharge = 0 | ||||
| 
 | ||||
|             # Draw remaining required power from the grid (discharge_losses are already substraved in the battery) | ||||
|             grid_import = shortfall - battery_discharge | ||||
| @@ -1,24 +0,0 @@ | ||||
| from typing import Optional | ||||
|  | ||||
| from pydantic import Field | ||||
|  | ||||
| from akkudoktoreos.config.configabc import SettingsBaseModel | ||||
| from akkudoktoreos.devices.battery import BaseBatteryParameters | ||||
| from akkudoktoreos.devices.generic import HomeApplianceParameters | ||||
| from akkudoktoreos.devices.inverter import InverterParameters | ||||
|  | ||||
|  | ||||
| class DevicesCommonSettings(SettingsBaseModel): | ||||
|     """Base configuration for devices simulation settings.""" | ||||
|  | ||||
|     batteries: Optional[list[BaseBatteryParameters]] = Field( | ||||
|         default=None, | ||||
|         description="List of battery/ev devices", | ||||
|         examples=[[{"device_id": "battery1", "capacity_wh": 8000}]], | ||||
|     ) | ||||
|     inverters: Optional[list[InverterParameters]] = Field( | ||||
|         default=None, description="List of inverters", examples=[[]] | ||||
|     ) | ||||
|     home_appliances: Optional[list[HomeApplianceParameters]] = Field( | ||||
|         default=None, description="List of home appliances", examples=[[]] | ||||
|     ) | ||||
		Reference in New Issue
	
	Block a user