-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathuse_cases_analysis.py
283 lines (261 loc) · 10.2 KB
/
use_cases_analysis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# -*- coding: utf-8 -*-
"""
This is a script for analysis plot.
To use this script you will need plotly and kaleido. Install them using:
pip install plotly
pip install kaleido
Before running this script you should perform a perfect optimization for each type of cost function:
profit, cost and self-consumption
"""
import pathlib
import pandas as pd
import plotly.io as pio
from emhass.forecast import Forecast
from emhass.optimization import Optimization
from emhass.retrieve_hass import RetrieveHass
from emhass.utils import (
build_config,
build_params,
build_secrets,
get_days_list,
get_logger,
get_root,
get_yaml_parse,
)
pio.renderers.default = "browser"
pd.options.plotting.backend = "plotly"
# the root folder
root = pathlib.Path(str(get_root(__file__, num_parent=2)))
emhass_conf = {}
emhass_conf["data_path"] = root / "data/"
emhass_conf["root_path"] = root / "src/emhass/"
emhass_conf["docs_path"] = root / "docs/"
emhass_conf["config_path"] = root / "config.json"
emhass_conf["secrets_path"] = root / "secrets_emhass.yaml"
emhass_conf["defaults_path"] = emhass_conf["root_path"] / "data/config_defaults.json"
emhass_conf["associations_path"] = emhass_conf["root_path"] / "data/associations.csv"
# create logger
logger, ch = get_logger(__name__, emhass_conf, save_to_file=False)
def get_forecast_optim_objects(
retrieve_hass_conf, optim_conf, plant_conf, params, get_data_from_file
):
fcst = Forecast(
retrieve_hass_conf,
optim_conf,
plant_conf,
params,
emhass_conf,
logger,
get_data_from_file=get_data_from_file,
)
df_weather = fcst.get_weather_forecast(method="solar.forecast")
P_PV_forecast = fcst.get_power_from_weather(df_weather)
P_load_forecast = fcst.get_load_forecast(method=optim_conf["load_forecast_method"])
df_input_data_dayahead = pd.concat([P_PV_forecast, P_load_forecast], axis=1)
df_input_data_dayahead.columns = ["P_PV_forecast", "P_load_forecast"]
opt = Optimization(
retrieve_hass_conf,
optim_conf,
plant_conf,
fcst.var_load_cost,
fcst.var_prod_price,
"profit",
emhass_conf,
logger,
)
return fcst, P_PV_forecast, P_load_forecast, df_input_data_dayahead, opt
if __name__ == "__main__":
get_data_from_file = False
params = None
save_figures = False
# Build params with default config and secrets file
config = build_config(emhass_conf, logger, emhass_conf["defaults_path"])
_, secrets = build_secrets(
emhass_conf, logger, secrets_path=emhass_conf["secrets_path"], no_response=True
)
params = build_params(emhass_conf, secrets, config, logger)
retrieve_hass_conf, optim_conf, plant_conf = get_yaml_parse(params, logger)
rh = RetrieveHass(
retrieve_hass_conf["hass_url"],
retrieve_hass_conf["long_lived_token"],
retrieve_hass_conf["optimization_time_step"],
retrieve_hass_conf["time_zone"],
params,
emhass_conf,
logger,
)
days_list = get_days_list(retrieve_hass_conf["historic_days_to_retrieve"])
var_list = [
retrieve_hass_conf["sensor_power_load_no_var_loads"],
retrieve_hass_conf["sensor_power_photovoltaics"],
]
rh.get_data(
days_list, var_list, minimal_response=False, significant_changes_only=False
)
rh.prepare_data(
retrieve_hass_conf["sensor_power_load_no_var_loads"],
load_negative=retrieve_hass_conf["load_negative"],
set_zero_min=retrieve_hass_conf["set_zero_min"],
var_replace_zero=retrieve_hass_conf["sensor_replace_zero"],
var_interp=retrieve_hass_conf["sensor_linear_interp"],
)
df_input_data = rh.df_final.copy()
fcst, P_PV_forecast, P_load_forecast, df_input_data_dayahead, opt = (
get_forecast_optim_objects(
retrieve_hass_conf, optim_conf, plant_conf, params, get_data_from_file
)
)
df_input_data = fcst.get_load_cost_forecast(df_input_data)
df_input_data = fcst.get_prod_price_forecast(df_input_data)
template = "presentation"
y_axis_title = "Power (W)"
# Let's plot the input data
fig_inputs1 = df_input_data[
[
str(retrieve_hass_conf["sensor_power_photovoltaics"]),
str(retrieve_hass_conf["sensor_power_load_no_var_loads"] + "_positive"),
]
].plot()
fig_inputs1.layout.template = template
fig_inputs1.update_yaxes(title_text=y_axis_title)
fig_inputs1.update_xaxes(title_text="Time")
fig_inputs1.show()
if save_figures:
fig_inputs1.write_image(
emhass_conf["docs_path"] / "images/inputs_power.svg",
width=1080,
height=0.8 * 1080,
)
fig_inputs2 = df_input_data[["unit_load_cost", "unit_prod_price"]].plot()
fig_inputs2.layout.template = template
fig_inputs2.update_yaxes(title_text="Load cost and production sell price (EUR)")
fig_inputs2.update_xaxes(title_text="Time")
fig_inputs2.show()
if save_figures:
fig_inputs2.write_image(
emhass_conf["docs_path"] / "images/inputs_cost_price.svg",
width=1080,
height=0.8 * 1080,
)
fig_inputs_dah = df_input_data_dayahead.plot()
fig_inputs_dah.layout.template = template
fig_inputs_dah.update_yaxes(title_text=y_axis_title)
fig_inputs_dah.update_xaxes(title_text="Time")
fig_inputs_dah.show()
if save_figures:
fig_inputs_dah.write_image(
emhass_conf["docs_path"] / "images/inputs_dayahead.svg",
width=1080,
height=0.8 * 1080,
)
# Let's first perform a perfect optimization
opt_res = opt.perform_perfect_forecast_optim(df_input_data, days_list)
fig_res = opt_res[["P_deferrable0", "P_deferrable1", "P_grid"]].plot()
fig_res.layout.template = template
fig_res.update_yaxes(title_text=y_axis_title)
fig_res.update_xaxes(title_text="Time")
fig_res.show()
if save_figures:
fig_res.write_image(
emhass_conf["docs_path"]
/ "images/optim_results_PV_defLoads_perfectOptim.svg",
width=1080,
height=0.8 * 1080,
)
print(
"System with: PV, two deferrable loads, perfect optimization, profit >> total cost function sum: "
+ str(opt_res["cost_profit"].sum())
)
# And then perform a dayahead optimization
df_input_data_dayahead = fcst.get_load_cost_forecast(df_input_data_dayahead)
df_input_data_dayahead = fcst.get_prod_price_forecast(df_input_data_dayahead)
opt_res_dah = opt.perform_dayahead_forecast_optim(
df_input_data_dayahead, P_PV_forecast, P_load_forecast
)
fig_res_dah = opt_res_dah[["P_deferrable0", "P_deferrable1", "P_grid"]].plot()
fig_res_dah.layout.template = template
fig_res_dah.update_yaxes(title_text=y_axis_title)
fig_res_dah.update_xaxes(title_text="Time")
fig_res_dah.show()
if save_figures:
fig_res_dah.write_image(
emhass_conf["docs_path"]
/ "images/optim_results_PV_defLoads_dayaheadOptim.svg",
width=1080,
height=0.8 * 1080,
)
print(
"System with: PV, two deferrable loads, dayahead optimization, profit >> total cost function sum: "
+ str(opt_res_dah["cost_profit"].sum())
)
# Let's simplify to a system with only two deferrable loads, no PV installation
retrieve_hass_conf["solar_forecast_kwp"] = 0
fcst, P_PV_forecast, P_load_forecast, df_input_data_dayahead, opt = (
get_forecast_optim_objects(
retrieve_hass_conf, optim_conf, plant_conf, params, get_data_from_file
)
)
df_input_data_dayahead = fcst.get_load_cost_forecast(df_input_data_dayahead)
df_input_data_dayahead = fcst.get_prod_price_forecast(df_input_data_dayahead)
opt_res_dah = opt.perform_dayahead_forecast_optim(
df_input_data_dayahead, P_PV_forecast, P_load_forecast
)
fig_res_dah = opt_res_dah[["P_deferrable0", "P_deferrable1", "P_grid"]].plot()
fig_res_dah.layout.template = template
fig_res_dah.update_yaxes(title_text=y_axis_title)
fig_res_dah.update_xaxes(title_text="Time")
fig_res_dah.show()
if save_figures:
fig_res_dah.write_image(
emhass_conf["docs_path"]
/ "images/optim_results_defLoads_dayaheadOptim.svg",
width=1080,
height=0.8 * 1080,
)
print(
"System with: two deferrable loads, dayahead optimization, profit >> total cost function sum: "
+ str(opt_res_dah["cost_profit"].sum())
)
# Now a complete system with PV, Battery and two deferrable loads
retrieve_hass_conf["solar_forecast_kwp"] = 5
optim_conf["set_use_battery"] = True
fcst, P_PV_forecast, P_load_forecast, df_input_data_dayahead, opt = (
get_forecast_optim_objects(
retrieve_hass_conf, optim_conf, plant_conf, params, get_data_from_file
)
)
df_input_data_dayahead = fcst.get_load_cost_forecast(df_input_data_dayahead)
df_input_data_dayahead = fcst.get_prod_price_forecast(df_input_data_dayahead)
opt_res_dah = opt.perform_dayahead_forecast_optim(
df_input_data_dayahead, P_PV_forecast, P_load_forecast
)
fig_res_dah = opt_res_dah[
["P_deferrable0", "P_deferrable1", "P_grid", "P_batt"]
].plot()
fig_res_dah.layout.template = template
fig_res_dah.update_yaxes(title_text=y_axis_title)
fig_res_dah.update_xaxes(title_text="Time")
fig_res_dah.show()
if save_figures:
fig_res_dah.write_image(
emhass_conf["docs_path"]
/ "images/optim_results_PV_Batt_defLoads_dayaheadOptim.svg",
width=1080,
height=0.8 * 1080,
)
fig_res_dah = opt_res_dah[["SOC_opt"]].plot()
fig_res_dah.layout.template = template
fig_res_dah.update_yaxes(title_text="Battery State of Charge (%)")
fig_res_dah.update_xaxes(title_text="Time")
fig_res_dah.show()
if save_figures:
fig_res_dah.write_image(
emhass_conf["docs_path"]
/ "images/optim_results_PV_Batt_defLoads_dayaheadOptim_SOC.svg",
width=1080,
height=0.8 * 1080,
)
print(
"System with: PV, Battery, two deferrable loads, dayahead optimization, profit >> total cost function sum: "
+ str(opt_res_dah["cost_profit"].sum())
)