-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrain_dqn.py
316 lines (291 loc) · 12.8 KB
/
train_dqn.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
from DQN import DAgent
import sys
from environment import *
from matplotlib import style
style.use('ggplot')
from vars import *
from itertools import count
import pickle as pkl
import os
import argparse
import sys
import torch
import pandas as pd
def train_dqn(ckpt,model_name,dynamic,soft, RL):
env = System(RL_building=RL)
scores = []
brain = DAgent(gamma=GAMMA, epsilon=EPSILON, batch_size=BATCH_SIZE, n_actions=N_ACTIONS,
input_dims=INPUT_DIMS, lr = LEARNING_RATE, eps_dec = EPS_DECAY, ckpt=ckpt)
for i_episode in range(NUM_EPISODES):
# Initialize the environment.rst and state
state = env.reset()
state = torch.tensor(state,dtype=torch.float).to(device)
# Normalizing data using an online algo
brain.normalizer.observe(state)
state = brain.normalizer.normalize(state).unsqueeze(0)
score = 0
for t in count():
# Select and perform an action
action = brain.select_action(state).type(torch.FloatTensor)
next_state, reward, done = env.step(action.item())
score += reward
reward = torch.tensor([reward],dtype=torch.float,device=device)
if not done:
next_state = torch.tensor(next_state,dtype=torch.float, device=device)
#normalize data using an online algo
brain.normalizer.observe(next_state)
next_state = brain.normalizer.normalize(next_state).unsqueeze(0)
else:
next_state = None
# Store the transition in memory
brain.memory.push(state, action, next_state, reward)
# Move to the next state
state = next_state
# Perform one step of the optimization (on the target network)
brain.optimize_model()
if done:
scores.append(score)
break
#sys.stdout.write('Finished episode {} with reward {}\n'.format(i_episode, score))
with open(os.getcwd() + '/data/output/' + model_name + '_rewards.txt', 'a') as f:
f.write('Episode {}, Reward {} \n'.format(i_episode, score))
# Soft update for target network:
if soft:
brain.soft_update(TAU)
# Update the target network, copying all weights and biases in DQN
else:
if i_episode % TARGET_UPDATE == 0:
brain.target_net.load_state_dict(brain.policy_net.state_dict())
model_params = {'NUM_EPISODES':NUM_EPISODES,
'EPSILON':EPSILON,
'EPS_DECAY':EPS_DECAY,
'LEARNING_RATE_':LEARNING_RATE,
'GAMMA':GAMMA,
'TARGET_UPDATE':TARGET_UPDATE,
'BATCH_SIZE':BATCH_SIZE,
'TIME_STEP_SIZE':TIME_STEP_SIZE,
'NUM_HOURS':NUM_HOURS,
'COMFORT_PENALTY':COMFORT_PENALTY,
'LOAD_PENALTY':LOAD_PENALTY,
'PRICE_PENALTY':PRICE_PENALTY,
'ZETA':ZETA}
scores.append(model_params)
with open(os.getcwd() + '/data/output/' + model_name + '_dynamic_' + str(dynamic) + '_rewards_dqn.pkl', 'wb') as f:
pkl.dump(scores,f)
# Saving the final model
torch.save(brain, os.getcwd() + model_name + 'model.pt')
print('Complete')
brain.epsilon = 0
brain.eps_end = 0
inside_temperatures_1 = []
inside_temperatures_2 = []
ambient_temperatures = []
times = []
base_loads = []
actions = []
for inside_temp_1 in np.arange(18, 22, 1 / 5):
print(inside_temp_1)
for inside_temp_2 in np.arange(18, 22, 1 / 5):
for ambient_temp in np.arange(-5, 5, 1 / 5):
for load in np.arange(0, 30, 1/2):
for time in range(0,24):
scaled_load = load/1000
state = [ambient_temp, scaled_load, inside_temp_1, inside_temp_2, time]
state = torch.tensor(state, dtype=torch.float).to(device)
state = brain.normalizer.normalize(state).unsqueeze(0)
action = brain.select_action(state).type(torch.FloatTensor).item()
inside_temperatures_1.append(inside_temp_1)
inside_temperatures_2.append(inside_temp_2)
ambient_temperatures.append(ambient_temp)
times.append(time)
base_loads.append(load)
actions.append(action)
eval_data = pd.DataFrame()
eval_data['Inside Temperatures 1'] = inside_temperatures_1
eval_data['Inside Temperatures 2'] = inside_temperatures_2
eval_data['Ambient Temperatures'] = ambient_temperatures
eval_data['Times'] = times
eval_data['Loads'] = base_loads
eval_data['Actions'] = actions
with open(os.getcwd() + '/data/output/' + model_name + 'policy_eval.pkl', 'wb') as f:
pkl.dump(eval_data, f)
env = System(eval=True, january=january, RL_building=RL)
# base_loads_2 = [env.buildings[1].base_load]
actions_building_1 = []
actions_building_2 = []
ambient_temperatures = [env.ambient_temperature]
total_loads = []
total_price = []
actions = []
rewards = []
print('Starting evaluation of the model')
state = env.reset()
state = torch.tensor(state, dtype=torch.float).to(device)
inside_temperatures_1 = [state[2].item()]
inside_temperatures_2 = [state[3].item()]
base_loads = [state[1].item()]
# Normalizing data using an online algo
brain.normalizer.observe(state)
state = brain.normalizer.normalize(state).unsqueeze(0)
for t_episode in range(NUM_TIME_STEPS):
action = brain.select_action(state).type(torch.FloatTensor)
actions.append(action.item())
next_state, reward, done = env.step(action.item())
rewards.append(reward)
inside_temperatures_1.append(next_state[2])
inside_temperatures_2.append(next_state[3])
actions_building_1.append(env.buildings[0].action)
actions_building_2.append(env.buildings[1].action)
base_loads.append(next_state[1])
# base_loads_2.append(env.buildings[1].base_load)
ambient_temperatures.append(env.ambient_temperature)
total_loads.append(env.total_load)
total_price.append(env.total_power_cost)
if not done:
next_state = torch.tensor(next_state, dtype=torch.float, device=device)
# normalize data using an online algo
brain.normalizer.observe(next_state)
next_state = brain.normalizer.normalize(next_state).unsqueeze(0)
else:
next_state = None
# Move to the next state
state = next_state
# actions.append(0)
# total_loads.append(env.total_load)
eval_data = pd.DataFrame()
eval_data['Inside Temperatures 1'] = inside_temperatures_1[:-1]
eval_data['Inside Temperatures 2'] = inside_temperatures_2[:-1]
eval_data['Base Loads'] = base_loads[:-1]
# eval_data['Base Loads 2'] = base_loads_2
eval_data['Actions 1'] = actions_building_1
eval_data['Actions 2'] = actions_building_2
eval_data['Ambient Temperatures'] = ambient_temperatures[:-1]
eval_data['Actions'] = actions
eval_data['Rewards'] = rewards
eval_data['Total Load'] = total_loads
eval_data['Total Price'] = total_price
with open(os.getcwd() + '/data/output/' + model_name + '_eval.pkl', 'wb') as f:
pkl.dump(eval_data, f)
# Evaluation if price was kept constant
# env = System(eval=True)
state = env.reset()
actions_building_1 = []
actions_building_2 = []
inside_temperatures_1 = [state[2].item()]
inside_temperatures_2 = [state[3].item()]
base_loads = [state[1].item()]
# base_loads_2 = [env.buildings[1].base_load]
ambient_temperatures = [env.ambient_temperature]
total_loads = []
total_price = []
rewards = []
print('Starting evaluation of the model')
for t_episode in range(NUM_TIME_STEPS):
next_state, reward, done = env.step(0)
rewards.append(reward)
inside_temperatures_1.append(next_state[2])
inside_temperatures_2.append(next_state[3])
actions_building_1.append(env.buildings[0].action)
actions_building_2.append(env.buildings[1].action)
base_loads.append(next_state[1])
# base_loads_2.append(env.buildings[1].base_load)
ambient_temperatures.append(env.ambient_temperature)
total_loads.append(env.total_load)
total_price.append(env.total_power_cost)
# total_loads.append(env.total_load)
eval_data = pd.DataFrame()
eval_data['Inside Temperatures 1'] = inside_temperatures_1[:-1]
eval_data['Inside Temperatures 2'] = inside_temperatures_2[:-1]
eval_data['Base Loads'] = base_loads[:-1]
# eval_data['Base Loads 2'] = base_loads_2
eval_data['Actions 1'] = actions_building_1
eval_data['Actions 2'] = actions_building_2
eval_data['Ambient Temperatures'] = ambient_temperatures[:-1]
eval_data['Rewards'] = rewards
eval_data['Total Load'] = total_loads
eval_data['Total Price'] = total_price
with open(os.getcwd() + '/data/output/' + model_name + str(PRICE_SET[0]) + '_base_eval.pkl', 'wb') as f:
pkl.dump(eval_data, f)
# With constant price at 30€
state = env.reset()
actions_building_1 = []
actions_building_2 = []
inside_temperatures_1 = [state[2].item()]
inside_temperatures_2 = [state[3].item()]
base_loads = [state[1].item()]
# base_loads_2 = [env.buildings[1].base_load]
ambient_temperatures = [env.ambient_temperature]
total_loads = []
total_price = []
rewards = []
print('Starting evaluation of the model')
price_index = 5
for t_episode in range(NUM_TIME_STEPS):
next_state, reward, done = env.step(price_index)
rewards.append(reward)
inside_temperatures_1.append(next_state[2])
inside_temperatures_2.append(next_state[3])
actions_building_1.append(env.buildings[0].action)
actions_building_2.append(env.buildings[1].action)
base_loads.append(next_state[1])
# base_loads_2.append(env.buildings[1].base_load)
ambient_temperatures.append(env.ambient_temperature)
total_loads.append(env.total_load)
total_price.append(env.total_power_cost)
eval_data = pd.DataFrame()
eval_data['Inside Temperatures 1'] = inside_temperatures_1[:-1]
eval_data['Inside Temperatures 2'] = inside_temperatures_2[:-1]
eval_data['Base Loads'] = base_loads[:-1]
# eval_data['Base Loads 2'] = base_loads_2
eval_data['Actions 1'] = actions_building_1
eval_data['Actions 2'] = actions_building_2
eval_data['Ambient Temperatures'] = ambient_temperatures[:-1]
eval_data['Rewards'] = rewards
eval_data['Total Load'] = total_loads
eval_data['Total Price'] = total_price
with open(os.getcwd() + '/data/output/' + model_name + str(PRICE_SET[price_index]) + 'base_eval.pkl', 'wb') as f:
pkl.dump(eval_data, f)
# With the spot prices
state = env.reset()
actions_building_1 = []
actions_building_2 = []
inside_temperatures_1 = [state[2].item()]
inside_temperatures_2 = [state[3].item()]
base_loads = [state[1].item()]
# base_loads_2 = [env.buildings[1].base_load]
ambient_temperatures = [env.ambient_temperature]
total_loads = []
total_price = []
rewards = []
print('Starting evaluation of the model')
env.spot = True
prices = pd.read_csv('data/environment/2014_ToU_prices.csv',
header=0).iloc[0:NUM_HOURS + 1, 1]
for t_episode in range(NUM_TIME_STEPS):
price = prices[t_episode]
next_state, reward, done = env.step(price)
rewards.append(reward)
inside_temperatures_1.append(next_state[2])
inside_temperatures_2.append(next_state[3])
actions_building_1.append(env.buildings[0].action)
actions_building_2.append(env.buildings[1].action)
base_loads.append(next_state[1])
# base_loads_2.append(env.buildings[1].base_load)
ambient_temperatures.append(env.ambient_temperature)
total_loads.append(env.total_load)
total_price.append(env.total_power_cost)
eval_data = pd.DataFrame()
eval_data['Inside Temperatures 1'] = inside_temperatures_1[:-1]
eval_data['Inside Temperatures 2'] = inside_temperatures_2[:-1]
eval_data['Base Loads'] = base_loads[:-1]
eval_data['Actions 1'] = actions_building_1
eval_data['Actions 2'] = actions_building_2
# eval_data['Base Loads 2'] = base_loads_2
eval_data['Ambient Temperatures'] = ambient_temperatures[:-1]
eval_data['Rewards'] = rewards
eval_data['Total Load'] = total_loads
eval_data['Total Price'] = total_price
eval_data['Prices'] = prices
with open(os.getcwd() + '/data/output/' + model_name + '_ToU_base_eval.pkl', 'wb') as f:
pkl.dump(eval_data, f)
print('Finished evaluation on January.')