-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpipeline.py
executable file
·439 lines (388 loc) · 19.6 KB
/
pipeline.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
import json
import os
import shutil
import xml.etree.ElementTree as ET
from generator import Generator
from construct_sample import ConstructSample
from updater import Updater
from multiprocessing import Process
from model_pool import ModelPool
import random
import pickle
import model_test
import pandas as pd
import numpy as np
from math import isnan
import sys
import time
class Pipeline:
_LIST_SUMO_FILES = [
"cross.car.type.xml",
"cross.con.xml",
"cross.edg.xml",
"cross.net.xml",
"cross.netccfg",
"cross.nod.xml",
"cross.sumocfg",
"cross.tll.xml",
"cross.typ.xml"
]
@staticmethod
def _set_traffic_file(sumo_config_file_tmp_name, sumo_config_file_output_name, list_traffic_file_name):
# update sumocfg
sumo_cfg = ET.parse(sumo_config_file_tmp_name)
config_node = sumo_cfg.getroot()
input_node = config_node.find("input")
for route_files in input_node.findall("route-files"):
input_node.remove(route_files)
input_node.append(
ET.Element("route-files", attrib={"value": ",".join(list_traffic_file_name)}))
sumo_cfg.write(sumo_config_file_output_name)
def _path_check(self):
# check path
if os.path.exists(self.dic_path["PATH_TO_WORK_DIRECTORY"]):
if self.dic_path["PATH_TO_WORK_DIRECTORY"] != "records/default":
raise FileExistsError
else:
pass
else:
os.makedirs(self.dic_path["PATH_TO_WORK_DIRECTORY"])
if os.path.exists(self.dic_path["PATH_TO_MODEL"]):
if self.dic_path["PATH_TO_MODEL"] != "model/default":
raise FileExistsError
else:
pass
else:
os.makedirs(self.dic_path["PATH_TO_MODEL"])
if self.dic_exp_conf["PRETRAIN"]:
if os.path.exists(self.dic_path["PATH_TO_PRETRAIN_WORK_DIRECTORY"]):
pass
else:
os.makedirs(self.dic_path["PATH_TO_PRETRAIN_WORK_DIRECTORY"])
if os.path.exists(self.dic_path["PATH_TO_PRETRAIN_MODEL"]):
pass
else:
os.makedirs(self.dic_path["PATH_TO_PRETRAIN_MODEL"])
def _copy_conf_file(self, path=None):
# write conf files
if path == None:
path = self.dic_path["PATH_TO_WORK_DIRECTORY"]
json.dump(self.dic_exp_conf, open(os.path.join(path, "exp.conf"), "w"),
indent=4)
json.dump(self.dic_agent_conf, open(os.path.join(path, "agent.conf"), "w"),
indent=4)
json.dump(self.dic_traffic_env_conf,
open(os.path.join(path, "traffic_env.conf"), "w"), indent=4)
def _copy_sumo_file(self, path=None):
if path == None:
path = self.dic_path["PATH_TO_WORK_DIRECTORY"]
# copy sumo files
for file_name in self._LIST_SUMO_FILES:
shutil.copy(os.path.join(self.dic_path["PATH_TO_DATA"], file_name),
os.path.join(path, file_name))
for file_name in self.dic_exp_conf["TRAFFIC_FILE"]:
shutil.copy(os.path.join(self.dic_path["PATH_TO_DATA"], file_name),
os.path.join(path, file_name))
def _copy_anon_file(self, path=None):
# hard code !!!
if path == None:
path = self.dic_path["PATH_TO_WORK_DIRECTORY"]
# copy sumo files
shutil.copy(os.path.join(self.dic_path["PATH_TO_DATA"], self.dic_exp_conf["TRAFFIC_FILE"][0]),
os.path.join(path, self.dic_exp_conf["TRAFFIC_FILE"][0]))
shutil.copy(os.path.join(self.dic_path["PATH_TO_DATA"], self.dic_exp_conf["ROADNET_FILE"]),
os.path.join(path, self.dic_exp_conf["ROADNET_FILE"]))
def _modify_sumo_file(self, path=None):
if path == None:
path = self.dic_path["PATH_TO_WORK_DIRECTORY"]
# modify sumo files
self._set_traffic_file(os.path.join(self.dic_path["PATH_TO_WORK_DIRECTORY"], "cross.sumocfg"),
os.path.join(path, "cross.sumocfg"),
self.dic_exp_conf["TRAFFIC_FILE"])
def __init__(self, dic_exp_conf, dic_agent_conf, dic_traffic_env_conf, dic_path):
# load configurations
self.dic_exp_conf = dic_exp_conf
self.dic_agent_conf = dic_agent_conf
self.dic_traffic_env_conf = dic_traffic_env_conf
self.dic_path = dic_path
# do file operations
self._path_check()
self._copy_conf_file()
if self.dic_traffic_env_conf["SIMULATOR_TYPE"] == 'sumo':
self._copy_sumo_file()
self._modify_sumo_file()
elif self.dic_traffic_env_conf["SIMULATOR_TYPE"] == 'anon':
self._copy_anon_file()
# test_duration
self.test_duration = []
def early_stopping(self, dic_path, cnt_round):
print("decide whether to stop")
record_dir = os.path.join(dic_path["PATH_TO_WORK_DIRECTORY"], "test_round", "round_"+str(cnt_round))
# compute duration
df_vehicle_inter_0 = pd.read_csv(os.path.join(record_dir, "vehicle_inter_0.csv"),
sep=',', header=0, dtype={0: str, 1: float, 2: float},
names=["vehicle_id", "enter_time", "leave_time"])
duration = df_vehicle_inter_0["leave_time"].values - df_vehicle_inter_0["enter_time"].values
ave_duration = np.mean([time for time in duration if not isnan(time)])
self.test_duration.append(ave_duration)
if len(self.test_duration) < 30:
return 0
else:
duration_under_exam = np.array(self.test_duration[-15:])
mean_duration = np.mean(duration_under_exam)
std_duration = np.std(duration_under_exam)
max_duration = np.max(duration_under_exam)
if std_duration/mean_duration < 0.1 and max_duration < 1.5 * mean_duration:
return 1
else:
return 0
def generator_wrapper(self, cnt_round, cnt_gen, dic_path, dic_exp_conf, dic_agent_conf, dic_traffic_env_conf,
best_round=None):
generator = Generator(cnt_round=cnt_round,
cnt_gen=cnt_gen,
dic_path=dic_path,
dic_exp_conf=dic_exp_conf,
dic_agent_conf=dic_agent_conf,
dic_traffic_env_conf=dic_traffic_env_conf,
best_round=best_round
)
print("make generator")
generator.generate()
print("generator_wrapper end")
return
def updater_wrapper(self, cnt_round, dic_agent_conf, dic_exp_conf, dic_traffic_env_conf, dic_path, best_round=None, bar_round=None):
updater = Updater(
cnt_round=cnt_round,
dic_agent_conf=dic_agent_conf,
dic_exp_conf=dic_exp_conf,
dic_traffic_env_conf=dic_traffic_env_conf,
dic_path=dic_path,
best_round=best_round,
bar_round=bar_round
)
updater.load_sample()
updater.update_network()
print("updater_wrapper end")
return
def model_pool_wrapper(self, dic_path, dic_exp_conf, cnt_round):
model_pool = ModelPool(dic_path, dic_exp_conf)
model_pool.model_compare(cnt_round)
model_pool.dump_model_pool()
return
#self.best_round = model_pool.get()
#print("self.best_round", self.best_round)
def downsample(self, path_to_log):
path_to_pkl = os.path.join(path_to_log, "inter_0.pkl")
f_logging_data = open(path_to_pkl, "rb")
logging_data = pickle.load(f_logging_data)
subset_data = logging_data[::10]
f_logging_data.close()
os.remove(path_to_pkl)
f_subset = open(path_to_pkl, "wb")
pickle.dump(subset_data, f_subset)
f_subset.close()
def run(self, multi_process=False):
best_round, bar_round = None, None
# pretrain for acceleration
if self.dic_exp_conf["PRETRAIN"]:
if os.listdir(self.dic_path["PATH_TO_PRETRAIN_MODEL"]):
shutil.copy(os.path.join(self.dic_path["PATH_TO_PRETRAIN_MODEL"],
"%s.h5" % self.dic_exp_conf["TRAFFIC_FILE"][0]),
os.path.join(self.dic_path["PATH_TO_MODEL"], "round_0.h5"))
else:
if not os.listdir(self.dic_path["PATH_TO_PRETRAIN_WORK_DIRECTORY"]):
for cnt_round in range(self.dic_exp_conf["PRETRAIN_NUM_ROUNDS"]):
print("round %d starts" % cnt_round)
process_list = []
# ============== generator =============
if multi_process:
for cnt_gen in range(self.dic_exp_conf["PRETRAIN_NUM_GENERATORS"]):
p = Process(target=self.generator_wrapper,
args=(cnt_round, cnt_gen, self.dic_path, self.dic_exp_conf,
self.dic_agent_conf, self.dic_sumo_env_conf, best_round)
)
print("before")
p.start()
print("end")
process_list.append(p)
print("before join")
for p in process_list:
p.join()
print("end join")
else:
for cnt_gen in range(self.dic_exp_conf["PRETRAIN_NUM_GENERATORS"]):
self.generator_wrapper(cnt_round=cnt_round,
cnt_gen=cnt_gen,
dic_path=self.dic_path,
dic_exp_conf=self.dic_exp_conf,
dic_agent_conf=self.dic_agent_conf,
dic_sumo_env_conf=self.dic_sumo_env_conf,
best_round=best_round)
# ============== make samples =============
# make samples and determine which samples are good
train_round = os.path.join(self.dic_path["PATH_TO_PRETRAIN_WORK_DIRECTORY"], "train_round")
if not os.path.exists(train_round):
os.makedirs(train_round)
cs = ConstructSample(path_to_samples=train_round, cnt_round=cnt_round,
dic_sumo_env_conf=self.dic_sumo_env_conf)
cs.make_reward()
if self.dic_exp_conf["MODEL_NAME"] in self.dic_exp_conf["LIST_MODEL_NEED_TO_UPDATE"]:
if multi_process:
p = Process(target=self.updater_wrapper,
args=(0,
self.dic_agent_conf,
self.dic_exp_conf,
self.dic_sumo_env_conf,
self.dic_path,
best_round))
p.start()
p.join()
else:
self.updater_wrapper(cnt_round=0,
dic_agent_conf=self.dic_agent_conf,
dic_exp_conf=self.dic_exp_conf,
dic_sumo_env_conf=self.dic_sumo_env_conf,
dic_path=self.dic_path,
best_round=best_round)
# train with aggregate samples
if self.dic_exp_conf["AGGREGATE"]:
if "aggregate.h5" in os.listdir("model/initial"):
shutil.copy("model/initial/aggregate.h5",
os.path.join(self.dic_path["PATH_TO_MODEL"], "round_0.h5"))
else:
if multi_process:
p = Process(target=self.updater_wrapper,
args=(0,
self.dic_agent_conf,
self.dic_exp_conf,
self.dic_sumo_env_conf,
self.dic_path,
best_round))
p.start()
p.join()
else:
self.updater_wrapper(cnt_round=0,
dic_agent_conf=self.dic_agent_conf,
dic_exp_conf=self.dic_exp_conf,
dic_sumo_env_conf=self.dic_sumo_env_conf,
dic_path=self.dic_path,
best_round=best_round)
self.dic_exp_conf["PRETRAIN"] = False
self.dic_exp_conf["AGGREGATE"] = False
# train
for cnt_round in range(self.dic_exp_conf["NUM_ROUNDS"]):
print("round %d starts" % cnt_round)
round_start_t = time.time()
process_list = []
# ============== generator =============
if multi_process:
for cnt_gen in range(self.dic_exp_conf["NUM_GENERATORS"]):
p = Process(target=self.generator_wrapper,
args=(cnt_round, cnt_gen, self.dic_path, self.dic_exp_conf,
self.dic_agent_conf, self.dic_traffic_env_conf, best_round)
)
p.start()
process_list.append(p)
for i in range(len(process_list)):
p = process_list[i]
p.join()
else:
for cnt_gen in range(self.dic_exp_conf["NUM_GENERATORS"]):
self.generator_wrapper(cnt_round=cnt_round,
cnt_gen=cnt_gen,
dic_path=self.dic_path,
dic_exp_conf=self.dic_exp_conf,
dic_agent_conf=self.dic_agent_conf,
dic_traffic_env_conf=self.dic_traffic_env_conf,
best_round=best_round)
# ============== make samples =============
# make samples and determine which samples are good
train_round = os.path.join(self.dic_path["PATH_TO_WORK_DIRECTORY"], "train_round")
if not os.path.exists(train_round):
os.makedirs(train_round)
cs = ConstructSample(path_to_samples=train_round, cnt_round=cnt_round,
dic_traffic_env_conf=self.dic_traffic_env_conf)
cs.make_reward()
# EvaluateSample()
# ============== update network =============
if self.dic_exp_conf["MODEL_NAME"] in self.dic_exp_conf["LIST_MODEL_NEED_TO_UPDATE"]:
if multi_process:
p = Process(target=self.updater_wrapper,
args=(cnt_round,
self.dic_agent_conf,
self.dic_exp_conf,
self.dic_traffic_env_conf,
self.dic_path,
best_round,
bar_round))
p.start()
p.join()
else:
self.updater_wrapper(cnt_round=cnt_round,
dic_agent_conf=self.dic_agent_conf,
dic_exp_conf=self.dic_exp_conf,
dic_traffic_env_conf=self.dic_traffic_env_conf,
dic_path=self.dic_path,
best_round=best_round,
bar_round=bar_round)
if not self.dic_exp_conf["DEBUG"]:
for cnt_gen in range(self.dic_exp_conf["NUM_GENERATORS"]):
path_to_log = os.path.join(self.dic_path["PATH_TO_WORK_DIRECTORY"], "train_round",
"round_" + str(cnt_round), "generator_" + str(cnt_gen))
self.downsample(path_to_log)
# ============== test evaluation =============
if multi_process:
p = Process(target=model_test.test,
args=(self.dic_path["PATH_TO_MODEL"], cnt_round, self.dic_exp_conf["TEST_RUN_COUNTS"], self.dic_traffic_env_conf, False))
p.start()
if self.dic_exp_conf["EARLY_STOP"] or self.dic_exp_conf["MODEL_POOL"]:
p.join()
else:
model_test.test(self.dic_path["PATH_TO_MODEL"], cnt_round, self.dic_exp_conf["RUN_COUNTS"], self.dic_traffic_env_conf, if_gui=False)
# ============== early stopping =============
if self.dic_exp_conf["EARLY_STOP"]:
flag = self.early_stopping(self.dic_path, cnt_round)
if flag == 1:
break
# ============== model pool evaluation =============
if self.dic_exp_conf["MODEL_POOL"]:
if multi_process:
p = Process(target=self.model_pool_wrapper,
args=(self.dic_path,
self.dic_exp_conf,
cnt_round),
)
p.start()
p.join()
else:
self.model_pool_wrapper(dic_path=self.dic_path,
dic_exp_conf=self.dic_exp_conf,
cnt_round=cnt_round)
model_pool_dir = os.path.join(self.dic_path["PATH_TO_WORK_DIRECTORY"], "best_model.pkl")
if os.path.exists(model_pool_dir):
model_pool = pickle.load(open(model_pool_dir, "rb"))
ind = random.randint(0, len(model_pool) - 1)
best_round = model_pool[ind][0]
ind_bar = random.randint(0, len(model_pool) - 1)
flag = 0
while ind_bar == ind and flag < 10:
ind_bar = random.randint(0, len(model_pool) - 1)
flag += 1
# bar_round = model_pool[ind_bar][0]
bar_round = None
else:
best_round = None
bar_round = None
# downsample
if not self.dic_exp_conf["DEBUG"]:
path_to_log = os.path.join(self.dic_path["PATH_TO_WORK_DIRECTORY"], "test_round",
"round_" + str(cnt_round))
self.downsample(path_to_log)
else:
best_round = None
print("best_round: ", best_round)
print("round %s ends" % cnt_round)
round_end_t = time.time()
f_timing = open(os.path.join(self.dic_path["PATH_TO_WORK_DIRECTORY"], "timing.txt"), "a+")
f_timing.write("round_{0}: {1}\n".format(cnt_round, round_end_t-round_start_t))
f_timing.close()