-
Notifications
You must be signed in to change notification settings - Fork 7
/
trainer.py
319 lines (284 loc) · 11.3 KB
/
trainer.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
import itertools
import logging
import numpy as np
import time
import os
import pandas as pd
import subprocess
from shutil import copy
def check_dir(cur_dir):
if not os.path.exists(cur_dir):
return False
return True
def copy_file(src_dir, tar_dir):
copy(src_dir, tar_dir)
def find_file(cur_dir, suffix='.ini'):
for file in os.listdir(cur_dir):
if file.endswith(suffix):
return cur_dir + '/' + file
logging.error('Cannot find %s file' % suffix)
return None
def init_dir(base_dir, pathes=['log', 'data', 'model']):
if not os.path.exists(base_dir):
os.mkdir(base_dir)
dirs = {}
for path in pathes:
cur_dir = base_dir + '/%s/' % path
if not os.path.exists(cur_dir):
os.mkdir(cur_dir)
dirs[path] = cur_dir
return dirs
def init_log(log_dir):
logging.basicConfig(format='%(asctime)s [%(levelname)s] %(message)s',
level=logging.INFO,
handlers=[
logging.FileHandler('%s/%d.log' % (log_dir, time.time())),
logging.StreamHandler()
])
def init_test_flag(test_mode):
if test_mode == 'no_test':
return False, False
if test_mode == 'in_train_test':
return True, False
if test_mode == 'after_train_test':
return False, True
if test_mode == 'all_test':
return True, True
return False, False
class Counter:
def __init__(self, total_step, test_step, log_step):
self.counter = itertools.count(1)
self.cur_step = 0
self.cur_test_step = 0
self.total_step = total_step
self.test_step = test_step
self.log_step = log_step
self.stop = False
def next(self):
self.cur_step = next(self.counter)
return self.cur_step
def should_test(self):
test = False
if (self.cur_step - self.cur_test_step) >= self.test_step:
test = True
self.cur_test_step = self.cur_step
return test
def should_log(self):
return (self.cur_step % self.log_step == 0)
def should_stop(self):
if self.cur_step >= self.total_step:
return True
return self.stop
class Trainer():
def __init__(self, env, model, global_counter, summary_writer, output_path=None):
self.cur_step = 0
self.global_counter = global_counter
self.env = env
self.agent = self.env.agent
self.model = model
self.n_step = self.model.n_step
self.summary_writer = summary_writer
assert self.env.T % self.n_step == 0
self.data = []
self.output_path = output_path
self.env.train_mode = True
def _add_summary(self, reward, global_step, is_train=True):
if is_train:
self.summary_writer.add_scalar('train_reward', reward, global_step=global_step)
else:
self.summary_writer.add_scalar('test_reward', reward, global_step=global_step)
def _get_policy(self, ob, done, mode='train'):
if self.agent.startswith('ma2c'):
self.ps = self.env.get_fingerprint()
policy = self.model.forward(ob, done, self.ps)
else:
policy = self.model.forward(ob, done)
action = []
for pi in policy:
if mode == 'train':
action.append(np.random.choice(np.arange(len(pi)), p=pi))
else:
action.append(np.argmax(pi))
return policy, np.array(action)
def _get_value(self, ob, done, action):
if self.agent.startswith('ma2c'):
value = self.model.forward(ob, done, self.ps, np.array(action), 'v')
else:
self.naction = self.env.get_neighbor_action(action)
if not self.naction:
self.naction = np.nan
value = self.model.forward(ob, done, self.naction, 'v')
return value
def _log_episode(self, global_step, mean_reward, std_reward):
log = {'agent': self.agent,
'step': global_step,
'test_id': -1,
'avg_reward': mean_reward,
'std_reward': std_reward}
self.data.append(log)
self._add_summary(mean_reward, global_step)
self.summary_writer.flush()
def explore(self, prev_ob, prev_done):
ob = prev_ob
done = prev_done
for _ in range(self.n_step):
# pre-decision
policy, action = self._get_policy(ob, done)
# post-decision
value = self._get_value(ob, done, action)
# transition
self.env.update_fingerprint(policy)
next_ob, reward, done, global_reward = self.env.step(action)
self.episode_rewards.append(global_reward)
global_step = self.global_counter.next()
self.cur_step += 1
# collect experience
if self.agent.startswith('ma2c'):
self.model.add_transition(ob, self.ps, action, reward, value, done)
else:
self.model.add_transition(ob, self.naction, action, reward, value, done)
# logging
if self.global_counter.should_log():
logging.info('''Training: global step %d, episode step %d,
ob: %s, a: %s, pi: %s, r: %.2f, train r: %.2f, done: %r''' %
(global_step, self.cur_step,
str(ob), str(action), str(policy), global_reward, np.mean(reward), done))
# print(self.model.r)
# terminal check must be inside batch loop for CACC env
if done:
break
ob = next_ob
if done:
R = np.zeros(self.model.n_agent)
else:
_, action = self._get_policy(ob, done)
R = self._get_value(ob, done, action)
return ob, done, R
def perform(self, test_ind, gui=False):
ob = self.env.reset(gui=gui, test_ind=test_ind)
rewards = []
# note this done is pre-decision to reset LSTM states!
done = True
self.model.reset()
while True:
if self.agent == 'greedy':
action = self.model.forward(ob)
else:
# in on-policy learning, test policy has to be stochastic
if self.env.name.startswith('atsc'):
policy, action = self._get_policy(ob, done)
else:
# for mission-critic tasks like CACC, we need deterministic policy
policy, action = self._get_policy(ob, done, mode='test')
self.env.update_fingerprint(policy)
next_ob, reward, done, global_reward = self.env.step(action)
rewards.append(global_reward)
if done:
break
ob = next_ob
mean_reward = np.mean(np.array(rewards))
std_reward = np.std(np.array(rewards))
return mean_reward, std_reward
def run(self):
while not self.global_counter.should_stop():
# np.random.seed(self.env.seed)
ob = self.env.reset()
# note this done is pre-decision to reset LSTM states!
done = True
self.model.reset()
self.cur_step = 0
self.episode_rewards = []
while True:
ob, done, R = self.explore(ob, done)
dt = self.env.T - self.cur_step
global_step = self.global_counter.cur_step
self.model.backward(R, dt, self.summary_writer, global_step)
# termination
if done:
self.env.terminate()
# pytorch implementation is faster, wait SUMO for 1s
time.sleep(1)
break
rewards = np.array(self.episode_rewards)
mean_reward = np.mean(rewards)
std_reward = np.std(rewards)
# NOTE: for CACC we have to run another testing episode after each
# training episode since the reward and policy settings are different!
if not self.env.name.startswith('atsc'):
self.env.train_mode = False
mean_reward, std_reward = self.perform(-1)
self.env.train_mode = True
self._log_episode(global_step, mean_reward, std_reward)
df = pd.DataFrame(self.data)
df.to_csv(self.output_path + 'train_reward.csv')
class Tester(Trainer):
def __init__(self, env, model, global_counter, summary_writer, output_path):
super().__init__(env, model, global_counter, summary_writer)
self.env.train_mode = False
self.test_num = self.env.test_num
self.output_path = output_path
self.data = []
logging.info('Testing: total test num: %d' % self.test_num)
def run_offline(self):
# enable traffic measurments for offline test
is_record = True
record_stats = False
self.env.cur_episode = 0
self.env.init_data(is_record, record_stats, self.output_path)
rewards = []
for test_ind in range(self.test_num):
rewards.append(self.perform(test_ind))
self.env.terminate()
time.sleep(2)
self.env.collect_tripinfo()
avg_reward = np.mean(np.array(rewards))
logging.info('Offline testing: avg R: %.2f' % avg_reward)
self.env.output_data()
def run_online(self, coord):
self.env.cur_episode = 0
while not coord.should_stop():
time.sleep(30)
if self.global_counter.should_test():
rewards = []
global_step = self.global_counter.cur_step
for test_ind in range(self.test_num):
cur_reward = self.perform(test_ind)
self.env.terminate()
rewards.append(cur_reward)
log = {'agent': self.agent,
'step': global_step,
'test_id': test_ind,
'reward': cur_reward}
self.data.append(log)
avg_reward = np.mean(np.array(rewards))
self._add_summary(avg_reward, global_step)
logging.info('Testing: global step %d, avg R: %.2f' %
(global_step, avg_reward))
# self.global_counter.update_test(avg_reward)
df = pd.DataFrame(self.data)
df.to_csv(self.output_path + 'train_reward.csv')
class Evaluator(Tester):
def __init__(self, env, model, output_path, gui=False):
self.env = env
self.model = model
self.agent = self.env.agent
self.env.train_mode = False
self.test_num = self.env.test_num
self.output_path = output_path
self.gui = gui
def run(self):
if self.gui:
is_record = False
else:
is_record = True
record_stats = False
self.env.cur_episode = 0
self.env.init_data(is_record, record_stats, self.output_path)
time.sleep(1)
for test_ind in range(self.test_num):
reward, _ = self.perform(test_ind, gui=self.gui)
self.env.terminate()
logging.info('test %i, avg reward %.2f' % (test_ind, reward))
time.sleep(2)
self.env.collect_tripinfo()
self.env.output_data()