-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmain_tsp.py
480 lines (405 loc) · 20.8 KB
/
main_tsp.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
import os
import json
import argparse
import time
import numpy as np
import torch
from torch.autograd import Variable
import torch.nn.functional as F
import torch.nn as nn
from sklearn.utils.class_weight import compute_class_weight
from tensorboardX import SummaryWriter
from fastprogress import master_bar, progress_bar
# Remove warning
import warnings
warnings.filterwarnings("ignore", category=UserWarning)
from scipy.sparse import SparseEfficiencyWarning
warnings.simplefilter('ignore', SparseEfficiencyWarning)
from config import *
from problems.tsp.tsp_reader import TSPReader
from problems.tsptw.tsptw_reader import TSPTWReader
from models.gcn_model import ResidualGatedGCNModel
from models.sparse_wrapper import wrap_sparse
from models.prep_wrapper import PrepWrapResidualGatedGCNModel
parser = argparse.ArgumentParser(description='gcn_tsp_parser')
parser.add_argument('-c','--config', type=str, default="configs/default.json")
args = parser.parse_args()
config_path = args.config
config = get_config(config_path)
print("Loaded {}:\n{}".format(config_path, config))
is_tsptw = config.get('problem', 'tsp') == 'tsptw'
DataReader = TSPTWReader if is_tsptw else TSPReader
if torch.cuda.is_available():
print("CUDA available, using {} GPUs".format(torch.cuda.device_count()))
dtypeFloat = torch.cuda.FloatTensor
dtypeLong = torch.cuda.LongTensor
torch.cuda.manual_seed(1)
else:
print("CUDA not available")
dtypeFloat = torch.FloatTensor
dtypeLong = torch.LongTensor
torch.manual_seed(1)
def mean_tour_len_edges(x_edges_values, y_pred_edges):
"""
Computes mean tour length for given batch prediction as edge adjacency matrices (for PyTorch tensors).
Args:
x_edges_values: Edge values (distance) matrix (batch_size, num_nodes, num_nodes)
y_pred_edges: Edge predictions (batch_size, num_nodes, num_nodes, voc_edges)
Returns:
mean_tour_len: Mean tour length over batch
"""
y = F.softmax(y_pred_edges, dim=-1) # B x V x V x voc_edges
y = y.argmax(dim=3) # B x V x V
# Divide by 2 because edges_values is symmetric
tour_lens = (y.float() * x_edges_values.float()).sum(dim=1).sum(dim=1) / 2
mean_tour_len = tour_lens.sum().to(dtype=torch.float).item() / tour_lens.numel()
return mean_tour_len
def train_one_epoch(net, optimizer, config, master_bar, dataset=None):
# Set training mode
net.train()
# Assign parameters
num_nodes = config.num_nodes
num_neighbors = config.num_neighbors
batch_size = config.batch_size
batches_per_epoch = config.batches_per_epoch
accumulation_steps = config.accumulation_steps
train_filepath = config.train_filepath
train_target_filepath = config.train_filepath_solution
if dataset is None:
dataset = DataReader(num_nodes, num_neighbors, batch_size, train_filepath, train_target_filepath, do_shuffle=True, do_prep=False)
else:
dataset.shuffle()
if batches_per_epoch != -1:
batches_per_epoch = min(batches_per_epoch, dataset.max_iter)
else:
batches_per_epoch = dataset.max_iter
# Convert dataset to iterable
dataset = iter(dataset)
# Initially set loss class weights as None
edge_cw = None
# Initialize running data
running_loss = 0.0
# running_err_edges = 0.0
# running_err_tour = 0.0
# running_err_tsp = 0.0
running_pred_tour_len = 0.0
running_gt_tour_len = 0.0
running_nb_data = 0
running_nb_batch = 0
start_epoch = time.time()
for batch_num in progress_bar(range(batches_per_epoch), parent=master_bar):
# Generate a batch of TSPs
try:
batch = next(dataset)
except StopIteration:
break
# Convert batch to torch Variables
# x_edges = Variable(torch.LongTensor(batch.edges).type(dtypeLong), requires_grad=False)
# x_edges_values = Variable(torch.FloatTensor(batch.edges_values).type(dtypeFloat), requires_grad=False)
# x_nodes = Variable(torch.LongTensor(batch.nodes).type(dtypeLong), requires_grad=False)
x_nodes_coord = Variable(torch.FloatTensor(batch.nodes_coord).type(dtypeFloat), requires_grad=False)
x_nodes_timew = Variable(torch.FloatTensor(batch.nodes_timew).type(dtypeFloat), requires_grad=False) if is_tsptw else None
# y_edges = Variable(torch.LongTensor(batch.edges_target).type(dtypeLong), requires_grad=False)
# y_nodes = Variable(torch.LongTensor(batch.nodes_target).type(dtypeLong), requires_grad=False)
y_tour = Variable(torch.LongTensor(batch.tour_nodes).type(dtypeLong), requires_grad=False)
# Compute class weights (if uncomputed)
if type(edge_cw) != torch.Tensor:
# edge_labels = y_edges.cpu().numpy().flatten()
# edge_cw = compute_class_weight("balanced", classes=np.unique(edge_labels), y=edge_labels)
# edge_cw = len(y_edges) / (num_edge_classes * edge_label_bincount)
num_nodes = x_nodes_coord.size(1)
num_edges = num_nodes * num_nodes
num_edge_classes = 2
# Don't make tensor since then it will mess up DataParallel, this is a parameter, not input!
edge_label_bincount = np.array([num_edges - 2 * num_nodes, 2 * num_nodes])
edge_cw = num_edges / (num_edge_classes * edge_label_bincount)
# Forward pass
# y_preds, loss = net.forward(x_edges, x_edges_values, x_nodes, x_nodes_coord, y_edges, edge_cw)
y_preds, loss, x_edges_values = net.forward(x_nodes_coord, x_nodes_timew, y_tour, edge_cw)
loss = loss.mean() # Take mean of loss across multiple GPUs
loss = loss / accumulation_steps # Scale loss by accumulation steps
loss.backward()
# Backward pass
if (batch_num+1) % accumulation_steps == 0:
optimizer.step()
optimizer.zero_grad()
# Compute error metrics and mean tour lengths
# err_edges, err_tour, err_tsp, tour_err_idx, tsp_err_idx = edge_error(y_preds, y_edges, x_edges)
pred_tour_len = mean_tour_len_edges(x_edges_values, y_preds)
gt_tour_len = np.mean(batch.tour_len)
# Update running data
running_nb_data += batch_size
running_loss += batch_size* loss.data.item()* accumulation_steps # Re-scale loss
# running_err_edges += batch_size* err_edges
# running_err_tour += batch_size* err_tour
# running_err_tsp += batch_size* err_tsp
running_pred_tour_len += batch_size* pred_tour_len
running_gt_tour_len += batch_size* gt_tour_len
running_nb_batch += 1
# Log intermediate statistics
result = ('loss:{loss:.4f} pred_tour_len:{pred_tour_len:.3f} gt_tour_len:{gt_tour_len:.3f}'.format(
loss=running_loss/running_nb_data,
pred_tour_len=running_pred_tour_len/running_nb_data,
gt_tour_len=running_gt_tour_len/running_nb_data))
master_bar.child.comment = result
# Compute statistics for full epoch
loss = running_loss/ running_nb_data
err_edges = 0 # running_err_edges/ running_nb_data
err_tour = 0 # running_err_tour/ running_nb_data
err_tsp = 0 # running_err_tsp/ running_nb_data
pred_tour_len = running_pred_tour_len/ running_nb_data
gt_tour_len = running_gt_tour_len/ running_nb_data
return time.time()-start_epoch, loss, err_edges, err_tour, err_tsp, pred_tour_len, gt_tour_len
def metrics_to_str(epoch, time, learning_rate, loss, err_edges, err_tour, err_tsp, pred_tour_len, gt_tour_len):
result = ( 'epoch:{epoch:0>2d}\t'
'time:{time:.1f}h\t'
'lr:{learning_rate:.2e}\t'
'loss:{loss:.4f}\t'
# 'err_edges:{err_edges:.2f}\t'
# 'err_tour:{err_tour:.2f}\t'
# 'err_tsp:{err_tsp:.2f}\t'
'pred_tour_len:{pred_tour_len:.3f}\t'
'gt_tour_len:{gt_tour_len:.3f}'.format(
epoch=epoch,
time=time/3600,
learning_rate=learning_rate,
loss=loss,
# err_edges=err_edges,
# err_tour=err_tour,
# err_tsp=err_tsp,
pred_tour_len=pred_tour_len,
gt_tour_len=gt_tour_len))
return result
def test(net, config, master_bar, mode='test'):
# Set evaluation mode
net.eval()
# Assign parameters
num_nodes = config.num_nodes
num_neighbors = config.num_neighbors
batch_size = config.batch_size
batches_per_epoch = config.batches_per_epoch
beam_size = config.beam_size
val_filepath = config.val_filepath
val_target_filepath = config.val_filepath_solution
test_filepath = config.test_filepath
test_target_filepath = config.test_filepath_solution
# Load TSP data
if mode == 'val':
dataset = DataReader(num_nodes, num_neighbors, batch_size=batch_size, filepath=val_filepath, target_filepath=val_target_filepath, do_prep=False)
elif mode == 'test':
dataset = DataReader(num_nodes, num_neighbors, batch_size=batch_size, filepath=test_filepath, target_filepath=test_target_filepath, do_prep=False)
batches_per_epoch = dataset.max_iter
# Convert dataset to iterable
dataset = iter(dataset)
# Initially set loss class weights as None
edge_cw = None
# Initialize running data
running_loss = 0.0
# running_err_edges = 0.0
# running_err_tour = 0.0
# running_err_tsp = 0.0
running_pred_tour_len = 0.0
running_gt_tour_len = 0.0
running_nb_data = 0
running_nb_batch = 0
with torch.no_grad():
start_test = time.time()
for batch_num in progress_bar(range(batches_per_epoch), parent=master_bar):
# Generate a batch of TSPs
try:
batch = next(dataset)
except StopIteration:
break
# Convert batch to torch Variables
# x_edges = Variable(torch.LongTensor(batch.edges).type(dtypeLong), requires_grad=False)
# x_edges_values = Variable(torch.FloatTensor(batch.edges_values).type(dtypeFloat), requires_grad=False)
# x_nodes = Variable(torch.LongTensor(batch.nodes).type(dtypeLong), requires_grad=False)
x_nodes_coord = Variable(torch.FloatTensor(batch.nodes_coord).type(dtypeFloat), requires_grad=False)
x_nodes_timew = Variable(torch.FloatTensor(batch.nodes_timew).type(dtypeFloat), requires_grad=False) if is_tsptw else None
# y_edges = Variable(torch.LongTensor(batch.edges_target).type(dtypeLong), requires_grad=False)
# y_nodes = Variable(torch.LongTensor(batch.nodes_target).type(dtypeLong), requires_grad=False)
y_tour = Variable(torch.LongTensor(batch.tour_nodes).type(dtypeLong), requires_grad=False)
# Compute class weights (if uncomputed)
if type(edge_cw) != torch.Tensor:
# edge_labels = y_edges.cpu().numpy().flatten()
# edge_cw = compute_class_weight("balanced", classes=np.unique(edge_labels), y=edge_labels)
# edge_cw = len(y_edges) / (num_edge_classes * edge_label_bincount)
num_nodes = x_nodes_coord.size(1)
num_edges = num_nodes * num_nodes
num_edge_classes = 2
# Don't make tensor since then it will mess up DataParallel, this is a parameter, not input!
edge_label_bincount = np.array([num_edges - 2 * num_nodes, 2 * num_nodes])
edge_cw = num_edges / (num_edge_classes * edge_label_bincount)
# Forward pass
# y_preds, loss = net.forward(x_edges, x_edges_values, x_nodes, x_nodes_coord, y_edges, edge_cw)
y_preds, loss, x_edges_values = net.forward(x_nodes_coord, x_nodes_timew, y_tour, edge_cw)
loss = loss.mean() # Take mean of loss across multiple GPUs
# Compute error metrics
# err_edges, err_tour, err_tsp, tour_err_idx, tsp_err_idx = edge_error(y_preds, y_edges, x_edges)
# Get batch beamsearch tour prediction
# if mode == 'val': # Validation: faster 'vanilla' beamsearch
# bs_nodes = beamsearch_tour_nodes(
# y_preds, beam_size, batch_size, num_nodes, dtypeFloat, dtypeLong, probs_type='logits')
# elif mode == 'test': # Testing: beamsearch with shortest tour heuristic
# bs_nodes = beamsearch_tour_nodes_shortest(
# y_preds, x_edges_values, beam_size, batch_size, num_nodes, dtypeFloat, dtypeLong, probs_type='logits')
#
# Compute mean tour length
# pred_tour_len = mean_tour_len_nodes(x_edges_values, bs_nodes)
gt_tour_len = np.mean(batch.tour_len)
# Update running data
running_nb_data += batch_size
running_loss += batch_size* loss.data.item()
# running_err_edges += batch_size* err_edges
# running_err_tour += batch_size* err_tour
# running_err_tsp += batch_size* err_tsp
# running_pred_tour_len += batch_size* pred_tour_len
running_gt_tour_len += batch_size* gt_tour_len
running_nb_batch += 1
# Log intermediate statistics
# result = ('loss:{loss:.4f} pred_tour_len:{pred_tour_len:.3f} gt_tour_len:{gt_tour_len:.3f}'.format(
result = ('loss:{loss:.4f} gt_tour_len:{gt_tour_len:.3f}'.format(
loss=running_loss/running_nb_data,
# pred_tour_len=running_pred_tour_len/running_nb_data,
gt_tour_len=running_gt_tour_len/running_nb_data))
master_bar.child.comment = result
# Compute statistics for full epoch
loss = running_loss/ running_nb_data
err_edges = 0 # running_err_edges/ running_nb_data
err_tour = 0 # running_err_tour/ running_nb_data
err_tsp = 0 # running_err_tsp/ running_nb_data
pred_tour_len = running_pred_tour_len/ running_nb_data
gt_tour_len = running_gt_tour_len/ running_nb_data
return time.time()-start_test, loss, err_edges, err_tour, err_tsp, pred_tour_len, gt_tour_len
def main(config):
# Instantiate the network
assert config.num_neighbors == -1, "KNN features is deprecated due to PrepWrap"
model = ResidualGatedGCNModel(config, dtypeFloat, dtypeLong)
if 'sparse' in config and config.sparse is not None:
model = wrap_sparse(model, config.sparse)
model = PrepWrapResidualGatedGCNModel(model)
net = nn.DataParallel(model)
if torch.cuda.is_available():
net.cuda()
print(net)
# Compute number of network parameters
nb_param = 0
for param in net.parameters():
nb_param += np.prod(list(param.data.size()))
print('Number of parameters:', nb_param)
# Create log directory
log_dir = f"./logs/{config.expt_name}/"
os.makedirs(log_dir, exist_ok=True)
json.dump(config, open(f"{log_dir}/config.json", "w"), indent=4)
writer = SummaryWriter(log_dir) # Define Tensorboard writer
# Training parameters
num_nodes = config.num_nodes
num_neighbors = config.num_neighbors
max_epochs = config.max_epochs
val_every = config.val_every
test_every = config.test_every
batch_size = config.batch_size
batches_per_epoch = config.batches_per_epoch
accumulation_steps = config.accumulation_steps
learning_rate = config.learning_rate
decay_rate = config.decay_rate
val_loss_old = 1e6 # For decaying LR based on validation loss
best_pred_tour_len = 1e6 # For saving checkpoints
best_val_loss = 1e6 # For saving checkpoints
# Define optimizer
optimizer = torch.optim.Adam(net.parameters(), lr=learning_rate)
print(optimizer)
dataset = DataReader(
config.num_nodes, config.num_neighbors, config.batch_size,
config.train_filepath, config.train_filepath_solution,
do_prep=False
)
if 'resume_from_dir' in config:
if torch.cuda.is_available():
checkpoint = torch.load(os.path.join(config.resume_from_dir, "last_train_checkpoint.tar"))
else:
checkpoint = torch.load(os.path.join(config.resume_from_dir, "last_train_checkpoint.tar"), map_location='cpu')
# Load network state
net.load_state_dict(checkpoint['model_state_dict'])
# Load optimizer state
optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
# Load other training parameters
epoch = checkpoint['epoch']
train_loss = checkpoint['train_loss']
val_loss = checkpoint['val_loss']
# Note: the learning_rate was set in load_state_dict,
# this is just to have the local variable for logging
for param_group in optimizer.param_groups:
learning_rate = param_group['lr']
print(f"Loaded checkpoint from epoch {epoch}")
else:
epoch = -1
epoch_bar = master_bar(range(epoch + 1, max_epochs))
for epoch in epoch_bar:
# Log to Tensorboard
writer.add_scalar('learning_rate', learning_rate, epoch)
# Train
train_time, train_loss, train_err_edges, train_err_tour, train_err_tsp, train_pred_tour_len, train_gt_tour_len = train_one_epoch(net, optimizer, config, epoch_bar, dataset=dataset)
epoch_bar.write('t: ' + metrics_to_str(epoch, train_time, learning_rate, train_loss, train_err_edges, train_err_tour, train_err_tsp, train_pred_tour_len, train_gt_tour_len))
writer.add_scalar('loss/train_loss', train_loss, epoch)
writer.add_scalar('pred_tour_len/train_pred_tour_len', train_pred_tour_len, epoch)
writer.add_scalar('optimality_gap/train_opt_gap', train_pred_tour_len/train_gt_tour_len - 1, epoch)
if epoch % val_every == 0 or epoch == max_epochs-1:
# Validate
val_time, val_loss, val_err_edges, val_err_tour, val_err_tsp, val_pred_tour_len, val_gt_tour_len = test(net, config, epoch_bar, mode='val')
epoch_bar.write('v: ' + metrics_to_str(epoch, val_time, learning_rate, val_loss, val_err_edges, val_err_tour, val_err_tsp, val_pred_tour_len, val_gt_tour_len))
writer.add_scalar('loss/val_loss', val_loss, epoch)
writer.add_scalar('pred_tour_len/val_pred_tour_len', val_pred_tour_len, epoch)
writer.add_scalar('optimality_gap/val_opt_gap', val_pred_tour_len/val_gt_tour_len - 1, epoch)
# Save checkpoint
if val_pred_tour_len < best_pred_tour_len:
best_pred_tour_len = val_pred_tour_len # Update best val predicted tour length
torch.save({
'epoch': epoch,
'model_state_dict': net.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'train_loss': train_loss,
'val_loss': val_loss,
}, log_dir+"best_val_tourlen_checkpoint.tar")
if val_loss < best_val_loss:
best_val_loss = val_loss # Update best val loss
torch.save({
'epoch': epoch,
'model_state_dict': net.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'train_loss': train_loss,
'val_loss': val_loss,
}, log_dir+"best_val_loss_checkpoint.tar")
# Update learning rate
if val_loss > 0.99 * val_loss_old:
learning_rate /= decay_rate
for param_group in optimizer.param_groups:
param_group['lr'] = learning_rate
val_loss_old = val_loss # Update old validation loss
if epoch % test_every == 0 or epoch == max_epochs-1:
# Test
test_time, test_loss, test_err_edges, test_err_tour, test_err_tsp, test_pred_tour_len, test_gt_tour_len = test(net, config, epoch_bar, mode='test')
epoch_bar.write('T: ' + metrics_to_str(epoch, test_time, learning_rate, test_loss, test_err_edges, test_err_tour, test_err_tsp, test_pred_tour_len, test_gt_tour_len))
writer.add_scalar('loss/test_loss', test_loss, epoch)
writer.add_scalar('pred_tour_len/test_pred_tour_len', test_pred_tour_len, epoch)
writer.add_scalar('optimality_gap/test_opt_gap', test_pred_tour_len/test_gt_tour_len - 1, epoch)
# Save training checkpoint at the end of epoch
torch.save({
'epoch': epoch,
'model_state_dict': net.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'train_loss': train_loss,
'val_loss': val_loss,
}, log_dir+"last_train_checkpoint.tar")
# Save checkpoint after every 250 epochs
if epoch != 0 and (epoch % 250 == 0 or epoch == max_epochs-1):
torch.save({
'epoch': epoch,
'model_state_dict': net.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'train_loss': train_loss,
'val_loss': val_loss,
}, log_dir+f"checkpoint_epoch{epoch}.tar")
return net
if __name__ == "__main__":
main(config)