forked from wingsweihua/colight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_test.py
executable file
·164 lines (132 loc) · 6.26 KB
/
model_test.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
import json
import os
import pickle
from config import DIC_AGENTS, DIC_ENVS
from copy import deepcopy
def check_all_workers_working(list_cur_p):
for i in range(len(list_cur_p)):
if not list_cur_p[i].is_alive():
return i
return -1
def downsample(path_to_log, i):
path_to_pkl = os.path.join(path_to_log, "inter_{0}.pkl".format(i))
with open(path_to_pkl, "rb") as f_logging_data:
logging_data = pickle.load(f_logging_data)
subset_data = logging_data[::10]
os.remove(path_to_pkl)
with open(path_to_pkl, "wb") as f_subset:
pickle.dump(subset_data, f_subset)
def downsample_for_system(path_to_log,dic_traffic_env_conf):
for i in range(dic_traffic_env_conf['NUM_INTERSECTIONS']):
downsample(path_to_log,i)
# TODO test on multiple intersections
def test(model_dir, cnt_round, run_cnt, _dic_traffic_env_conf, if_gui):
dic_traffic_env_conf = deepcopy(_dic_traffic_env_conf)
records_dir = model_dir.replace("model", "records")
model_round = "round_%d"%cnt_round
dic_path = {}
dic_path["PATH_TO_MODEL"] = model_dir
dic_path["PATH_TO_WORK_DIRECTORY"] = records_dir
with open(os.path.join(records_dir, "agent.conf"), "r") as f:
dic_agent_conf = json.load(f)
with open(os.path.join(records_dir, "exp.conf"), "r") as f:
dic_exp_conf = json.load(f)
if os.path.exists(os.path.join(records_dir, "sumo_env.conf")):
with open(os.path.join(records_dir, "sumo_env.conf"), "r") as f:
dic_traffic_env_conf = json.load(f)
elif os.path.exists(os.path.join(records_dir, "anon_env.conf")):
with open(os.path.join(records_dir, "anon_env.conf"), "r") as f:
dic_traffic_env_conf = json.load(f)
dic_exp_conf["RUN_COUNTS"] = run_cnt
dic_traffic_env_conf["IF_GUI"] = if_gui
# dump dic_exp_conf
with open(os.path.join(records_dir, "test_exp.conf"), "w") as f:
json.dump(dic_exp_conf, f)
if dic_exp_conf["MODEL_NAME"] in dic_exp_conf["LIST_MODEL_NEED_TO_UPDATE"]:
dic_agent_conf["EPSILON"] = 0 # dic_agent_conf["EPSILON"] # + 0.1*cnt_gen
dic_agent_conf["MIN_EPSILON"] = 0
agents = []
try:
path_to_log = os.path.join(dic_path["PATH_TO_WORK_DIRECTORY"], "test_round", model_round)
if not os.path.exists(path_to_log):
os.makedirs(path_to_log)
env = DIC_ENVS[dic_traffic_env_conf["SIMULATOR_TYPE"]](path_to_log=path_to_log,
path_to_work_directory=dic_path[
"PATH_TO_WORK_DIRECTORY"],
dic_traffic_env_conf=dic_traffic_env_conf)
done = False
state = env.reset()
for i in range(dic_traffic_env_conf['NUM_AGENTS']):
agent_name = dic_exp_conf["MODEL_NAME"]
if agent_name=='CoLight_Signal':
agent = DIC_AGENTS[agent_name](
dic_agent_conf=dic_agent_conf,
dic_traffic_env_conf=dic_traffic_env_conf,
dic_path=dic_path,
cnt_round=1, # useless
inter_info=env.list_intersection,
intersection_id=str(i)
)
else:
agent = DIC_AGENTS[agent_name](
dic_agent_conf=dic_agent_conf,
dic_traffic_env_conf=dic_traffic_env_conf,
dic_path=dic_path,
cnt_round=1, # useless
intersection_id=str(i)
)
agents.append(agent)
for i in range(dic_traffic_env_conf['NUM_AGENTS']):
if dic_traffic_env_conf["ONE_MODEL"]:
agents[i].load_network("{0}".format(model_round))
else:
agents[i].load_network("{0}_inter_{1}".format(model_round, agents[i].intersection_id))
step_num = 0
attention_dict = {}
while not done and step_num < int(dic_exp_conf["RUN_COUNTS"] / dic_traffic_env_conf["MIN_ACTION_TIME"]):
action_list = []
for i in range(dic_traffic_env_conf["NUM_AGENTS"]):
if "CoLight" in dic_exp_conf["MODEL_NAME"]:
one_state = state
action_list, attention = agents[i].choose_action(step_num, one_state)
cur_time = env.get_current_time()
attention_dict[cur_time] = attention
elif "GCN" in dic_exp_conf["MODEL_NAME"]:
one_state = state
# print('one_state:',one_state)
action_list = agents[i].choose_action(step_num, one_state)
# print('action_list:',action_list)
elif "SimpleDQNOne" in dic_exp_conf["MODEL_NAME"]:
one_state = state
if True:
action_list = agents[i].choose_action(step_num, one_state)
else:
action_list = agents[i].choose_action_separate(step_num, one_state)
else:
one_state = state[i]
action = agents[i].choose_action(step_num, one_state)
action_list.append(action)
next_state, reward, done, _ = env.step(action_list)
state = next_state
step_num += 1
# print('bulk_log_multi_process')
env.bulk_log_multi_process()
env.log_attention(attention_dict)
env.end_sumo()
if not dic_exp_conf["DEBUG"]:
path_to_log = os.path.join(dic_path["PATH_TO_WORK_DIRECTORY"], "test_round",
model_round)
# print("downsample", path_to_log)
downsample_for_system(path_to_log, dic_traffic_env_conf)
# print("end down")
except:
error_dir = model_dir.replace("model", "errors")
if os.path.exists(error_dir):
f = open(os.path.join(error_dir, "error_info.txt"), "a")
f.write("round_%d fail to test model"%cnt_round)
f.close()
else:
os.makedirs(error_dir)
f = open(os.path.join(error_dir, "error_info.txt"), "a")
f.write("round_%d fail to test model"%cnt_round)
f.close()