chore: prepare for update of genetic algorithm (#1190)
Bump Version / Bump Version Workflow (push) Has been cancelled
CodeQL Advanced / Analyze (actions) (push) Has been cancelled
CodeQL Advanced / Analyze (python) (push) Has been cancelled
docker-build / platform-excludes (push) Has been cancelled
docker-build / build (push) Has been cancelled
docker-build / merge (push) Has been cancelled
pre-commit / pre-commit (push) Has been cancelled
Run Pytest on Pull Request / test (push) Has been cancelled

Andreas will update the genetic algorithm for 15-minutes optimization
intervals.

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

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

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

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

Signed-off-by: Bobby Noelte <b0661n0e17e@gmail.com>
This commit is contained in:
Bobby Noelte
2026-07-29 12:56:08 +02:00
committed by GitHub
parent 7e5aa2f218
commit e23bb7b497
51 changed files with 13086 additions and 955 deletions
+26 -73
View File
@@ -1,4 +1,4 @@
from typing import Optional, Union
from typing import Optional
from pydantic import Field, computed_field
@@ -8,92 +8,54 @@ from akkudoktoreos.core.pydantic import (
PydanticBaseModel,
PydanticDateTimeDataFrame,
)
from akkudoktoreos.optimization.genetic0.genetic0settings import Genetic0CommonSettings
from akkudoktoreos.optimization.genetic.geneticsettings import GeneticCommonSettings
from akkudoktoreos.utils.datetimeutil import DateTime
class GeneticCommonSettings(SettingsBaseModel):
"""General Genetic Optimization Algorithm Configuration."""
individuals: Optional[int] = Field(
default=300,
ge=10,
json_schema_extra={
"description": "Number of individuals (solutions) in the population [>= 10]. Defaults to 300.",
"examples": [300],
},
)
generations: Optional[int] = Field(
default=400,
ge=10,
json_schema_extra={
"description": "Number of generations to evolve [>= 10]. Defaults to 400.",
"examples": [400],
},
)
seed: Optional[int] = Field(
default=None,
ge=0,
json_schema_extra={
"description": "Random seed for reproducibility. None = random.",
"examples": [None, 42],
},
)
# --- Penalties (existing) -------------------------------------------------
penalties: dict[str, Union[float, int, str]] = Field(
default_factory=lambda: {
"ev_soc_miss": 10,
"ac_charge_break_even": 1.0,
},
json_schema_extra={
"description": "Penalty parameters used in fitness evaluation.",
"examples": [{"ev_soc_miss": 10}],
},
)
def optimization_algorithms() -> list[str]:
"""Valid optimization algorithms."""
# Return static built-in optimization algorithms.
return [
"GENETIC",
"GENETIC0",
]
class OptimizationCommonSettings(SettingsBaseModel):
"""General Optimization Configuration."""
horizon_hours: int = Field(
default=24,
ge=0,
json_schema_extra={
"description": "The general time window within which the energy optimization goal shall be achieved [h]. Defaults to 24 hours.",
"examples": [24],
},
)
interval: int = Field(
default=3600,
ge=15 * 60,
le=60 * 60,
json_schema_extra={
"description": "The optimization interval [sec]. Defaults to 3600 seconds (1 hour)",
"examples": [60 * 60, 15 * 60],
},
)
algorithm: str = Field(
default="GENETIC",
json_schema_extra={
"description": "The optimization algorithm. Defaults to GENETIC",
"examples": ["GENETIC"],
"examples": ["GENETIC", "GENETIC0"],
},
)
genetic: GeneticCommonSettings = Field(
default_factory=GeneticCommonSettings,
json_schema_extra={
"description": "Genetic optimization algorithm configuration.",
"description": "GENETIC optimization algorithm configuration.",
"examples": [{"individuals": 400, "seed": None, "penalties": {"ev_soc_miss": 10}}],
},
)
genetic0: Genetic0CommonSettings = Field(
default_factory=Genetic0CommonSettings,
json_schema_extra={
"description": "GENETIC0 optimization algorithm configuration.",
"examples": [{"individuals": 400, "seed": None, "penalties": {"ev_soc_miss": 10}}],
},
)
# Computed fields
@computed_field # type: ignore[prop-decorator]
@property
def algorithms(self) -> list[str]:
"""Available optimization algorithms."""
return optimization_algorithms()
@computed_field # type: ignore[prop-decorator]
@property
def keys(self) -> list[str]:
@@ -112,15 +74,6 @@ class OptimizationCommonSettings(SettingsBaseModel):
key_list = df.columns.tolist()
return sorted(set(key_list))
@computed_field # type: ignore[prop-decorator]
@property
def horizon(self) -> int:
"""Number of optimization steps."""
if self.interval is None or self.interval == 0 or self.horizon_hours is None:
return 0
num_steps = int(float(self.horizon_hours * 3600) / self.interval)
return num_steps
class OptimizationSolution(PydanticBaseModel):
"""General Optimization Solution."""