-
Notifications
You must be signed in to change notification settings - Fork 3
/
run.py
92 lines (73 loc) · 2.95 KB
/
run.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
import os
import json
import torch
import pprint
import numpy as np
import random
from tensorboard_logger import Logger as TbLogger
import warnings
from options import get_options
from problems.problem_tsp import TSP
from problems.problem_cvrp import CVRP
from agent.ppo import PPO
def load_problem(name):
problem = {
'tsp': TSP,
'cvrp': CVRP,
}.get(name, None)
assert problem is not None, "Currently unsupported problem: {}!".format(name)
return problem
def run(opts):
# Pretty print the run args
pprint.pprint(vars(opts))
# Set the random seed
torch.manual_seed(opts.seed)
np.random.seed(opts.seed)
random.seed(opts.seed)
# Optionally configure tensorboard
tb_logger = None
if not opts.no_tb and not opts.distributed:
tb_logger = TbLogger(os.path.join(opts.log_dir, "{}_{}".format(opts.problem,
opts.graph_size), opts.run_name))
if not opts.no_saving and not os.path.exists(opts.save_dir):
os.makedirs(opts.save_dir)
# Save arguments so exact configuration can always be found
if not opts.no_saving:
with open(os.path.join(opts.save_dir, "args.json"), 'w') as f:
json.dump(vars(opts), f, indent=True)
# Set the device
opts.device = torch.device("cuda" if opts.use_cuda else "cpu")
# Figure out what's the problem
problem = load_problem(opts.problem)(
p_size = opts.graph_size,
init_val_met = opts.init_val_met,
with_assert = opts.use_assert,
DUMMY_RATE = opts.dummy_rate,
k = opts.k,
with_bonus = not opts.wo_bonus,
with_regular = not opts.wo_regular)
# Figure out the RL algorithm
agent = PPO(problem, opts)
# Load data from load_path
assert opts.load_path is None or opts.resume is None, "Only one of load path and resume can be given"
load_path = opts.load_path if opts.load_path is not None else opts.resume
if load_path is not None:
agent.load(load_path)
# Do validation only
if opts.eval_only:
# Load the validation datasets
agent.start_inference(problem, opts.val_dataset, tb_logger)
else:
if opts.resume:
epoch_resume = int(os.path.splitext(os.path.split(opts.resume)[-1])[0].split("-")[1])
print("Resuming after {}".format(epoch_resume))
agent.opts.epoch_start = epoch_resume + 1
# Start the actual training loop
agent.start_training(problem, opts.val_dataset, tb_logger)
if __name__ == "__main__":
torch.multiprocessing.set_start_method('spawn')
warnings.filterwarnings("ignore")
os.environ['KMP_DUPLICATE_LIB_OK']='True'
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
run(get_options())