forked from wingsweihua/presslight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updater.py
139 lines (116 loc) · 6.51 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
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_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)
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
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):
start_time = time.time()
print("Start load samples at", start_time)
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)
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)