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,3 +1,5 @@ | ||||
| """Genetic algorithm.""" | ||||
| 
 | ||||
| import random | ||||
| import time | ||||
| from typing import Any, Optional | ||||
| @@ -5,101 +7,302 @@ from typing import Any, Optional | ||||
| import numpy as np | ||||
| from deap import algorithms, base, creator, tools | ||||
| from loguru import logger | ||||
| from pydantic import Field, field_validator, model_validator | ||||
| from typing_extensions import Self | ||||
| from numpydantic import NDArray, Shape | ||||
| from pydantic import ConfigDict, Field | ||||
| 
 | ||||
| from akkudoktoreos.core.coreabc import ( | ||||
|     ConfigMixin, | ||||
|     DevicesMixin, | ||||
|     EnergyManagementSystemMixin, | ||||
| from akkudoktoreos.core.pydantic import PydanticBaseModel | ||||
| from akkudoktoreos.devices.genetic.battery import Battery | ||||
| from akkudoktoreos.devices.genetic.homeappliance import HomeAppliance | ||||
| from akkudoktoreos.devices.genetic.inverter import Inverter | ||||
| from akkudoktoreos.optimization.genetic.geneticparams import ( | ||||
|     GeneticEnergyManagementParameters, | ||||
|     GeneticOptimizationParameters, | ||||
| ) | ||||
| from akkudoktoreos.core.ems import EnergyManagementParameters, SimulationResult | ||||
| from akkudoktoreos.core.pydantic import ParametersBaseModel | ||||
| from akkudoktoreos.devices.battery import ( | ||||
|     Battery, | ||||
|     ElectricVehicleParameters, | ||||
|     ElectricVehicleResult, | ||||
|     SolarPanelBatteryParameters, | ||||
| from akkudoktoreos.optimization.genetic.geneticsolution import ( | ||||
|     GeneticSimulationResult, | ||||
|     GeneticSolution, | ||||
| ) | ||||
| from akkudoktoreos.devices.generic import HomeAppliance, HomeApplianceParameters | ||||
| from akkudoktoreos.devices.inverter import Inverter, InverterParameters | ||||
| from akkudoktoreos.utils.utils import NumpyEncoder | ||||
| from akkudoktoreos.optimization.optimizationabc import OptimizationBase | ||||
| 
 | ||||
| 
 | ||||
| class OptimizationParameters(ParametersBaseModel): | ||||
|     ems: EnergyManagementParameters | ||||
|     pv_akku: Optional[SolarPanelBatteryParameters] | ||||
|     inverter: Optional[InverterParameters] | ||||
|     eauto: Optional[ElectricVehicleParameters] | ||||
|     dishwasher: Optional[HomeApplianceParameters] = None | ||||
|     temperature_forecast: Optional[list[Optional[float]]] = Field( | ||||
| class GeneticSimulation(PydanticBaseModel): | ||||
|     """Device simulation for GENETIC optimization algorithm.""" | ||||
| 
 | ||||
|     # Disable validation on assignment to speed up simulation runs. | ||||
|     model_config = ConfigDict( | ||||
|         validate_assignment=False, | ||||
|     ) | ||||
| 
 | ||||
|     start_hour: int = Field( | ||||
|         default=0, ge=0, le=23, description="Starting hour on day for optimizations." | ||||
|     ) | ||||
| 
 | ||||
|     optimization_hours: Optional[int] = Field( | ||||
|         default=24, ge=0, description="Number of hours into the future for optimizations." | ||||
|     ) | ||||
| 
 | ||||
|     prediction_hours: Optional[int] = Field( | ||||
|         default=48, ge=0, description="Number of hours into the future for predictions" | ||||
|     ) | ||||
| 
 | ||||
|     load_energy_array: Optional[NDArray[Shape["*"], float]] = Field( | ||||
|         default=None, | ||||
|         description="An array of floats representing the temperature forecast in degrees Celsius for different time intervals.", | ||||
|         description="An array of floats representing the total load (consumption) in watts for different time intervals.", | ||||
|     ) | ||||
|     start_solution: Optional[list[float]] = Field( | ||||
|         default=None, description="Can be `null` or contain a previous solution (if available)." | ||||
|     ) | ||||
| 
 | ||||
|     @model_validator(mode="after") | ||||
|     def validate_list_length(self) -> Self: | ||||
|         arr_length = len(self.ems.pv_prognose_wh) | ||||
|         if self.temperature_forecast is not None and arr_length != len(self.temperature_forecast): | ||||
|             raise ValueError("Input lists have different lengths") | ||||
|         return self | ||||
| 
 | ||||
|     @field_validator("start_solution") | ||||
|     def validate_start_solution( | ||||
|         cls, start_solution: Optional[list[float]] | ||||
|     ) -> Optional[list[float]]: | ||||
|         if start_solution is not None and len(start_solution) < 2: | ||||
|             raise ValueError("Requires at least two values.") | ||||
|         return start_solution | ||||
| 
 | ||||
| 
 | ||||
| class OptimizeResponse(ParametersBaseModel): | ||||
|     """**Note**: The first value of "Last_Wh_per_hour", "Netzeinspeisung_Wh_per_hour", and "Netzbezug_Wh_per_hour", will be set to null in the JSON output and represented as NaN or None in the corresponding classes' data returns. This approach is adopted to ensure that the current hour's processing remains unchanged.""" | ||||
| 
 | ||||
|     ac_charge: list[float] = Field( | ||||
|         description="Array with AC charging values as relative power (0-1), other values set to 0." | ||||
|     ) | ||||
|     dc_charge: list[float] = Field( | ||||
|         description="Array with DC charging values as relative power (0-1), other values set to 0." | ||||
|     ) | ||||
|     discharge_allowed: list[int] = Field( | ||||
|         description="Array with discharge values (1 for discharge, 0 otherwise)." | ||||
|     ) | ||||
|     eautocharge_hours_float: Optional[list[float]] = Field(description="TBD") | ||||
|     result: SimulationResult | ||||
|     eauto_obj: Optional[ElectricVehicleResult] | ||||
|     start_solution: Optional[list[float]] = Field( | ||||
|     pv_prediction_wh: Optional[NDArray[Shape["*"], float]] = Field( | ||||
|         default=None, | ||||
|         description="An array of binary values (0 or 1) representing a possible starting solution for the simulation.", | ||||
|         description="An array of floats representing the forecasted photovoltaic output in watts for different time intervals.", | ||||
|     ) | ||||
|     washingstart: Optional[int] = Field( | ||||
|     elect_price_hourly: Optional[NDArray[Shape["*"], float]] = Field( | ||||
|         default=None, | ||||
|         description="Can be `null` or contain an object representing the start of washing (if applicable).", | ||||
|         description="An array of floats representing the electricity price in euros per watt-hour for different time intervals.", | ||||
|     ) | ||||
|     elect_revenue_per_hour_arr: Optional[NDArray[Shape["*"], float]] = Field( | ||||
|         default=None, | ||||
|         description="An array of floats representing the feed-in compensation in euros per watt-hour.", | ||||
|     ) | ||||
| 
 | ||||
|     @field_validator( | ||||
|         "ac_charge", | ||||
|         "dc_charge", | ||||
|         "discharge_allowed", | ||||
|         mode="before", | ||||
|     ) | ||||
|     def convert_numpy(cls, field: Any) -> Any: | ||||
|         return NumpyEncoder.convert_numpy(field)[0] | ||||
|     battery: Optional[Battery] = Field(default=None, description="TBD.") | ||||
|     ev: Optional[Battery] = Field(default=None, description="TBD.") | ||||
|     home_appliance: Optional[HomeAppliance] = Field(default=None, description="TBD.") | ||||
|     inverter: Optional[Inverter] = Field(default=None, description="TBD.") | ||||
| 
 | ||||
|     @field_validator( | ||||
|         "eauto_obj", | ||||
|         mode="before", | ||||
|     ) | ||||
|     def convert_eauto(cls, field: Any) -> Any: | ||||
|         if isinstance(field, Battery): | ||||
|             return ElectricVehicleResult(**field.to_dict()) | ||||
|         return field | ||||
|     ac_charge_hours: Optional[NDArray[Shape["*"], float]] = Field(default=None, description="TBD") | ||||
|     dc_charge_hours: Optional[NDArray[Shape["*"], float]] = Field(default=None, description="TBD") | ||||
|     ev_charge_hours: Optional[NDArray[Shape["*"], float]] = Field(default=None, description="TBD") | ||||
| 
 | ||||
|     def prepare( | ||||
|         self, | ||||
|         parameters: GeneticEnergyManagementParameters, | ||||
|         optimization_hours: int, | ||||
|         prediction_hours: int, | ||||
|         ev: Optional[Battery] = None, | ||||
|         home_appliance: Optional[HomeAppliance] = None, | ||||
|         inverter: Optional[Inverter] = None, | ||||
|     ) -> None: | ||||
|         self.optimization_hours = optimization_hours | ||||
|         self.prediction_hours = prediction_hours | ||||
|         self.load_energy_array = np.array(parameters.gesamtlast, float) | ||||
|         self.pv_prediction_wh = np.array(parameters.pv_prognose_wh, float) | ||||
|         self.elect_price_hourly = np.array(parameters.strompreis_euro_pro_wh, float) | ||||
|         self.elect_revenue_per_hour_arr = ( | ||||
|             parameters.einspeiseverguetung_euro_pro_wh | ||||
|             if isinstance(parameters.einspeiseverguetung_euro_pro_wh, list) | ||||
|             else np.full( | ||||
|                 len(self.load_energy_array), parameters.einspeiseverguetung_euro_pro_wh, float | ||||
|             ) | ||||
|         ) | ||||
|         if inverter: | ||||
|             self.battery = inverter.battery | ||||
|         else: | ||||
|             self.battery = None | ||||
|         self.ev = ev | ||||
|         self.home_appliance = home_appliance | ||||
|         self.inverter = inverter | ||||
|         self.ac_charge_hours = np.full(self.prediction_hours, 0.0) | ||||
|         self.dc_charge_hours = np.full(self.prediction_hours, 1.0) | ||||
|         self.ev_charge_hours = np.full(self.prediction_hours, 0.0) | ||||
|         """Prepare simulation runs.""" | ||||
|         self.load_energy_array = np.array(parameters.gesamtlast, float) | ||||
|         self.pv_prediction_wh = np.array(parameters.pv_prognose_wh, float) | ||||
|         self.elect_price_hourly = np.array(parameters.strompreis_euro_pro_wh, float) | ||||
|         self.elect_revenue_per_hour_arr = ( | ||||
|             parameters.einspeiseverguetung_euro_pro_wh | ||||
|             if isinstance(parameters.einspeiseverguetung_euro_pro_wh, list) | ||||
|             else np.full( | ||||
|                 len(self.load_energy_array), parameters.einspeiseverguetung_euro_pro_wh, float | ||||
|             ) | ||||
|         ) | ||||
| 
 | ||||
|     def set_akku_discharge_hours(self, ds: np.ndarray) -> None: | ||||
|         if self.battery: | ||||
|             self.battery.set_discharge_per_hour(ds) | ||||
| 
 | ||||
|     def set_akku_ac_charge_hours(self, ds: np.ndarray) -> None: | ||||
|         self.ac_charge_hours = ds | ||||
| 
 | ||||
|     def set_akku_dc_charge_hours(self, ds: np.ndarray) -> None: | ||||
|         self.dc_charge_hours = ds | ||||
| 
 | ||||
|     def set_ev_charge_hours(self, ds: np.ndarray) -> None: | ||||
|         self.ev_charge_hours = ds | ||||
| 
 | ||||
|     def set_home_appliance_start(self, ds: int, global_start_hour: int = 0) -> None: | ||||
|         if self.home_appliance: | ||||
|             self.home_appliance.set_starting_time(ds, global_start_hour=global_start_hour) | ||||
| 
 | ||||
|     def reset(self) -> None: | ||||
|         if self.ev: | ||||
|             self.ev.reset() | ||||
|         if self.battery: | ||||
|             self.battery.reset() | ||||
| 
 | ||||
|     def simulate(self, start_hour: int) -> dict[str, Any]: | ||||
|         """Simulate energy usage and costs for the given start hour. | ||||
| 
 | ||||
|         akku_soc_pro_stunde begin of the hour, initial hour state! | ||||
|         last_wh_pro_stunde integral of last hour (end state) | ||||
|         """ | ||||
|         # Remember start hour | ||||
|         self.start_hour = start_hour | ||||
| 
 | ||||
|         # Check for simulation integrity | ||||
|         required_attrs = [ | ||||
|             "load_energy_array", | ||||
|             "pv_prediction_wh", | ||||
|             "elect_price_hourly", | ||||
|             "ev_charge_hours", | ||||
|             "ac_charge_hours", | ||||
|             "dc_charge_hours", | ||||
|             "elect_revenue_per_hour_arr", | ||||
|         ] | ||||
|         missing_data = [ | ||||
|             attr.replace("_", " ").title() for attr in required_attrs if getattr(self, attr) is None | ||||
|         ] | ||||
| 
 | ||||
|         if missing_data: | ||||
|             logger.error("Mandatory data missing - %s", ", ".join(missing_data)) | ||||
|             raise ValueError(f"Mandatory data missing: {', '.join(missing_data)}") | ||||
| 
 | ||||
|         # Pre-fetch data | ||||
|         load_energy_array = np.array(self.load_energy_array) | ||||
|         pv_prediction_wh = np.array(self.pv_prediction_wh) | ||||
|         elect_price_hourly = np.array(self.elect_price_hourly) | ||||
|         ev_charge_hours = np.array(self.ev_charge_hours) | ||||
|         ac_charge_hours = np.array(self.ac_charge_hours) | ||||
|         dc_charge_hours = np.array(self.dc_charge_hours) | ||||
|         elect_revenue_per_hour_arr = np.array(self.elect_revenue_per_hour_arr) | ||||
| 
 | ||||
|         # Fetch objects | ||||
|         battery = self.battery | ||||
|         ev = self.ev | ||||
|         home_appliance = self.home_appliance | ||||
|         inverter = self.inverter | ||||
| 
 | ||||
|         if not (len(load_energy_array) == len(pv_prediction_wh) == len(elect_price_hourly)): | ||||
|             error_msg = f"Array sizes do not match: Load Curve = {len(load_energy_array)}, PV Forecast = {len(pv_prediction_wh)}, Electricity Price = {len(elect_price_hourly)}" | ||||
|             logger.error(error_msg) | ||||
|             raise ValueError(error_msg) | ||||
| 
 | ||||
|         end_hour = len(load_energy_array) | ||||
|         total_hours = end_hour - start_hour | ||||
| 
 | ||||
|         # Pre-allocate arrays for the results, optimized for speed | ||||
|         loads_energy_per_hour = np.full((total_hours), np.nan) | ||||
|         feedin_energy_per_hour = np.full((total_hours), np.nan) | ||||
|         consumption_energy_per_hour = np.full((total_hours), np.nan) | ||||
|         costs_per_hour = np.full((total_hours), np.nan) | ||||
|         revenue_per_hour = np.full((total_hours), np.nan) | ||||
|         soc_per_hour = np.full((total_hours), np.nan) | ||||
|         soc_ev_per_hour = np.full((total_hours), np.nan) | ||||
|         losses_wh_per_hour = np.full((total_hours), np.nan) | ||||
|         home_appliance_wh_per_hour = np.full((total_hours), np.nan) | ||||
|         electricity_price_per_hour = np.full((total_hours), np.nan) | ||||
| 
 | ||||
|         # Set initial state | ||||
|         if battery: | ||||
|             soc_per_hour[0] = battery.current_soc_percentage() | ||||
|         if ev: | ||||
|             soc_ev_per_hour[0] = ev.current_soc_percentage() | ||||
| 
 | ||||
|         for hour in range(start_hour, end_hour): | ||||
|             hour_idx = hour - start_hour | ||||
| 
 | ||||
|             # save begin states | ||||
|             if battery: | ||||
|                 soc_per_hour[hour_idx] = battery.current_soc_percentage() | ||||
|             if ev: | ||||
|                 soc_ev_per_hour[hour_idx] = ev.current_soc_percentage() | ||||
| 
 | ||||
|             # Accumulate loads and PV generation | ||||
|             consumption = load_energy_array[hour] | ||||
|             losses_wh_per_hour[hour_idx] = 0.0 | ||||
| 
 | ||||
|             # Home appliances | ||||
|             if home_appliance: | ||||
|                 ha_load = home_appliance.get_load_for_hour(hour) | ||||
|                 consumption += ha_load | ||||
|                 home_appliance_wh_per_hour[hour_idx] = ha_load | ||||
| 
 | ||||
|             # E-Auto handling | ||||
|             if ev and ev_charge_hours[hour] > 0: | ||||
|                 loaded_energy_ev, verluste_eauto = ev.charge_energy( | ||||
|                     None, hour, relative_power=ev_charge_hours[hour] | ||||
|                 ) | ||||
|                 consumption += loaded_energy_ev | ||||
|                 losses_wh_per_hour[hour_idx] += verluste_eauto | ||||
| 
 | ||||
|             # Process inverter logic | ||||
|             energy_feedin_grid_actual = energy_consumption_grid_actual = losses = eigenverbrauch = ( | ||||
|                 0.0 | ||||
|             ) | ||||
| 
 | ||||
|             hour_ac_charge = ac_charge_hours[hour] | ||||
|             hour_dc_charge = dc_charge_hours[hour] | ||||
|             hourly_electricity_price = elect_price_hourly[hour] | ||||
|             hourly_energy_revenue = elect_revenue_per_hour_arr[hour] | ||||
| 
 | ||||
|             if battery: | ||||
|                 battery.set_charge_allowed_for_hour(hour_dc_charge, hour) | ||||
| 
 | ||||
|             if inverter: | ||||
|                 energy_produced = pv_prediction_wh[hour] | ||||
|                 ( | ||||
|                     energy_feedin_grid_actual, | ||||
|                     energy_consumption_grid_actual, | ||||
|                     losses, | ||||
|                     eigenverbrauch, | ||||
|                 ) = inverter.process_energy(energy_produced, consumption, hour) | ||||
| 
 | ||||
|             # AC PV Battery Charge | ||||
|             if battery and hour_ac_charge > 0.0: | ||||
|                 battery.set_charge_allowed_for_hour(1, hour) | ||||
|                 battery_charged_energy_actual, battery_losses_actual = battery.charge_energy( | ||||
|                     None, hour, relative_power=hour_ac_charge | ||||
|                 ) | ||||
| 
 | ||||
|                 total_battery_energy = battery_charged_energy_actual + battery_losses_actual | ||||
|                 consumption += total_battery_energy | ||||
|                 energy_consumption_grid_actual += total_battery_energy | ||||
|                 losses_wh_per_hour[hour_idx] += battery_losses_actual | ||||
| 
 | ||||
|             # Update hourly arrays | ||||
|             feedin_energy_per_hour[hour_idx] = energy_feedin_grid_actual | ||||
|             consumption_energy_per_hour[hour_idx] = energy_consumption_grid_actual | ||||
|             losses_wh_per_hour[hour_idx] += losses | ||||
|             loads_energy_per_hour[hour_idx] = consumption | ||||
|             electricity_price_per_hour[hour_idx] = hourly_electricity_price | ||||
| 
 | ||||
|             # Financial calculations | ||||
|             costs_per_hour[hour_idx] = energy_consumption_grid_actual * hourly_electricity_price | ||||
|             revenue_per_hour[hour_idx] = energy_feedin_grid_actual * hourly_energy_revenue | ||||
| 
 | ||||
|         total_cost = np.nansum(costs_per_hour) | ||||
|         total_losses = np.nansum(losses_wh_per_hour) | ||||
|         total_revenue = np.nansum(revenue_per_hour) | ||||
| 
 | ||||
|         # Prepare output dictionary | ||||
|         return { | ||||
|             "Last_Wh_pro_Stunde": loads_energy_per_hour, | ||||
|             "Netzeinspeisung_Wh_pro_Stunde": feedin_energy_per_hour, | ||||
|             "Netzbezug_Wh_pro_Stunde": consumption_energy_per_hour, | ||||
|             "Kosten_Euro_pro_Stunde": costs_per_hour, | ||||
|             "akku_soc_pro_stunde": soc_per_hour, | ||||
|             "Einnahmen_Euro_pro_Stunde": revenue_per_hour, | ||||
|             "Gesamtbilanz_Euro": total_cost - total_revenue, | ||||
|             "EAuto_SoC_pro_Stunde": soc_ev_per_hour, | ||||
|             "Gesamteinnahmen_Euro": total_revenue, | ||||
|             "Gesamtkosten_Euro": total_cost, | ||||
|             "Verluste_Pro_Stunde": losses_wh_per_hour, | ||||
|             "Gesamt_Verluste": total_losses, | ||||
|             "Home_appliance_wh_per_hour": home_appliance_wh_per_hour, | ||||
|             "Electricity_price": electricity_price_per_hour, | ||||
|         } | ||||
| 
 | ||||
| 
 | ||||
| class optimization_problem(ConfigMixin, DevicesMixin, EnergyManagementSystemMixin): | ||||
| class GeneticOptimization(OptimizationBase): | ||||
|     """GENETIC algorithm to solve energy optimization.""" | ||||
| 
 | ||||
|     def __init__( | ||||
|         self, | ||||
|         verbose: bool = False, | ||||
| @@ -107,8 +310,10 @@ class optimization_problem(ConfigMixin, DevicesMixin, EnergyManagementSystemMixi | ||||
|     ): | ||||
|         """Initialize the optimization problem with the required parameters.""" | ||||
|         self.opti_param: dict[str, Any] = {} | ||||
|         self.fixed_eauto_hours = self.config.prediction.hours - self.config.optimization.hours | ||||
|         self.possible_charge_values = self.config.optimization.ev_available_charge_rates_percent | ||||
|         self.fixed_eauto_hours = ( | ||||
|             self.config.prediction.hours - self.config.optimization.horizon_hours | ||||
|         ) | ||||
|         self.ev_possible_charge_values: list[float] = [1.0] | ||||
|         self.verbose = verbose | ||||
|         self.fix_seed = fixed_seed | ||||
|         self.optimize_ev = True | ||||
| @@ -122,12 +327,15 @@ class optimization_problem(ConfigMixin, DevicesMixin, EnergyManagementSystemMixi | ||||
|             self.fix_seed = random.randint(1, 100000000000)  # noqa: S311 | ||||
|             random.seed(self.fix_seed) | ||||
| 
 | ||||
|         # Create Simulation | ||||
|         self.simulation = GeneticSimulation() | ||||
| 
 | ||||
|     def decode_charge_discharge( | ||||
|         self, discharge_hours_bin: np.ndarray | ||||
|     ) -> tuple[np.ndarray, np.ndarray, np.ndarray]: | ||||
|         """Decode the input array into ac_charge, dc_charge, and discharge arrays.""" | ||||
|         discharge_hours_bin_np = np.array(discharge_hours_bin) | ||||
|         len_ac = len(self.possible_charge_values) | ||||
|         len_ac = len(self.ev_possible_charge_values) | ||||
| 
 | ||||
|         # Categorization: | ||||
|         # Idle:       0 .. len_ac-1 | ||||
| @@ -159,7 +367,7 @@ class optimization_problem(ConfigMixin, DevicesMixin, EnergyManagementSystemMixi | ||||
|         discharge[discharge_mask] = 1  # Set Discharge states to 1 | ||||
| 
 | ||||
|         ac_charge = np.zeros_like(discharge_hours_bin_np, dtype=float) | ||||
|         ac_charge[ac_mask] = [self.possible_charge_values[i] for i in ac_indices] | ||||
|         ac_charge[ac_mask] = [self.ev_possible_charge_values[i] for i in ac_indices] | ||||
| 
 | ||||
|         # Idle is just 0, already default. | ||||
| 
 | ||||
| @@ -168,7 +376,7 @@ class optimization_problem(ConfigMixin, DevicesMixin, EnergyManagementSystemMixi | ||||
|     def mutate(self, individual: list[int]) -> tuple[list[int]]: | ||||
|         """Custom mutation function for the individual.""" | ||||
|         # Calculate the number of states | ||||
|         len_ac = len(self.possible_charge_values) | ||||
|         len_ac = len(self.ev_possible_charge_values) | ||||
|         if self.optimize_dc_charge: | ||||
|             total_states = 3 * len_ac + 2 | ||||
|         else: | ||||
| @@ -303,7 +511,7 @@ class optimization_problem(ConfigMixin, DevicesMixin, EnergyManagementSystemMixi | ||||
|         creator.create("Individual", list, fitness=creator.FitnessMin) | ||||
| 
 | ||||
|         self.toolbox = base.Toolbox() | ||||
|         len_ac = len(self.possible_charge_values) | ||||
|         len_ac = len(self.ev_possible_charge_values) | ||||
| 
 | ||||
|         # Total number of states without DC: | ||||
|         # Idle: len_ac states | ||||
| @@ -362,38 +570,39 @@ class optimization_problem(ConfigMixin, DevicesMixin, EnergyManagementSystemMixi | ||||
| 
 | ||||
|         This is an internal function. | ||||
|         """ | ||||
|         self.ems.reset() | ||||
|         self.simulation.reset() | ||||
|         discharge_hours_bin, eautocharge_hours_index, washingstart_int = self.split_individual( | ||||
|             individual | ||||
|         ) | ||||
|         if self.opti_param.get("home_appliance", 0) > 0: | ||||
|             self.ems.set_home_appliance_start( | ||||
|         if self.opti_param.get("home_appliance", 0) > 0 and washingstart_int: | ||||
|             self.simulation.set_home_appliance_start( | ||||
|                 washingstart_int, global_start_hour=self.ems.start_datetime.hour | ||||
|             ) | ||||
| 
 | ||||
|         ac, dc, discharge = self.decode_charge_discharge(discharge_hours_bin) | ||||
| 
 | ||||
|         self.ems.set_akku_discharge_hours(discharge) | ||||
|         self.simulation.set_akku_discharge_hours(discharge) | ||||
|         # Set DC charge hours only if DC optimization is enabled | ||||
|         if self.optimize_dc_charge: | ||||
|             self.ems.set_akku_dc_charge_hours(dc) | ||||
|         self.ems.set_akku_ac_charge_hours(ac) | ||||
|             self.simulation.set_akku_dc_charge_hours(dc) | ||||
|         self.simulation.set_akku_ac_charge_hours(ac) | ||||
| 
 | ||||
|         if eautocharge_hours_index is not None: | ||||
|             eautocharge_hours_float = np.array( | ||||
|                 [self.possible_charge_values[i] for i in eautocharge_hours_index], | ||||
|                 [self.ev_possible_charge_values[i] for i in eautocharge_hours_index], | ||||
|                 float, | ||||
|             ) | ||||
|             self.ems.set_ev_charge_hours(eautocharge_hours_float) | ||||
|             self.simulation.set_ev_charge_hours(eautocharge_hours_float) | ||||
|         else: | ||||
|             self.ems.set_ev_charge_hours(np.full(self.config.prediction.hours, 0)) | ||||
|             self.simulation.set_ev_charge_hours(np.full(self.config.prediction.hours, 0)) | ||||
| 
 | ||||
|         return self.ems.simulate(self.ems.start_datetime.hour) | ||||
|         # Do the simulation and return result. | ||||
|         return self.simulation.simulate(self.ems.start_datetime.hour) | ||||
| 
 | ||||
|     def evaluate( | ||||
|         self, | ||||
|         individual: list[int], | ||||
|         parameters: OptimizationParameters, | ||||
|         parameters: GeneticOptimizationParameters, | ||||
|         start_hour: int, | ||||
|         worst_case: bool, | ||||
|     ) -> tuple[float]: | ||||
| @@ -456,7 +665,8 @@ class optimization_problem(ConfigMixin, DevicesMixin, EnergyManagementSystemMixi | ||||
|         #     #     len_ac + 2 | ||||
|         #     # )  # Activate discharge for these hours | ||||
| 
 | ||||
|         #     # When Battery SoC then set the Discharge randomly to 0 or 1. otherwise it's very unlikely to get a state where a battery can store energy for a longer time | ||||
|         #     # When Battery SoC then set the Discharge randomly to 0 or 1. otherwise it's very | ||||
|         #     # unlikely to get a state where a battery can store energy for a longer time | ||||
|         #     # Find hours where battery SoC is 0 | ||||
|         #     zero_soc_mask = battery_soc_per_hour_tail == 0 | ||||
|         #     # discharge_hours_bin_tail[zero_soc_mask] = ( | ||||
| @@ -478,43 +688,67 @@ class optimization_problem(ConfigMixin, DevicesMixin, EnergyManagementSystemMixi | ||||
|         individual.extra_data = (  # type: ignore[attr-defined] | ||||
|             o["Gesamtbilanz_Euro"], | ||||
|             o["Gesamt_Verluste"], | ||||
|             parameters.eauto.min_soc_percentage - self.ems.ev.current_soc_percentage() | ||||
|             if parameters.eauto and self.ems.ev | ||||
|             parameters.eauto.min_soc_percentage - self.simulation.ev.current_soc_percentage() | ||||
|             if parameters.eauto and self.simulation.ev | ||||
|             else 0, | ||||
|         ) | ||||
| 
 | ||||
|         # Adjust total balance with battery value and penalties for unmet SOC | ||||
|         restwert_akku = ( | ||||
|             self.ems.battery.current_energy_content() * parameters.ems.preis_euro_pro_wh_akku | ||||
|         ) | ||||
|         gesamtbilanz += -restwert_akku | ||||
|         if self.simulation.battery: | ||||
|             restwert_akku = ( | ||||
|                 self.simulation.battery.current_energy_content() | ||||
|                 * parameters.ems.preis_euro_pro_wh_akku | ||||
|             ) | ||||
|             gesamtbilanz += -restwert_akku | ||||
| 
 | ||||
|         if self.optimize_ev: | ||||
|             try: | ||||
|                 penalty = self.config.optimization.genetic.penalties["ev_soc_miss"] | ||||
|             except: | ||||
|                 # Use default | ||||
|                 penalty = 10 | ||||
|                 logger.error( | ||||
|                     "Penalty function parameter `ev_soc_miss` not configured, using {}.", penalty | ||||
|                 ) | ||||
|             gesamtbilanz += max( | ||||
|                 0, | ||||
|                 ( | ||||
|                     parameters.eauto.min_soc_percentage - self.ems.ev.current_soc_percentage() | ||||
|                     if parameters.eauto and self.ems.ev | ||||
|                     parameters.eauto.min_soc_percentage | ||||
|                     - self.simulation.ev.current_soc_percentage() | ||||
|                     if parameters.eauto and self.simulation.ev | ||||
|                     else 0 | ||||
|                 ) | ||||
|                 * self.config.optimization.penalty, | ||||
|                 * penalty, | ||||
|             ) | ||||
| 
 | ||||
|         return (gesamtbilanz,) | ||||
| 
 | ||||
|     def optimize( | ||||
|         self, start_solution: Optional[list[float]] = None, ngen: int = 200 | ||||
|         self, | ||||
|         start_solution: Optional[list[float]] = None, | ||||
|         ngen: int = 200, | ||||
|     ) -> tuple[Any, dict[str, list[Any]]]: | ||||
|         """Run the optimization process using a genetic algorithm.""" | ||||
|         population = self.toolbox.population(n=300) | ||||
|         """Run the optimization process using a genetic algorithm. | ||||
| 
 | ||||
|         @TODO: optimize() ngen default (200) is different from optimierung_ems() ngen default (400). | ||||
|         """ | ||||
|         # Set the number of inviduals in a generation | ||||
|         try: | ||||
|             individuals = self.config.optimization.genetic.individuals | ||||
|             if individuals is None: | ||||
|                 raise | ||||
|         except: | ||||
|             individuals = 300 | ||||
|             logger.error("Individuals not configured. Using {}.", individuals) | ||||
| 
 | ||||
|         population = self.toolbox.population(n=individuals) | ||||
|         hof = tools.HallOfFame(1) | ||||
|         stats = tools.Statistics(lambda ind: ind.fitness.values) | ||||
|         stats.register("min", np.min) | ||||
|         stats.register("avg", np.mean) | ||||
|         stats.register("max", np.max) | ||||
| 
 | ||||
|         if self.verbose: | ||||
|             print("Start optimize:", start_solution) | ||||
|         logger.debug("Start optimize: {}", start_solution) | ||||
| 
 | ||||
|         # Insert the start solution into the population if provided | ||||
|         if start_solution is not None: | ||||
| @@ -555,39 +789,63 @@ class optimization_problem(ConfigMixin, DevicesMixin, EnergyManagementSystemMixi | ||||
| 
 | ||||
|     def optimierung_ems( | ||||
|         self, | ||||
|         parameters: OptimizationParameters, | ||||
|         parameters: GeneticOptimizationParameters, | ||||
|         start_hour: Optional[int] = None, | ||||
|         worst_case: bool = False, | ||||
|         ngen: int = 400, | ||||
|     ) -> OptimizeResponse: | ||||
|         ngen: Optional[int] = None, | ||||
|     ) -> GeneticSolution: | ||||
|         """Perform EMS (Energy Management System) optimization and visualize results.""" | ||||
|         if start_hour is None: | ||||
|             start_hour = self.ems.start_datetime.hour | ||||
|         # Start hour has to be in sync with energy management | ||||
|         if start_hour != self.ems.start_datetime.hour: | ||||
|             raise ValueError( | ||||
|                 f"Start hour not synced. EMS {self.ems.start_datetime.hour} vs. GENETIC {start_hour}." | ||||
|             ) | ||||
| 
 | ||||
|         # Set the number of generations | ||||
|         generations = ngen | ||||
|         if generations is None: | ||||
|             try: | ||||
|                 generations = self.config.optimization.genetic.generations | ||||
|             except: | ||||
|                 generations = 400 | ||||
|                 logger.error("Generations not configured. Using {}.", generations) | ||||
| 
 | ||||
|         einspeiseverguetung_euro_pro_wh = np.full( | ||||
|             self.config.prediction.hours, parameters.ems.einspeiseverguetung_euro_pro_wh | ||||
|         ) | ||||
| 
 | ||||
|         # TODO: Refactor device setup phase out | ||||
|         self.devices.reset() | ||||
|         self.simulation.reset() | ||||
| 
 | ||||
|         # Initialize PV and EV batteries | ||||
|         akku: Optional[Battery] = None | ||||
|         if parameters.pv_akku: | ||||
|             akku = Battery(parameters.pv_akku) | ||||
|             self.devices.add_device(akku) | ||||
|             akku = Battery( | ||||
|                 parameters.pv_akku, | ||||
|                 prediction_hours=self.config.prediction.hours, | ||||
|             ) | ||||
|             akku.set_charge_per_hour(np.full(self.config.prediction.hours, 1)) | ||||
| 
 | ||||
|         eauto: Optional[Battery] = None | ||||
|         if parameters.eauto: | ||||
|             eauto = Battery( | ||||
|                 parameters.eauto, | ||||
|                 prediction_hours=self.config.prediction.hours, | ||||
|             ) | ||||
|             self.devices.add_device(eauto) | ||||
|             eauto.set_charge_per_hour(np.full(self.config.prediction.hours, 1)) | ||||
|             self.optimize_ev = ( | ||||
|                 parameters.eauto.min_soc_percentage - parameters.eauto.initial_soc_percentage >= 0 | ||||
|             ) | ||||
|             try: | ||||
|                 charge_rates = self.config.devices.electric_vehicles[0].charge_rates | ||||
|                 if charge_rates is None: | ||||
|                     raise | ||||
|             except: | ||||
|                 error_msg = "No charge rates provided for electric vehicle." | ||||
|                 logger.exception(error_msg) | ||||
|                 raise ValueError(error_msg) | ||||
|             self.ev_possible_charge_values = charge_rates | ||||
|         else: | ||||
|             self.optimize_ev = False | ||||
| 
 | ||||
| @@ -595,29 +853,30 @@ class optimization_problem(ConfigMixin, DevicesMixin, EnergyManagementSystemMixi | ||||
|         dishwasher = ( | ||||
|             HomeAppliance( | ||||
|                 parameters=parameters.dishwasher, | ||||
|                 optimization_hours=self.config.optimization.horizon_hours, | ||||
|                 prediction_hours=self.config.prediction.hours, | ||||
|             ) | ||||
|             if parameters.dishwasher is not None | ||||
|             else None | ||||
|         ) | ||||
|         self.devices.add_device(dishwasher) | ||||
| 
 | ||||
|         # Initialize the inverter and energy management system | ||||
|         inverter: Optional[Inverter] = None | ||||
|         if parameters.inverter: | ||||
|             inverter = Inverter( | ||||
|                 parameters.inverter, | ||||
|                 battery=akku, | ||||
|             ) | ||||
|             self.devices.add_device(inverter) | ||||
| 
 | ||||
|         self.devices.post_setup() | ||||
| 
 | ||||
|         self.ems.set_parameters( | ||||
|             parameters.ems, | ||||
|             inverter=inverter, | ||||
|         # Prepare device simulation | ||||
|         self.simulation.prepare( | ||||
|             parameters=parameters.ems, | ||||
|             optimization_hours=self.config.optimization.horizon_hours, | ||||
|             prediction_hours=self.config.prediction.hours, | ||||
|             inverter=inverter,  # battery is part of inverter | ||||
|             ev=eauto, | ||||
|             home_appliance=dishwasher, | ||||
|         ) | ||||
|         self.ems.set_start_hour(start_hour) | ||||
| 
 | ||||
|         # Setup the DEAP environment and optimization process | ||||
|         self.setup_deap_environment({"home_appliance": 1 if dishwasher else 0}, start_hour) | ||||
| @@ -626,20 +885,24 @@ class optimization_problem(ConfigMixin, DevicesMixin, EnergyManagementSystemMixi | ||||
|             lambda ind: self.evaluate(ind, parameters, start_hour, worst_case), | ||||
|         ) | ||||
| 
 | ||||
|         if self.verbose: | ||||
|             start_time = time.time() | ||||
|         start_solution, extra_data = self.optimize(parameters.start_solution, ngen=ngen) | ||||
|         start_time = time.time() | ||||
|         start_solution, extra_data = self.optimize(parameters.start_solution, ngen=generations) | ||||
|         elapsed_time = time.time() - start_time | ||||
|         logger.debug(f"Time evaluate inner: {elapsed_time:.4f} sec.") | ||||
| 
 | ||||
|         if self.verbose: | ||||
|             elapsed_time = time.time() - start_time | ||||
|             print(f"Time evaluate inner: {elapsed_time:.4f} sec.") | ||||
|         # Perform final evaluation on the best solution | ||||
|         o = self.evaluate_inner(start_solution) | ||||
|         simulation_result = self.evaluate_inner(start_solution) | ||||
| 
 | ||||
|         # Prepare results | ||||
|         discharge_hours_bin, eautocharge_hours_index, washingstart_int = self.split_individual( | ||||
|             start_solution | ||||
|         ) | ||||
|         # home appliance may have choosen a different appliance start hour | ||||
|         if self.simulation.home_appliance: | ||||
|             washingstart_int = self.simulation.home_appliance.get_appliance_start() | ||||
| 
 | ||||
|         eautocharge_hours_float = ( | ||||
|             [self.possible_charge_values[i] for i in eautocharge_hours_index] | ||||
|             [self.ev_possible_charge_values[i] for i in eautocharge_hours_index] | ||||
|             if eautocharge_hours_index is not None | ||||
|             else None | ||||
|         ) | ||||
| @@ -651,8 +914,8 @@ class optimization_problem(ConfigMixin, DevicesMixin, EnergyManagementSystemMixi | ||||
|             "dc_charge": dc_charge.tolist(), | ||||
|             "discharge_allowed": discharge.tolist(), | ||||
|             "eautocharge_hours_float": eautocharge_hours_float, | ||||
|             "result": o, | ||||
|             "eauto_obj": self.ems.ev.to_dict(), | ||||
|             "result": simulation_result, | ||||
|             "eauto_obj": self.simulation.ev.to_dict() if self.simulation.ev else None, | ||||
|             "start_solution": start_solution, | ||||
|             "spuelstart": washingstart_int, | ||||
|             "extra_data": extra_data, | ||||
| @@ -663,14 +926,14 @@ class optimization_problem(ConfigMixin, DevicesMixin, EnergyManagementSystemMixi | ||||
| 
 | ||||
|         prepare_visualize(parameters, visualize, start_hour=start_hour) | ||||
| 
 | ||||
|         return OptimizeResponse( | ||||
|         return GeneticSolution( | ||||
|             **{ | ||||
|                 "ac_charge": ac_charge, | ||||
|                 "dc_charge": dc_charge, | ||||
|                 "discharge_allowed": discharge, | ||||
|                 "eautocharge_hours_float": eautocharge_hours_float, | ||||
|                 "result": SimulationResult(**o), | ||||
|                 "eauto_obj": self.ems.ev, | ||||
|                 "result": GeneticSimulationResult(**simulation_result), | ||||
|                 "eauto_obj": self.simulation.ev, | ||||
|                 "start_solution": start_solution, | ||||
|                 "washingstart": washingstart_int, | ||||
|             } | ||||
							
								
								
									
										11
									
								
								src/akkudoktoreos/optimization/genetic/geneticabc.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								src/akkudoktoreos/optimization/genetic/geneticabc.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| """Genetic optimization algorithm abstract and base classes.""" | ||||
|  | ||||
| from pydantic import ConfigDict | ||||
|  | ||||
| from akkudoktoreos.core.pydantic import PydanticBaseModel | ||||
|  | ||||
|  | ||||
| class GeneticParametersBaseModel(PydanticBaseModel): | ||||
|     """Pydantic base model for parameters for the GENETIC algorithm.""" | ||||
|  | ||||
|     model_config = ConfigDict(extra="forbid") | ||||
							
								
								
									
										127
									
								
								src/akkudoktoreos/optimization/genetic/geneticdevices.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										127
									
								
								src/akkudoktoreos/optimization/genetic/geneticdevices.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,127 @@ | ||||
| """Genetic optimization algorithm device interfaces/ parameters.""" | ||||
|  | ||||
| from typing import Optional | ||||
|  | ||||
| from pydantic import Field | ||||
|  | ||||
| from akkudoktoreos.optimization.genetic.geneticabc import GeneticParametersBaseModel | ||||
| from akkudoktoreos.utils.datetimeutil import TimeWindowSequence | ||||
|  | ||||
|  | ||||
| class DeviceParameters(GeneticParametersBaseModel): | ||||
|     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], | ||||
|     ) | ||||
|  | ||||
|  | ||||
| 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): | ||||
|     """PV battery device simulation configuration.""" | ||||
|  | ||||
|     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 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], | ||||
|     ) | ||||
|     time_windows: Optional[TimeWindowSequence] = Field( | ||||
|         default=None, | ||||
|         description="List of allowed time windows. Defaults to optimization general time window.", | ||||
|         examples=[ | ||||
|             [ | ||||
|                 {"start_time": "10:00", "duration": "2 hours"}, | ||||
|             ], | ||||
|         ], | ||||
|     ) | ||||
|  | ||||
|  | ||||
| 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"] | ||||
|     ) | ||||
							
								
								
									
										630
									
								
								src/akkudoktoreos/optimization/genetic/geneticparams.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										630
									
								
								src/akkudoktoreos/optimization/genetic/geneticparams.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,630 @@ | ||||
| """GENETIC algorithm paramters. | ||||
|  | ||||
| This module defines the Pydantic-based configuration and input parameter models | ||||
| used in the energy optimization routines, including photovoltaic forecasts, | ||||
| electricity pricing, and system component parameters. | ||||
|  | ||||
| It also provides a method to assemble these parameters from predictions, | ||||
| forecasts, and fallback defaults, preparing them for optimization runs. | ||||
| """ | ||||
|  | ||||
| from typing import Optional, Union | ||||
|  | ||||
| from loguru import logger | ||||
| from pydantic import Field, field_validator, model_validator | ||||
| from typing_extensions import Self | ||||
|  | ||||
| from akkudoktoreos.core.coreabc import ( | ||||
|     ConfigMixin, | ||||
|     MeasurementMixin, | ||||
|     PredictionMixin, | ||||
| ) | ||||
| from akkudoktoreos.optimization.genetic.geneticabc import GeneticParametersBaseModel | ||||
| from akkudoktoreos.optimization.genetic.geneticdevices import ( | ||||
|     ElectricVehicleParameters, | ||||
|     HomeApplianceParameters, | ||||
|     InverterParameters, | ||||
|     SolarPanelBatteryParameters, | ||||
| ) | ||||
| from akkudoktoreos.utils.datetimeutil import to_duration | ||||
|  | ||||
| # Do not import directly from akkudoktoreos.core.coreabc | ||||
| # EnergyManagementSystemMixin - Creates circular dependency with ems.py | ||||
| # StartMixin                  - Creates circular dependency with ems.py | ||||
|  | ||||
|  | ||||
| class GeneticEnergyManagementParameters(GeneticParametersBaseModel): | ||||
|     """Encapsulates energy-related forecasts and costs used in GENETIC optimization.""" | ||||
|  | ||||
|     pv_prognose_wh: list[float] = Field( | ||||
|         description="An array of floats representing the forecasted photovoltaic output in watts for different time intervals." | ||||
|     ) | ||||
|     strompreis_euro_pro_wh: list[float] = Field( | ||||
|         description="An array of floats representing the electricity price in euros per watt-hour for different time intervals." | ||||
|     ) | ||||
|     einspeiseverguetung_euro_pro_wh: Union[list[float], float] = Field( | ||||
|         description="A float or array of floats representing the feed-in compensation in euros per watt-hour." | ||||
|     ) | ||||
|     preis_euro_pro_wh_akku: float = Field( | ||||
|         description="A float representing the cost of battery energy per watt-hour." | ||||
|     ) | ||||
|     gesamtlast: list[float] = Field( | ||||
|         description="An array of floats representing the total load (consumption) in watts for different time intervals." | ||||
|     ) | ||||
|  | ||||
|     @model_validator(mode="after") | ||||
|     def validate_list_length(self) -> Self: | ||||
|         """Validate that all input lists are of the same length. | ||||
|  | ||||
|         Raises: | ||||
|             ValueError: If input list lengths differ. | ||||
|         """ | ||||
|         pv_prognose_length = len(self.pv_prognose_wh) | ||||
|         if ( | ||||
|             pv_prognose_length != len(self.strompreis_euro_pro_wh) | ||||
|             or pv_prognose_length != len(self.gesamtlast) | ||||
|             or ( | ||||
|                 isinstance(self.einspeiseverguetung_euro_pro_wh, list) | ||||
|                 and pv_prognose_length != len(self.einspeiseverguetung_euro_pro_wh) | ||||
|             ) | ||||
|         ): | ||||
|             raise ValueError("Input lists have different lengths") | ||||
|         return self | ||||
|  | ||||
|  | ||||
| class GeneticOptimizationParameters( | ||||
|     ConfigMixin, | ||||
|     MeasurementMixin, | ||||
|     PredictionMixin, | ||||
|     # EnergyManagementSystemMixin, # Creates circular dependency with ems.py | ||||
|     # StartMixin,                  # Creates circular dependency with ems.py | ||||
|     GeneticParametersBaseModel, | ||||
| ): | ||||
|     """Main parameter class for running the genetic energy optimization. | ||||
|  | ||||
|     Collects all model and configuration parameters necessary to run the | ||||
|     optimization process, such as forecasts, pricing, battery and appliance models. | ||||
|     """ | ||||
|  | ||||
|     ems: GeneticEnergyManagementParameters | ||||
|     pv_akku: Optional[SolarPanelBatteryParameters] | ||||
|     inverter: Optional[InverterParameters] | ||||
|     eauto: Optional[ElectricVehicleParameters] | ||||
|     dishwasher: Optional[HomeApplianceParameters] = None | ||||
|     temperature_forecast: Optional[list[Optional[float]]] = Field( | ||||
|         default=None, | ||||
|         description="An array of floats representing the temperature forecast in degrees Celsius for different time intervals.", | ||||
|     ) | ||||
|     start_solution: Optional[list[float]] = Field( | ||||
|         default=None, description="Can be `null` or contain a previous solution (if available)." | ||||
|     ) | ||||
|  | ||||
|     @model_validator(mode="after") | ||||
|     def validate_list_length(self) -> Self: | ||||
|         """Ensure that temperature forecast list matches the PV forecast length. | ||||
|  | ||||
|         Raises: | ||||
|             ValueError: If list lengths mismatch. | ||||
|         """ | ||||
|         arr_length = len(self.ems.pv_prognose_wh) | ||||
|         if self.temperature_forecast is not None and arr_length != len(self.temperature_forecast): | ||||
|             raise ValueError("Input lists have different lengths") | ||||
|         return self | ||||
|  | ||||
|     @field_validator("start_solution") | ||||
|     def validate_start_solution( | ||||
|         cls, start_solution: Optional[list[float]] | ||||
|     ) -> Optional[list[float]]: | ||||
|         """Validate that the starting solution has at least two elements. | ||||
|  | ||||
|         Args: | ||||
|             start_solution (list[float]): Optional list of solution values. | ||||
|  | ||||
|         Returns: | ||||
|             list[float]: Validated list. | ||||
|  | ||||
|         Raises: | ||||
|             ValueError: If the solution is too short. | ||||
|         """ | ||||
|         if start_solution is not None and len(start_solution) < 2: | ||||
|             raise ValueError("Requires at least two values.") | ||||
|         return start_solution | ||||
|  | ||||
|     @classmethod | ||||
|     def prepare(cls) -> "Optional[GeneticOptimizationParameters]": | ||||
|         """Prepare optimization parameters from config, forecast and measurement data. | ||||
|  | ||||
|         Fills in values needed for optimization from available configuration, predictions and | ||||
|         measurements. If some data is missing, default or demo values are used. | ||||
|  | ||||
|         Parameters start by definition of the genetic algorithm at hour 0 of the actual date | ||||
|         (not at start datetime of energy management run) | ||||
|  | ||||
|         Returns: | ||||
|             GeneticOptimizationParameters: The fully prepared optimization parameters. | ||||
|  | ||||
|         Raises: | ||||
|             ValueError: If required configuration values like start time are missing. | ||||
|         """ | ||||
|         # Avoid circular dependency | ||||
|         from akkudoktoreos.core.ems import get_ems | ||||
|  | ||||
|         ems = get_ems() | ||||
|  | ||||
|         # The optimization paramters | ||||
|         oparams: "Optional[GeneticOptimizationParameters]" = None | ||||
|  | ||||
|         # Check for run definitions | ||||
|         if ems.start_datetime is None: | ||||
|             error_msg = "Start datetime unknown." | ||||
|             logger.error(error_msg) | ||||
|             raise ValueError(error_msg) | ||||
|         # Check for general predictions conditions | ||||
|         if cls.config.general.latitude is None: | ||||
|             default_latitude = 52.52 | ||||
|             logger.error(f"Latitude unknown - defaulting to {default_latitude}.") | ||||
|             cls.config.general.latitude = default_latitude | ||||
|         if cls.config.general.longitude is None: | ||||
|             default_longitude = 13.405 | ||||
|             logger.error(f"Longitude unknown - defaulting to {default_longitude}.") | ||||
|             cls.config.general.longitude = default_longitude | ||||
|         if cls.config.prediction.hours is None: | ||||
|             logger.error("Prediction hours unknown - defaulting to 48 hours.") | ||||
|             cls.config.prediction.hours = 48 | ||||
|         if cls.config.prediction.historic_hours is None: | ||||
|             logger.error("Prediction historic hours unknown - defaulting to 24 hours.") | ||||
|             cls.config.prediction.historic_hours = 24 | ||||
|         # Check optimization definitions | ||||
|         if cls.config.optimization.horizon_hours is None: | ||||
|             logger.error("Optimization horizon unknown - defaulting to 24 hours.") | ||||
|             cls.config.optimization.horizon_hours = 24 | ||||
|         if cls.config.optimization.interval is None: | ||||
|             logger.error("Optimization interval unknown - defaulting to 3600 seconds.") | ||||
|             cls.config.optimization.interval = 3600 | ||||
|         if cls.config.optimization.interval != 3600: | ||||
|             logger.error( | ||||
|                 "Optimization interval '{}' seconds not supported - forced to 3600 seconds." | ||||
|             ) | ||||
|             cls.config.optimization.interval = 3600 | ||||
|         # Check genetic algorithm definitions | ||||
|         if cls.config.optimization.genetic is None: | ||||
|             logger.error( | ||||
|                 "Genetic optimization configuration not configured - defaulting to demo config." | ||||
|             ) | ||||
|             cls.config.optimization.genetic = { | ||||
|                 "individuals": 300, | ||||
|                 "generations": 400, | ||||
|                 "seed": None, | ||||
|                 "penalties": { | ||||
|                     "ev_soc_miss": 10, | ||||
|                 }, | ||||
|             } | ||||
|         if cls.config.optimization.genetic.individuals is None: | ||||
|             logger.error("Genetic individuals unknown - defaulting to 300.") | ||||
|             cls.config.optimization.genetic.individuals = 300 | ||||
|         if cls.config.optimization.genetic.generations is None: | ||||
|             logger.error("Genetic generations unknown - defaulting to 400.") | ||||
|             cls.config.optimization.genetic.generations = 400 | ||||
|         if cls.config.optimization.genetic.penalties is None: | ||||
|             logger.error("Genetic penalties unknown - defaulting to demo config.") | ||||
|             cls.config.optimization.genetic.penalties = {"ev_soc_miss": 10} | ||||
|         if "ev_soc_miss" not in cls.config.optimization.genetic.penalties: | ||||
|             logger.error("ev_soc_miss penalty function parameter unknown - defaulting to 100.") | ||||
|             cls.config.optimization.genetic.penalties["ev_soc_miss"] = 10 | ||||
|  | ||||
|         # Add forecast and device data | ||||
|         interval = to_duration(cls.config.optimization.interval) | ||||
|         power_to_energy_per_interval_factor = cls.config.optimization.interval / 3600 | ||||
|         parameter_start_datetime = ems.start_datetime.set(hour=0, second=0, microsecond=0) | ||||
|         parameter_end_datetime = parameter_start_datetime.add(hours=cls.config.prediction.hours) | ||||
|         max_retries = 10 | ||||
|  | ||||
|         for attempt in range(1, max_retries + 1): | ||||
|             # Collect all the data for optimisation, but do not exceed max retries | ||||
|             if attempt > max_retries: | ||||
|                 error_msg = f"Maximum retries {max_retries} for parameter collection exceeded. Parameter preparation attempt {attempt}." | ||||
|                 logger.error(error_msg) | ||||
|                 raise ValueError(error_msg) | ||||
|  | ||||
|             # Assure predictions are uptodate | ||||
|             cls.prediction.update_data() | ||||
|  | ||||
|             try: | ||||
|                 pvforecast_ac_power = ( | ||||
|                     cls.prediction.key_to_array( | ||||
|                         key="pvforecast_ac_power", | ||||
|                         start_datetime=parameter_start_datetime, | ||||
|                         end_datetime=parameter_end_datetime, | ||||
|                         interval=interval, | ||||
|                         fill_method="linear", | ||||
|                     ) | ||||
|                     * power_to_energy_per_interval_factor | ||||
|                 ).tolist() | ||||
|             except: | ||||
|                 logger.exception( | ||||
|                     "No PV forecast data available - defaulting to demo data. Parameter preparation attempt {}.", | ||||
|                     attempt, | ||||
|                 ) | ||||
|                 cls.config.merge_settings_from_dict( | ||||
|                     { | ||||
|                         "pvforecast": { | ||||
|                             "provider": "PVForecastAkkudoktor", | ||||
|                             "planes": [ | ||||
|                                 { | ||||
|                                     "peakpower": 5.0, | ||||
|                                     "surface_azimuth": 170, | ||||
|                                     "surface_tilt": 7, | ||||
|                                     "userhorizon": [20, 27, 22, 20], | ||||
|                                     "inverter_paco": 10000, | ||||
|                                 }, | ||||
|                                 { | ||||
|                                     "peakpower": 4.8, | ||||
|                                     "surface_azimuth": 90, | ||||
|                                     "surface_tilt": 7, | ||||
|                                     "userhorizon": [30, 30, 30, 50], | ||||
|                                     "inverter_paco": 10000, | ||||
|                                 }, | ||||
|                                 { | ||||
|                                     "peakpower": 1.4, | ||||
|                                     "surface_azimuth": 140, | ||||
|                                     "surface_tilt": 60, | ||||
|                                     "userhorizon": [60, 30, 0, 30], | ||||
|                                     "inverter_paco": 2000, | ||||
|                                 }, | ||||
|                                 { | ||||
|                                     "peakpower": 1.6, | ||||
|                                     "surface_azimuth": 185, | ||||
|                                     "surface_tilt": 45, | ||||
|                                     "userhorizon": [45, 25, 30, 60], | ||||
|                                     "inverter_paco": 1400, | ||||
|                                 }, | ||||
|                             ], | ||||
|                         }, | ||||
|                     } | ||||
|                 ) | ||||
|                 # Retry | ||||
|                 continue | ||||
|             try: | ||||
|                 elecprice_marketprice_wh = cls.prediction.key_to_array( | ||||
|                     key="elecprice_marketprice_wh", | ||||
|                     start_datetime=parameter_start_datetime, | ||||
|                     end_datetime=parameter_end_datetime, | ||||
|                     interval=interval, | ||||
|                     fill_method="ffill", | ||||
|                 ).tolist() | ||||
|             except: | ||||
|                 logger.exception( | ||||
|                     "No Electricity Marketprice forecast data available - defaulting to demo data. Parameter preparation attempt {}.", | ||||
|                     attempt, | ||||
|                 ) | ||||
|                 cls.config.elecprice.provider = "ElecPriceAkkudoktor" | ||||
|                 # Retry | ||||
|                 continue | ||||
|             try: | ||||
|                 load_mean_adjusted = cls.prediction.key_to_array( | ||||
|                     key="load_mean_adjusted", | ||||
|                     start_datetime=parameter_start_datetime, | ||||
|                     end_datetime=parameter_end_datetime, | ||||
|                     interval=interval, | ||||
|                     fill_method="ffill", | ||||
|                 ).tolist() | ||||
|             except: | ||||
|                 logger.exception( | ||||
|                     "No Load forecast data available - defaulting to demo data. Parameter preparation attempt {}.", | ||||
|                     attempt, | ||||
|                 ) | ||||
|                 cls.config.merge_settings_from_dict( | ||||
|                     { | ||||
|                         "load": { | ||||
|                             "provider": "LoadAkkudoktor", | ||||
|                             "provider_settings": { | ||||
|                                 "LoadAkkudoktor": { | ||||
|                                     "loadakkudoktor_year_energy": "1000", | ||||
|                                 }, | ||||
|                             }, | ||||
|                         }, | ||||
|                     } | ||||
|                 ) | ||||
|                 # Retry | ||||
|                 continue | ||||
|             try: | ||||
|                 feed_in_tariff_wh = cls.prediction.key_to_array( | ||||
|                     key="feed_in_tariff_wh", | ||||
|                     start_datetime=parameter_start_datetime, | ||||
|                     end_datetime=parameter_end_datetime, | ||||
|                     interval=interval, | ||||
|                     fill_method="ffill", | ||||
|                 ).tolist() | ||||
|             except: | ||||
|                 logger.exception( | ||||
|                     "No feed in tariff forecast data available - defaulting to demo data. Parameter preparation attempt {}.", | ||||
|                     attempt, | ||||
|                 ) | ||||
|                 cls.config.merge_settings_from_dict( | ||||
|                     { | ||||
|                         "feedintariff": { | ||||
|                             "provider": "FeedInTariffFixed", | ||||
|                             "provider_settings": { | ||||
|                                 "FeedInTariffFixed": { | ||||
|                                     "feed_in_tariff_kwh": 0.078, | ||||
|                                 }, | ||||
|                             }, | ||||
|                         }, | ||||
|                     } | ||||
|                 ) | ||||
|                 # Retry | ||||
|                 continue | ||||
|             try: | ||||
|                 weather_temp_air = cls.prediction.key_to_array( | ||||
|                     key="weather_temp_air", | ||||
|                     start_datetime=parameter_start_datetime, | ||||
|                     end_datetime=parameter_end_datetime, | ||||
|                     interval=interval, | ||||
|                     fill_method="ffill", | ||||
|                 ).tolist() | ||||
|             except: | ||||
|                 logger.exception( | ||||
|                     "No weather forecast data available - defaulting to demo data. Parameter preparation attempt {}.", | ||||
|                     attempt, | ||||
|                 ) | ||||
|                 cls.config.weather.provider = "BrightSky" | ||||
|                 # Retry | ||||
|                 continue | ||||
|  | ||||
|             # Add device data | ||||
|  | ||||
|             # Batteries | ||||
|             # --------- | ||||
|             if cls.config.devices.max_batteries is None: | ||||
|                 logger.error("Number of battery devices not configured - defaulting to 1.") | ||||
|                 cls.config.devices.max_batteries = 1 | ||||
|             if cls.config.devices.max_batteries == 0: | ||||
|                 battery_params = None | ||||
|                 battery_lcos_kwh = 0 | ||||
|             else: | ||||
|                 if cls.config.devices.batteries is None: | ||||
|                     logger.error("No battery device data available - defaulting to demo data.") | ||||
|                     cls.config.devices.batteries = [{"device_id": "battery1", "capacity_wh": 8000}] | ||||
|                 try: | ||||
|                     battery_config = cls.config.devices.batteries[0] | ||||
|                     battery_params = SolarPanelBatteryParameters( | ||||
|                         device_id=battery_config.device_id, | ||||
|                         capacity_wh=battery_config.capacity_wh, | ||||
|                         charging_efficiency=battery_config.charging_efficiency, | ||||
|                         discharging_efficiency=battery_config.discharging_efficiency, | ||||
|                         max_charge_power_w=battery_config.max_charge_power_w, | ||||
|                         min_soc_percentage=battery_config.min_soc_percentage, | ||||
|                         max_soc_percentage=battery_config.max_soc_percentage, | ||||
|                     ) | ||||
|                 except: | ||||
|                     logger.exception( | ||||
|                         "No battery device data available - defaulting to demo data. Parameter preparation attempt {}.", | ||||
|                         attempt, | ||||
|                     ) | ||||
|                     cls.config.devices.batteries = [{"device_id": "battery1", "capacity_wh": 8000}] | ||||
|                     # Retry | ||||
|                     continue | ||||
|                 # Levelized cost of ownership | ||||
|                 if battery_config.levelized_cost_of_storage_kwh is None: | ||||
|                     logger.error( | ||||
|                         "No battery device LCOS data available - defaulting to 0 €/kWh. Parameter preparation attempt {}.", | ||||
|                         attempt, | ||||
|                     ) | ||||
|                     battery_config.levelized_cost_of_storage_kwh = 0 | ||||
|                 battery_lcos_kwh = battery_config.levelized_cost_of_storage_kwh | ||||
|                 # Initial SOC | ||||
|                 try: | ||||
|                     initial_soc_factor = cls.measurement.key_to_value( | ||||
|                         key=battery_config.measurement_key_soc_factor, | ||||
|                         target_datetime=ems.start_datetime, | ||||
|                     ) | ||||
|                     if initial_soc_factor > 1.0 or initial_soc_factor < 0.0: | ||||
|                         logger.error( | ||||
|                             f"Invalid battery initial SoC factor {initial_soc_factor} - defaulting to 0.0." | ||||
|                         ) | ||||
|                         initial_soc_factor = 0.0 | ||||
|                     # genetic parameter is 0..100 as int | ||||
|                     initial_soc_percentage = int(initial_soc_factor * 100) | ||||
|                 except: | ||||
|                     initial_soc_percentage = None | ||||
|                 if initial_soc_percentage is None: | ||||
|                     logger.error( | ||||
|                         f"No battery device SoC data (measurement key = '{battery_config.measurement_key_soc_factor}') available - defaulting to 0." | ||||
|                     ) | ||||
|                     initial_soc_percentage = 0 | ||||
|                 battery_params.initial_soc_percentage = initial_soc_percentage | ||||
|  | ||||
|             # Electric Vehicles | ||||
|             # ----------------- | ||||
|             if cls.config.devices.max_electric_vehicles is None: | ||||
|                 logger.error("Number of electric_vehicle devices not configured - defaulting to 1.") | ||||
|                 cls.config.devices.max_electric_vehicles = 1 | ||||
|             if cls.config.devices.max_electric_vehicles == 0: | ||||
|                 electric_vehicle_params = None | ||||
|             else: | ||||
|                 if cls.config.devices.electric_vehicles is None: | ||||
|                     logger.error( | ||||
|                         "No electric vehicle device data available - defaulting to demo data." | ||||
|                     ) | ||||
|                     cls.config.devices.max_electric_vehicles = 1 | ||||
|                     cls.config.devices.electric_vehicles = [ | ||||
|                         { | ||||
|                             "device_id": "ev11", | ||||
|                             "capacity_wh": 50000, | ||||
|                             "charge_rates": [0.0, 0.375, 0.5, 0.625, 0.75, 0.875, 1.0], | ||||
|                             "min_soc_percentage": 70, | ||||
|                         } | ||||
|                     ] | ||||
|                 try: | ||||
|                     electric_vehicle_config = cls.config.devices.electric_vehicles[0] | ||||
|                     electric_vehicle_params = ElectricVehicleParameters( | ||||
|                         device_id=electric_vehicle_config.device_id, | ||||
|                         capacity_wh=electric_vehicle_config.capacity_wh, | ||||
|                         charging_efficiency=electric_vehicle_config.charging_efficiency, | ||||
|                         discharging_efficiency=electric_vehicle_config.discharging_efficiency, | ||||
|                         max_charge_power_w=electric_vehicle_config.max_charge_power_w, | ||||
|                         min_soc_percentage=electric_vehicle_config.min_soc_percentage, | ||||
|                         max_soc_percentage=electric_vehicle_config.max_soc_percentage, | ||||
|                     ) | ||||
|                 except: | ||||
|                     logger.exception( | ||||
|                         "No electric_vehicle device data available - defaulting to demo data. Parameter preparation attempt {}.", | ||||
|                         attempt, | ||||
|                     ) | ||||
|                     cls.config.devices.max_electric_vehicles = 1 | ||||
|                     cls.config.devices.electric_vehicles = [ | ||||
|                         { | ||||
|                             "device_id": "ev12", | ||||
|                             "capacity_wh": 50000, | ||||
|                             "charge_rates": [0.0, 0.375, 0.5, 0.625, 0.75, 0.875, 1.0], | ||||
|                             "min_soc_percentage": 70, | ||||
|                         } | ||||
|                     ] | ||||
|                     # Retry | ||||
|                     continue | ||||
|                 # Initial SOC | ||||
|                 try: | ||||
|                     initial_soc_factor = cls.measurement.key_to_value( | ||||
|                         key=electric_vehicle_config.measurement_key_soc_factor, | ||||
|                         target_datetime=ems.start_datetime, | ||||
|                     ) | ||||
|                     if initial_soc_factor > 1.0 or initial_soc_factor < 0.0: | ||||
|                         logger.error( | ||||
|                             f"Invalid electric vehicle initial SoC factor {initial_soc_factor} - defaulting to 0.0." | ||||
|                         ) | ||||
|                         initial_soc_factor = 0.0 | ||||
|                     # genetic parameter is 0..100 as int | ||||
|                     initial_soc_percentage = int(initial_soc_factor * 100) | ||||
|                 except: | ||||
|                     initial_soc_percentage = None | ||||
|                 if initial_soc_percentage is None: | ||||
|                     logger.error( | ||||
|                         f"No electric vehicle device SoC data (measurement key = '{electric_vehicle_config.measurement_key_soc_factor}') available - defaulting to 0." | ||||
|                     ) | ||||
|                     initial_soc_percentage = 0 | ||||
|                 electric_vehicle_params.initial_soc_percentage = initial_soc_percentage | ||||
|  | ||||
|             # Inverters | ||||
|             # --------- | ||||
|             if cls.config.devices.max_inverters is None: | ||||
|                 logger.error("Number of inverter devices not configured - defaulting to 1.") | ||||
|                 cls.config.devices.max_inverters = 1 | ||||
|             if cls.config.devices.max_inverters == 0: | ||||
|                 inverter_params = None | ||||
|             else: | ||||
|                 if cls.config.devices.inverters is None: | ||||
|                     logger.error("No inverter device data available - defaulting to demo data.") | ||||
|                     cls.config.devices.inverters = [ | ||||
|                         { | ||||
|                             "device_id": "inverter1", | ||||
|                             "max_power_w": 10000, | ||||
|                             "battery_id": battery_config.device_id, | ||||
|                         } | ||||
|                     ] | ||||
|                 try: | ||||
|                     inverter_config = cls.config.devices.inverters[0] | ||||
|                     inverter_params = InverterParameters( | ||||
|                         device_id=inverter_config.device_id, | ||||
|                         max_power_wh=inverter_config.max_power_w, | ||||
|                         battery_id=inverter_config.battery_id, | ||||
|                     ) | ||||
|                 except: | ||||
|                     logger.exception( | ||||
|                         "No inverter device data available - defaulting to demo data. Parameter preparation attempt {}.", | ||||
|                         attempt, | ||||
|                     ) | ||||
|                     cls.config.devices.inverters = [ | ||||
|                         { | ||||
|                             "device_id": "inverter1", | ||||
|                             "max_power_w": 10000, | ||||
|                             "battery_id": battery_config.device_id, | ||||
|                         } | ||||
|                     ] | ||||
|                     # Retry | ||||
|                     continue | ||||
|  | ||||
|             # Home Appliances | ||||
|             # --------------- | ||||
|             if cls.config.devices.max_home_appliances is None: | ||||
|                 logger.error("Number of home appliance devices not configured - defaulting to 1.") | ||||
|                 cls.config.devices.max_home_appliances = 1 | ||||
|             if cls.config.devices.max_home_appliances == 0: | ||||
|                 home_appliance_params = None | ||||
|             else: | ||||
|                 home_appliance_params = None | ||||
|                 if cls.config.devices.home_appliances is None: | ||||
|                     logger.error( | ||||
|                         "No home appliance device data available - defaulting to demo data." | ||||
|                     ) | ||||
|                     cls.config.devices.home_appliances = [ | ||||
|                         { | ||||
|                             "device_id": "dishwasher1", | ||||
|                             "consumption_wh": 2000, | ||||
|                             "duration_h": 3.0, | ||||
|                             "time_windows": { | ||||
|                                 "windows": [ | ||||
|                                     { | ||||
|                                         "start_time": "08:00", | ||||
|                                         "duration": "5 hours", | ||||
|                                     }, | ||||
|                                     { | ||||
|                                         "start_time": "15:00", | ||||
|                                         "duration": "3 hours", | ||||
|                                     }, | ||||
|                                 ], | ||||
|                             }, | ||||
|                         } | ||||
|                     ] | ||||
|                 try: | ||||
|                     home_appliance_config = cls.config.devices.home_appliances[0] | ||||
|                     home_appliance_params = HomeApplianceParameters( | ||||
|                         device_id=home_appliance_config.device_id, | ||||
|                         consumption_wh=home_appliance_config.consumption_wh, | ||||
|                         duration_h=home_appliance_config.duration_h, | ||||
|                         time_windows=home_appliance_config.time_windows, | ||||
|                     ) | ||||
|                 except: | ||||
|                     logger.exception( | ||||
|                         "No home appliance device data available - defaulting to demo data. Parameter preparation attempt {}.", | ||||
|                         attempt, | ||||
|                     ) | ||||
|                     cls.config.devices.home_appliances = [ | ||||
|                         { | ||||
|                             "device_id": "dishwasher1", | ||||
|                             "consumption_wh": 2000, | ||||
|                             "duration_h": 3.0, | ||||
|                             "time_windows": None, | ||||
|                         } | ||||
|                     ] | ||||
|                     # Retry | ||||
|                     continue | ||||
|  | ||||
|             # We got all parameter data | ||||
|             try: | ||||
|                 oparams = GeneticOptimizationParameters( | ||||
|                     ems=GeneticEnergyManagementParameters( | ||||
|                         pv_prognose_wh=pvforecast_ac_power, | ||||
|                         strompreis_euro_pro_wh=elecprice_marketprice_wh, | ||||
|                         einspeiseverguetung_euro_pro_wh=feed_in_tariff_wh, | ||||
|                         gesamtlast=load_mean_adjusted, | ||||
|                         preis_euro_pro_wh_akku=battery_lcos_kwh / 1000, | ||||
|                     ), | ||||
|                     temperature_forecast=weather_temp_air, | ||||
|                     pv_akku=battery_params, | ||||
|                     eauto=electric_vehicle_params, | ||||
|                     inverter=inverter_params, | ||||
|                     dishwasher=home_appliance_params, | ||||
|                 ) | ||||
|             except: | ||||
|                 logger.exception( | ||||
|                     "Can not prepare optimization parameters - will retry. Parameter preparation attempt {}.", | ||||
|                     attempt, | ||||
|                 ) | ||||
|                 oparams = None | ||||
|                 # Retry | ||||
|                 continue | ||||
|  | ||||
|             # Parameters prepared | ||||
|             break | ||||
|  | ||||
|         return oparams | ||||
							
								
								
									
										480
									
								
								src/akkudoktoreos/optimization/genetic/geneticsolution.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										480
									
								
								src/akkudoktoreos/optimization/genetic/geneticsolution.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,480 @@ | ||||
| """Genetic algorithm optimisation solution.""" | ||||
|  | ||||
| from typing import Any, Optional | ||||
|  | ||||
| import pandas as pd | ||||
| from loguru import logger | ||||
| from pydantic import Field, field_validator | ||||
|  | ||||
| from akkudoktoreos.config.config import get_config | ||||
| from akkudoktoreos.core.emplan import ( | ||||
|     DDBCInstruction, | ||||
|     EnergyManagementPlan, | ||||
|     FRBCInstruction, | ||||
| ) | ||||
| from akkudoktoreos.core.pydantic import PydanticDateTimeDataFrame | ||||
| from akkudoktoreos.devices.devicesabc import ( | ||||
|     ApplianceOperationMode, | ||||
|     BatteryOperationMode, | ||||
| ) | ||||
| from akkudoktoreos.devices.genetic.battery import Battery | ||||
| from akkudoktoreos.optimization.genetic.geneticdevices import GeneticParametersBaseModel | ||||
| from akkudoktoreos.optimization.optimization import OptimizationSolution | ||||
| from akkudoktoreos.prediction.prediction import get_prediction | ||||
| from akkudoktoreos.utils.datetimeutil import to_datetime, to_duration | ||||
| from akkudoktoreos.utils.utils import NumpyEncoder | ||||
|  | ||||
|  | ||||
| class DeviceOptimizeResult(GeneticParametersBaseModel): | ||||
|     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 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 GeneticSimulationResult(GeneticParametersBaseModel): | ||||
|     """This object contains the results of the simulation and provides insights into various parameters over the entire forecast period.""" | ||||
|  | ||||
|     Last_Wh_pro_Stunde: list[float] = Field(description="TBD") | ||||
|     EAuto_SoC_pro_Stunde: list[float] = Field( | ||||
|         description="The state of charge of the EV for each hour." | ||||
|     ) | ||||
|     Einnahmen_Euro_pro_Stunde: list[float] = Field( | ||||
|         description="The revenue from grid feed-in or other sources in euros per hour." | ||||
|     ) | ||||
|     Gesamt_Verluste: float = Field( | ||||
|         description="The total losses in watt-hours over the entire period." | ||||
|     ) | ||||
|     Gesamtbilanz_Euro: float = Field( | ||||
|         description="The total balance of revenues minus costs in euros." | ||||
|     ) | ||||
|     Gesamteinnahmen_Euro: float = Field(description="The total revenues in euros.") | ||||
|     Gesamtkosten_Euro: float = Field(description="The total costs in euros.") | ||||
|     Home_appliance_wh_per_hour: list[Optional[float]] = Field( | ||||
|         description="The energy consumption of a household appliance in watt-hours per hour." | ||||
|     ) | ||||
|     Kosten_Euro_pro_Stunde: list[float] = Field(description="The costs in euros per hour.") | ||||
|     Netzbezug_Wh_pro_Stunde: list[float] = Field( | ||||
|         description="The grid energy drawn in watt-hours per hour." | ||||
|     ) | ||||
|     Netzeinspeisung_Wh_pro_Stunde: list[float] = Field( | ||||
|         description="The energy fed into the grid in watt-hours per hour." | ||||
|     ) | ||||
|     Verluste_Pro_Stunde: list[float] = Field(description="The losses in watt-hours per hour.") | ||||
|     akku_soc_pro_stunde: list[float] = Field( | ||||
|         description="The state of charge of the battery (not the EV) in percentage per hour." | ||||
|     ) | ||||
|     Electricity_price: list[float] = Field( | ||||
|         description="Used Electricity Price, including predictions" | ||||
|     ) | ||||
|  | ||||
|     @field_validator( | ||||
|         "Last_Wh_pro_Stunde", | ||||
|         "Netzeinspeisung_Wh_pro_Stunde", | ||||
|         "akku_soc_pro_stunde", | ||||
|         "Netzbezug_Wh_pro_Stunde", | ||||
|         "Kosten_Euro_pro_Stunde", | ||||
|         "Einnahmen_Euro_pro_Stunde", | ||||
|         "EAuto_SoC_pro_Stunde", | ||||
|         "Verluste_Pro_Stunde", | ||||
|         "Home_appliance_wh_per_hour", | ||||
|         "Electricity_price", | ||||
|         mode="before", | ||||
|     ) | ||||
|     def convert_numpy(cls, field: Any) -> Any: | ||||
|         return NumpyEncoder.convert_numpy(field)[0] | ||||
|  | ||||
|  | ||||
| class GeneticSolution(GeneticParametersBaseModel): | ||||
|     """**Note**: The first value of "Last_Wh_per_hour", "Netzeinspeisung_Wh_per_hour", and "Netzbezug_Wh_per_hour", will be set to null in the JSON output and represented as NaN or None in the corresponding classes' data returns. This approach is adopted to ensure that the current hour's processing remains unchanged.""" | ||||
|  | ||||
|     ac_charge: list[float] = Field( | ||||
|         description="Array with AC charging values as relative power (0.0-1.0), other values set to 0." | ||||
|     ) | ||||
|     dc_charge: list[float] = Field( | ||||
|         description="Array with DC charging values as relative power (0-1), other values set to 0." | ||||
|     ) | ||||
|     discharge_allowed: list[int] = Field( | ||||
|         description="Array with discharge values (1 for discharge, 0 otherwise)." | ||||
|     ) | ||||
|     eautocharge_hours_float: Optional[list[float]] = Field(description="TBD") | ||||
|     result: GeneticSimulationResult | ||||
|     eauto_obj: Optional[ElectricVehicleResult] | ||||
|     start_solution: Optional[list[float]] = Field( | ||||
|         default=None, | ||||
|         description="An array of binary values (0 or 1) representing a possible starting solution for the simulation.", | ||||
|     ) | ||||
|     washingstart: Optional[int] = Field( | ||||
|         default=None, | ||||
|         description="Can be `null` or contain an object representing the start of washing (if applicable).", | ||||
|     ) | ||||
|  | ||||
|     @field_validator( | ||||
|         "ac_charge", | ||||
|         "dc_charge", | ||||
|         "discharge_allowed", | ||||
|         mode="before", | ||||
|     ) | ||||
|     def convert_numpy(cls, field: Any) -> Any: | ||||
|         return NumpyEncoder.convert_numpy(field)[0] | ||||
|  | ||||
|     @field_validator( | ||||
|         "eauto_obj", | ||||
|         mode="before", | ||||
|     ) | ||||
|     def convert_eauto(cls, field: Any) -> Any: | ||||
|         if isinstance(field, Battery): | ||||
|             return ElectricVehicleResult(**field.to_dict()) | ||||
|         return field | ||||
|  | ||||
|     def _battery_operation_from_solution( | ||||
|         self, | ||||
|         ac_charge: float, | ||||
|         dc_charge: float, | ||||
|         discharge_allowed: bool, | ||||
|     ) -> tuple[BatteryOperationMode, float]: | ||||
|         """Maps low-level solution to a representative operation mode and factor. | ||||
|  | ||||
|         Args: | ||||
|             ac_charge (float): Allowed AC-side charging power (relative units). | ||||
|             dc_charge (float): Allowed DC-side charging power (relative units). | ||||
|             discharge_allowed (bool): Whether discharging is permitted. | ||||
|  | ||||
|         Returns: | ||||
|             tuple[BatteryOperationMode, float]: | ||||
|                 A tuple containing: | ||||
|                 - `BatteryOperationMode`: the representative high-level operation mode. | ||||
|                 - `float`: the operation factor corresponding to the active signal. | ||||
|  | ||||
|         Notes: | ||||
|             - The mapping prioritizes AC charge > DC charge > discharge. | ||||
|             - Multiple strategies can produce the same low-level signals; this function | ||||
|             returns a representative mode based on a defined priority order. | ||||
|         """ | ||||
|         # (0,0,0) → Nothing allowed | ||||
|         if ac_charge <= 0.0 and dc_charge <= 0.0 and not discharge_allowed: | ||||
|             return BatteryOperationMode.IDLE, 1.0 | ||||
|  | ||||
|         # (0,0,1) → Discharge only | ||||
|         if ac_charge <= 0.0 and dc_charge <= 0.0 and discharge_allowed: | ||||
|             return BatteryOperationMode.PEAK_SHAVING, 1.0 | ||||
|  | ||||
|         # (ac>0,0,0) → AC charge only | ||||
|         if ac_charge > 0.0 and dc_charge <= 0.0 and not discharge_allowed: | ||||
|             return BatteryOperationMode.GRID_SUPPORT_IMPORT, ac_charge | ||||
|  | ||||
|         # (0,dc>0,0) → DC charge only | ||||
|         if ac_charge <= 0.0 and dc_charge > 0.0 and not discharge_allowed: | ||||
|             return BatteryOperationMode.NON_EXPORT, dc_charge | ||||
|  | ||||
|         # (ac>0,dc>0,0) → Both charge paths, no discharge | ||||
|         if ac_charge > 0.0 and dc_charge > 0.0 and not discharge_allowed: | ||||
|             return BatteryOperationMode.FORCED_CHARGE, ac_charge | ||||
|  | ||||
|         # (ac>0,0,1) → AC charge + discharge - does not make sense | ||||
|         if ac_charge > 0.0 and dc_charge <= 0.0 and discharge_allowed: | ||||
|             raise ValueError( | ||||
|                 f"Illegal state: ac_charge: {ac_charge} and discharge_allowed: {discharge_allowed}" | ||||
|             ) | ||||
|  | ||||
|         # (0,dc>0,1) → DC charge + discharge | ||||
|         if ac_charge <= 0.0 and dc_charge > 0.0 and discharge_allowed: | ||||
|             return BatteryOperationMode.SELF_CONSUMPTION, dc_charge | ||||
|  | ||||
|         # (ac>0,dc>0,1) → Fully flexible - does not make sense | ||||
|         if ac_charge > 0.0 and dc_charge > 0.0 and discharge_allowed: | ||||
|             raise ValueError( | ||||
|                 f"Illegal state: ac_charge: {ac_charge} and discharge_allowed: {discharge_allowed}" | ||||
|             ) | ||||
|  | ||||
|         # Fallback → safe idle | ||||
|         return BatteryOperationMode.IDLE, 1.0 | ||||
|  | ||||
|     def optimization_solution(self) -> OptimizationSolution: | ||||
|         """Provide the genetic solution as a general optimization solution. | ||||
|  | ||||
|         The battery modes are controlled by the grid control triggers: | ||||
|         - ac_charge: charge from grid | ||||
|         - discharge_allowed: discharge to grid | ||||
|  | ||||
|         The following battery modes are supported: | ||||
|         - SELF_CONSUMPTION:    ac_charge == 0 and discharge_allowed == 0 | ||||
|         - GRID_SUPPORT_EXPORT: ac_charge == 0 and discharge_allowed == 1 | ||||
|         - GRID_SUPPORT_IMPORT: ac_charge  > 0 and discharge_allowed == 0 or 1 | ||||
|         """ | ||||
|         from akkudoktoreos.core.ems import get_ems | ||||
|  | ||||
|         config = get_config() | ||||
|         start_datetime = get_ems().start_datetime | ||||
|         interval_hours = 1 | ||||
|  | ||||
|         # --- Create index based on list length and interval --- | ||||
|         n_points = len(self.result.Kosten_Euro_pro_Stunde) | ||||
|         time_index = pd.date_range( | ||||
|             start=start_datetime, | ||||
|             periods=n_points, | ||||
|             freq=f"{interval_hours}h", | ||||
|         ) | ||||
|         end_datetime = start_datetime.add(hours=n_points) | ||||
|  | ||||
|         # Fill data into dataframe with correct column names | ||||
|         # - load_energy_wh: Load of all energy consumers in wh" | ||||
|         # - grid_energy_wh: Grid energy feed in (negative) or consumption (positive) in wh" | ||||
|         # - pv_prediction_energy_wh: PV energy prediction (positive) in wh" | ||||
|         # - elec_price_prediction_amt_kwh: Electricity price prediction in money per kwh" | ||||
|         # - costs_amt: Costs in money amount" | ||||
|         # - revenue_amt: Revenue in money amount" | ||||
|         # - losses_energy_wh: Energy losses in wh" | ||||
|         # - <device-id>_<operation>_op_mode: Operation mode of the device (1.0 when active)." | ||||
|         # - <device-id>_<operation>_op_factor: Operation mode factor of the device." | ||||
|         # - <device-id>_soc_factor: State of charge of a battery/ electric vehicle device as factor of total capacity." | ||||
|         # - <device-id>_energy_wh: Energy consumption (positive) of a device in wh." | ||||
|  | ||||
|         data = pd.DataFrame( | ||||
|             { | ||||
|                 "date_time": time_index, | ||||
|                 "load_energy_wh": self.result.Last_Wh_pro_Stunde, | ||||
|                 "grid_feedin_energy_wh": self.result.Netzeinspeisung_Wh_pro_Stunde, | ||||
|                 "grid_consumption_energy_wh": self.result.Netzbezug_Wh_pro_Stunde, | ||||
|                 "elec_price_prediction_amt_kwh": [v * 1000 for v in self.result.Electricity_price], | ||||
|                 "costs_amt": self.result.Kosten_Euro_pro_Stunde, | ||||
|                 "revenue_amt": self.result.Einnahmen_Euro_pro_Stunde, | ||||
|                 "losses_energy_wh": self.result.Verluste_Pro_Stunde, | ||||
|             }, | ||||
|             index=time_index, | ||||
|         ) | ||||
|  | ||||
|         # Add battery data | ||||
|         data["battery1_soc_factor"] = [v / 100 for v in self.result.akku_soc_pro_stunde] | ||||
|         operation: dict[str, list[float]] = {} | ||||
|         for hour, rate in enumerate(self.ac_charge): | ||||
|             if hour >= n_points: | ||||
|                 break | ||||
|             operation_mode, operation_mode_factor = self._battery_operation_from_solution( | ||||
|                 self.ac_charge[hour], self.dc_charge[hour], bool(self.discharge_allowed[hour]) | ||||
|             ) | ||||
|             for mode in BatteryOperationMode: | ||||
|                 mode_key = f"battery1_{mode.lower()}_op_mode" | ||||
|                 factor_key = f"battery1_{mode.lower()}_op_factor" | ||||
|                 if mode_key not in operation.keys(): | ||||
|                     operation[mode_key] = [] | ||||
|                     operation[factor_key] = [] | ||||
|                 if mode == operation_mode: | ||||
|                     operation[mode_key].append(1.0) | ||||
|                     operation[factor_key].append(operation_mode_factor) | ||||
|                 else: | ||||
|                     operation[mode_key].append(0.0) | ||||
|                     operation[factor_key].append(0.0) | ||||
|         for key in operation.keys(): | ||||
|             data[key] = operation[key] | ||||
|  | ||||
|         # Add EV battery data | ||||
|         if self.eauto_obj: | ||||
|             if self.eautocharge_hours_float is None: | ||||
|                 # Electric vehicle is full enough. No load times. | ||||
|                 data[f"{self.eauto_obj.device_id}_soc_factor"] = [ | ||||
|                     self.eauto_obj.initial_soc_percentage / 100.0 | ||||
|                 ] * n_points | ||||
|                 # operation modes | ||||
|                 operation_mode = BatteryOperationMode.IDLE | ||||
|                 for mode in BatteryOperationMode: | ||||
|                     mode_key = f"{self.eauto_obj.device_id}_{mode.lower()}_op_mode" | ||||
|                     factor_key = f"{self.eauto_obj.device_id}_{mode.lower()}_op_factor" | ||||
|                     if mode == operation_mode: | ||||
|                         data[mode_key] = [1.0] * n_points | ||||
|                         data[factor_key] = [1.0] * n_points | ||||
|                     else: | ||||
|                         data[mode_key] = [0.0] * n_points | ||||
|                         data[factor_key] = [0.0] * n_points | ||||
|             else: | ||||
|                 data[f"{self.eauto_obj.device_id}_soc_factor"] = [ | ||||
|                     v / 100 for v in self.result.EAuto_SoC_pro_Stunde | ||||
|                 ] | ||||
|                 operation = {} | ||||
|                 for hour, rate in enumerate(self.eautocharge_hours_float): | ||||
|                     if hour >= n_points: | ||||
|                         break | ||||
|                     operation_mode, operation_mode_factor = self._battery_operation_from_solution( | ||||
|                         rate, 0.0, False | ||||
|                     ) | ||||
|                     for mode in BatteryOperationMode: | ||||
|                         mode_key = f"{self.eauto_obj.device_id}_{mode.lower()}_op_mode" | ||||
|                         factor_key = f"{self.eauto_obj.device_id}_{mode.lower()}_op_factor" | ||||
|                         if mode_key not in operation.keys(): | ||||
|                             operation[mode_key] = [] | ||||
|                             operation[factor_key] = [] | ||||
|                         if mode == operation_mode: | ||||
|                             operation[mode_key].append(1.0) | ||||
|                             operation[factor_key].append(operation_mode_factor) | ||||
|                         else: | ||||
|                             operation[mode_key].append(0.0) | ||||
|                             operation[factor_key].append(0.0) | ||||
|                 for key in operation.keys(): | ||||
|                     data[key] = operation[key] | ||||
|  | ||||
|         # Add home appliance data | ||||
|         if self.washingstart: | ||||
|             data["homeappliance1_energy_wh"] = self.result.Home_appliance_wh_per_hour | ||||
|  | ||||
|         # Add important predictions that are not already available from the GenericSolution | ||||
|         prediction = get_prediction() | ||||
|         power_to_energy_per_interval_factor = 1.0 | ||||
|         if "pvforecast_ac_power" in prediction.record_keys: | ||||
|             data["pv_prediction_energy_wh"] = ( | ||||
|                 prediction.key_to_array( | ||||
|                     key="pvforecast_ac_power", | ||||
|                     start_datetime=start_datetime, | ||||
|                     end_datetime=end_datetime, | ||||
|                     interval=to_duration(f"{interval_hours} hours"), | ||||
|                     fill_method="linear", | ||||
|                 ) | ||||
|                 * power_to_energy_per_interval_factor | ||||
|             ).tolist() | ||||
|         if "weather_temp_air" in prediction.record_keys: | ||||
|             data["weather_temp_air"] = ( | ||||
|                 prediction.key_to_array( | ||||
|                     key="weather_temp_air", | ||||
|                     start_datetime=start_datetime, | ||||
|                     end_datetime=end_datetime, | ||||
|                     interval=to_duration(f"{interval_hours} hours"), | ||||
|                     fill_method="linear", | ||||
|                 ) | ||||
|             ).tolist() | ||||
|  | ||||
|         solution = OptimizationSolution( | ||||
|             id=f"optimization-genetic@{to_datetime(as_string=True)}", | ||||
|             generated_at=to_datetime(), | ||||
|             comment="Optimization solution derived from GeneticSolution.", | ||||
|             valid_from=start_datetime, | ||||
|             valid_until=start_datetime.add(hours=config.optimization.horizon_hours), | ||||
|             total_losses_energy_wh=self.result.Gesamt_Verluste, | ||||
|             total_revenues_amt=self.result.Gesamteinnahmen_Euro, | ||||
|             total_costs_amt=self.result.Gesamtkosten_Euro, | ||||
|             data=PydanticDateTimeDataFrame.from_dataframe(data), | ||||
|         ) | ||||
|  | ||||
|         return solution | ||||
|  | ||||
|     def energy_management_plan(self) -> EnergyManagementPlan: | ||||
|         """Provide the genetic solution as an energy management plan.""" | ||||
|         from akkudoktoreos.core.ems import get_ems | ||||
|  | ||||
|         start_datetime = get_ems().start_datetime | ||||
|         plan = EnergyManagementPlan( | ||||
|             id=f"plan-genetic@{to_datetime(as_string=True)}", | ||||
|             generated_at=to_datetime(), | ||||
|             instructions=[], | ||||
|             comment="Energy management plan derived from GeneticSolution.", | ||||
|         ) | ||||
|  | ||||
|         # Add battery instructions (fill rate based control) | ||||
|         last_operation_mode: Optional[str] = None | ||||
|         last_operation_mode_factor: Optional[float] = None | ||||
|         resource_id = "battery1" | ||||
|         logger.debug("BAT: {} - {}", resource_id, self.ac_charge) | ||||
|         for hour, rate in enumerate(self.ac_charge): | ||||
|             operation_mode, operation_mode_factor = self._battery_operation_from_solution( | ||||
|                 self.ac_charge[hour], self.dc_charge[hour], bool(self.discharge_allowed[hour]) | ||||
|             ) | ||||
|             if ( | ||||
|                 operation_mode == last_operation_mode | ||||
|                 and operation_mode_factor == last_operation_mode_factor | ||||
|             ): | ||||
|                 # Skip, we already added the instruction | ||||
|                 continue | ||||
|             last_operation_mode = operation_mode | ||||
|             last_operation_mode_factor = operation_mode_factor | ||||
|             execution_time = start_datetime.add(hours=hour) | ||||
|             plan.add_instruction( | ||||
|                 FRBCInstruction( | ||||
|                     resource_id=resource_id, | ||||
|                     execution_time=execution_time, | ||||
|                     actuator_id=resource_id, | ||||
|                     operation_mode_id=operation_mode, | ||||
|                     operation_mode_factor=operation_mode_factor, | ||||
|                 ) | ||||
|             ) | ||||
|  | ||||
|         # Add EV battery instructions (fill rate based control) | ||||
|         if self.eauto_obj: | ||||
|             resource_id = self.eauto_obj.device_id | ||||
|             if self.eautocharge_hours_float is None: | ||||
|                 # Electric vehicle is full enough. No load times. | ||||
|                 logger.debug("EV: {} - SoC >= min, no optimization", resource_id) | ||||
|                 plan.add_instruction( | ||||
|                     FRBCInstruction( | ||||
|                         resource_id=resource_id, | ||||
|                         execution_time=start_datetime, | ||||
|                         actuator_id=resource_id, | ||||
|                         operation_mode_id=BatteryOperationMode.IDLE, | ||||
|                         operation_mode_factor=1.0, | ||||
|                     ) | ||||
|                 ) | ||||
|             else: | ||||
|                 last_operation_mode = None | ||||
|                 last_operation_mode_factor = None | ||||
|                 logger.debug("EV: {} - {}", resource_id, self.eauto_obj.charge_array) | ||||
|                 for hour, rate in enumerate(self.eautocharge_hours_float): | ||||
|                     operation_mode, operation_mode_factor = self._battery_operation_from_solution( | ||||
|                         rate, 0.0, False | ||||
|                     ) | ||||
|                     if ( | ||||
|                         operation_mode == last_operation_mode | ||||
|                         and operation_mode_factor == last_operation_mode_factor | ||||
|                     ): | ||||
|                         # Skip, we already added the instruction | ||||
|                         continue | ||||
|                     last_operation_mode = operation_mode | ||||
|                     last_operation_mode_factor = operation_mode_factor | ||||
|                     execution_time = start_datetime.add(hours=hour) | ||||
|                     plan.add_instruction( | ||||
|                         FRBCInstruction( | ||||
|                             resource_id=resource_id, | ||||
|                             execution_time=execution_time, | ||||
|                             actuator_id=resource_id, | ||||
|                             operation_mode_id=operation_mode, | ||||
|                             operation_mode_factor=operation_mode_factor, | ||||
|                         ) | ||||
|                     ) | ||||
|  | ||||
|         # Add home appliance instructions (demand driven based control) | ||||
|         if self.washingstart: | ||||
|             resource_id = "homeappliance1" | ||||
|             operation_mode = ApplianceOperationMode.RUN  # type: ignore[assignment] | ||||
|             operation_mode_factor = 1.0 | ||||
|             execution_time = start_datetime.add(hours=self.washingstart) | ||||
|             plan.add_instruction( | ||||
|                 DDBCInstruction( | ||||
|                     resource_id=resource_id, | ||||
|                     execution_time=execution_time, | ||||
|                     actuator_id=resource_id, | ||||
|                     operation_mode_id=operation_mode, | ||||
|                     operation_mode_factor=operation_mode_factor, | ||||
|                 ) | ||||
|             ) | ||||
|  | ||||
|         return plan | ||||
| @@ -1,37 +1,111 @@ | ||||
| from typing import List, Optional | ||||
| from typing import Optional, Union | ||||
|  | ||||
| from pydantic import Field | ||||
|  | ||||
| from akkudoktoreos.config.configabc import SettingsBaseModel | ||||
| from akkudoktoreos.core.pydantic import PydanticBaseModel, PydanticDateTimeDataFrame | ||||
| from akkudoktoreos.utils.datetimeutil import DateTime | ||||
|  | ||||
|  | ||||
| class GeneticCommonSettings(SettingsBaseModel): | ||||
|     """General Genetic Optimization Algorithm Configuration.""" | ||||
|  | ||||
|     individuals: Optional[int] = Field( | ||||
|         default=300, | ||||
|         ge=10, | ||||
|         description="Number of individuals (solutions) to generate for the (initial) generation [>= 10]. Defaults to 300.", | ||||
|         examples=[300], | ||||
|     ) | ||||
|  | ||||
|     generations: Optional[int] = Field( | ||||
|         default=400, | ||||
|         ge=10, | ||||
|         description="Number of generations to evaluate the optimal solution [>= 10]. Defaults to 400.", | ||||
|         examples=[400], | ||||
|     ) | ||||
|  | ||||
|     seed: Optional[int] = Field( | ||||
|         default=None, | ||||
|         ge=0, | ||||
|         description="Fixed seed for genetic algorithm. Defaults to 'None' which means random seed.", | ||||
|         examples=[None], | ||||
|     ) | ||||
|  | ||||
|     penalties: Optional[dict[str, Union[float, int, str]]] = Field( | ||||
|         default=None, | ||||
|         description="A dictionary of penalty function parameters consisting of a penalty function parameter name and the associated value.", | ||||
|         examples=[ | ||||
|             {"ev_soc_miss": 10}, | ||||
|         ], | ||||
|     ) | ||||
|  | ||||
|  | ||||
| class OptimizationCommonSettings(SettingsBaseModel): | ||||
|     """General Optimization Configuration. | ||||
|     """General Optimization Configuration.""" | ||||
|  | ||||
|     Attributes: | ||||
|         hours (int): Number of hours for optimizations. | ||||
|     """ | ||||
|  | ||||
|     hours: Optional[int] = Field( | ||||
|         default=48, ge=0, description="Number of hours into the future for optimizations." | ||||
|     horizon_hours: Optional[int] = Field( | ||||
|         default=24, | ||||
|         ge=0, | ||||
|         description="The general time window within which the energy optimization goal shall be achieved [h]. Defaults to 24 hours.", | ||||
|         examples=[24], | ||||
|     ) | ||||
|  | ||||
|     penalty: Optional[int] = Field(default=10, description="Penalty factor used in optimization.") | ||||
|  | ||||
|     ev_available_charge_rates_percent: Optional[List[float]] = Field( | ||||
|         default=[ | ||||
|             0.0, | ||||
|             6.0 / 16.0, | ||||
|             # 7.0 / 16.0, | ||||
|             8.0 / 16.0, | ||||
|             # 9.0 / 16.0, | ||||
|             10.0 / 16.0, | ||||
|             # 11.0 / 16.0, | ||||
|             12.0 / 16.0, | ||||
|             # 13.0 / 16.0, | ||||
|             14.0 / 16.0, | ||||
|             # 15.0 / 16.0, | ||||
|             1.0, | ||||
|         ], | ||||
|         description="Charge rates available for the EV in percent of maximum charge.", | ||||
|     interval: Optional[int] = Field( | ||||
|         default=3600, | ||||
|         ge=15 * 60, | ||||
|         le=60 * 60, | ||||
|         description="The optimization interval [sec].", | ||||
|         examples=[60 * 60, 15 * 60], | ||||
|     ) | ||||
|  | ||||
|     genetic: Optional[GeneticCommonSettings] = Field( | ||||
|         default=None, | ||||
|         description="Genetic optimization algorithm configuration.", | ||||
|         examples=[{"individuals": 400, "seed": None, "penalties": {"ev_soc_miss": 10}}], | ||||
|     ) | ||||
|  | ||||
|  | ||||
| class OptimizationSolution(PydanticBaseModel): | ||||
|     """General Optimization Solution.""" | ||||
|  | ||||
|     id: str = Field(..., description="Unique ID for the optimization solution.") | ||||
|  | ||||
|     generated_at: DateTime = Field(..., description="Timestamp when the solution was generated.") | ||||
|  | ||||
|     comment: Optional[str] = Field( | ||||
|         default=None, description="Optional comment or annotation for the solution." | ||||
|     ) | ||||
|  | ||||
|     valid_from: Optional[DateTime] = Field( | ||||
|         default=None, description="Start time of the optimization solution." | ||||
|     ) | ||||
|  | ||||
|     valid_until: Optional[DateTime] = Field( | ||||
|         default=None, | ||||
|         description="End time of the optimization solution.", | ||||
|     ) | ||||
|  | ||||
|     total_losses_energy_wh: float = Field( | ||||
|         description="The total losses in watt-hours over the entire period." | ||||
|     ) | ||||
|  | ||||
|     total_revenues_amt: float = Field(description="The total revenues [money amount].") | ||||
|  | ||||
|     total_costs_amt: float = Field(description="The total costs [money amount].") | ||||
|  | ||||
|     data: PydanticDateTimeDataFrame = Field( | ||||
|         description=( | ||||
|             "Datetime data frame with time series optimization data per optimization interval:" | ||||
|             "- load_energy_wh: Load of all energy consumers in wh" | ||||
|             "- grid_energy_wh: Grid energy feed in (negative) or consumption (positive) in wh" | ||||
|             "- pv_prediction_energy_wh: PV energy prediction (positive) in wh" | ||||
|             "- elec_price_prediction_amt_kwh: Electricity price prediction in money per kwh" | ||||
|             "- costs_amt: Costs in money amount" | ||||
|             "- revenue_amt: Revenue in money amount" | ||||
|             "- losses_energy_wh: Energy losses in wh" | ||||
|             "- <device-id>_operation_mode_id: Operation mode id of the device." | ||||
|             "- <device-id>_operation_mode_factor: Operation mode factor of the device." | ||||
|             "- <device-id>_soc_factor: State of charge of a battery/ electric vehicle device as factor of total capacity." | ||||
|             "- <device-id>_energy_wh: Energy consumption (positive) of a device in wh." | ||||
|         ) | ||||
|     ) | ||||
|   | ||||
| @@ -2,11 +2,14 @@ | ||||
|  | ||||
| from pydantic import ConfigDict | ||||
|  | ||||
| from akkudoktoreos.core.coreabc import ConfigMixin, PredictionMixin | ||||
| from akkudoktoreos.core.pydantic import PydanticBaseModel | ||||
| from akkudoktoreos.core.coreabc import ( | ||||
|     ConfigMixin, | ||||
|     EnergyManagementSystemMixin, | ||||
|     PredictionMixin, | ||||
| ) | ||||
|  | ||||
|  | ||||
| class OptimizationBase(ConfigMixin, PredictionMixin, PydanticBaseModel): | ||||
| class OptimizationBase(ConfigMixin, PredictionMixin, EnergyManagementSystemMixin): | ||||
|     """Base class for handling optimization data. | ||||
|  | ||||
|     Enables access to EOS configuration data (attribute `config`) and EOS prediction data (attribute | ||||
|   | ||||
		Reference in New Issue
	
	Block a user