Improve Configuration and Prediction Usability (#220)

* Update utilities in utils submodule.
* Add base configuration modules.
* Add server base configuration modules.
* Add devices base configuration modules.
* Add optimization base configuration modules.
* Add utils base configuration modules.
* Add prediction abstract and base classes plus tests.
* Add PV forecast to prediction submodule.
   The PV forecast modules are adapted from the class_pvforecast module and
   replace it.
* Add weather forecast to prediction submodule.
   The modules provide classes and methods to retrieve, manage, and process weather forecast data
   from various sources. Includes are structured representations of weather data and utilities
   for fetching forecasts for specific locations and time ranges.
   BrightSky and ClearOutside are currently supported.
* Add electricity price forecast to prediction submodule.
* Adapt fastapi server to base config and add fasthtml server.
* Add ems to core submodule.
* Adapt genetic to config.
* Adapt visualize to config.
* Adapt common test fixtures to config.
* Add load forecast to prediction submodule.
* Add core abstract and base classes.
* Adapt single test optimization to config.
* Adapt devices to config.

Signed-off-by: Bobby Noelte <b0661n0e17e@gmail.com>
This commit is contained in:
Bobby Noelte
2024-12-15 14:40:03 +01:00
committed by GitHub
parent a5e637ab4c
commit aa334d0b61
80 changed files with 29048 additions and 2451 deletions

View File

@@ -1,299 +1,240 @@
#!/usr/bin/env python3
import argparse
import cProfile
import pstats
import sys
import time
import numpy as np
from akkudoktoreos.config import get_working_dir, load_config
from akkudoktoreos.config.config import get_config
from akkudoktoreos.optimization.genetic import (
OptimizationParameters,
optimization_problem,
)
start_hour = 0
# PV Forecast (in W)
pv_forecast = np.zeros(48)
pv_forecast[12] = 5000
# [
# 0,
# 0,
# 0,
# 0,
# 0,
# 0,
# 0,
# 8.05,
# 352.91,
# 728.51,
# 930.28,
# 1043.25,
# 1106.74,
# 1161.69,
# 1018.82,
# 1519.07,
# 1969.88,
# 1017.96,
# 1043.07,
# 1007.17,
# 319.67,
# 7.88,
# 0,
# 0,
# 0,
# 0,
# 0,
# 0,
# 0,
# 0,
# 0,
# 5.04,
# 335.59,
# 705.32,
# 1121.12,
# 1604.79,
# 2157.38,
# 1433.25,
# 5718.49,
# 4553.96,
# 3027.55,
# 2574.46,
# 1720.4,
# 963.4,
# 383.3,
# 0,
# 0,
# 0,
# ]
def prepare_optimization_parameters() -> OptimizationParameters:
"""Prepare and return optimization parameters with predefined data.
# Temperature Forecast (in degree C)
temperature_forecast = [
18.3,
17.8,
16.9,
16.2,
15.6,
15.1,
14.6,
14.2,
14.3,
14.8,
15.7,
16.7,
17.4,
18.0,
18.6,
19.2,
19.1,
18.7,
18.5,
17.7,
16.2,
14.6,
13.6,
13.0,
12.6,
12.2,
11.7,
11.6,
11.3,
11.0,
10.7,
10.2,
11.4,
14.4,
16.4,
18.3,
19.5,
20.7,
21.9,
22.7,
23.1,
23.1,
22.8,
21.8,
20.2,
19.1,
18.0,
17.4,
]
Returns:
OptimizationParameters: Configured optimization parameters
"""
# PV Forecast (in W)
pv_forecast = np.zeros(48)
pv_forecast[12] = 5000
# Electricity Price (in Euro per Wh)
strompreis_euro_pro_wh = np.full(48, 0.001)
strompreis_euro_pro_wh[0:10] = 0.00001
strompreis_euro_pro_wh[11:15] = 0.00005
strompreis_euro_pro_wh[20] = 0.00001
# [
# 0.0000384,
# 0.0000318,
# 0.0000284,
# 0.0008283,
# 0.0008289,
# 0.0008334,
# 0.0008290,
# 0.0003302,
# 0.0003042,
# 0.0002430,
# 0.0002280,
# 0.0002212,
# 0.0002093,
# 0.0001879,
# 0.0001838,
# 0.0002004,
# 0.0002198,
# 0.0002270,
# 0.0002997,
# 0.0003195,
# 0.0003081,
# 0.0002969,
# 0.0002921,
# 0.0002780,
# 0.0003384,
# 0.0003318,
# 0.0003284,
# 0.0003283,
# 0.0003289,
# 0.0003334,
# 0.0003290,
# 0.0003302,
# 0.0003042,
# 0.0002430,
# 0.0002280,
# 0.0002212,
# 0.0002093,
# 0.0001879,
# 0.0001838,
# 0.0002004,
# 0.0002198,
# 0.0002270,
# 0.0002997,
# 0.0003195,
# 0.0003081,
# 0.0002969,
# 0.0002921,
# 0.0002780,
# ]
# Temperature Forecast (in degree C)
temperature_forecast = [
18.3,
17.8,
16.9,
16.2,
15.6,
15.1,
14.6,
14.2,
14.3,
14.8,
15.7,
16.7,
17.4,
18.0,
18.6,
19.2,
19.1,
18.7,
18.5,
17.7,
16.2,
14.6,
13.6,
13.0,
12.6,
12.2,
11.7,
11.6,
11.3,
11.0,
10.7,
10.2,
11.4,
14.4,
16.4,
18.3,
19.5,
20.7,
21.9,
22.7,
23.1,
23.1,
22.8,
21.8,
20.2,
19.1,
18.0,
17.4,
]
# Overall System Load (in W)
gesamtlast = [
676.71,
876.19,
527.13,
468.88,
531.38,
517.95,
483.15,
472.28,
1011.68,
995.00,
1053.07,
1063.91,
1320.56,
1132.03,
1163.67,
1176.82,
1216.22,
1103.78,
1129.12,
1178.71,
1050.98,
988.56,
912.38,
704.61,
516.37,
868.05,
694.34,
608.79,
556.31,
488.89,
506.91,
804.89,
1141.98,
1056.97,
992.46,
1155.99,
827.01,
1257.98,
1232.67,
871.26,
860.88,
1158.03,
1222.72,
1221.04,
949.99,
987.01,
733.99,
592.97,
]
# Electricity Price (in Euro per Wh)
strompreis_euro_pro_wh = np.full(48, 0.001)
strompreis_euro_pro_wh[0:10] = 0.00001
strompreis_euro_pro_wh[11:15] = 0.00005
strompreis_euro_pro_wh[20] = 0.00001
# Start Solution (binary)
start_solution = None
# Overall System Load (in W)
gesamtlast = [
676.71,
876.19,
527.13,
468.88,
531.38,
517.95,
483.15,
472.28,
1011.68,
995.00,
1053.07,
1063.91,
1320.56,
1132.03,
1163.67,
1176.82,
1216.22,
1103.78,
1129.12,
1178.71,
1050.98,
988.56,
912.38,
704.61,
516.37,
868.05,
694.34,
608.79,
556.31,
488.89,
506.91,
804.89,
1141.98,
1056.97,
992.46,
1155.99,
827.01,
1257.98,
1232.67,
871.26,
860.88,
1158.03,
1222.72,
1221.04,
949.99,
987.01,
733.99,
592.97,
]
# Define parameters for the optimization problem
parameters = OptimizationParameters(
**{
"ems": {
# Value of energy in battery (per Wh)
"preis_euro_pro_wh_akku": 0e-05,
# Feed-in tariff for exporting electricity (per Wh)
"einspeiseverguetung_euro_pro_wh": 7e-05,
# Overall load on the system
"gesamtlast": gesamtlast,
# PV generation forecast (48 hours)
"pv_prognose_wh": pv_forecast,
# Electricity price forecast (48 hours)
"strompreis_euro_pro_wh": strompreis_euro_pro_wh,
},
"pv_akku": {
# Battery capacity (in Wh)
"kapazitaet_wh": 26400,
# Initial state of charge (SOC) of PV battery (%)
"start_soc_prozent": 15,
# Minimum Soc PV Battery
"min_soc_prozent": 15,
},
"eauto": {
# Minimum SOC for electric car
"min_soc_prozent": 50,
# Electric car battery capacity (Wh)
"kapazitaet_wh": 60000,
# Charging efficiency of the electric car
"lade_effizienz": 0.95,
# Charging power of the electric car (W)
"max_ladeleistung_w": 11040,
# Current SOC of the electric car (%)
"start_soc_prozent": 5,
},
# "dishwasher": {
# # Household appliance consumption (Wh)
# "consumption_wh": 5000,
# # Duration of appliance usage (hours)
# "duration_h": 0,
# },
# Temperature forecast (48 hours)
"temperature_forecast": temperature_forecast,
# Initial solution for the optimization
"start_solution": start_solution,
}
)
# Start Solution (binary)
start_solution = None
# Startzeit nehmen
start_time = time.time()
# Initialize the optimization problem using the default configuration
working_dir = get_working_dir()
config = load_config(working_dir)
opt_class = optimization_problem(config, verbose=True, fixed_seed=42)
# Perform the optimisation based on the provided parameters and start hour
ergebnis = opt_class.optimierung_ems(parameters=parameters, start_hour=start_hour)
# Endzeit nehmen
end_time = time.time()
# Berechnete Zeit ausgeben
elapsed_time = end_time - start_time
print(f"Elapsed time: {elapsed_time:.4f} seconds")
# Define parameters for the optimization problem
return OptimizationParameters(
**{
"ems": {
"preis_euro_pro_wh_akku": 0e-05,
"einspeiseverguetung_euro_pro_wh": 7e-05,
"gesamtlast": gesamtlast,
"pv_prognose_wh": pv_forecast,
"strompreis_euro_pro_wh": strompreis_euro_pro_wh,
},
"pv_akku": {
"kapazitaet_wh": 26400,
"start_soc_prozent": 15,
"min_soc_prozent": 15,
},
"eauto": {
"min_soc_prozent": 50,
"kapazitaet_wh": 60000,
"lade_effizienz": 0.95,
"max_ladeleistung_w": 11040,
"start_soc_prozent": 5,
},
"temperature_forecast": temperature_forecast,
"start_solution": start_solution,
}
)
print(ergebnis.model_dump())
def run_optimization(start_hour: int = 0, verbose: bool = False) -> dict:
"""Run the optimization problem.
Args:
start_hour (int, optional): Starting hour for optimization. Defaults to 0.
verbose (bool, optional): Whether to print verbose output. Defaults to False.
Returns:
dict: Optimization result as a dictionary
"""
# Initialize the optimization problem using the default configuration
config_eos = get_config()
config_eos.merge_settings_from_dict({"prediction_hours": 48, "optimization_hours": 24})
opt_class = optimization_problem(verbose=verbose, fixed_seed=42)
# Prepare parameters
parameters = prepare_optimization_parameters()
# Perform the optimisation based on the provided parameters and start hour
result = opt_class.optimierung_ems(parameters=parameters, start_hour=start_hour)
return result.model_dump()
def main():
"""Main function to run the optimization script with optional profiling."""
parser = argparse.ArgumentParser(description="Run Energy Optimization Simulation")
parser.add_argument("--profile", action="store_true", help="Enable performance profiling")
parser.add_argument(
"--verbose", action="store_true", help="Enable verbose output during optimization"
)
parser.add_argument(
"--start-hour", type=int, default=0, help="Starting hour for optimization (default: 0)"
)
args = parser.parse_args()
if args.profile:
# Run with profiling
profiler = cProfile.Profile()
try:
result = profiler.runcall(
run_optimization, start_hour=args.start_hour, verbose=args.verbose
)
# Print profiling statistics
stats = pstats.Stats(profiler)
stats.strip_dirs().sort_stats("cumulative").print_stats(200)
# Print result
print("\nOptimization Result:")
print(result)
except Exception as e:
print(f"Error during optimization: {e}", file=sys.stderr)
sys.exit(1)
else:
# Run without profiling
try:
start_time = time.time()
result = run_optimization(start_hour=args.start_hour, verbose=args.verbose)
end_time = time.time()
elapsed_time = end_time - start_time
print(f"\nElapsed time: {elapsed_time:.4f} seconds.")
print("\nOptimization Result:")
print(result)
except Exception as e:
print(f"Error during optimization: {e}", file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
main()