- Akku mit Verlusten + Bug in der test.py Gesamtlast kein update

This commit is contained in:
Bla Bla
2024-03-03 18:32:47 +01:00
parent 356abf1b08
commit 133bfc6727
6 changed files with 126 additions and 58 deletions

80
test.py
View File

@@ -20,7 +20,7 @@ import os
prediction_hours = 48
prediction_hours = 24
date = (datetime.now().date() + timedelta(hours = prediction_hours)).strftime("%Y-%m-%d")
date_now = datetime.now().strftime("%Y-%m-%d")
@@ -32,12 +32,13 @@ max_heizleistung = 1000 # 5 kW Heizleistung
wp = Waermepumpe(max_heizleistung,prediction_hours)
akku = PVAkku(akku_size,prediction_hours)
discharge_array = np.full(prediction_hours,1)
laden_moeglich = np.full(prediction_hours,1)
eauto = EAuto(soc=60, capacity = 60000, power_charge = 7000, load_allowed = laden_moeglich)
discharge_array = np.full(prediction_hours,1) #np.array([1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0]) #
laden_moeglich = np.full(prediction_hours,1) # np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0])
#np.full(prediction_hours,1)
eauto = EAuto(soc=10, capacity = 60000, power_charge = 7000, load_allowed = laden_moeglich)
min_soc_eauto = 20
hohe_strafe = 10.0
@@ -52,12 +53,14 @@ gesamtlast = Gesamtlast()
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)
# PV Forecast
###############
#PVforecast = PVForecast(filepath=os.path.join(r'test_data', r'pvprognose.json'))
PVforecast = PVForecast(url="https://api.akkudoktor.net/forecast?lat=50.8588&lon=7.3747&power=5400&azimuth=-10&tilt=7&powerInvertor=2500&horizont=20,40,30,30&power=4800&azimuth=-90&tilt=7&powerInvertor=2500&horizont=20,40,45,50&power=1480&azimuth=-90&tilt=70&powerInvertor=1120&horizont=60,45,30,70&power=1600&azimuth=5&tilt=60&powerInvertor=1200&horizont=60,45,30,70&past_days=5&cellCoEff=-0.36&inverterEfficiency=0.8&albedo=0.25&timezone=Europe%2FBerlin&hourly=relativehumidity_2m%2Cwindspeed_10m")
PVforecast = PVForecast(prediction_hours = prediction_hours, url="https://api.akkudoktor.net/forecast?lat=50.8588&lon=7.3747&power=5400&azimuth=-10&tilt=7&powerInvertor=2500&horizont=20,40,30,30&power=4800&azimuth=-90&tilt=7&powerInvertor=2500&horizont=20,40,45,50&power=1480&azimuth=-90&tilt=70&powerInvertor=1120&horizont=60,45,30,70&power=1600&azimuth=5&tilt=60&powerInvertor=1200&horizont=60,45,30,70&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)
temperature_forecast = PVforecast.get_temperature_for_date_range(date_now,date)
@@ -79,19 +82,20 @@ gesamtlast.hinzufuegen("Heatpump", leistung_wp)
# EAuto
######################
leistung_eauto = eauto.get_stuendliche_last()
soc_eauto = eauto.get_stuendlicher_soc()
gesamtlast.hinzufuegen("eauto", leistung_eauto)
print(gesamtlast.gesamtlast_berechnen())
# print(gesamtlast.gesamtlast_berechnen())
# EMS / Stromzähler Bilanz
ems = EnergieManagementSystem(akku, gesamtlast.gesamtlast_berechnen(), pv_forecast, specific_date_prices, einspeiseverguetung_cent_pro_wh)
o = ems.simuliere(0)#ems.simuliere_ab_jetzt()
pprint(o)
pprint(o["Gesamtbilanz_Euro"])
#pprint(o)
#pprint(o["Gesamtbilanz_Euro"])
visualisiere_ergebnisse(gesamtlast,leistung_haushalt,leistung_wp, pv_forecast, specific_date_prices, o)
#visualisiere_ergebnisse(gesamtlast,leistung_haushalt,leistung_wp, pv_forecast, specific_date_prices, o, soc_eauto)
#sys.exit()
@@ -100,23 +104,40 @@ visualisiere_ergebnisse(gesamtlast,leistung_haushalt,leistung_wp, pv_forecast, s
# Fitness-Funktion (muss Ihre EnergieManagementSystem-Logik integrieren)
def evaluate(individual):
# Hier müssen Sie Ihre Logik einbauen, um die Gesamtbilanz zu berechnen
# basierend auf dem gegebenen `individual` (discharge_array)
#akku.set_discharge_per_hour(individual)
ems.reset()
ems.set_akku_discharge_hours(individual)
o = ems.simuliere_ab_jetzt()
eauto.reset()
ems.set_akku_discharge_hours(individual[:prediction_hours])
eauto.set_laden_moeglich(individual[prediction_hours:])
eauto.berechne_ladevorgang()
leistung_eauto = eauto.get_stuendliche_last()
gesamtlast.hinzufuegen("eauto", leistung_eauto)
ems.set_gesamtlast(gesamtlast.gesamtlast_berechnen())
o = ems.simuliere(0)
gesamtbilanz = o["Gesamtbilanz_Euro"]
#print(individual, " ",gesamtbilanz)
# Überprüfung, ob der Mindest-SoC erreicht wird
final_soc = eauto.get_stuendlicher_soc()[-1] # 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
#if strafe > 0.0:
# print(min_soc_eauto," - ",final_soc,"*10 = ",strafe)
return (gesamtbilanz,)
# Werkzeug-Setup
creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
creator.create("Individual", list, fitness=creator.FitnessMin)
toolbox = base.Toolbox()
toolbox.register("attr_bool", random.randint, 0, 1)
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, prediction_hours)
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, prediction_hours*2)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
toolbox.register("evaluate", evaluate)
@@ -126,7 +147,7 @@ toolbox.register("select", tools.selTournament, tournsize=3)
# Genetischer Algorithmus
def optimize():
population = toolbox.population(n=100)
population = toolbox.population(n=500)
hof = tools.HallOfFame(1)
stats = tools.Statistics(lambda ind: ind.fitness.values)
@@ -134,20 +155,31 @@ def optimize():
stats.register("min", np.min)
stats.register("max", np.max)
algorithms.eaSimple(population, toolbox, cxpb=0.5, mutpb=0.2, ngen=100,
algorithms.eaSimple(population, toolbox, cxpb=0.4, mutpb=0.3, ngen=100,
stats=stats, halloffame=hof, verbose=True)
return hof[0]
best_solution = optimize()
print("Beste Lösung:", best_solution)
ems.set_akku_discharge_hours(best_solution)
o = ems.simuliere_ab_jetzt()
pprint(o["Gesamtbilanz_Euro"])
#ems.set_akku_discharge_hours(best_solution)
ems.reset()
eauto.reset()
ems.set_akku_discharge_hours(best_solution[:prediction_hours])
eauto.set_laden_moeglich(best_solution[prediction_hours:])
eauto.berechne_ladevorgang()
leistung_eauto = eauto.get_stuendliche_last()
gesamtlast.hinzufuegen("eauto", leistung_eauto)
ems.set_gesamtlast(gesamtlast.gesamtlast_berechnen())
o = ems.simuliere(0)
soc_eauto = eauto.get_stuendlicher_soc()
print(soc_eauto)
pprint(o)
pprint(eauto.get_stuendlicher_soc())
visualisiere_ergebnisse(load,leistung_haushalt,leistung_wp, pv_forecast, specific_date_prices, o)
visualisiere_ergebnisse(gesamtlast,leistung_haushalt,leistung_wp, pv_forecast, specific_date_prices, o,soc_eauto,best_solution[:prediction_hours],best_solution[prediction_hours:] )
# for data in forecast.get_forecast_data():