forked from wouterkool/attention-learn-to-route
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.py
209 lines (163 loc) · 6.35 KB
/
functions.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
import warnings
import torch
import numpy as np
import os
import json
from tqdm import tqdm
from multiprocessing.dummy import Pool as ThreadPool
from multiprocessing import Pool
import torch.nn.functional as F
def load_problem(name):
from problems import TSP, CVRP, SDVRP, OP, PCTSPDet, PCTSPStoch
problem = {
'tsp': TSP,
'cvrp': CVRP,
'sdvrp': SDVRP,
'op': OP,
'pctsp_det': PCTSPDet,
'pctsp_stoch': PCTSPStoch,
}.get(name, None)
assert problem is not None, "Currently unsupported problem: {}!".format(name)
return problem
def torch_load_cpu(load_path):
return torch.load(load_path, map_location=lambda storage, loc: storage) # Load on CPU
def move_to(var, device):
if isinstance(var, dict):
return {k: move_to(v, device) for k, v in var.items()}
return var.to(device)
def _load_model_file(load_path, model):
"""Loads the model with parameters from the file and returns optimizer state dict if it is in the file"""
# Load the model parameters from a saved state
load_optimizer_state_dict = None
print(' [*] Loading model from {}'.format(load_path))
load_data = torch.load(
os.path.join(
os.getcwd(),
load_path
), map_location=lambda storage, loc: storage)
if isinstance(load_data, dict):
load_optimizer_state_dict = load_data.get('optimizer', None)
load_model_state_dict = load_data.get('model', load_data)
else:
load_model_state_dict = load_data.state_dict()
state_dict = model.state_dict()
state_dict.update(load_model_state_dict)
model.load_state_dict(state_dict)
return model, load_optimizer_state_dict
def load_args(filename):
with open(filename, 'r') as f:
args = json.load(f)
# Backwards compatibility
if 'data_distribution' not in args:
args['data_distribution'] = None
probl, *dist = args['problem'].split("_")
if probl == "op":
args['problem'] = probl
args['data_distribution'] = dist[0]
return args
def load_model(path, epoch=None):
from nets.attention_model import AttentionModel
from nets.pointer_network import PointerNetwork
if os.path.isfile(path):
model_filename = path
path = os.path.dirname(model_filename)
elif os.path.isdir(path):
if epoch is None:
epoch = max(
int(os.path.splitext(filename)[0].split("-")[1])
for filename in os.listdir(path)
if os.path.splitext(filename)[1] == '.pt'
)
model_filename = os.path.join(path, 'epoch-{}.pt'.format(epoch))
else:
assert False, "{} is not a valid directory or file".format(path)
args = load_args(os.path.join(path, 'args.json'))
problem = load_problem(args['problem'])
model_class = {
'attention': AttentionModel,
'pointer': PointerNetwork
}.get(args.get('model', 'attention'), None)
assert model_class is not None, "Unknown model: {}".format(model_class)
model = model_class(
args['embedding_dim'],
args['hidden_dim'],
problem,
n_encode_layers=args['n_encode_layers'],
mask_inner=True,
mask_logits=True,
normalization=args['normalization'],
tanh_clipping=args['tanh_clipping'],
checkpoint_encoder=args.get('checkpoint_encoder', False),
shrink_size=args.get('shrink_size', None)
)
# Overwrite model parameters by parameters to load
load_data = torch_load_cpu(model_filename)
model.load_state_dict({**model.state_dict(), **load_data.get('model', {})})
model, *_ = _load_model_file(model_filename, model)
model.eval() # Put in eval mode
return model, args
def parse_softmax_temperature(raw_temp):
# Load from file
if os.path.isfile(raw_temp):
return np.loadtxt(raw_temp)[-1, 0]
return float(raw_temp)
def run_all_in_pool(func, directory, dataset, opts, use_multiprocessing=True):
# # Test
# res = func((directory, 'test', *dataset[0]))
# return [res]
num_cpus = os.cpu_count() if opts.cpus is None else opts.cpus
w = len(str(len(dataset) - 1))
offset = getattr(opts, 'offset', None)
if offset is None:
offset = 0
ds = dataset[offset:(offset + opts.n if opts.n is not None else len(dataset))]
pool_cls = (Pool if use_multiprocessing and num_cpus > 1 else ThreadPool)
with pool_cls(num_cpus) as pool:
results = list(tqdm(pool.imap(
func,
[
(
directory,
str(i + offset).zfill(w),
*problem
)
for i, problem in enumerate(ds)
]
), total=len(ds), mininterval=opts.progress_bar_mininterval))
failed = [str(i + offset) for i, res in enumerate(results) if res is None]
assert len(failed) == 0, "Some instances failed: {}".format(" ".join(failed))
return results, num_cpus
def do_batch_rep(v, n):
if isinstance(v, dict):
return {k: do_batch_rep(v_, n) for k, v_ in v.items()}
elif isinstance(v, list):
return [do_batch_rep(v_, n) for v_ in v]
elif isinstance(v, tuple):
return tuple(do_batch_rep(v_, n) for v_ in v)
return v[None, ...].expand(n, *v.size()).contiguous().view(-1, *v.size()[1:])
def sample_many(inner_func, get_cost_func, input, batch_rep=1, iter_rep=1):
"""
:param input: (batch_size, graph_size, node_dim) input node features
:return:
"""
input = do_batch_rep(input, batch_rep)
costs = []
pis = []
for i in range(iter_rep):
_log_p, pi = inner_func(input)
# pi.view(-1, batch_rep, pi.size(-1))
cost, mask = get_cost_func(input, pi)
costs.append(cost.view(batch_rep, -1).t())
pis.append(pi.view(batch_rep, -1, pi.size(-1)).transpose(0, 1))
max_length = max(pi.size(-1) for pi in pis)
# (batch_size * batch_rep, iter_rep, max_length) => (batch_size, batch_rep * iter_rep, max_length)
pis = torch.cat(
[F.pad(pi, (0, max_length - pi.size(-1))) for pi in pis],
1
) # .view(embeddings.size(0), batch_rep * iter_rep, max_length)
costs = torch.cat(costs, 1)
# (batch_size)
mincosts, argmincosts = costs.min(-1)
# (batch_size, minlength)
minpis = pis[torch.arange(pis.size(0), out=argmincosts.new()), argmincosts]
return minpis, mincosts