2024-02-18 14:32:27 +01:00
from flask import Flask , jsonify , request
import numpy as np
from datetime import datetime
2024-05-02 10:27:33 +02:00
from modules . class_optimize import *
# from modules.class_load import *
# from modules.class_ems import *
# from modules.class_pv_forecast import *
# from modules.class_akku import *
# from modules.class_strompreis import *
# from modules.class_heatpump import *
# from modules.class_load_container import *
# from modules.class_eauto import *
from modules . class_optimize import *
2024-03-03 10:46:46 +01:00
2024-02-18 14:32:27 +01:00
from pprint import pprint
2024-02-18 15:07:20 +01:00
import matplotlib . pyplot as plt
from modules . visualize import *
2024-02-18 15:53:29 +01:00
from deap import base , creator , tools , algorithms
import numpy as np
import random
2024-02-18 16:57:57 +01:00
import os
2024-02-18 14:32:27 +01:00
2024-08-30 11:49:44 +02:00
start_hour = 11
2024-02-25 16:47:28 +01:00
2024-08-21 08:34:31 +02:00
pv_forecast = [
0 ,
0 ,
0 ,
0 ,
0 ,
0 ,
0 ,
2024-08-30 11:49:44 +02:00
35.4104640357043 ,
436.191574979506 ,
734.585613834398 ,
914.346108603927 ,
1019.5228214119 ,
1766.84136350058 ,
5980.60975052259 ,
6236.00681862336 ,
5893.38154543782 ,
4309.88538120413 ,
3338.29004915145 ,
2177.55660706753 ,
1091.00542545193 ,
437.819525591319 ,
44.2226537829726 ,
0 ,
0 ,
2024-08-21 08:34:31 +02:00
0 ,
0 ,
2024-08-30 11:49:44 +02:00
- 0.0269415125679914 ,
2024-08-21 08:34:31 +02:00
0 ,
0 ,
0 ,
0 ,
2024-08-30 11:49:44 +02:00
25.5745140893473 ,
494.188146846569 ,
943.821134036728 ,
1458.66413119635 ,
1819.46147983229 ,
2127.45430524539 ,
2267.78128099068 ,
5944.86706099518 ,
5337.1322153025 ,
4376.56125932204 ,
3020.00212091936 ,
2414.53994231359 ,
1373.626161377 ,
517.764497317134 ,
35.619750070296 ,
2024-08-21 08:34:31 +02:00
0 ,
0
]
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 ,
18.6 ,
19.2 ,
19.1 ,
18.7 ,
18.5 ,
17.7 ,
16.2 ,
14.6 ,
13.6 ,
13 ,
12.6 ,
12.2 ,
11.7 ,
11.6 ,
11.3 ,
11 ,
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 ,
17.4
]
2024-02-18 14:32:27 +01:00
2024-08-21 08:34:31 +02:00
strompreis_euro_pro_wh = [
2024-08-30 11:49:44 +02:00
0.00033840228 ,
0.00033180228 ,
0.00032840228 ,
0.00032830228 ,
0.00032890228 ,
0.00033340228 ,
0.00032900228 ,
0.00033020228 ,
0.00030420228 ,
0.00024300228 ,
0.00022800228 ,
0.00022120228 ,
0.00020930228 ,
0.00018790228 ,
0.00018380228 ,
0.00020040228 ,
0.00021980228 ,
0.00022700228 ,
0.00029970228 ,
0.00031950228 ,
0.00030810228 ,
0.00029690228 ,
0.00029210228 ,
0.00027800228 ,
0.00033840228 ,
0.00033180228 ,
0.00032840228 ,
0.00032830228 ,
0.00032890228 ,
0.00033340228 ,
0.00032900228 ,
0.00033020228 ,
0.00030420228 ,
0.00024300228 ,
0.00022800228 ,
0.00022120228 ,
0.00020930228 ,
0.00018790228 ,
0.00018380228 ,
0.00020040228 ,
0.00021980228 ,
0.00022700228 ,
0.00029970228 ,
0.00031950228 ,
0.00030810228 ,
0.00029690228 ,
0.00029210228 ,
0.00027800228
2024-08-21 08:34:31 +02:00
]
gesamtlast = [
2024-08-30 11:49:44 +02:00
546.16318964697 ,
893.072526185525 ,
448.7325491406 ,
460.696954446666 ,
497.688171532182 ,
468.186120420737 ,
424.440426628658 ,
454.341890696582 ,
1070.45287392313 ,
1096.46234344204 ,
1199.71317588613 ,
1294.39989535284 ,
1459.42631059004 ,
1295.23757474948 ,
1304.65748778424 ,
1187.47511606455 ,
1309.49984671163 ,
1106.60773651081 ,
1098.98136451936 ,
2112.82264661039 ,
1143.37118921705 ,
858.863135790621 ,
787.018517493612 ,
693.683533270357 ,
545.860858342826 ,
892.702692835489 ,
448.372058076642 ,
460.284228901714 ,
498.457870099476 ,
469.01826331988 ,
424.293997897019 ,
454.660942633609 ,
1070.71990586461 ,
1096.44557410693 ,
1199.72027861112 ,
1294.75410706442 ,
1459.61174223338 ,
1295.77661554687 ,
1304.95591385395 ,
1188.62778631227 ,
1310.43099742786 ,
1108.58589249073 ,
1101.73849714744 ,
2114.05576978017 ,
1143.68031998738 ,
858.607350786608 ,
786.574111043611 ,
693.463415886943
2024-08-21 08:34:31 +02:00
]
2024-02-18 15:53:29 +01:00
2024-08-21 08:34:31 +02:00
start_solution = [
0 ,
1 ,
0 ,
1 ,
0 ,
1 ,
0 ,
0 ,
1 ,
1 ,
1 ,
0 ,
1 ,
0 ,
1 ,
0 ,
1 ,
0 ,
1 ,
0 ,
1 ,
0 ,
1 ,
0 ,
1 ,
0 ,
0 ,
0 ,
0 ,
0 ,
0 ,
0 ,
1 ,
0 ,
0 ,
0 ,
1 ,
0 ,
1 ,
0 ,
1 ,
0 ,
1 ,
0 ,
1 ,
0 ,
1 ,
0 ,
1 ,
0 ,
1 ,
0 ,
1 ,
0 ,
1 ,
0 ,
1 ,
0 ,
1 ,
0 ,
1 ,
0 ,
1 ,
0 ,
1 ,
0 ,
1 ,
0 ,
1 ,
0 ,
1 ,
0 ,
1 ,
1 ,
1 ,
1 ,
1 ,
1 ,
1 ,
1 ,
1 ,
1 ,
1 ,
1 ,
1 ,
1 ,
1 ,
1 ,
1 ,
1 ,
1 ,
1 ,
1 ,
1 ,
1 ,
1
]
2024-08-30 11:49:44 +02:00
parameter = { " preis_euro_pro_wh_akku " : 30e-05 , ' pv_soc ' : 60.4052 , ' pv_akku_cap ' : 30000 , ' year_energy ' : 4100000 , ' einspeiseverguetung_euro_pro_wh ' : 7e-05 , ' max_heizleistung ' : 1000 , " gesamtlast " : gesamtlast , ' pv_forecast ' : pv_forecast , " temperature_forecast " : temperature_forecast , " strompreis_euro_pro_wh " : strompreis_euro_pro_wh , ' eauto_min_soc ' : 80 , ' eauto_cap ' : 60000 , ' eauto_charge_efficiency ' : 0.95 , ' eauto_charge_power ' : 7590 , ' eauto_soc ' : 53 , ' pvpowernow ' : 211.137503624 , ' start_solution ' : start_solution , ' haushaltsgeraet_wh ' : 937 , ' haushaltsgeraet_dauer ' : 0 }
2024-03-03 10:46:46 +01:00
2024-08-24 10:22:49 +02:00
opt_class = optimization_problem ( prediction_hours = 48 , strafe = 10 , optimization_hours = 24 )
2024-05-02 10:27:33 +02:00
ergebnis = opt_class . optimierung_ems ( parameter = parameter , start_hour = start_hour )
2024-03-03 10:03:32 +01:00
2024-05-02 10:27:33 +02:00
# #Gesamtlast
# #############
# gesamtlast = Gesamtlast()
2024-03-03 10:03:32 +01:00
2024-05-02 10:27:33 +02:00
# # Load Forecast
# ###############
# lf = LoadForecast(filepath=r'load_profiles.npz', year_energy=year_energy)
# #leistung_haushalt = lf.get_daily_stats(date)[0,...] # Datum anpassen
# leistung_haushalt = lf.get_stats_for_date_range(date_now,date)[0,...].flatten()
# # print(date_now," ",date)
# # print(leistung_haushalt.shape)
# gesamtlast.hinzufuegen("Haushalt", leistung_haushalt)
2024-03-08 14:22:11 +01:00
2024-05-02 10:27:33 +02:00
# # PV Forecast
# ###############
# #PVforecast = PVForecast(filepath=os.path.join(r'test_data', r'pvprognose.json'))
# PVforecast = PVForecast(prediction_hours = prediction_hours, url="https://api.akkudoktor.net/forecast?lat=50.8588&lon=7.3747&power=5000&azimuth=-10&tilt=7&powerInvertor=10000&horizont=20,27,22,20&power=4800&azimuth=-90&tilt=7&powerInvertor=10000&horizont=30,30,30,50&power=1400&azimuth=-40&tilt=60&powerInvertor=2000&horizont=60,30,0,30&power=1600&azimuth=5&tilt=45&powerInvertor=1400&horizont=45,25,30,60&past_days=5&cellCoEff=-0.36&inverterEfficiency=0.8&albedo=0.25&timezone=Europe%2FBerlin&hourly=relativehumidity_2m%2Cwindspeed_10m")
# pv_forecast = PVforecast.get_pv_forecast_for_date_range(date_now,date) #get_forecast_for_date(date)
2024-02-25 15:32:43 +01:00
2024-05-02 10:27:33 +02:00
# temperature_forecast = PVforecast.get_temperature_for_date_range(date_now,date)
2024-02-25 15:32:43 +01:00
2024-03-03 10:03:32 +01:00
2024-02-25 15:12:10 +01:00
2024-05-02 10:27:33 +02:00
# # Strompreise
# ###############
# filepath = os.path.join (r'test_data', r'strompreise_akkudokAPI.json') # Pfad zur JSON-Datei anpassen
# #price_forecast = HourlyElectricityPriceForecast(source=filepath)
# price_forecast = HourlyElectricityPriceForecast(source="https://api.akkudoktor.net/prices?start="+date_now+"&end="+date+"")
# specific_date_prices = price_forecast.get_price_for_daterange(date_now,date)
# # print("13:",specific_date_prices[13])
# # print("14:",specific_date_prices[14])
# # print("15:",specific_date_prices[15])
# # sys.exit()
# # WP
# ##############
# leistung_wp = wp.simulate_24h(temperature_forecast)
# gesamtlast.hinzufuegen("Heatpump", leistung_wp)
2024-03-03 10:46:46 +01:00
2024-05-02 10:27:33 +02:00
# # EAuto
# ######################
# # leistung_eauto = eauto.get_stuendliche_last()
# # soc_eauto = eauto.get_stuendlicher_soc()
# # gesamtlast.hinzufuegen("eauto", leistung_eauto)
2024-02-25 15:12:10 +01:00
2024-05-02 10:27:33 +02:00
# # print(gesamtlast.gesamtlast_berechnen())
2024-03-03 10:03:32 +01:00
2024-05-02 10:27:33 +02:00
# # EMS / Stromzähler Bilanz
# #akku=None, pv_prognose_wh=None, strompreis_cent_pro_wh=None, einspeiseverguetung_cent_pro_wh=None, eauto=None, gesamtlast=None
2024-03-03 10:03:32 +01:00
2024-05-02 10:27:33 +02:00
# ems = EnergieManagementSystem(akku=akku, gesamtlast = gesamtlast, pv_prognose_wh=pv_forecast, strompreis_cent_pro_wh=specific_date_prices, einspeiseverguetung_cent_pro_wh=einspeiseverguetung_cent_pro_wh, eauto=eauto)
2024-03-25 14:40:48 +01:00
2024-03-03 10:03:32 +01:00
2024-05-02 10:27:33 +02:00
# o = ems.simuliere(start_hour)#ems.simuliere_ab_jetzt()
# #pprint(o)
# #pprint(o["Gesamtbilanz_Euro"])
2024-03-03 10:03:32 +01:00
2024-05-02 10:27:33 +02:00
# #visualisiere_ergebnisse(gesamtlast, pv_forecast, specific_date_prices, o,discharge_array,laden_moeglich, temperature_forecast, start_hour, prediction_hours)
2024-03-03 10:03:32 +01:00
2024-02-18 15:53:29 +01:00
2024-05-02 10:27:33 +02:00
# # Optimierung
# def evaluate_inner(individual):
# #print(individual)
# discharge_hours_bin = individual[0::2]
# eautocharge_hours_float = individual[1::2]
2024-03-04 12:37:47 +01:00
2024-05-02 10:27:33 +02:00
# #print(discharge_hours_bin)
# #print(len(eautocharge_hours_float))
# ems.reset()
# #eauto.reset()
# ems.set_akku_discharge_hours(discharge_hours_bin)
# ems.set_eauto_charge_hours(eautocharge_hours_float)
2024-03-03 18:32:47 +01:00
2024-05-02 10:27:33 +02:00
# #eauto.set_laden_moeglich(eautocharge_hours_float)
# #eauto.berechne_ladevorgang()
# #leistung_eauto = eauto.get_stuendliche_last()
# #gesamtlast.hinzufuegen("eauto", leistung_eauto)
2024-03-03 18:32:47 +01:00
2024-05-02 10:27:33 +02:00
# #ems.set_gesamtlast(gesamtlast.gesamtlast_berechnen())
2024-03-25 14:40:48 +01:00
2024-05-02 10:27:33 +02:00
# o = ems.simuliere(start_hour)
# return o, eauto
2024-03-04 12:37:47 +01:00
2024-05-02 10:27:33 +02:00
# # Fitness-Funktion (muss Ihre EnergieManagementSystem-Logik integrieren)
# def evaluate(individual):
# o,eauto = evaluate_inner(individual)
# gesamtbilanz = o["Gesamtbilanz_Euro"]
2024-03-03 18:32:47 +01:00
2024-05-02 10:27:33 +02:00
# # Überprüfung, ob der Mindest-SoC erreicht wird
# final_soc = eauto.ladezustand_in_prozent() # Nimmt den SoC am Ende des Optimierungszeitraums
# strafe = 0.0
# #if final_soc < min_soc_eauto:
# # Fügt eine Strafe hinzu, wenn der Mindest-SoC nicht erreicht wird
# strafe = max(0,(min_soc_eauto - final_soc) * hohe_strafe ) # `hohe_strafe` ist ein vorher festgelegter Strafwert
# gesamtbilanz += strafe
# gesamtbilanz += o["Gesamt_Verluste"]/1000.0
# return (gesamtbilanz,)
2024-02-18 15:53:29 +01:00
2024-03-03 18:32:47 +01:00
2024-03-04 12:37:47 +01:00
2024-05-02 10:27:33 +02:00
# # Werkzeug-Setup
# creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
# creator.create("Individual", list, fitness=creator.FitnessMin)
2024-02-18 15:53:29 +01:00
2024-05-02 10:27:33 +02:00
# toolbox = base.Toolbox()
2024-03-04 12:37:47 +01:00
2024-05-02 10:27:33 +02:00
# toolbox.register("attr_bool", random.randint, 0, 1)
# toolbox.register("attr_bool", random.randint, 0, 1)
# toolbox.register("individual", tools.initCycle, creator.Individual, (toolbox.attr_bool,toolbox.attr_bool), n=prediction_hours)
2024-03-04 12:37:47 +01:00
2024-05-02 10:27:33 +02:00
# toolbox.register("population", tools.initRepeat, list, toolbox.individual)
2024-02-18 15:53:29 +01:00
2024-05-02 10:27:33 +02:00
# toolbox.register("evaluate", evaluate)
# toolbox.register("mate", tools.cxTwoPoint)
# toolbox.register("mutate", tools.mutFlipBit, indpb=0.05)
# toolbox.register("select", tools.selTournament, tournsize=3)
2024-02-18 15:53:29 +01:00
2024-05-02 10:27:33 +02:00
# # Genetischer Algorithmus
# def optimize():
# population = toolbox.population(n=500)
# hof = tools.HallOfFame(1)
2024-02-18 15:53:29 +01:00
2024-05-02 10:27:33 +02:00
# stats = tools.Statistics(lambda ind: ind.fitness.values)
# stats.register("avg", np.mean)
# stats.register("min", np.min)
# stats.register("max", np.max)
2024-02-18 15:53:29 +01:00
2024-05-02 10:27:33 +02:00
# algorithms.eaMuPlusLambda(population, toolbox, 50, 100, cxpb=0.5, mutpb=0.5, ngen=500, stats=stats, halloffame=hof, verbose=True)
# #algorithms.eaSimple(population, toolbox, cxpb=0.2, mutpb=0.2, ngen=1000, stats=stats, halloffame=hof, verbose=True)
# return hof[0]
2024-03-04 12:37:47 +01:00
2024-05-02 10:27:33 +02:00
# start_solution = optimize()
2024-03-04 12:37:47 +01:00
2024-05-02 10:27:33 +02:00
# print("Start Lösung:", start_solution)
2024-03-04 12:37:47 +01:00
2024-05-02 10:27:33 +02:00
# # # Werkzeug-Setup
# # creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
# # creator.create("Individual", list, fitness=creator.FitnessMin)
2024-03-04 12:37:47 +01:00
2024-05-02 10:27:33 +02:00
# # toolbox = base.Toolbox()
2024-03-04 12:37:47 +01:00
2024-05-02 10:27:33 +02:00
# # toolbox.register("attr_bool", random.randint, 0, 1)
# # toolbox.register("attr_float", random.uniform, 0.0, 1.0)
# # toolbox.register("individual", tools.initCycle, creator.Individual, (toolbox.attr_bool,toolbox.attr_float), n=prediction_hours)
2024-03-04 12:37:47 +01:00
2024-05-02 10:27:33 +02:00
# # start_individual = toolbox.individual()
# # start_individual[:] = start_solution
2024-03-04 12:37:47 +01:00
2024-05-02 10:27:33 +02:00
# # toolbox.register("population", tools.initRepeat, list, toolbox.individual)
2024-03-04 12:37:47 +01:00
2024-05-02 10:27:33 +02:00
# # toolbox.register("evaluate", evaluate)
# # toolbox.register("mate", tools.cxTwoPoint)
# # toolbox.register("mutate", tools.mutFlipBit, indpb=0.05)
# # toolbox.register("select", tools.selTournament, tournsize=3)
2024-03-04 12:37:47 +01:00
2024-05-02 10:27:33 +02:00
# # # Genetischer Algorithmus
# # def optimize():
# # population = toolbox.population(n=1000)
# # population[0] = start_individual
# # hof = tools.HallOfFame(1)
2024-03-04 12:37:47 +01:00
2024-05-02 10:27:33 +02:00
# # stats = tools.Statistics(lambda ind: ind.fitness.values)
# # stats.register("avg", np.mean)
# # stats.register("min", np.min)
# # stats.register("max", np.max)
2024-03-04 12:37:47 +01:00
2024-05-02 10:27:33 +02:00
# # algorithms.eaMuPlusLambda(population, toolbox, 100, 200, cxpb=0.5, mutpb=0.2, ngen=1000, stats=stats, halloffame=hof, verbose=True)
# # #algorithms.eaSimple(population, toolbox, cxpb=0.2, mutpb=0.2, ngen=1000, stats=stats, halloffame=hof, verbose=True)
# # return hof[0]
2024-02-18 15:53:29 +01:00
2024-05-02 10:27:33 +02:00
# # best_solution = optimize()
# best_solution = start_solution
# print("Beste Lösung:", best_solution)
2024-02-18 15:53:29 +01:00
2024-05-02 10:27:33 +02:00
# #ems.set_akku_discharge_hours(best_solution)
# o,eauto = evaluate_inner(best_solution)
2024-02-18 14:32:27 +01:00
2024-05-02 10:27:33 +02:00
# # soc_eauto = eauto.get_stuendlicher_soc()
# # print(soc_eauto)
# # pprint(o)
# # pprint(eauto.get_stuendlicher_soc())
2024-03-08 14:22:11 +01:00
2024-03-25 14:40:48 +01:00
2024-05-02 10:27:33 +02:00
# #visualisiere_ergebnisse(gesamtlast,leistung_haushalt,leistung_wp, pv_forecast, specific_date_prices, o,soc_eauto,best_solution[0::2],best_solution[1::2] , temperature_forecast)
# visualisiere_ergebnisse(gesamtlast, pv_forecast, specific_date_prices, o,best_solution[0::2],best_solution[1::2] , temperature_forecast, start_hour, prediction_hours)
2024-02-18 15:07:20 +01:00
2024-05-02 10:27:33 +02:00
# # for data in forecast.get_forecast_data():
# # print(data.get_date_time(), data.get_dc_power(), data.get_ac_power(), data.get_windspeed_10m(), data.get_temperature())for data in forecast.get_forecast_data():
2024-02-18 14:32:27 +01:00
2024-05-02 10:27:33 +02:00
# # app = Flask(__name__)
2024-02-18 14:32:27 +01:00
2024-05-02 10:27:33 +02:00
# # @app.route('/getdata', methods=['GET'])
# # def get_data():
# # # Hole das Datum aus den Query-Parametern
# # date_str = request.args.get('date')
# # year_energy = request.args.get('year_energy')
2024-02-18 14:32:27 +01:00
2024-05-02 10:27:33 +02:00
# # try:
# # # Konvertiere das Datum in ein datetime-Objekt
# # date_obj = datetime.strptime(date_str, '%Y-%m-%d')
# # filepath = r'.\load_profiles.npz' # Pfad zur JSON-Datei anpassen
# # lf = cl.LoadForecast(filepath=filepath, year_energy=float(year_energy))
# # specific_date_prices = lf.get_daily_stats('2024-02-16')
2024-02-18 14:32:27 +01:00
2024-05-02 10:27:33 +02:00
# # # Berechne den Tag des Jahres
# # #day_of_year = date_obj.timetuple().tm_yday
2024-02-18 14:32:27 +01:00
2024-05-02 10:27:33 +02:00
# # # Konvertiere den Tag des Jahres in einen String, falls die Schlüssel als Strings gespeichert sind
# # #day_key = int(day_of_year)
# # #print(day_key)
# # # Überprüfe, ob der Tag im Jahr in den Daten vorhanden ist
# # array_list = lf.get_daily_stats(date_str)
# # pprint(array_list)
# # pprint(array_list.shape)
# # if array_list.shape == (2,24):
# # #if day_key < len(load_profiles_exp):
# # # Konvertiere das Array in eine Liste für die JSON-Antwort
# # #((load_profiles_exp_l[day_key]).tolist(),(load_profiles_std_l)[day_key].tolist())
2024-02-18 14:32:27 +01:00
2024-05-02 10:27:33 +02:00
# # return jsonify({date_str: array_list.tolist()})
# # else:
# # return jsonify({"error": "Datum nicht gefunden"}), 404
# # except ValueError:
# # # Wenn das Datum nicht im richtigen Format ist oder ungültig ist
# # return jsonify({"error": "Ungültiges Datum"}), 400
# # if __name__ == '__main__':
# # app.run(debug=True)
2024-02-18 14:32:27 +01:00