-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrainer.py
274 lines (243 loc) · 10.1 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
import time
import numpy as np
import tqdm
from utils.utils import *
def Q_train(agent,
training_env,
logger,
epsilon,
total_steps,
max_steps,
epoch,
bid,
b_num,
):
def done_check(done):
return np.sum(done) >= done.shape[0]
obs = training_env.reset()
all_done = False
pbar = tqdm.tqdm(total=max_steps,
desc=f'training epoch {epoch} batch {bid + 1} / {b_num}',
bar_format='{l_bar}{bar:20}{r_bar}{bar:-20b}')
bs = training_env.env_num
obs_rec = []
rew_rec = []
act_rec = []
done_rec = []
step_count = np.zeros(bs, dtype=np.int32)
while not all_done:
# log_obs(logger, obs, total_steps)
qvalues = agent.predict_q_value(obs)
act = agent.epsilon_greedy_policy(qvalues, epsilon)
actions = [{'action': act[i], 'qvalue': qvalues[i].cpu()} for i in range(training_env.env_num)]
obs_next, rewards, is_done, info = training_env.step(actions)
obs_rec.append(obs)
act_rec.append(np.array(act))
rew_rec.append(rewards)
done_rec.append(is_done)
step_count[is_done == False] += 1
returns, loss = agent.learning()
obs = obs_next
all_done = done_check(is_done)
avg_q = qvalues.mean(0)
logger.write('train/rewards', total_steps, {'train/rewards': rewards.mean()})
logger.write('train/loss', total_steps, {'train/loss': loss})
logger.write('train/returns', total_steps, {'train/returns': returns})
act_count = [0] * avg_q.shape[0]
for i in range(training_env.env_num):
act_count[act[i]] += 1
logger.write_together('train/action', total_steps, {f'action{i}': act_count[i] / training_env.env_num for i in range(len(act_count))})
logger.write_together('train/qvalue', total_steps, {f'action{i}': avg_q[i] for i in range(avg_q.shape[0])})
if all_done:
logger.write('train/FEs', total_steps, {'train/FEs': mean_info(info, 'FEs')})
logger.write('train/descent', total_steps, {'train/descent': mean_info(info, 'descent')})
obs_rec.append(obs)
step_count += 1
award = max_steps / step_count
rew_rec = np.array(rew_rec, dtype=np.float32)
award = np.array(award, dtype=np.float32)
# for i in range(bs):
# rew_rec[:, i] *= info[i]['info'].get()['descent']
for i in range(np.max(step_count)):
rew_rec[i] *= award
# agent.update_memory(to_transition(obs_rec[i], act_rec[i], obs_rec[i + 1], rew_rec[i], done_rec[i]))
obs, act, next_obs, rew, done = obs_rec[i], act_rec[i], obs_rec[i + 1], rew_rec[i], done_rec[i]
agent.memory.append(obs[rew >= 0], act[rew >= 0], next_obs[rew >= 0], rew[rew >= 0], done[rew >= 0])
total_steps += 1
pbar.update()
pbar.close()
return total_steps
def Q_test(agent,
testing_env,
test_steps,
test_repeat,
max_steps,
bid,
b_num,
):
def done_check(done):
return np.sum(done) >= done.shape[0]
avg_descent = []
avg_FEs = 0
pbar = tqdm.tqdm(total=max_steps * test_repeat,
desc=f'testing {test_steps} batch {bid + 1} / {b_num}',
bar_format='{l_bar}{bar:20}{r_bar}{bar:-20b}')
feature_label = [[], [], []]
act_rew = np.zeros(3)
act_count = np.zeros(3)
for repeat in range(test_repeat):
obs = testing_env.reset()
all_done = False
info = None
while not all_done:
feature, qvalues = agent.predict_q_value(obs, test=True)
act = torch.argmax(qvalues, -1).cpu()
# act = agent.epsilon_greedy_policy(qvalues, 0.3)
actions = [{'action': act[i], 'qvalue': qvalues[i].cpu()} for i in range(testing_env.env_num)]
obs_next, reward, is_done, info = testing_env.step(actions)
obs = obs_next
all_done = done_check(is_done)
pbar.update()
for i in range(testing_env.env_num):
feature_label[act[i]].append(feature[i])
act_rew[act[i]] += reward[i]
act_count[act[i]] += 1
avg_descent.append(mean_info(info, 'descent_seq'))
avg_FEs += mean_info(info, 'FEs')
avg_descent = np.array(avg_descent)
pbar.close()
out = np.mean(avg_descent, 0), avg_FEs / test_repeat, feature_label, act_count
return out
def Policy_train(agent,
training_env,
logger,
total_steps,
train_steps,
k_epoch,
max_steps,
epoch,
bid,
b_num,
):
def done_check(done):
return np.sum(done) >= done.shape[0]
obs = training_env.reset()
all_done = False
memory = {'states': [],
'logprobs': [],
'actions': [],
'rewards': [],
'bl_val': [],
'bl_val_detached': [],
}
memory['states'].append(obs)
bs = training_env.env_num
step_count = np.zeros(bs, dtype=np.int32)
pbar = tqdm.tqdm(total=max_steps,
desc=f'training epoch {epoch} batch {bid + 1} / {b_num}',
bar_format='{l_bar}{bar:20}{r_bar}{bar:-20b}')
while not all_done:
# log_obs(logger, obs, total_steps)
probs, log_probs, act = agent.actor_sample(agent.actor_forward(obs))
actions = [{'action': act[i].cpu(), 'qvalue': probs[i].detach().cpu()} for i in range(training_env.env_num)]
baseline_val_detached, baseline_val = agent.critic_forward(obs)
obs_next, rewards, is_done, info = training_env.step(actions)
obs = obs_next
all_done = done_check(is_done)
memory['states'].append(obs)
memory['logprobs'].append(log_probs)
memory['actions'].append(act)
memory['rewards'].append(np.array(rewards, dtype=np.float32))
memory['bl_val'].append(baseline_val)
memory['bl_val_detached'].append(baseline_val_detached)
step_count[is_done == False] += 1
avg_probs = probs.cpu().mean(0)
act_count = np.zeros(probs.shape[1])
for i in range(training_env.env_num):
act_count[act[i]] += 1
logger.write_together('train/action', total_steps, {f'action{i}': act_count[i] / training_env.env_num for i in range(len(act_count))})
logger.write_together('train/probs', total_steps, {f'probs{i}': avg_probs[i].cpu() for i in range(avg_probs.shape[0])})
if all_done:
logger.write('train/FEs', total_steps, {'train/FEs': mean_info(info, 'FEs')})
logger.write('train/descent', total_steps, {'train/descent': mean_info(info, 'descent')})
step_count += 1
memory['rewards'] = np.array(memory['rewards'])
# for i in range(bs):
# memory['rewards'][:, i] *= info[i]['info'].get()['descent']
award = max_steps / step_count
#for i in range(len(memory['rewards'])):
#memory['rewards'][i] *= award
total_steps += 1
pbar.update()
pbar.close()
agent.learn(memory, k_epoch, logger, train_steps)
return total_steps
def Policy_test(agent,
testing_env,
test_steps,
test_repeat,
max_steps,
bid,
b_num,
):
def done_check(done):
return np.sum(done) >= done.shape[0]
avg_descent = []
avg_FEs = 0
pbar = tqdm.tqdm(total=max_steps * test_repeat,
desc=f'testing {test_steps} batch {bid + 1} / {b_num}',
bar_format='{l_bar}{bar:20}{r_bar}{bar:-20b}')
n_action = testing_env.action_space[0].n
feature_label = [[] for _ in range(n_action)]
act_count = np.zeros(n_action)
for repeat in range(test_repeat):
obs = testing_env.reset()
all_done = False
descent = []
info = None
while not all_done:
feature, logits = agent.actor_forward_without_grad(obs, test=True)
probs, log_probs, act = agent.actor_sample(logits)
probs = probs.detach().cpu()
actions = [{'action': act[i].cpu(), 'qvalue': probs[i]} for i in range(testing_env.env_num)]
obs_next, rewards, is_done, info = testing_env.step(actions)
obs = obs_next
all_done = done_check(is_done)
descent.append(mean_info(info, 'descent'))
pbar.update()
for i in range(testing_env.env_num):
feature_label[act[i]].append(feature[i])
act_count[act[i]] += 1
avg_descent.append(mean_info(info, 'descent_seq'))
avg_FEs += mean_info(info, 'FEs')
avg_descent = np.array(avg_descent)
pbar.close()
return np.mean(avg_descent, 0), avg_FEs / test_repeat, feature_label, act_count
def baseline_test(baselines,
base_test_env,
test_repeat,
MaxFEs,
period,
bid,
b_num,
):
avg_bases = {}
bases_FEs = {}
avg_gbest = 0
for i, baseline in enumerate(baselines):
FEs = 0
avg_descent = np.zeros(MaxFEs // period)
pbar = tqdm.tqdm(total=test_repeat,
desc=f'Baseline algorithm {baseline.__class__.__name__} batch {bid + 1} / {b_num}',
bar_format='{l_bar}{bar:20}{r_bar}{bar:-20b}')
for repeat in range(test_repeat):
base_test_env.reset()
_, _, _, info = base_test_env.step([{'action': i, 'qvalue': [0, 0]} for _ in range(base_test_env.env_num)])
avg_descent += mean_info(info, 'descent_seq')
FEs += mean_info(info, 'FEs')
avg_gbest += mean_info(info, 'best_cost')
pbar.update()
pbar.close()
avg_bases[baseline.__class__.__name__] = avg_descent / test_repeat
bases_FEs[baseline.__class__.__name__] = FEs / test_repeat
return avg_bases, bases_FEs, avg_gbest / test_repeat / len(baselines)