2024-02-18 14:32:27 +01:00
from flask import Flask , jsonify , request
import numpy as np
from datetime import datetime
from modules . class_load import *
from modules . class_ems import *
from modules . class_pv_forecast import *
from modules . class_akku import *
2024-02-18 15:07:20 +01:00
from modules . class_strompreis import *
2024-02-18 21:28:02 +01:00
from modules . class_heatpump import *
2024-03-03 10:03:32 +01:00
from modules . class_generic_load import *
from modules . class_load_container import *
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-03-03 10:03:32 +01:00
2024-02-25 16:47:28 +01:00
prediction_hours = 48
2024-03-03 10:03:32 +01:00
date = ( datetime . now ( ) . date ( ) + timedelta ( hours = prediction_hours ) ) . strftime ( " % Y- % m- %d " )
2024-02-25 16:47:28 +01:00
date_now = datetime . now ( ) . strftime ( " % Y- % m- %d " )
akku_size = 30000 # Wh
2024-02-18 15:07:20 +01:00
year_energy = 2000 * 1000 #Wh
2024-02-25 16:47:28 +01:00
einspeiseverguetung_cent_pro_wh = np . full ( prediction_hours , 7 / ( 1000.0 * 100.0 ) ) # € / Wh
2024-02-18 14:32:27 +01:00
2024-02-25 16:47:28 +01:00
max_heizleistung = 1000 # 5 kW Heizleistung
2024-03-03 10:03:32 +01:00
wp = Waermepumpe ( max_heizleistung , prediction_hours )
2024-02-18 15:53:29 +01:00
2024-02-25 16:47:28 +01:00
akku = PVAkku ( akku_size , prediction_hours )
discharge_array = np . full ( prediction_hours , 1 )
2024-02-18 14:32:27 +01:00
2024-03-03 10:03:32 +01:00
#Gesamtlast
#############
gesamtlast = Gesamtlast ( )
2024-02-18 14:32:27 +01:00
# Load Forecast
2024-02-25 15:32:43 +01:00
###############
2024-02-18 14:32:27 +01:00
lf = LoadForecast ( filepath = r ' load_profiles.npz ' , year_energy = year_energy )
2024-02-25 16:47:28 +01:00
#leistung_haushalt = lf.get_daily_stats(date)[0,...] # Datum anpassen
leistung_haushalt = lf . get_stats_for_date_range ( date_now , date ) [ 0 , . . . ] . flatten ( )
2024-03-03 10:03:32 +01:00
gesamtlast . hinzufuegen ( " Haushalt " , leistung_haushalt )
# Generic Load
##############
# zusatzlast1 = generic_load()
# zusatzlast1.setze_last(24+12, 0.5, 2000) # Startet um 1 Uhr, dauert 0.5 Stunden, mit 2 kW
2024-02-25 16:47:28 +01:00
2024-02-18 14:32:27 +01:00
# PV Forecast
2024-02-25 15:32:43 +01:00
###############
2024-02-25 15:12:10 +01:00
#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 %2F Berlin&hourly=relativehumidity_2m % 2Cwindspeed_10m " )
2024-02-25 16:47:28 +01:00
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 )
2024-02-25 15:32:43 +01:00
2024-03-03 10:03:32 +01:00
2024-02-18 15:07:20 +01:00
# Strompreise
2024-02-25 15:32:43 +01:00
###############
filepath = os . path . join ( r ' test_data ' , r ' strompreise_akkudokAPI.json ' ) # Pfad zur JSON-Datei anpassen
#price_forecast = HourlyElectricityPriceForecast(source=filepath)
2024-02-25 16:47:28 +01:00
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 )
2024-02-18 14:32:27 +01:00
2024-02-18 21:28:02 +01:00
# WP
2024-03-03 10:03:32 +01:00
##############
2024-02-18 21:28:02 +01:00
leistung_wp = wp . simulate_24h ( temperature_forecast )
2024-03-03 10:03:32 +01:00
gesamtlast . hinzufuegen ( " Heatpump " , leistung_wp )
2024-02-25 15:12:10 +01:00
2024-03-03 10:03:32 +01:00
# print(gesamtlast.gesamtlast_berechnen())
# sys.exit()
2024-02-25 15:12:10 +01:00
2024-02-18 15:07:20 +01:00
# EMS / Stromzähler Bilanz
2024-03-03 10:03:32 +01:00
ems = EnergieManagementSystem ( akku , gesamtlast . gesamtlast_berechnen ( ) , pv_forecast , specific_date_prices , einspeiseverguetung_cent_pro_wh )
2024-02-25 16:47:28 +01:00
o = ems . simuliere_ab_jetzt ( )
pprint ( o )
2024-02-18 15:53:29 +01:00
pprint ( o [ " Gesamtbilanz_Euro " ] )
2024-03-03 10:03:32 +01:00
visualisiere_ergebnisse ( gesamtlast . gesamtlast_berechnen ( ) , leistung_haushalt , leistung_wp , pv_forecast , specific_date_prices , o )
sys . exit ( )
2024-02-18 15:53:29 +01:00
# Optimierung
# 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 )
2024-02-25 16:47:28 +01:00
o = ems . simuliere_ab_jetzt ( )
2024-02-18 15:53:29 +01:00
gesamtbilanz = o [ " Gesamtbilanz_Euro " ]
#print(individual, " ",gesamtbilanz)
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 )
2024-02-25 16:47:28 +01:00
toolbox . register ( " individual " , tools . initRepeat , creator . Individual , toolbox . attr_bool , prediction_hours )
2024-02-18 15:53:29 +01:00
toolbox . register ( " population " , tools . initRepeat , list , toolbox . individual )
toolbox . register ( " evaluate " , evaluate )
toolbox . register ( " mate " , tools . cxTwoPoint )
toolbox . register ( " mutate " , tools . mutFlipBit , indpb = 0.05 )
toolbox . register ( " select " , tools . selTournament , tournsize = 3 )
# Genetischer Algorithmus
def optimize ( ) :
population = toolbox . population ( n = 100 )
hof = tools . HallOfFame ( 1 )
stats = tools . Statistics ( lambda ind : ind . fitness . values )
stats . register ( " avg " , np . mean )
stats . register ( " min " , np . min )
stats . register ( " max " , np . max )
algorithms . eaSimple ( population , toolbox , cxpb = 0.5 , mutpb = 0.2 , 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 )
2024-02-25 16:47:28 +01:00
o = ems . simuliere_ab_jetzt ( )
2024-02-18 15:53:29 +01:00
pprint ( o [ " Gesamtbilanz_Euro " ] )
2024-02-18 14:32:27 +01:00
2024-02-18 15:07:20 +01:00
2024-02-18 21:28:02 +01:00
visualisiere_ergebnisse ( load , leistung_haushalt , leistung_wp , pv_forecast , specific_date_prices , o )
2024-02-18 15:07:20 +01:00
2024-02-18 14:32:27 +01: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():
# app = Flask(__name__)
# @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')
# 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')
# # Berechne den Tag des Jahres
# #day_of_year = date_obj.timetuple().tm_yday
# # 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())
# 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)