-
Notifications
You must be signed in to change notification settings - Fork 18
/
updater.py
338 lines (295 loc) · 17.5 KB
/
updater.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
import pickle
import os
from config import DIC_AGENTS
import pandas as pd
import shutil
import pandas as pd
import time
from multiprocessing import Pool
import traceback
import random
import numpy as np
class Updater:
def __init__(self, cnt_round, dic_agent_conf, dic_exp_conf, dic_traffic_env_conf, dic_path, best_round=None, bar_round=None):
self.cnt_round = cnt_round
self.dic_path = dic_path
self.dic_exp_conf = dic_exp_conf
self.dic_traffic_env_conf = dic_traffic_env_conf
self.dic_agent_conf = dic_agent_conf
self.agents = []
self.sample_set_list = []
self.sample_indexes = None
print("Number of agents: ", dic_traffic_env_conf['NUM_AGENTS'])
for i in range(dic_traffic_env_conf['NUM_AGENTS']):
agent_name = self.dic_exp_conf["MODEL_NAME"]
agent= DIC_AGENTS[agent_name](
self.dic_agent_conf, self.dic_traffic_env_conf,
self.dic_path, self.cnt_round, intersection_id=str(i))
self.agents.append(agent)
def load_sample(self, i):
sample_set = []
try:
if self.dic_exp_conf["PRETRAIN"]:
sample_file = open(os.path.join(self.dic_path["PATH_TO_PRETRAIN_WORK_DIRECTORY"],
"train_round", "total_samples" + ".pkl"), "rb")
elif self.dic_exp_conf["AGGREGATE"]:
sample_file = open(os.path.join(self.dic_path["PATH_TO_AGGREGATE_SAMPLES"],
"aggregate_samples.pkl"), "rb")
else:
sample_file = open(os.path.join(self.dic_path["PATH_TO_WORK_DIRECTORY"], "train_round",
"total_samples_inter_{0}".format(i) + ".pkl"), "rb")
try:
while True:
sample_set += pickle.load(sample_file)
except EOFError:
pass
except Exception as e:
error_dir = os.path.join(self.dic_path["PATH_TO_WORK_DIRECTORY"]).replace("records", "errors")
if not os.path.exists(error_dir):
os.makedirs(error_dir)
f = open(os.path.join(error_dir, "error_info_inter_{0}.txt".format(i)), "a")
f.write("Fail to load samples for inter {0}\n".format(i))
f.write('traceback.format_exc():\n%s\n' % traceback.format_exc())
f.close()
print('traceback.format_exc():\n%s' % traceback.format_exc())
pass
if i %100 ==0:
print("load_sample for inter {0}".format(i))
return sample_set
def load_hidden_states_with_forget(self): # hidden state is a list [#time, agent, # dim]
hidden_states_set = []
try:
hidden_state_file = open(os.path.join(self.dic_path["PATH_TO_WORK_DIRECTORY"], "train_round",
"total_hidden_states.pkl"), "rb")
try:
while True:
hidden_states_set.append(pickle.load(hidden_state_file))
hidden_states_set = np.vstack(hidden_states_set)
ind_end = len(hidden_states_set)
print("hidden_state_set shape: ",hidden_states_set.shape)
if self.dic_exp_conf["PRETRAIN"] or self.dic_exp_conf["AGGREGATE"]:
pass
else:
ind_sta = max(0, ind_end - self.dic_agent_conf["MAX_MEMORY_LEN"])
hidden_states_after_forget = hidden_states_set[ind_sta: ind_end]
hidden_states_set = [np.array([hidden_states_after_forget[k] for k in self.sample_indexes])]
except EOFError:
pass
except Exception as e:
error_dir = os.path.join(self.dic_path["PATH_TO_WORK_DIRECTORY"]).replace("records", "errors")
if not os.path.exists(error_dir):
os.makedirs(error_dir)
f = open(os.path.join(error_dir, "error_info.txt"), "a")
f.write("Fail to load hidden_states for inter\n")
f.write('traceback.format_exc():\n%s\n' % traceback.format_exc())
f.close()
print('traceback.format_exc():\n%s' % traceback.format_exc())
pass
return hidden_states_set
def load_sample_with_forget(self, i):
sample_set = []
try:
if self.dic_exp_conf["PRETRAIN"]:
sample_file = open(os.path.join(self.dic_path["PATH_TO_PRETRAIN_WORK_DIRECTORY"],
"train_round", "total_samples" + ".pkl"), "rb")
elif self.dic_exp_conf["AGGREGATE"]:
sample_file = open(os.path.join(self.dic_path["PATH_TO_AGGREGATE_SAMPLES"],
"aggregate_samples.pkl"), "rb")
else:
sample_file = open(os.path.join(self.dic_path["PATH_TO_WORK_DIRECTORY"], "train_round",
"total_samples_inter_{0}".format(i) + ".pkl"), "rb")
try:
while True:
sample_set += pickle.load(sample_file)
ind_end = len(sample_set)
if self.dic_exp_conf["PRETRAIN"] or self.dic_exp_conf["AGGREGATE"]:
pass
else:
ind_sta = max(0, ind_end - self.dic_agent_conf["MAX_MEMORY_LEN"])
memory_after_forget = sample_set[ind_sta: ind_end]
# print("memory size after forget:", len(memory_after_forget))
# sample the memory
sample_size = min(self.dic_agent_conf["SAMPLE_SIZE"], len(memory_after_forget))
if self.sample_indexes is None:
self.sample_indexes = random.sample(range(len(memory_after_forget)), sample_size)
sample_set = [memory_after_forget[k] for k in self.sample_indexes]
# print("memory samples number:", sample_size)
# print(self.sample_indexes)
except EOFError:
pass
except Exception as e:
error_dir = os.path.join(self.dic_path["PATH_TO_WORK_DIRECTORY"]).replace("records", "errors")
if not os.path.exists(error_dir):
os.makedirs(error_dir)
f = open(os.path.join(error_dir, "error_info_inter_{0}.txt".format(i)), "a")
f.write("Fail to load samples for inter {0}\n".format(i))
f.write('traceback.format_exc():\n%s\n' % traceback.format_exc())
f.close()
print('traceback.format_exc():\n%s' % traceback.format_exc())
pass
if i %100 == 0:
print("load_sample for inter {0}".format(i))
return sample_set
def load_sample_with_forget(self, i):
sample_set = []
try:
if self.dic_exp_conf["PRETRAIN"]:
sample_file = open(os.path.join(self.dic_path["PATH_TO_PRETRAIN_WORK_DIRECTORY"],
"train_round", "total_samples" + ".pkl"), "rb")
elif self.dic_exp_conf["AGGREGATE"]:
sample_file = open(os.path.join(self.dic_path["PATH_TO_AGGREGATE_SAMPLES"],
"aggregate_samples.pkl"), "rb")
else:
sample_file = open(os.path.join(self.dic_path["PATH_TO_WORK_DIRECTORY"], "train_round",
"total_samples_inter_{0}".format(i) + ".pkl"), "rb")
try:
while True:
sample_set += pickle.load(sample_file)
ind_end = len(sample_set)
if self.dic_exp_conf["PRETRAIN"] or self.dic_exp_conf["AGGREGATE"]:
pass
else:
ind_sta = max(0, ind_end - self.dic_agent_conf["MAX_MEMORY_LEN"])
memory_after_forget = sample_set[ind_sta: ind_end]
sample_set = memory_after_forget
# print("memory size after forget:", len(memory_after_forget))
# sample the memory
# sample_size = min(self.dic_agent_conf["SAMPLE_SIZE"], len(memory_after_forget))
# if self.sample_indexes is None:
# self.sample_indexes = random.sample(range(len(memory_after_forget)), sample_size)
# sample_set = [memory_after_forget[k] for k in self.sample_indexes]
# print("memory samples number:", sample_size)
# print(self.sample_indexes)
except EOFError:
pass
except Exception as e:
error_dir = os.path.join(self.dic_path["PATH_TO_WORK_DIRECTORY"]).replace("records", "errors")
if not os.path.exists(error_dir):
os.makedirs(error_dir)
f = open(os.path.join(error_dir, "error_info_inter_{0}.txt".format(i)), "a")
f.write("Fail to load samples for inter {0}\n".format(i))
f.write('traceback.format_exc():\n%s\n' % traceback.format_exc())
f.close()
print('traceback.format_exc():\n%s' % traceback.format_exc())
pass
if i %100 == 0:
print("load_sample for inter {0}".format(i))
return sample_set
def load_sample_for_agents(self):
# TODO should be number of agents
start_time = time.time()
print("Start load samples at", start_time)
if self.dic_exp_conf['MODEL_NAME'] not in ["GCN","DGN","STGAT"]:
if self.dic_traffic_env_conf["ONE_MODEL"] or self.dic_exp_conf['MODEL_NAME'] in ["SimpleDQNOne","TransferDQNOne","TransferDQNPressOne"]: # for one model
sample_set_all = []
for i in range(self.dic_traffic_env_conf['NUM_INTERSECTIONS']):
sample_set = self.load_sample_with_forget(i)
# sample_set = self.load_sample(i)
# sample, 10 samples from each intersection
# ind_end = len(sample_set)
# ind_sta = max(0, ind_end - self.dic_agent_conf["MAX_MEMORY_LEN"])
# sample_set = sample_set[ind_sta: ind_end]
# sample_size = min(int(self.dic_agent_conf["SAMPLE_SIZE"]/100)+1, len(sample_set))
# sample_set = random.sample(sample_set, sample_size)
#
# >>>>>>> ana_simulator
sample_set_all.extend(sample_set)
self.agents[0].prepare_Xs_Y(sample_set_all, self.dic_exp_conf)
else:
for i in range(self.dic_traffic_env_conf['NUM_INTERSECTIONS']):
sample_set = self.load_sample_with_forget(i)
self.agents[i].prepare_Xs_Y(sample_set, self.dic_exp_conf)
else:
samples_gcn_df = None
# <<<<<<< HEAD
for i in range(self.dic_traffic_env_conf['NUM_INTERSECTIONS']):
sample_set = self.load_sample_with_forget(i)
samples_set_df = pd.DataFrame.from_records(sample_set,columns= ['state','action','next_state','inst_reward','reward','time','generator'])
samples_set_df['input'] = samples_set_df[['state','action','next_state','inst_reward','reward']].values.tolist()
samples_set_df.drop(['state','action','next_state','inst_reward','reward'], axis=1, inplace=True)
# samples_set_df['inter_id'] = i
if i == 0:
samples_gcn_df = samples_set_df
samples_gcn_df = []
print("start get samples")
get_samples_start_time = time.time()
for i in range(self.dic_traffic_env_conf['NUM_INTERSECTIONS']):
sample_set = self.load_sample_with_forget(i)
if self.dic_traffic_env_conf['MODEL_NAME']=="DGN":
samples_set_df = pd.DataFrame.from_records(sample_set,columns= ['state','action','next_state','inst_reward','reward','time','generator','c_state','h_state'])
else:
samples_set_df = pd.DataFrame.from_records(sample_set,columns= ['state','action','next_state','inst_reward','reward','time','generator'])
samples_set_df['input'] = samples_set_df[['state','action','next_state','inst_reward','reward']].values.tolist()
samples_set_df.drop(['state','action','next_state','inst_reward','reward','time','generator'], axis=1, inplace=True)
# samples_set_df['inter_id'] = i
samples_gcn_df.append(samples_set_df['input'])
if i%100 == 0:
print("inter {0} samples_set_df.shape: ".format(i), samples_set_df.shape)
samples_gcn_df = pd.concat(samples_gcn_df, axis=1)
print("samples_gcn_df.shape :", samples_gcn_df.shape)
print("Getting samples time: ", time.time()-get_samples_start_time)
if self.dic_exp_conf['MODEL_NAME'] == "STGAT":
print("start load hidden states")
hidden_states = np.vstack(self.load_hidden_states_with_forget()) # hidden state is a list [#time, agent, # dim]
print("hidden states shape: ", hidden_states.shape)
assert len(hidden_states) == len(samples_gcn_df)
hidden_states = pd.Series(list(hidden_states))
next_hidden_states = hidden_states.shift(-1)
for i in range(self.dic_traffic_env_conf['NUM_AGENTS']):
sample_set_list = pd.concat([samples_gcn_df, hidden_states, next_hidden_states], axis=1)
sample_set_list = sample_set_list[:-1].values.tolist()
### TODO MATCH with Nan!!!!
self.agents[i].prepare_Xs_Y(sample_set_list, self.dic_exp_conf)
# >>>>>>> ana_simulator
else:
for i in range(self.dic_traffic_env_conf['NUM_AGENTS']):
sample_set_list = samples_gcn_df.values.tolist()
self.agents[i].prepare_Xs_Y(sample_set_list, self.dic_exp_conf)
print("------------------Load samples time: ", time.time()-start_time)
def sample_set_to_sample_gcn_df(self,sample_set):
print("make results")
samples_set_df = pd.DataFrame.from_records(sample_set,columns= ['state','action','next_state','inst_reward','reward','time','generator'])
samples_set_df = samples_set_df.set_index(['time','generator'])
samples_set_df['input'] = samples_set_df[['state','action','next_state','inst_reward','reward']].values.tolist()
samples_set_df.drop(['state','action','next_state','inst_reward','reward'], axis=1, inplace=True)
self.sample_set_list.append(samples_set_df)
def update_network(self,i):
print('update agent %d'%i)
self.agents[i].train_network(self.dic_exp_conf)
if self.dic_traffic_env_conf["ONE_MODEL"]:
if self.dic_exp_conf["PRETRAIN"]:
self.agents[i].q_network.save(os.path.join(self.dic_path["PATH_TO_PRETRAIN_MODEL"],
"{0}.h5".format(self.dic_exp_conf["TRAFFIC_FILE"][0]))
)
shutil.copy(os.path.join(self.dic_path["PATH_TO_PRETRAIN_MODEL"],
"{0}.h5".format(self.dic_exp_conf["TRAFFIC_FILE"][0])),
os.path.join(self.dic_path["PATH_TO_MODEL"], "round_0.h5"))
elif self.dic_exp_conf["AGGREGATE"]:
self.agents[i].q_network.save("model/initial", "aggregate.h5")
shutil.copy("model/initial/aggregate.h5",
os.path.join(self.dic_path["PATH_TO_MODEL"], "round_0.h5"))
else:
self.agents[i].save_network("round_{0}".format(self.cnt_round))
else:
if self.dic_exp_conf["PRETRAIN"]:
self.agents[i].q_network.save(os.path.join(self.dic_path["PATH_TO_PRETRAIN_MODEL"],
"{0}_inter_{1}.h5".format(self.dic_exp_conf["TRAFFIC_FILE"][0],
self.agents[i].intersection_id))
)
shutil.copy(os.path.join(self.dic_path["PATH_TO_PRETRAIN_MODEL"],
"{0}_inter_{1}.h5".format(self.dic_exp_conf["TRAFFIC_FILE"][0],
self.agents[i].intersection_id)),
os.path.join(self.dic_path["PATH_TO_MODEL"], "round_0.h5"))
elif self.dic_exp_conf["AGGREGATE"]:
self.agents[i].q_network.save("model/initial", "aggregate_inter_{0}.h5".format(self.agents[i].intersection_id))
shutil.copy("model/initial/aggregate.h5",
os.path.join(self.dic_path["PATH_TO_MODEL"], "round_0_inter_{0}.h5".format(self.agents[i].intersection_id)))
else:
self.agents[i].save_network("round_{0}_inter_{1}".format(self.cnt_round,self.agents[i].intersection_id))
def update_network_for_agents(self):
if self.dic_traffic_env_conf["ONE_MODEL"]:
self.update_network(0)
else:
print("update_network_for_agents", self.dic_traffic_env_conf['NUM_AGENTS'])
for i in range(self.dic_traffic_env_conf['NUM_AGENTS']):
self.update_network(i)