-
Notifications
You must be signed in to change notification settings - Fork 2
/
smart_nanogrid_environment.py
369 lines (304 loc) · 19.6 KB
/
smart_nanogrid_environment.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import json
import random
import numpy as np
import gym
from gym import spaces
from gym.utils import seeding
from scipy.io import savemat
import time
from smart_nanogrid_gym.utils.central_management_system import CentralManagementSystem
from smart_nanogrid_gym.utils.charging_station import ChargingStation
from smart_nanogrid_gym.utils.pv_system_manager import PVSystemManager
from ..utils.config import data_files_directory_path, solvers_files_directory_path
# Todo: Feat: Set possibility of using different filetypes for saving and loading only for predictions
# Todo: Feat: Add stohasticity in vehicle departures
# Todo: Feat: Add model training visualisation using plotly or matplotlib
# Todo: Feat: Add penalty for discharging vehicles (v2v) if it happens except for steps in which some other vehicle is
# departing or plans to depart in next n steps
# Todo: Feat: Add possibility to load model specifications from json or csv..., e.g. load pricing model for energy
# Todo: Feat: Add possibility for using pv system data from the paper I'm writing
# Todo: Train models in DDPG and PPO for these cases: a) basic, only battery, only PV, battery and PV, only v2x,
# only v2g, only v2v, v2v and battery, v2v and PV, v2g and battery, v2g and PV, v2v and battery and PV,
# v2g and battery and PV, v2x and battery and PV
class SmartNanogridEnv(gym.Env):
def __init__(self, price_model=0, number_of_chargers=8, pv_system_available_in_model=True, battery_system_available_in_model=True,
vehicle_to_everything=False, enable_different_vehicle_battery_capacities=True, enable_requested_state_of_charge=False,
algorithm_used='', environment_mode='', time_interval='', charging_mode='', vehicle_uncharged_penalty_mode=''):
# Todo: Feat: Add possibility to specify whether to use same capacity or different ones for vehicle battery
self.CURRENT_PRICE_MODEL = price_model
self.NUMBER_OF_CHARGERS = number_of_chargers
self.PV_SYSTEM_AVAILABLE_IN_MODEL = pv_system_available_in_model
self.BATTERY_SYSTEM_AVAILABLE_IN_MODEL = battery_system_available_in_model
self.VEHICLE_TO_EVERYTHING = vehicle_to_everything
self.ALGORITHM_USED = algorithm_used
self.ENVIRONMENT_MODE = environment_mode
self.REQUESTED_TIME_INTERVAL = time_interval
self.TIME_INTERVAL = self.set_time_interval(time_interval)
self.CHARGING_MODE = charging_mode
self.VEHICLE_UNCHARGED_PENALTY_MODE = vehicle_uncharged_penalty_mode
self.NUMBER_OF_DAYS_TO_PREDICT = 1
self.NUMBER_OF_HOURS_AHEAD = 3
self.central_management_system = CentralManagementSystem(self.BATTERY_SYSTEM_AVAILABLE_IN_MODEL,
self.PV_SYSTEM_AVAILABLE_IN_MODEL,
self.VEHICLE_TO_EVERYTHING, self.CURRENT_PRICE_MODEL,
self.NUMBER_OF_DAYS_TO_PREDICT, self.TIME_INTERVAL,
self.NUMBER_OF_CHARGERS,
enable_different_vehicle_battery_capacities,
enable_requested_state_of_charge,
self.CHARGING_MODE, self.VEHICLE_UNCHARGED_PENALTY_MODE)
self.timestep = None
self.info = None
self.random_pv_shift_ratio = 1.0
self.grid_energy_per_timestep, self.solar_energy_utilization_per_timestep = None, None
self.total_cost_per_timestep, self.total_vehicle_penalty_per_timestep = None, None
self.total_penalty_per_timestep, self.total_battery_penalty_per_timestep = None, None
self.battery_per_timestep, self.grid_energy_cost_per_timestep = None, None
self.initial_battery_per_simulation_day, self.grid_power_per_timestep = None, None
self.battery_soc_below_dod_penalty_per_timestep = None
self.low_resource_utilisation_penalty_per_timestep = None
self.battery_overcharging_penalty_per_timestep = None
self.battery_over_discharging_penalty_per_timestep = None
self.insufficiently_charged_vehicle_penalty_per_timestep = None
self.needlessly_charged_vehicle_penalty_per_timestep = None
self.overcharged_vehicle_penalty_per_timestep = None
self.over_discharged_vehicle_penalty_per_timestep = None
self.battery_calculated_power_value_per_timestep = None
self.battery_action_per_timestep, self.charger_actions_per_timestep = None, None
self.total_charging_power_per_timestep, self.total_discharging_power_per_timestep = None, None
self.charger_power_values_per_timestep, self.battery_power_value_per_timestep = None, None
self.dis_charging_nonexistent_vehicles_penalty_per_timestep = None
self.simulated_single_day = False
amount_of_observed_variables = 1 + int(self.PV_SYSTEM_AVAILABLE_IN_MODEL)
number_of_observed_charger_values = 2
amount_of_charger_predictions = self.NUMBER_OF_CHARGERS * number_of_observed_charger_values
amount_of_states = amount_of_observed_variables + (self.NUMBER_OF_HOURS_AHEAD * amount_of_observed_variables)
self.total_amount_of_states = amount_of_states + amount_of_charger_predictions + int(self.BATTERY_SYSTEM_AVAILABLE_IN_MODEL)
spaces_low = np.array(np.zeros(self.total_amount_of_states), dtype=np.float32)
spaces_high = np.array(np.ones(self.total_amount_of_states), dtype=np.float32)
if self.BATTERY_SYSTEM_AVAILABLE_IN_MODEL:
if self.VEHICLE_TO_EVERYTHING:
actions_low = np.array(np.ones(self.NUMBER_OF_CHARGERS + 1), dtype=np.float32) * (-1)
else:
actions_low = np.array(np.zeros(self.NUMBER_OF_CHARGERS), dtype=np.float32)
actions_low = np.insert(actions_low, self.NUMBER_OF_CHARGERS, -1)
actions_high = np.array(np.ones(self.NUMBER_OF_CHARGERS + 1), dtype=np.float32)
self.action_space = spaces.Box(low=actions_low, high=actions_high, shape=(self.NUMBER_OF_CHARGERS + 1,),
dtype=np.float32)
else:
if self.VEHICLE_TO_EVERYTHING:
actions_low = -1
else:
actions_low = 0
actions_high = 1
self.action_space = spaces.Box(low=actions_low, high=actions_high, shape=(self.NUMBER_OF_CHARGERS,),
dtype=np.float32)
self.observation_space = spaces.Box(low=spaces_low, high=spaces_high, dtype=np.float32)
# Todo: Add look-ahead action_space for looking at agents planned actions to see will departing vehicles be
# charged enough based on current action, and penalize wrong future actions
def set_time_interval(self, requested_time_interval):
# Method for setting time_interval by keyword argument from ['1h', '2h'...-> '?h'; '15min'...->'?min']
# Todo: Feat: Add security check for value provided as an argument
if requested_time_interval:
if 'h' in requested_time_interval:
time_interval = float(requested_time_interval.replace('h', ''))
return time_interval
elif 'min' in requested_time_interval:
time_interval = float(requested_time_interval.replace('min', '')) / 60.0
return time_interval
else:
raise ValueError('Wrong time interval was provided')
else:
return float(1)
def step(self, actions):
results = self.central_management_system.simulate(self.timestep, actions, self.random_pv_shift_ratio)
self.total_cost_per_timestep.append(results['Total cost'])
self.grid_energy_cost_per_timestep.append(results['Grid energy cost'])
self.grid_energy_per_timestep.append(results['Grid energy'])
self.grid_power_per_timestep.append(results['Grid power'])
self.solar_energy_utilization_per_timestep.append(results['Utilized solar energy'])
self.total_penalty_per_timestep.append(results['Total penalty'])
self.total_battery_penalty_per_timestep.append(results['Total battery penalty'])
self.battery_soc_below_dod_penalty_per_timestep.append(results['Battery soc below dod penalty'])
self.low_resource_utilisation_penalty_per_timestep.append(results['Low resource utilisation penalty'])
self.battery_overcharging_penalty_per_timestep.append(results['Battery overcharging penalty'])
self.battery_over_discharging_penalty_per_timestep.append(results['Battery over discharging penalty'])
self.total_vehicle_penalty_per_timestep.append(results['Total vehicle penalty'])
self.insufficiently_charged_vehicle_penalty_per_timestep.append(results['Insufficiently charged vehicles penalty'])
self.needlessly_charged_vehicle_penalty_per_timestep.append(results['Needlessly charged vehicles penalty'])
self.overcharged_vehicle_penalty_per_timestep.append(results['Overcharged vehicles penalty'])
self.over_discharged_vehicle_penalty_per_timestep.append(results['Over discharged vehicles penalty'])
self.battery_action_per_timestep.append(results['Battery action'])
self.charger_actions_per_timestep.append(results['Charger actions'])
self.total_charging_power_per_timestep.append(results['Total charging power'])
self.total_discharging_power_per_timestep.append(results['Total discharging power'])
self.charger_power_values_per_timestep.append(results['Charger power values'])
self.battery_power_value_per_timestep.append(results['Battery power value'])
self.battery_calculated_power_value_per_timestep.append(results['Battery calculated power value'])
self.battery_per_timestep.append(results['Battery state of charge'])
self.initial_battery_per_simulation_day = results['Initial battery state of charge']
self.dis_charging_nonexistent_vehicles_penalty_per_timestep.append(results['DisCharging nonexistent vehicles penalty'])
observations = self.__get_observations()
self.timestep = self.timestep + 1
self.simulated_single_day = self.__check_is_single_day_simulated()
if self.simulated_single_day:
self.timestep = 0
# To simulate different solar days
self.__save_prediction_results()
self.random_pv_shift_ratio = random.randint(0, 180) / 100
reward = -results['Total cost']
self.info = {}
out_of_scope = False
return observations, reward, self.simulated_single_day, out_of_scope, self.info
def __get_observations(self):
min_timesteps_ahead = self.timestep + 1
max_timesteps_ahead = min_timesteps_ahead + self.NUMBER_OF_HOURS_AHEAD
results = self.central_management_system.observe(self.timestep, min_timesteps_ahead, max_timesteps_ahead,
self.random_pv_shift_ratio)
if self.PV_SYSTEM_AVAILABLE_IN_MODEL:
normalized_disturbances_observation_at_current_timestep = np.array([results['solar_radiation'],
results['energy_price']])
normalized_predictions = np.concatenate((np.array([results['radiation_predictions']]),
np.array([results['price_predictions']])),
axis=None)
else:
normalized_disturbances_observation_at_current_timestep = np.array([results['energy_price']])
normalized_predictions = np.array([results['price_predictions']])
departures_array = np.array(results['departures'])
normalized_departures = departures_array / 24
if self.BATTERY_SYSTEM_AVAILABLE_IN_MODEL:
normalized_states = np.concatenate((
np.array(results['vehicles_state_of_charge']),
normalized_departures,
np.array(results['battery_soc'])),
axis=None
)
else:
normalized_states = np.concatenate((
np.array(results['vehicles_state_of_charge']),
normalized_departures),
axis=None
)
observations = np.concatenate((
normalized_disturbances_observation_at_current_timestep,
normalized_predictions,
normalized_states),
axis=None, dtype=np.float32
)
return observations
def __check_is_single_day_simulated(self):
if self.timestep == (24.0 / self.TIME_INTERVAL):
return True
else:
return False
def __save_prediction_results(self):
if self.PV_SYSTEM_AVAILABLE_IN_MODEL:
available_solar_energy = self.central_management_system.pv_system_manager.get_available_solar_energy()
available_solar_energy = available_solar_energy.tolist()
else:
available_solar_energy = []
prediction_results = {
'SOC': self.central_management_system.charging_station.get_vehicles_state_of_charge().tolist(),
'Grid_power': self.grid_power_per_timestep,
'Grid_energy': self.grid_energy_per_timestep,
'Utilized_solar_energy': self.solar_energy_utilization_per_timestep,
'Total_vehicle_penalties': self.total_vehicle_penalty_per_timestep,
'Total_battery_penalties': self.total_battery_penalty_per_timestep,
'Total_penalties': self.total_penalty_per_timestep,
'Available_solar_energy': available_solar_energy,
'Total_cost': self.total_cost_per_timestep,
'Battery_state_of_charge': self.battery_per_timestep,
'Initial_battery_state_of_charge': self.initial_battery_per_simulation_day,
'Grid_energy_cost': self.grid_energy_cost_per_timestep,
'Battery_action': self.battery_action_per_timestep,
'Charger_actions': self.charger_actions_per_timestep,
'Total_charging_power': self.total_charging_power_per_timestep,
'Total_discharging_power': self.total_discharging_power_per_timestep,
'Charger_power_values': self.charger_power_values_per_timestep,
'Battery_power_value': self.battery_power_value_per_timestep,
'Battery_SOC_below_DoD_penalties': self.battery_soc_below_dod_penalty_per_timestep,
'Low_resource_utilisation_penalties': self.low_resource_utilisation_penalty_per_timestep,
'Battery_overcharging_penalties': self.battery_overcharging_penalty_per_timestep,
'Battery_over_discharging_penalties': self.battery_over_discharging_penalty_per_timestep,
'Insufficiently_charged_vehicle_penalties': self.insufficiently_charged_vehicle_penalty_per_timestep,
'Needlessly_charged_vehicle_penalties': self.needlessly_charged_vehicle_penalty_per_timestep,
'Overcharged_vehicle_penalties': self.overcharged_vehicle_penalty_per_timestep,
'Over_discharged_vehicle_penalties': self.over_discharged_vehicle_penalty_per_timestep,
'Battery_calculated_power_value': self.battery_calculated_power_value_per_timestep,
'DisCharging_nonexistent_vehicles_penalties': self.dis_charging_nonexistent_vehicles_penalty_per_timestep
}
with open(data_files_directory_path + "prediction_results.json", "w") as fp:
json.dump(prediction_results, fp, indent=4)
if self.BATTERY_SYSTEM_AVAILABLE_IN_MODEL and self.PV_SYSTEM_AVAILABLE_IN_MODEL and self.VEHICLE_TO_EVERYTHING:
model_variant_name = 'v2x-b-pv'
elif self.VEHICLE_TO_EVERYTHING:
model_variant_name = 'v2x'
elif self.BATTERY_SYSTEM_AVAILABLE_IN_MODEL and self.PV_SYSTEM_AVAILABLE_IN_MODEL:
model_variant_name = 'b-pv'
else:
model_variant_name = 'basic'
if self.ENVIRONMENT_MODE == 'training':
file_destination = 'training_files'
elif self.ENVIRONMENT_MODE == 'evaluation':
file_destination = 'evaluation_files'
elif self.ENVIRONMENT_MODE == 'prediction':
file_destination = 'single_prediction_files'
else:
file_destination = ''
saving_directory_path = solvers_files_directory_path + '\\RL\\' + file_destination + '\\'
file_name_prefix = f'{self.ALGORITHM_USED}'
file_name_root = f'{model_variant_name}-{self.CHARGING_MODE}-{self.VEHICLE_UNCHARGED_PENALTY_MODE}'
file_name_suffix = f'{self.NUMBER_OF_CHARGERS}ch-{self.REQUESTED_TIME_INTERVAL}'
file_name = f'{file_name_prefix}-{file_name_root}-{file_name_suffix}'
with open(saving_directory_path + file_name + "-prediction_results.json", "w") as fp:
json.dump(prediction_results, fp, indent=4)
self.central_management_system.charging_station.save_initial_values_to_json_file(saving_directory_path,
filename=file_name)
def reset(self, generate_new_initial_values=True, algorithm_used='', environment_mode='', **kwargs):
self.timestep = 0
self.simulated_single_day = False
self.total_cost_per_timestep = []
self.grid_power_per_timestep = []
self.grid_energy_per_timestep = []
self.solar_energy_utilization_per_timestep = []
self.total_vehicle_penalty_per_timestep = []
self.total_battery_penalty_per_timestep = []
self.total_penalty_per_timestep = []
self.battery_per_timestep = []
self.initial_battery_per_simulation_day = 0.0
self.grid_energy_cost_per_timestep = []
self.battery_action_per_timestep = []
self.charger_actions_per_timestep = []
self.total_charging_power_per_timestep = []
self.total_discharging_power_per_timestep = []
self.charger_power_values_per_timestep = []
self.battery_power_value_per_timestep = []
self.battery_soc_below_dod_penalty_per_timestep = []
self.low_resource_utilisation_penalty_per_timestep = []
self.battery_overcharging_penalty_per_timestep = []
self.battery_over_discharging_penalty_per_timestep = []
self.insufficiently_charged_vehicle_penalty_per_timestep = []
self.needlessly_charged_vehicle_penalty_per_timestep = []
self.overcharged_vehicle_penalty_per_timestep = []
self.over_discharged_vehicle_penalty_per_timestep = []
self.battery_calculated_power_value_per_timestep = []
self.dis_charging_nonexistent_vehicles_penalty_per_timestep = []
self.ALGORITHM_USED = algorithm_used if algorithm_used else self.ALGORITHM_USED
self.ENVIRONMENT_MODE = environment_mode if environment_mode else self.ENVIRONMENT_MODE
# Todo: Feat: Add reset to all subclasses and to price and pv if different models have different configs for them
self.__load_initial_simulation_values(generate_new_initial_values)
self.random_pv_shift_ratio = random.randint(0, 180) / 100
return self.__get_observations(), {}
def __load_initial_simulation_values(self, generate_new_initial_values):
if generate_new_initial_values:
self.central_management_system.charging_station.generate_new_initial_values(self.TIME_INTERVAL)
else:
self.central_management_system.charging_station.load_initial_values()
def render(self, mode="human"):
pass
def seed(self, seed=None):
# self.np_random, seed = seeding.np_random(seed)
# return [seed]
pass
def close(self):
# return 0
pass