From b037b6dcfc39f12840d2b34777e547f0a5a27c70 Mon Sep 17 00:00:00 2001 From: Bla Bla Date: Wed, 17 Apr 2024 15:34:03 +0200 Subject: [PATCH] =?UTF-8?q?Violinenplot=20zur=20Optimierung=20/=20Zielfunk?= =?UTF-8?q?tionalen=20Worst=20/=20Best=20Case=20im=20Vergleich=20(bei=20Er?= =?UTF-8?q?f=C3=BCllung=20der=20Nebenbedingungen)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/class_optimize.py | 48 +++++++++++++++++++--------------- modules/visualize.py | 55 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 81 insertions(+), 22 deletions(-) diff --git a/modules/class_optimize.py b/modules/class_optimize.py index 120c54a..599bcce 100644 --- a/modules/class_optimize.py +++ b/modules/class_optimize.py @@ -110,16 +110,17 @@ class optimization_problem: final_soc = ems.eauto.ladezustand_in_prozent() # Nimmt den SoC am Ende des Optimierungszeitraums + eauto_roi = max(0,(parameter['eauto_min_soc']-ems.eauto.ladezustand_in_prozent()) ) + + individual.extra_data = (o["Gesamtbilanz_Euro"],o["Gesamt_Verluste"], eauto_roi ) + + + strafe = 0.0 - if worst_case: - strafe = abs(parameter['eauto_min_soc']-ems.eauto.ladezustand_in_prozent()) * self.strafe - gesamtbilanz += strafe - - gesamtbilanz -= o["Gesamt_Verluste"]/1000.0 - else: - strafe = max(0,(parameter['eauto_min_soc']-ems.eauto.ladezustand_in_prozent()) * self.strafe ) - gesamtbilanz += strafe - gesamtbilanz += o["Gesamt_Verluste"]/1000.0 + strafe = max(0,(parameter['eauto_min_soc']-ems.eauto.ladezustand_in_prozent()) * self.strafe ) + gesamtbilanz += strafe + gesamtbilanz += o["Gesamt_Verluste"]/1000.0 + return (gesamtbilanz,) @@ -128,7 +129,7 @@ class optimization_problem: # Genetischer Algorithmus def optimize(self,start_solution=None): - population = self.toolbox.population(n=100) + population = self.toolbox.population(n=300) hof = tools.HallOfFame(1) stats = tools.Statistics(lambda ind: ind.fitness.values) @@ -141,9 +142,19 @@ class optimization_problem: if start_solution is not None and start_solution != -1: population.insert(0, creator.Individual(start_solution)) - #algorithms.eaMuPlusLambda(population, self.toolbox, 100, 200, cxpb=0.3, mutpb=0.3, ngen=500, stats=stats, halloffame=hof, verbose=True) - algorithms.eaSimple(population, self.toolbox, cxpb=0.8, mutpb=0.8, ngen=400, stats=stats, halloffame=hof, verbose=True) - return hof[0] + #algorithms.eaMuPlusLambda(population, self.toolbox, 100, 200, cxpb=0.4, mutpb=0.5, ngen=500, stats=stats, halloffame=hof, verbose=True) + algorithms.eaSimple(population, self.toolbox, cxpb=0.4, mutpb=0.4, ngen=400, stats=stats, halloffame=hof, verbose=True) + + member = {"bilanz":[],"verluste":[],"nebenbedingung":[]} + for ind in population: + if hasattr(ind, 'extra_data'): + extra_value1, extra_value2,extra_value3 = ind.extra_data + member["bilanz"].append(extra_value1) + member["verluste"].append(extra_value2) + member["nebenbedingung"].append(extra_value3) + + + return hof[0], member def optimierung_ems(self,parameter=None, start_hour=None,worst_case=False): @@ -240,7 +251,7 @@ class optimization_problem: return self.evaluate(individual, ems, parameter,start_hour,worst_case) self.toolbox.register("evaluate", evaluate_wrapper) - start_solution = self.optimize(start_params) + start_solution, extra_data = self.optimize(start_params) best_solution = start_solution o = self.evaluate_inner(best_solution, ems,start_hour) eauto = ems.eauto.to_dict() @@ -256,13 +267,8 @@ class optimization_problem: print(o) - if worst_case: - visualisiere_ergebnisse(gesamtlast, pv_forecast, specific_date_prices, o,best_solution[0::2],best_solution[1::2] , temperature_forecast, start_hour, self.prediction_hours,einspeiseverguetung_euro_pro_wh,filename="visualisierungsergebnisse_worst.pdf") - os.system("scp visualisierungsergebnisse_worst.pdf andreas@192.168.1.135:") - else: - visualisiere_ergebnisse(gesamtlast, pv_forecast, specific_date_prices, o,best_solution[0::2],best_solution[1::2] , temperature_forecast, start_hour, self.prediction_hours,einspeiseverguetung_euro_pro_wh) - - os.system("scp visualisierungsergebnisse.pdf andreas@192.168.1.135:") + visualisiere_ergebnisse(gesamtlast, pv_forecast, specific_date_prices, o,best_solution[0::2],best_solution[1::2] , temperature_forecast, start_hour, self.prediction_hours,einspeiseverguetung_euro_pro_wh,extra_data=extra_data) + os.system("scp visualisierungsergebnisse.pdf andreas@192.168.1.135:") #print(eauto) return {"discharge_hours_bin":discharge_hours_bin, "eautocharge_hours_float":eautocharge_hours_float ,"result":o ,"eauto_obj":eauto,"start_solution":best_solution,"spuelstart":spuelstart_int} diff --git a/modules/visualize.py b/modules/visualize.py index 082406b..3f88a40 100644 --- a/modules/visualize.py +++ b/modules/visualize.py @@ -9,7 +9,7 @@ from matplotlib.backends.backend_pdf import PdfPages from datetime import datetime -def visualisiere_ergebnisse(gesamtlast, pv_forecast, strompreise, ergebnisse, discharge_hours, laden_moeglich, temperature, start_hour, prediction_hours,einspeiseverguetung_euro_pro_wh, filename="visualisierungsergebnisse.pdf"): +def visualisiere_ergebnisse(gesamtlast, pv_forecast, strompreise, ergebnisse, discharge_hours, laden_moeglich, temperature, start_hour, prediction_hours,einspeiseverguetung_euro_pro_wh, filename="visualisierungsergebnisse.pdf", extra_data=None): ##################### # 24h @@ -118,6 +118,7 @@ def visualisiere_ergebnisse(gesamtlast, pv_forecast, strompreise, ergebnisse, d ax1 = plt.subplot(3, 2, 3) for hour, value in enumerate(discharge_hours): #if value == 1: + print(hour) ax1.axvspan(hour, hour+1, color='red',ymax=value, alpha=0.3, label='Entlademöglichkeit' if hour == 0 else "") for hour, value in enumerate(laden_moeglich): #if value == 1: @@ -168,6 +169,58 @@ def visualisiere_ergebnisse(gesamtlast, pv_forecast, strompreise, ergebnisse, d plt.close() # Schließt die Figure + + + plt.figure(figsize=(14, 10)) + plt.subplot(1, 2, 1) + f1 = np.array(extra_data["verluste"]) + f2 = np.array(extra_data["bilanz"]) + n1 = np.array(extra_data["nebenbedingung"]) + scatter = plt.scatter(f1, f2, c=n1, cmap='viridis') + + # Farblegende hinzufügen + plt.colorbar(scatter, label='Nebenbedingung') + + pdf.savefig() # Speichert die komplette Figure im PDF + plt.close() # Schließt die Figure + + + plt.figure(figsize=(14, 10)) + filtered_verluste = np.array([v for v, n in zip(extra_data["verluste"], extra_data["nebenbedingung"]) if n < 0.01]) + filtered_bilanz = np.array([b for b, n in zip(extra_data["bilanz"], extra_data["nebenbedingung"]) if n< 0.01]) + + beste_verluste = min(filtered_verluste) + schlechteste_verluste = max(filtered_verluste) + beste_bilanz = min(filtered_bilanz) + schlechteste_bilanz = max(filtered_bilanz) + + data = [filtered_verluste, filtered_bilanz] + labels = ['Verluste', 'Bilanz'] + # Plot-Erstellung + fig, axs = plt.subplots(1, 2, figsize=(10, 6), sharey=False) # Zwei Subplots, getrennte y-Achsen + + # Erster Boxplot für Verluste + #axs[0].boxplot(data[0]) + axs[0].violinplot(data[0], + showmeans=True, + showmedians=True) + axs[0].set_title('Verluste') + axs[0].set_xticklabels(['Verluste']) + + # Zweiter Boxplot für Bilanz + axs[1].violinplot(data[1], + showmeans=True, + showmedians=True) + axs[1].set_title('Bilanz') + axs[1].set_xticklabels(['Bilanz']) + + # Feinabstimmung + plt.tight_layout() + + + pdf.savefig() # Speichert den aktuellen Figure-State im PDF + plt.close() + # plt.figure(figsize=(14, 10)) # # Kosten und Einnahmen pro Stunde # plt.subplot(1, 2, 1)