-
Notifications
You must be signed in to change notification settings - Fork 6
/
Tester.py
297 lines (255 loc) · 15 KB
/
Tester.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
import os, re, time
from torch.optim import Adam as Optimizer
import envs
from utils import *
class Tester:
def __init__(self, args, env_params, model_params, tester_params):
# save arguments
self.args = args
self.env_params = env_params
self.model_params = model_params
self.tester_params = tester_params
# ENV, MODEL, & Load checkpoint
self.envs = get_env(self.args.problem) # Env Class
self.device = args.device
self.checkpoint = torch.load(args.checkpoint, map_location=self.device)
self.model_params['problem'] = self.checkpoint['problem'] # training problem of the checkpoint
self.model = get_model(self.args.model_type)(**self.model_params)
self.fine_tune_model = get_model(self.args.model_type)(**self.model_params)
num_param(self.model)
self.model.load_state_dict(self.checkpoint['model_state_dict'], strict=True)
print(">> Checkpoint (Epoch: {}) Loaded!".format(self.checkpoint['epoch']))
# load dataset
if tester_params['test_set_path'] is None or tester_params['test_set_path'].endswith(".pkl"):
self.data_dir = "./data"
else:
# for solving benchmark instances
self.path_list = [os.path.join(tester_params['test_set_path'], f) for f in sorted(os.listdir(tester_params['test_set_path']))] \
if os.path.isdir(tester_params['test_set_path']) else [tester_params['test_set_path']]
assert self.path_list[-1].endswith(".vrp") or self.path_list[-1].endswith(".txt"), "Unsupported file types."
# utility
self.time_estimator = TimeEstimator()
def run(self):
for env_class in self.envs:
start_time = time.time()
if self.tester_params['test_set_path'] is None or self.tester_params['test_set_path'].endswith(".pkl"):
compute_gap = not (self.tester_params['test_set_path'] is not None and self.tester_params['test_set_opt_sol_path'] is None)
if self.tester_params['fine_tune_epochs'] > 0:
self._test(self.model, env_class, compute_gap=compute_gap)
self._fine_tune(self.model, env_class)
else:
self._test(self.model, env_class, compute_gap=compute_gap)
else:
for path in self.path_list:
if env_class is envs.CVRPEnv:
self._solve_cvrplib(self.model, path, env_class)
elif env_class is envs.VRPTWEnv:
self._solve_cvrptwlib(self.model, path, env_class)
else:
raise NotImplementedError
print(">> Evaluation finished within {:.2f}s\n".format(time.time() - start_time))
def _test(self, model, env_class, compute_gap=False):
self.time_estimator.reset()
env = env_class(**self.env_params)
score_AM, gap_AM = AverageMeter(), AverageMeter()
aug_score_AM, aug_gap_AM = AverageMeter(), AverageMeter()
scores, aug_scores = torch.zeros(0).to(self.device), torch.zeros(0).to(self.device)
episode, test_num_episode = 0, self.tester_params['test_episodes']
data_path = self.tester_params['test_set_path'] if self.tester_params['test_set_path'] \
else os.path.join(self.data_dir, env.problem, "{}{}_uniform.pkl".format(env.problem.lower(), env.problem_size))
while episode < test_num_episode:
remaining = test_num_episode - episode
batch_size = min(self.tester_params['test_batch_size'], remaining)
data = env.load_dataset(data_path, offset=episode, num_samples=batch_size)
score, aug_score, all_score, all_aug_score = self._test_one_batch(model, data, env)
score_AM.update(score, batch_size)
aug_score_AM.update(aug_score, batch_size)
scores = torch.cat((scores, all_score), dim=0)
aug_scores = torch.cat((aug_scores, all_aug_score), dim=0)
if compute_gap:
opt_sol_path = self.tester_params['test_set_opt_sol_path'] if self.tester_params['test_set_opt_sol_path'] \
else get_opt_sol_path(os.path.join(self.data_dir, env.problem), env.problem, env.problem_size)
opt_sol = load_dataset(opt_sol_path, disable_print=True)[episode: episode + batch_size] # [(obj, route), ...]
opt_sol = [i[0] for i in opt_sol]
gap = [(all_score[i].item() - opt_sol[i]) / opt_sol[i] * 100 for i in range(batch_size)]
aug_gap = [(all_aug_score[i].item() - opt_sol[i]) / opt_sol[i] * 100 for i in range(batch_size)]
gap_AM.update(sum(gap)/batch_size, batch_size)
aug_gap_AM.update(sum(aug_gap)/batch_size, batch_size)
episode += batch_size
elapsed_time_str, remain_time_str = self.time_estimator.get_est_string(episode, test_num_episode)
print("episode {:3d}/{:3d}, Elapsed[{}], Remain[{}], score:{:.3f}, aug_score:{:.3f}".format(
episode, test_num_episode, elapsed_time_str, remain_time_str, score, aug_score))
all_done = (episode == test_num_episode)
if all_done:
print(" \n*** Test Done on {} *** ".format(env.problem))
print(" NO-AUG SCORE: {:.4f}, Gap: {:.4f} ".format(score_AM.avg, gap_AM.avg))
print(" AUGMENTATION SCORE: {:.4f}, Gap: {:.4f} ".format(aug_score_AM.avg, aug_gap_AM.avg))
print("{:.3f} ({:.3f}%)".format(score_AM.avg, gap_AM.avg))
print("{:.3f} ({:.3f}%)".format(aug_score_AM.avg, aug_gap_AM.avg))
return scores, aug_scores
def _test_one_batch(self, model, test_data, env):
aug_factor = self.tester_params['aug_factor']
batch_size = test_data.size(0) if isinstance(test_data, torch.Tensor) else test_data[-1].size(0)
sample_size = self.tester_params['sample_size'] if self.model_params['eval_type'] == "softmax" else 1
# Sampling: augment data based on sample_size: [batch_size, ...] -> [batch_size x sample_size, ...]
if self.model_params['eval_type'] == "softmax":
test_data = list(test_data)
for i, data in enumerate(test_data):
if data.dim() == 1:
test_data[i] = data.repeat(sample_size)
elif data.dim() == 2:
test_data[i] = data.repeat(sample_size, 1)
elif data.dim() == 3:
test_data[i] = data.repeat(sample_size, 1, 1)
# Ready
model.eval()
with torch.no_grad():
env.load_problems(batch_size, problems=test_data, aug_factor=aug_factor)
reset_state, _, _ = env.reset()
model.pre_forward(reset_state)
# POMO Rollout
state, reward, done = env.pre_step()
while not done:
selected, _ = model(state)
# shape: (batch, pomo)
state, reward, done = env.step(selected)
# Return
aug_reward = reward.reshape(aug_factor * sample_size, batch_size, env.pomo_size)
# shape: (augmentation, batch, pomo)
max_pomo_reward, _ = aug_reward.max(dim=2) # get best results from pomo
# shape: (augmentation, batch)
no_aug_score = -max_pomo_reward[0, :].float() # negative sign to make positive value
no_aug_score_mean = no_aug_score.mean()
max_aug_pomo_reward, _ = max_pomo_reward.max(dim=0) # get best results from augmentation
# shape: (batch,)
aug_score = -max_aug_pomo_reward.float() # negative sign to make positive value
aug_score_mean = aug_score.mean()
return no_aug_score_mean.item(), aug_score_mean.item(), no_aug_score, aug_score
def _fine_tune(self, model, env_class):
self.fine_tune_model.load_state_dict(model.state_dict(), strict=True)
optimizer = Optimizer(self.fine_tune_model.parameters(), lr=self.tester_params['lr'], weight_decay=self.tester_params['weight_decay'])
# optimizer.load_state_dict(self.checkpoint['optimizer_state_dict'])
env = env_class(**self.env_params)
fine_tune_episode = self.tester_params['fine_tune_episodes']
for i in range(self.tester_params['fine_tune_epochs']):
episode = 0
while episode < fine_tune_episode:
remaining = fine_tune_episode - episode
batch_size = min(self.tester_params['fine_tune_batch_size'], remaining)
data = env.get_random_problems(batch_size, self.env_params["problem_size"])
self._fine_tune_one_batch(self.fine_tune_model, data, env, optimizer)
episode += batch_size
print("\n>> Fine-Tuning Epoch {} Finished. Staring Evaluation ...".format(i + 1))
self._test(self.fine_tune_model, env_class, compute_gap=True)
def _fine_tune_one_batch(self, model, data, env, optimizer):
model.train()
model.set_eval_type(self.model_params["eval_type"])
aug_factor = self.tester_params['fine_tune_aug_factor']
batch_size = data.size(0) * aug_factor if isinstance(data, torch.Tensor) else data[-1].size(0) * aug_factor
env.load_problems(batch_size, problems=data, aug_factor=aug_factor)
reset_state, _, _ = env.reset()
model.pre_forward(reset_state)
prob_list = torch.zeros(size=(batch_size, env.pomo_size, 0))
# shape: (batch, pomo, 0~problem)
# POMO Rollout
state, reward, done = env.pre_step()
while not done:
selected, prob = model(state)
# shape: (batch, pomo)
state, reward, done = env.step(selected)
prob_list = torch.cat((prob_list, prob[:, :, None]), dim=2)
# Loss
advantage = reward - reward.float().mean(dim=1, keepdims=True) # (batch, pomo)
log_prob = prob_list.log().sum(dim=2)
loss = -advantage * log_prob # Minus Sign: To Increase REWARD
loss_mean = loss.mean()
if hasattr(model, "aux_loss"):
loss_mean = loss_mean + model.aux_loss # add aux(moe)_loss for load balancing (default coefficient: 1e-2)
# Step & Return
model.zero_grad()
loss_mean.backward()
optimizer.step()
def _solve_cvrplib(self, model, path, env_class):
"""
Solving one instance with CVRPLIB format.
"""
file = open(path, "r")
lines = [ll.strip() for ll in file]
i = 0
while i < len(lines):
line = lines[i]
if line.startswith("DIMENSION"):
dimension = int(line.split(':')[1])
elif line.startswith("CAPACITY"):
capacity = int(line.split(':')[1])
elif line.startswith('NODE_COORD_SECTION'):
locations = np.loadtxt(lines[i + 1:i + 1 + dimension], dtype=int)
i = i + dimension
elif line.startswith('DEMAND_SECTION'):
demand = np.loadtxt(lines[i + 1:i + 1 + dimension], dtype=int)
i = i + dimension
i += 1
original_locations = locations[:, 1:]
original_locations = np.expand_dims(original_locations, axis=0) # [1, n+1, 2]
loc_scaler = 1000
assert original_locations.max() <= loc_scaler, ">> Scaler is too small"
locations = original_locations / loc_scaler # [1, n+1, 2]: Scale location coordinates to [0, 1]
depot_xy, node_xy = torch.Tensor(locations[:, :1, :]), torch.Tensor(locations[:, 1:, :])
node_demand = torch.Tensor(demand[1:, 1:].reshape((1, -1))) / capacity # [1, n]
env_params = {'problem_size': node_xy.size(1), 'pomo_size': node_xy.size(1), 'loc_scaler': loc_scaler, 'device': self.device}
env = env_class(**env_params)
data = (depot_xy, node_xy, node_demand)
_, _, no_aug_score, aug_score = self._test_one_batch(model, data, env)
no_aug_score = torch.round(no_aug_score * loc_scaler).long()
aug_score = torch.round(aug_score * loc_scaler).long()
print(">> Finish solving {} -> no_aug: {} aug: {}".format(path, no_aug_score, aug_score))
return no_aug_score, aug_score
def _solve_cvrptwlib(self, model, path, env_class):
"""
Solving one instance with VRPTW benchmark (e.g., Solomon) format.
"""
file = open(path, "r")
lines = [ll.strip() for ll in file]
i = 0
while i < len(lines):
line = lines[i]
if line.startswith("NUMBER"):
line = lines[i+1]
vehicle_number = int(line.split(' ')[0]) # TODO: check vehicle number constraint
capacity = int(line.split(' ')[-1])
elif line.startswith("CUST NO."):
data = np.loadtxt(lines[i + 1:], dtype=int)
break
i += 1
original_locations = data[:, 1:3]
original_locations = np.expand_dims(original_locations, axis=0) # [1, n+1, 2]
scaler = max(original_locations.max(), data[0, 5] / 3.) # we set the time window of the depot node as [0, 3]
# scaler = 1000
assert original_locations.max() <= scaler, ">> Scaler is too small for {}".format(path)
locations = original_locations / scaler # [1, n+1, 2]: Scale location coordinates to [0, 1]
depot_xy, node_xy = torch.Tensor(locations[:, :1, :]), torch.Tensor(locations[:, 1:, :])
node_demand = torch.Tensor(data[1:, 3].reshape((1, -1))) / capacity # [1, n]
service_time = torch.Tensor(data[1:, -1].reshape((1, -1))) / scaler # [1, n]
tw_start = torch.Tensor(data[1:, 4].reshape((1, -1))) / scaler # [1, n]
tw_end = torch.Tensor(data[1:, 5].reshape((1, -1))) / scaler # [1, n]
env_params = {'problem_size': node_xy.size(1), 'pomo_size': node_xy.size(1), 'loc_scaler': scaler, 'device': self.device}
env = env_class(**env_params)
env.depot_end = data[0, 5] / scaler
data = (depot_xy, node_xy, node_demand, service_time, tw_start, tw_end)
_, _, no_aug_score, aug_score = self._test_one_batch(model, data, env)
# Check distance
original_locations = torch.Tensor(original_locations)
depot_xy = env.augment_xy_data_by_8_fold(original_locations[:, :1, :])
node_xy = env.augment_xy_data_by_8_fold(original_locations[:, 1:, :])
original_locations = torch.cat((depot_xy, node_xy), dim=1)
gathering_index = env.selected_node_list[:, :, :, None].expand(-1, -1, -1, 2)
all_xy = original_locations[:, None, :, :].expand(-1, env_params["pomo_size"], -1, -1)
ordered_seq = all_xy.gather(dim=2, index=gathering_index)
rolled_seq = ordered_seq.roll(dims=2, shifts=-1)
segment_lengths = ((ordered_seq - rolled_seq) ** 2).sum(3).sqrt()
travel_distances = segment_lengths.sum(2)
# print(travel_distances.min())
no_aug_score = torch.round(no_aug_score * scaler).long()
aug_score = torch.round(aug_score * scaler).long()
print(">> Finish solving {} -> no_aug: {} aug: {} real: {}".format(path, no_aug_score, aug_score, travel_distances.min()))
return no_aug_score, aug_score