-
Notifications
You must be signed in to change notification settings - Fork 6
/
algorithms.py
executable file
·564 lines (511 loc) · 24.1 KB
/
algorithms.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
import argparse
import os
from sqlalchemy import all_
from tqdm import tqdm
import torch
import numpy as np
import json
import copy
import os.path as osp
import math
import torch.optim as optim
from torch.optim import lr_scheduler
from cutsel_agent_parallel import CutSelectAgent, HierarchyCutSelectAgent
from logger import logger
from utils import setup_logger, create_stats_ordered_dict, set_global_seed
from utilss.mean_std import RunningMeanStd
class ReinforceBaselineAlg():
def __init__(
self,
env,
pointer_net, # policy net in cutsel_agent
value_net,
sel_cuts_percent,
device,
evaluate_freq=1,
evaluate_samples=1,
optimizer_class='Adam',
actor_net_lr=1e-4,
critic_net_lr=1e-4,
reward_scale=1,
num_epochs=100,
max_grad_norm=2.0,
batch_size=32,
train_decode_type='stochastic',
evaluate_decode_type='greedy',
reward_type='solving_time',
baseline_type="no_baseline", # ['no_baseline', 'simple', 'net']
critic_beta=0.9,
train_steps_per_epoch=1,
lr_decay=False,
lr_decay_step=5,
lr_decay_rate=0.96,
normalize=False,
normalize_reward=True
):
self.env = env
self.pointer_net = pointer_net
self.sel_cuts_percent = sel_cuts_percent
self.value_net = value_net
self.actor_net_lr = actor_net_lr
self.critic_net_lr = critic_net_lr
self.reward_scale = reward_scale
self.num_epochs = num_epochs
self.max_grad_norm = max_grad_norm
self.batch_size = batch_size
self.device = device
self.reward_type = reward_type
self.train_steps_per_epoch = train_steps_per_epoch
# decode type
self.train_decode_type = train_decode_type
self.evaluate_decode_type = evaluate_decode_type
# evaluate
self.evaluate_freq = evaluate_freq
self.evaluate_samples = evaluate_samples
# optimizer
if isinstance(optimizer_class, str):
optimizer_class = eval('optim.'+optimizer_class)
self.optimizer_class = optimizer_class
self.policy_optimizer = optimizer_class(
self.pointer_net.parameters(),
lr=self.actor_net_lr
)
self.baseline_type = baseline_type
self.critic_beta = critic_beta
if self.baseline_type == 'net':
# using critic net as a baseline function
self.value_optimizer = optimizer_class(
self.value_net.parameters(),
lr=self.critic_net_lr
)
self.critic_mse = torch.nn.MSELoss()
elif self.baseline_type == 'simple':
self.critic_exp_mvg_avg = torch.zeros(1)
# .to(self.device)
# lr scheduler
self.lr_decay = lr_decay
self.lr_decay_step = lr_decay_step
self.lr_decay_rate = lr_decay_rate
if self.lr_decay:
self.policy_lr_scheduler = lr_scheduler.StepLR(
self.policy_optimizer,
self.lr_decay_step,
gamma=self.lr_decay_rate
)
# normalizer
self.normalize = normalize
if normalize:
feature_shape = (self.pointer_net.embedding_dim,)
self.mean_std = RunningMeanStd(feature_shape)
self.normalize_reward = normalize_reward
def _prob_to_logp(self, prob):
logprob = 0
for p in prob:
logp = torch.log(p)
logprob += logp
# logprob[(logprob < -10000).detach()] = 0.
return logprob
def evaluate(self, epoch):
logger.log(f"evaluating... epoch: {epoch}")
neg_solving_time = np.zeros((1, self.evaluate_samples))
neg_total_nodes = np.zeros((1, self.evaluate_samples))
for i in range(self.evaluate_samples):
self.env.reset()
cutsel_agent = CutSelectAgent(
self.env.m,
self.pointer_net,
self.value_net,
self.sel_cuts_percent,
self.device,
self.evaluate_decode_type,
self.baseline_type
)
env_step_info = self.env.step(cutsel_agent)
neg_solving_time[:,i] = env_step_info['solving_time']
neg_total_nodes[:,i] = env_step_info['ntotal_nodes']
logger.record_tabular('evaluating/Neg Solving time', np.mean(neg_solving_time))
logger.record_tabular('evaluating/Neg Total Nodes', np.mean(neg_total_nodes))
def save_checkpoint(self, epoch):
state_dict = {}
state_dict['pointer_net'] = self.pointer_net.state_dict()
if self.normalize:
state_dict['mean'] = self.mean_std.mean
state_dict['std'] = self.mean_std.std
state_dict['epsilon'] = self.mean_std.epsilon
logger.save_itr_params(epoch, state_dict)
def _process_env_info(self, env_step_infos):
env_info = {}
for k in env_step_infos[0].keys():
env_info[k] = []
for info in env_step_infos:
env_info[k].extend(info[k])
return env_info
def _normalize_state(self, state):
mean = self.mean_std.mean
std = self.mean_std.std
epsilon = self.mean_std.epsilon
return (state - mean) / (std+epsilon)
def _process_data(self, raw_results):
env_step_infos = [result[0] for result in raw_results]
training_datasets = [result[1] for result in raw_results] # list of dict
states = []
actions = []
sel_cuts_nums = []
neg_rewards = []
new_step_infos = {}
for dict_data in training_datasets:
states.extend(dict_data['state']) # list of numpy
actions.extend(dict_data['action']) # list of list
sel_cuts_nums.extend(dict_data['sel_cuts_num'])
neg_rewards.extend(dict_data['neg_reward'])
print(f"debug log neg_rewards: {neg_rewards}")
print(f"debug log states len: {len(states)}")
neg_rewards = np.vstack(neg_rewards)
neg_rewards = self.reward_scale * neg_rewards
if self.normalize_reward:
# log raw neg rewards
logger.record_dict(create_stats_ordered_dict('training/Nonnormalize Neg Reward', neg_rewards))
neg_rewards_mean = np.mean(neg_rewards)
neg_rewards_std = np.std(neg_rewards)
neg_rewards = (neg_rewards - neg_rewards_mean) / (neg_rewards_std + 1e-3)
for k in env_step_infos[0].keys():
new_step_infos[k] = []
for step_info in env_step_infos:
for k in step_info.keys():
new_step_infos[k].extend(step_info[k])
if self.normalize:
# update mean_std
logger.log("normalizing data .....")
stack_states = np.vstack(states)
self.mean_std.update(stack_states)
# log non-normalize states
feature_len = stack_states.shape[1]
for i in range(feature_len):
logger.record_dict(create_stats_ordered_dict(f'training/cut {i+1} th non-normalize feature', stack_states[:,i:i+1]))
# normalize states
normalize_states = [self._normalize_state(state) for state in states]
return neg_rewards, normalize_states, actions, sel_cuts_nums, new_step_infos
return neg_rewards, states, actions, sel_cuts_nums, new_step_infos
def _compute_sm_entropy(self, probs):
probs = torch.squeeze(probs)
entropy = 0
for prob in probs:
entropy -= prob * torch.log(prob)
return entropy
def train(self, raw_results, epoch):
neg_rewards, states, actions, sel_cuts_nums, env_step_infos = self._process_data(raw_results)
# states to torch
### compute policy gradient
# compute baseline function
total_num_samples = len(states)
if total_num_samples < self.batch_size:
train_loop = 1
elif total_num_samples % self.batch_size == 0:
train_loop = int(total_num_samples / self.batch_size)
else:
train_loop = int(total_num_samples / self.batch_size) + 1
if self.baseline_type == 'simple':
if epoch == 1:
self.critic_exp_mvg_avg = neg_rewards.mean()
else:
self.critic_exp_mvg_avg = (self.critic_exp_mvg_avg * self.critic_beta) + ((1. - self.critic_beta) * neg_rewards.mean())
for i in range(train_loop):
if i == (train_loop - 1):
batch_size = len(states[i*self.batch_size:])
else:
batch_size = self.batch_size
logger.log(f"training epoch: {epoch}, training loop: {train_loop}")
log_prefix = f"training epoch: {epoch}, training loop: {train_loop}"
logprobs = torch.zeros((batch_size, 1)).to(self.device)
if self.baseline_type == 'net':
neg_baseline_value = torch.zeros((batch_size, 1)).to(self.device)
for j in range(batch_size):
logger.log(f"{log_prefix} step {j}: cuda memory: {torch.cuda.memory_allocated(0)/1024**3} GB")
logger.log(f"{log_prefix} step {j}: cuda cached: {torch.cuda.memory_cached(0)/1024**3} GB")
if (torch.cuda.memory_cached(0)/1024**3) > 4.5:
# empty torch cache
torch.cuda.empty_cache()
cur_index = int(i * self.batch_size + j)
state = torch.from_numpy(states[cur_index]).float().to(self.device)
state = state.reshape(state.shape[0], 1, state.shape[1])
action = actions[cur_index]
sel_cuts_num = sel_cuts_nums[cur_index]
pointer_probs, logprob = self.pointer_net.logprobs(
state, sel_cuts_num, action
)
if logprob.item() < -4000:
logprobs[j,:] = logprob.detach()
logger.log('warning: logprob too small, we drop it!!!')
else:
logprobs[j,:] = logprob
if self.baseline_type == 'net':
neg_baseline_value[j,:] = self.value_net(state).squeeze()
if i == 0 and j == 0:
# log tensorboard
logger.tb_logger.add_histogram("selected_idxes", np.array(action), global_step=epoch)
for pos, prob_distribution in enumerate(pointer_probs):
logger.tb_logger.add_histogram(f"position {pos} probability distribution", prob_distribution, global_step=epoch)
pos_1_entropy = self._compute_sm_entropy(pointer_probs[0])
logger.record_tabular('pos_1_entropy', pos_1_entropy.item())
if i == (train_loop - 1) :
minibatch_neg_rewards = torch.from_numpy(neg_rewards[i*self.batch_size:]).float().to(self.device)
else:
minibatch_neg_rewards = torch.from_numpy(neg_rewards[i*self.batch_size:(i+1)*self.batch_size]).float().to(self.device)
if self.baseline_type == 'simple':
neg_advantage = minibatch_neg_rewards - torch.tensor([self.critic_exp_mvg_avg], dtype=torch.float, device=self.device)
elif self.baseline_type == 'no_baseline':
neg_advantage = minibatch_neg_rewards
elif self.baseline_type == 'net':
neg_advantage = minibatch_neg_rewards - neg_baseline_value.detach()
# compute policy loss
reinforce_loss = (neg_advantage * logprobs).mean()
self.policy_optimizer.zero_grad()
reinforce_loss.backward() # compute gradient
# clip gradient norms
torch.nn.utils.clip_grad_norm(self.pointer_net.parameters(),
float(self.max_grad_norm), norm_type=2)
self.policy_optimizer.step()
# compute value loss
if self.baseline_type == 'net':
critic_loss = self.critic_mse(neg_baseline_value, minibatch_neg_rewards)
self.value_optimizer.zero_grad()
critic_loss.backward()
torch.nn.utils.clip_grad_norm(self.value_net.parameters(),
float(self.max_grad_norm), norm_type=2)
self.value_optimizer.step()
torch.cuda.empty_cache()
# lr update
if self.lr_decay:
self.policy_lr_scheduler.step()
logger.log(f"epoch: {epoch}, policy optimizer lr: {self.policy_optimizer.param_groups[0]['lr']}")
# save model
self.save_checkpoint(epoch)
# log data
logger.tb_logger.add_histogram("neg_rewards", neg_rewards, global_step=epoch)
logger.record_tabular('Epoch', epoch)
logger.record_dict(create_stats_ordered_dict('training/Neg Reward', neg_rewards))
# logger.record_tabular('training/Neg Reward', neg_rewards.mean().item())
logger.record_tabular('training/Neg Advantage', neg_advantage.mean().item())
logger.record_dict(create_stats_ordered_dict('training/logprobs',logprobs.cpu().detach().numpy()))
logger.record_tabular('training/reinforce loss', reinforce_loss.item())
logger.record_tabular('training/Critic Value', self.critic_exp_mvg_avg.item())
if self.baseline_type == 'net':
logger.record_tabular('training/Critic Loss', critic_loss.item())
logger.record_tabular('training/Critic Value', neg_baseline_value.mean().item())
# log states
logger.record_dict(create_stats_ordered_dict('training/len cuts', [len(state) for state in states]))
feature_len = states[0].shape[1]
for i in range(feature_len):
logger.record_dict(create_stats_ordered_dict(f'training/cut {i+1} th feature', np.vstack([state[:,i:i+1] for state in states])))
logger.record_dict(create_stats_ordered_dict('training/sel cuts num', sel_cuts_nums))
stats = {}
for k in env_step_infos:
stats.update(create_stats_ordered_dict(
'training/'+k,
env_step_infos[k]
))
logger.record_dict(stats)
def log_evaluate_stats(self, evaluate_results):
neg_solving_time = np.vstack([result[0] for result in evaluate_results])
neg_total_nodes = np.vstack([result[1] for result in evaluate_results])
primaldualintegral = np.vstack([result[2] for result in evaluate_results])
primal_dual_gap = np.vstack([result[4] for result in evaluate_results])
stats = {}
stats.update(
create_stats_ordered_dict('evaluating/Neg Solving time', neg_solving_time)
)
stats.update(
create_stats_ordered_dict('evaluating/Neg Total Nodes', neg_total_nodes)
)
stats.update(
create_stats_ordered_dict('evaluating/PrimalDualIntegral', primaldualintegral)
)
stats.update(
create_stats_ordered_dict('evaluating/primal_dual_gap', primal_dual_gap)
)
if self.reward_type == "lp_solution_value":
lp_solution_value = []
for result in evaluate_results:
lp_solution_value.extend(result[3])
stats.update(
create_stats_ordered_dict('evaluating/lp_solution_value', lp_solution_value)
)
return stats
class HRLReinforceAlg(ReinforceBaselineAlg):
def __init__(
self,
env,
pointer_net,
value_net,
cutsel_percent_policy,
sel_cuts_percent,
device,
train_highlevel_policy_freq,
train_highlevel_batch_size,
highlevel_actor_lr,
**alg_kwargs
):
ReinforceBaselineAlg.__init__(
self,
env,
pointer_net,
value_net,
sel_cuts_percent,
device,
**alg_kwargs
)
self.highlevel_actor_lr = highlevel_actor_lr
self.cutsel_percent_policy = cutsel_percent_policy
self.cutsel_percent_policy_optimizer = self.optimizer_class(
self.cutsel_percent_policy.parameters(),
lr=self.highlevel_actor_lr
)
if self.lr_decay:
self.cutsel_percent_policy_lr_scheduler = lr_scheduler.StepLR(
self.cutsel_percent_policy_optimizer,
self.lr_decay_step,
gamma=self.lr_decay_rate
)
self.train_highlevel_policy_freq = train_highlevel_policy_freq
self.train_highlevel_batch_size = train_highlevel_batch_size
self.train_highlevel_epoch = 1
self.training_highlevel_dataset = {
'states': [],
'actions': [],
'neg_rewards': []
}
self.critic_exp_mvg_avg_high_level = torch.zeros(1)
# .to(self.device)
def train_highlevel_policy(self, raw_results, epoch):
stats = {}
if epoch % self.train_highlevel_policy_freq == 0:
self._update_highlevel_dataset(raw_results)
stats = self._train_highlevel()
self.train_highlevel_epoch += 1
# clear old dataset
self.training_highlevel_dataset = {
'states': [],
'actions': [],
'neg_rewards': []
}
return stats
# def _update_highlevel_dataset(self, raw_results):
# training_highlevel_dataset = [result[2] for result in raw_results]
# for dict_data in training_highlevel_dataset:
# self.training_highlevel_dataset['states'].extend(dict_data['state']) # list of array
# self.training_highlevel_dataset['actions'].extend(dict_data['action']) # list of int
# self.training_highlevel_dataset['neg_rewards'].extend(dict_data['neg_reward']) # list of int
def _update_highlevel_dataset(self, raw_results):
# clear past data
self.training_highlevel_dataset = {
'states': [],
'actions': [],
'neg_rewards': []
}
training_highlevel_dataset = [result[2] for result in raw_results]
for dict_data in training_highlevel_dataset:
self.training_highlevel_dataset['states'].extend(dict_data['state']) # list of array
self.training_highlevel_dataset['actions'].extend(dict_data['action']) # list of int
self.training_highlevel_dataset['neg_rewards'].extend(dict_data['neg_reward']) # list of int
def save_checkpoint(self, epoch):
model_state_dict = self.pointer_net.state_dict()
cutsel_percent_state_dict = self.cutsel_percent_policy.state_dict()
state_dict = {
"pointer_net": model_state_dict,
"cutsel_percent_net": cutsel_percent_state_dict
}
if self.normalize:
state_dict['mean'] = self.mean_std.mean
state_dict['std'] = self.mean_std.std
state_dict['epsilon'] = self.mean_std.epsilon
logger.save_itr_params(epoch, state_dict)
def _train_highlevel(self):
# get data
states = self.training_highlevel_dataset['states']
actions = self.training_highlevel_dataset['actions']
neg_rewards = np.vstack(self.training_highlevel_dataset['neg_rewards'])
neg_rewards = neg_rewards * self.reward_scale
if self.normalize:
# update mean_std
logger.log("normalizing data .....")
stack_states = np.vstack(states)
self.mean_std.update(stack_states)
# log non-normalize states
# feature_len = stack_states.shape[1]
# for i in range(feature_len):
# logger.record_dict(create_stats_ordered_dict(f'training/cut {i+1} th non-normalize feature', stack_states[:,i:i+1]))
# normalize states
states = [self._normalize_state(state) for state in states]
total_num_samples = len(states)
if total_num_samples < self.train_highlevel_batch_size:
train_loop = 1
elif total_num_samples % self.train_highlevel_batch_size == 0:
train_loop = int(total_num_samples / self.train_highlevel_batch_size)
else:
train_loop = int(total_num_samples / self.train_highlevel_batch_size) + 1
if self.train_highlevel_epoch == 1:
self.critic_exp_mvg_avg_high_level = neg_rewards.mean()
else:
self.critic_exp_mvg_avg_high_level = (self.critic_exp_mvg_avg_high_level * self.critic_beta) + ((1. - self.critic_beta) * neg_rewards.mean())
infos = {}
for i in range(train_loop):
if i == (train_loop - 1):
batch_size = len(states[i*self.train_highlevel_batch_size:])
else:
batch_size = self.train_highlevel_batch_size
logger.log(f"training epoch: {self.train_highlevel_epoch}, training loop: {i}/{train_loop}")
log_prefix = f"training epoch: {self.train_highlevel_epoch}, training loop: {i}/{train_loop}"
logprobs = torch.zeros((batch_size, 1)).to(self.device)
for j in range(batch_size):
logger.log(f"{log_prefix} step {j}: cuda memory: {torch.cuda.memory_allocated(0)/1024**3} GB")
logger.log(f"{log_prefix} step {j}: cuda cached: {torch.cuda.memory_cached(0)/1024**3} GB")
cur_index = int(i * self.train_highlevel_batch_size + j)
state = torch.from_numpy(states[cur_index]).float().to(self.device)
state = state.reshape(state.shape[0], 1, state.shape[1])
action = torch.tensor(actions[cur_index], dtype=torch.float, device=self.device)
logprob, info = self.cutsel_percent_policy.log_prob(state, action=action)
if logprob.item() < -1e5:
logprobs[j,:] = logprob.detach()
logger.log('warning: high level policy logprob too small, we detach it to drop it!!!')
else:
logprobs[j,:] = logprob
if i == 0 and j == 0:
for k in info.keys():
infos[k] = []
for k in info.keys():
infos[k].append(info[k].item())
if i == (train_loop - 1) :
minibatch_neg_rewards = torch.from_numpy(neg_rewards[i*self.train_highlevel_batch_size:]).float().to(self.device)
else:
minibatch_neg_rewards = torch.from_numpy(neg_rewards[i*self.train_highlevel_batch_size:(i+1)*self.train_highlevel_batch_size]).float().to(self.device)
if self.baseline_type == 'simple':
neg_advantage = minibatch_neg_rewards - torch.tensor([self.critic_exp_mvg_avg_high_level], dtype=torch.float, device=self.device)
else:
neg_advantage = minibatch_neg_rewards
reinforce_loss = (neg_advantage * logprobs).mean()
self.cutsel_percent_policy_optimizer.zero_grad()
reinforce_loss.backward() # compute gradient
# clip gradient norms
torch.nn.utils.clip_grad_norm(self.cutsel_percent_policy.parameters(),
float(self.max_grad_norm), norm_type=2)
self.cutsel_percent_policy_optimizer.step()
torch.cuda.empty_cache()
# lr update
if self.lr_decay:
self.cutsel_percent_policy_lr_scheduler.step()
logger.log(f"high level policy epoch: {self.train_highlevel_epoch}, policy optimizer lr: {self.cutsel_percent_policy_optimizer.param_groups[0]['lr']}")
# log data
stats = {}
Prefix = 'training_highlevel_policy'
stats.update(create_stats_ordered_dict(f'{Prefix}/Neg Reward', neg_rewards))
stats.update(create_stats_ordered_dict(f'{Prefix}/Neg Advantage', neg_advantage.mean().item()))
stats.update(create_stats_ordered_dict(f'{Prefix}/logprobs', logprobs.cpu().detach().numpy()))
stats.update(create_stats_ordered_dict(f'{Prefix}/cut_percent_actions', actions))
stats[f'{Prefix}/reinforce loss'] = reinforce_loss.item()
stats[f'{Prefix}/Critic Value'] = self.critic_exp_mvg_avg_high_level.item()
for k in infos.keys():
stats.update(create_stats_ordered_dict(
Prefix+k,
infos[k]
))
return stats