clean and update HEMS code

This commit is contained in:
Sadam93 2024-05-31 11:22:07 -04:00
parent 7cc3fd335c
commit 975e27741d

122
HEMS.py
View File

@ -7,15 +7,9 @@ import numpy as np
import pandas as pd import pandas as pd
import pylab as pl import pylab as pl
import pyomo.environ as py import pyomo.environ as py
from matplotlib import pyplot as plt
# NOTE: model
from matplotlib import pyplot as plt, gridspec
from matplotlib.pyplot import legend
from pyomo.core import value, RangeSet
# data = pd.ExcelFile('input_new_cluster.xlsx') #it will read the excel one time you do not need to read again and gain # data = pd.ExcelFile('input_new_cluster.xlsx') #it will read the excel one time you do not need to read again and gain
# Parse the different tab
sheet_data = pd.read_excel("input_new_cluster.xlsx", sheet_name='data') sheet_data = pd.read_excel("input_new_cluster.xlsx", sheet_name='data')
sheet_EV = pd.read_excel("input_new_cluster.xlsx", sheet_name='EV') sheet_EV = pd.read_excel("input_new_cluster.xlsx", sheet_name='EV')
sheet_EWH = pd.read_excel("input_new_cluster.xlsx", sheet_name='EWH') sheet_EWH = pd.read_excel("input_new_cluster.xlsx", sheet_name='EWH')
@ -28,9 +22,9 @@ sheet_ambient = pd.read_excel("input_new_cluster.xlsx", sheet_name='Ambient_Temp
sheet_load = pd.read_excel("input_new_cluster.xlsx", sheet_name='Base_Load') sheet_load = pd.read_excel("input_new_cluster.xlsx", sheet_name='Base_Load')
# </editor-fold> # </editor-fold>
# </editor-fold>
model = py.ConcreteModel() model = py.ConcreteModel()
# <editor-fold desc="Reading the data from Excel file"> # i think it should be outside the loop
# <editor-fold desc="Sets: time intervals"> # <editor-fold desc="Sets: time intervals">
model.h = py.Set(initialize=sheet_home.home) model.h = py.Set(initialize=sheet_home.home)
model.alpha = py.Param(initialize=39, doc='time of arrival ') # 6:30 = 39 model.alpha = py.Param(initialize=39, doc='time of arrival ') # 6:30 = 39
@ -38,16 +32,16 @@ model.beta = py.Param(initialize=89, doc='time of departure ') # 7:00 = 89
model.t_final = py.Param(initialize=96, doc='final time of the day ') model.t_final = py.Param(initialize=96, doc='final time of the day ')
model.second = py.Param(initialize=2, doc='second time of the day ') model.second = py.Param(initialize=2, doc='second time of the day ')
model.t = py.Set(initialize=sheet_data.time, ordered=True, doc='time period') model.t = py.Set(initialize=sheet_data.time, ordered=True, doc='time period')
model.EV_Dur = py.Set(initialize=RangeSet(model.alpha.value, model.beta.value), model.EV_Dur = py.Set(initialize=py.RangeSet(model.alpha.value, model.beta.value),
doc='time interval for EV') doc='time interval for EV')
model.EV_Dur1 = py.Set(initialize=RangeSet(model.alpha.value + 1, model.beta.value), model.EV_Dur1 = py.Set(initialize=py.RangeSet(model.alpha.value + 1, model.beta.value),
doc='time interval for EV after arrival') doc='time interval for EV after arrival')
model.t_second = py.Set(initialize=RangeSet(model.second.value, model.t_final.value), model.t_second = py.Set(initialize=py.RangeSet(model.second.value, model.t_final.value),
doc='time greater then 1') doc='time greater then 1')
model.t_first = py.Param(initialize=1, doc='first time of the day ') model.t_first = py.Param(initialize=1, doc='first time of the day ')
model.ist_interval = py.Set(initialize=RangeSet(model.t_first.value, model.alpha.value - 1), model.ist_interval = py.Set(initialize=py.RangeSet(model.t_first.value, model.alpha.value - 1),
doc='time period before the arrival time') doc='time period before the arrival time')
model.second_interval = py.Set(initialize=RangeSet(model.beta.value + 1, model.t_final.value), model.second_interval = py.Set(initialize=py.RangeSet(model.beta.value + 1, model.t_final.value),
doc='time interval after departure') doc='time interval after departure')
# </editor-fold> # </editor-fold>
@ -65,8 +59,9 @@ model.PV = py.Param(model.h, model.t, initialize=dict(zip(list(itertools.product
[i for x in sheet_PV.columns if "PV" in x for i in [i for x in sheet_PV.columns if "PV" in x for i in
sheet_PV.loc[:, x].values])), doc="PV Production") sheet_PV.loc[:, x].values])), doc="PV Production")
model.max = py.Param(initialize=100, doc='maximum value for selling and buying power') model.max = py.Param(initialize=100, doc='maximum value for selling and buying power')
# <editor-fold desc="Electric Vehicle Parameters"> # <editor-fold desc="Electric Vehicle Parameters">
# todo this is need to be done for single value but tow home has seperate values
model.ChargeRate_EV = py.Param(model.h, initialize=dict(zip(list(itertools.product(model.h.data())), model.ChargeRate_EV = py.Param(model.h, initialize=dict(zip(list(itertools.product(model.h.data())),
[i for x in sheet_EV.columns if "charging_rate" in x for i [i for x in sheet_EV.columns if "charging_rate" in x for i
in in
@ -125,10 +120,6 @@ model.DischRate_ESS = py.Param(model.h, initialize=dict(zip(list(itertools.produ
i in i in
sheet_ESS.loc[:, x].values])), sheet_ESS.loc[:, x].values])),
doc="Discharging rate of ESS") doc="Discharging rate of ESS")
# model.ChargeRate_ESS = py.Param(initialize=float(sheet_ESS.charging_rate1), doc='Charging rate of ESS ')
# model.DischRate_ESS = py.Param(initialize=float(sheet_ESS.discharging_rate1),
# doc='Discharging rate of ESS ')
model.Cap_ESS = py.Param(model.h, initialize=dict(zip((model.h.data()), model.Cap_ESS = py.Param(model.h, initialize=dict(zip((model.h.data()),
[i for x in sheet_ESS.columns if "capacity" in x for i in [i for x in sheet_ESS.columns if "capacity" in x for i in
sheet_ESS.loc[:, x].values])), doc="Capacity of ESS") sheet_ESS.loc[:, x].values])), doc="Capacity of ESS")
@ -217,14 +208,13 @@ model.water_use = py.Param(model.h, model.t,
sheet_wateruse.loc[:, x].values])), doc="Hot Water usage") sheet_wateruse.loc[:, x].values])), doc="Hot Water usage")
# </editor-fold2 # </editor-fold2
print('this is first home')
# <editor-fold desc="Price, Base load, and time duration parameters"> # <editor-fold desc="Price, Base load, and time duration parameters">
model.Buy_price = py.Param(model.t, initialize=dict(zip(sheet_data.time, sheet_data.Buy_price)), model.Buy_price = py.Param(model.t, initialize=dict(zip(sheet_data.time, sheet_data.Buy_price)),
doc='Buying Price') doc='Buying Price')
model.Sell_price = py.Param(model.t, initialize=dict(zip(sheet_data.time, sheet_data.Sell_price)), doc='Selling Price') model.Sell_price = py.Param(model.t, initialize=dict(zip(sheet_data.time, sheet_data.Sell_price)), doc='Selling Price')
# Time duration: we took 15 mint granularity so in one hour it will be 1/4 # Time duration: we took 15 mint granularity so in one hour it will be 1/4
model.time_d = py.Param(initialize=(1 / 4), doc='time duration ') model.time_d = py.Param(initialize=(1 / 4), doc='time duration ')
# model.DPT = py.Param(initialize=0.069, doc='Daily power tariff ') # .069
# </editor-fold> # </editor-fold>
# NOTE:Variable # NOTE:Variable
@ -317,47 +307,6 @@ model.Const_3 = py.Constraint(model.h, model.t, rule=Status_Power,
# </editor-fold> # </editor-fold>
# <editor-fold desc="flexibility">
# model.P_max = py.Var(model.h, model.t, bounds=(0, None), doc=" Updated maximum demand of house")
# model.P_flex = py.Var(model.h, model.t, bounds=(0, None), doc=" Flexibility available in one house")
# model.P_flex_all = py.Var(model.h, model.t, bounds=(0, None), doc=" Sum of the Flexibility in all")
# model.S_device = py.Var(model.h, model.t, within=py.Binary, doc=" Status of the devices") # but i not using this
# model.flex_index = py.Param(doc='flexibility index ')
#
# #Todo need to verify these equation with omer:
#
# #
# def P_h_flex(model, h, i):
# return model.P_flex_all[i] == sum(
# model.base_load[h, i] * model.S_device[h, i] - model.base_load[h, i] * model.S_device[h, i] for h in model.h
# for i in model.t)
#
#
# model.Const_4a = py.Constraint(model.h, model.t, rule=P_h_flex, doc='sum of the flexibility')
#
#
# def P_flexibility(model, h, i):
# return model.P_flex_all[i] == sum([model.P_flex[h, i] for h in model.h for i in model.t])
#
#
# model.Const_4b = py.Constraint(model.h, model.t, rule=P_flexibility, doc='sum of the flexibility')
#
#
# def Flexibility_index(model, h, i):
# return model.flex_index[h, i] == model.P_flex[h, i] / model.P_flex_all[i]
#
#
# model.Const_4c = py.Constraint(model.h, model.t, rule=Flexibility_index, doc='sum of the flexibility')
#
#
# # this equation make the two way communication
# def P_maximum(model, h, i):
# return model.P_max[h, i] == model.P_max[h, i - 1] - model.P_flex[h, i]
#
#
# model.Const_4d = py.Constraint(model.h, model.t, rule=P_maximum, doc='updated maximum power')
# </editor-fold> # </editor-fold>
# <editor-fold desc="Electric vehicle Constraints"> # <editor-fold desc="Electric vehicle Constraints">
@ -412,10 +361,6 @@ model.Const_EV_6 = py.Constraint(model.h, model.t, rule=Power_EV_Disch, doc='Dis
def SoC_EV1(model, h, i): def SoC_EV1(model, h, i):
return model.Energy_EV[h, i] == model.Energy_EV_In[h] return model.Energy_EV[h, i] == model.Energy_EV_In[h]
# + (
# model.P_ESS_Charge[h, i] * model.Ch_Effc_ESS[h] - model.P_ESS_Disch[h, i]) * model.time_d
model.Const_EV_7a = py.Constraint(model.h, [model.alpha.value], rule=SoC_EV1, doc='SoC of the EV at arrival') model.Const_EV_7a = py.Constraint(model.h, [model.alpha.value], rule=SoC_EV1, doc='SoC of the EV at arrival')
@ -426,14 +371,6 @@ def SoC_EV2(model, h, i):
model.Const_EV_7b = py.Constraint(model.h, model.EV_Dur1, rule=SoC_EV2, doc='SoC of the EV after arrival time') model.Const_EV_7b = py.Constraint(model.h, model.EV_Dur1, rule=SoC_EV2, doc='SoC of the EV after arrival time')
# one equation of the SoC
# def SoC_EV(model, i):
# return model.Energy_EV[i] == model.Energy_EV_In + (model.P_EV_Charge[i] * model.Ch_Effc_EV - model.P_EV_Disch[i]) * model.time_d
#
#
# model.Const_EV_7 = py.Constraint(model.EV_Dur, rule=SoC_EV, doc='SoC of the EV')
def EV_availability1(model, h, i): def EV_availability1(model, h, i):
return model.Energy_EV[h, i] == 0 return model.Energy_EV[h, i] == 0
@ -490,15 +427,9 @@ model.Const_EV_12 = py.Constraint(model.h, [model.beta.value], rule=EV_final_SoC
# </editor-fold> # </editor-fold>
# <editor-fold desc="Energy Storage System Constraints">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> # <editor-fold desc="Energy Storage System Constraints">
def SoC_ESS1(model, h, i): def SoC_ESS1(model, h, i):
return model.Energy_ESS[h, i] == model.Energy_ESS_In[h] return model.Energy_ESS[h, i] == model.Energy_ESS_In[h]
# #+ (
# model.P_ESS_Charge[h, i] * model.Ch_Effc_ESS[h] - model.P_ESS_Disch[h, i]) * model.time_d
model.Const_ESS_1a = py.Constraint(model.h, [model.t_first.value], rule=SoC_ESS1, model.Const_ESS_1a = py.Constraint(model.h, [model.t_first.value], rule=SoC_ESS1,
doc='SoC of ESS') # need to check wather we need to put in the brackets or not doc='SoC of ESS') # need to check wather we need to put in the brackets or not
@ -506,15 +437,11 @@ model.Const_ESS_1a = py.Constraint(model.h, [model.t_first.value], rule=SoC_ESS1
def SoC_ESS2(model, h, i): def SoC_ESS2(model, h, i):
return model.Energy_ESS[h, i] == model.Energy_ESS[h, i - 1] + ( return model.Energy_ESS[h, i] == model.Energy_ESS[h, i - 1] + (
model.P_ESS_Charge[h, i] * model.Ch_Effc_ESS[h] - model.P_ESS_Disch[h, i]) * model.time_d model.P_ESS_Charge[h, i] * model.Ch_Effc_ESS[h] - model.P_ESS_Disch[h, i]) * model.time_d
model.Const_ESS_1b = py.Constraint(model.h, model.t_second, rule=SoC_ESS2, doc='SoC of ESS') model.Const_ESS_1b = py.Constraint(model.h, model.t_second, rule=SoC_ESS2, doc='SoC of ESS')
def Power_ESS_Charge(model, h, i): def Power_ESS_Charge(model, h, i):
return model.P_ESS_Charge[h, i] == model.P_ESS_Charge_Grid[h, i] + model.PV_Battery[h, i] return model.P_ESS_Charge[h, i] == model.P_ESS_Charge_Grid[h, i] + model.PV_Battery[h, i]
model.Const_ESS_2 = py.Constraint(model.h, model.t, rule=Power_ESS_Charge, model.Const_ESS_2 = py.Constraint(model.h, model.t, rule=Power_ESS_Charge,
doc='Charging power of ESS ') doc='Charging power of ESS ')
@ -614,8 +541,6 @@ def EWH_power(model, h, i):
model.Const_EWH_3 = py.Constraint(model.h, model.t, rule=EWH_power, doc='Electric water heater power') model.Const_EWH_3 = py.Constraint(model.h, model.t, rule=EWH_power, doc='Electric water heater power')
#
# # NOTE: I write this function different from the GAMS
def EWH_temp_1(model, h, i): def EWH_temp_1(model, h, i):
return model.tetta_EWH_wat[h, i] == model.tetta_amb[h, i] + model.Q[h] * model.S_EWH[h, i] * model.R[ return model.tetta_EWH_wat[h, i] == model.tetta_amb[h, i] + model.Q[h] * model.S_EWH[h, i] * model.R[
h] * model.time_d - ( h] * model.time_d - (
@ -640,24 +565,14 @@ model.Const_EWH_5 = py.Constraint(model.h, model.t_second, rule=EWH_temp_2, doc=
# </editor-fold> # </editor-fold>
# <editor-fold desc="PV Constraints"> # need to include the PV equation of omer paper # <editor-fold desc="PV Constraints"> # need to include the PV equation of omer paper
# the below equation did not work got an error ( i dont know why!!!)
# def PV_production(model, h, i):
# return model.PV[ h,i] == model.PV_Grid[ h,i] + model.PV_Home[ h,i]
#
#
# model.Const_PV = py.Constraint(model.h, model.t, rule=PV_production, doc='PV Production')
def PV_production(model, h, i): def PV_production(model, h, i):
return model.PV_Grid[h, i] + model.PV_Home[h, i] + model.PV_Battery[h, i] == model.PV[h, i] return model.PV_Grid[h, i] + model.PV_Home[h, i] + model.PV_Battery[h, i] == model.PV[h, i]
model.Const_PV = py.Constraint(model.h, model.t, rule=PV_production, doc='PV Production') model.Const_PV = py.Constraint(model.h, model.t, rule=PV_production, doc='PV Production')
# </editor-fold> # </editor-fold>
# NOTE: I need to include the third term which is in the paper: daily peak demand * charge of the power
def objective_rule(model): def objective_rule(model):
return sum((model.P_Buy_Grid[h, i] * model.Buy_price[i]) - ( return sum((model.P_Buy_Grid[h, i] * model.Buy_price[i]) - (
model.P_Sell_Grid[h, i] * model.Sell_price[i]) for i in model.P_Sell_Grid[h, i] * model.Sell_price[i]) for i in
@ -671,13 +586,10 @@ model.write('model2.lp', io_options={'symbolic_solver_labels': True})
opt = py.SolverFactory('cplex') opt = py.SolverFactory('cplex')
opt.options["mipgap"] = 0.09 # 0.155 for load and 0.8 for other load opt.options["mipgap"] = 0.09 # 0.155 for load and 0.8 for other load
result = opt.solve(model, tee=True) result = opt.solve(model, tee=True)
# result = opt.solve(model, tee = True)
# model.pprint()
print(result) print(result)
# </editor-fold> # </editor-fold>
# <editor-fold desc="Check optimility"> # <editor-fold desc="Check optimility">
if (result.solver.status == py.SolverStatus.ok) and ( if (result.solver.status == py.SolverStatus.ok) and (
result.solver.termination_condition == py.TerminationCondition.optimal): result.solver.termination_condition == py.TerminationCondition.optimal):
print('optimal solution') # Do something when the solution in optimal and feasible print('optimal solution') # Do something when the solution in optimal and feasible
@ -799,10 +711,10 @@ for j in model.h:
SEWH[j - 1].append(py.value(v)) SEWH[j - 1].append(py.value(v))
for i in model.Buy_price: for i in model.Buy_price:
Buyprice.append(value(model.Buy_price[i])) Buyprice.append(py.value(model.Buy_price[i]))
for i in model.Sell_price: for i in model.Sell_price:
Sellprice.append(value(model.Sell_price[i])) Sellprice.append(py.value(model.Sell_price[i]))
# alone # alone
# for k in range(len(model.h)): # for k in range(len(model.h)):
@ -895,8 +807,7 @@ for i in model.Sell_price:
# ax[2,0].set_ylabel('Power (kW)') # ax[2,0].set_ylabel('Power (kW)')
# ax[0, k].set_title(f"Home {k+1}") # ax[0, k].set_title(f"Home {k+1}")
# #
# # pl.tight_layout()
#
# plt.show() # plt.show()
@ -914,7 +825,4 @@ for k in range(len(model.h)):
ax[1].set_ylabel('Electricity price ($/W/h)') ax[1].set_ylabel('Electricity price ($/W/h)')
plt.suptitle(f"Home {k+1}") plt.suptitle(f"Home {k+1}")
plt.show() plt.show()
# print('this is the end of code')
# # </editor-fold> # # </editor-fold>