forked from henry-yeh/GLOP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval_cvrp.py
70 lines (55 loc) · 2.83 KB
/
eval_cvrp.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
import time
import argparse
import torch
import pprint as pp
from problems.cvrp import init
from heatmap.cvrp.inst import sum_cost
from utils.lkh import lkh_solve
def cvrp_lkh_eval(path, opts):
pp.pprint(vars(opts))
start = time.time()
dataset, n_tsps_per_route_lst = init(dataset_path, opts)
sum_time = time.time() - start
avg_time_4_heatmap = sum_time / opts.val_size
cost_lst = []
time_lst = []
for instance_id in range(opts.val_size):
subtsps = dataset[instance_id].numpy().tolist()
costs, duration = lkh_solve(opts, subtsps, instance_id)
costs = sum_cost(costs, n_tsps_per_route_lst[instance_id])
assert costs.shape == (opts.n_partition,)
cost = costs.min()
total_time = duration + avg_time_4_heatmap
cost_lst.append(cost)
time_lst.append(total_time)
print('cost', cost.item(), '| time', total_time)
print('Average cost:', torch.stack(cost_lst).mean().item())
print('Average duration:', torch.tensor(time_lst).mean().item())
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--problem_size', type=int, default=1000, help='Problem size of CVRP')
parser.add_argument('--val_size', type=int, default=100, help='Number of instances used for reporting test performance')
parser.add_argument('--n_partition', type=int, default=1, help='The number of stochastically constructed CVRP partitions')
parser.add_argument("--cpus", type=int, required=True, help="Number of CPUs to use")
parser.add_argument('--disable_cache', action='store_true', help='Disable caching')
parser.add_argument('--max_calc_batch_size', type=int, default=1000, help='Size for subbatches')
parser.add_argument('--progress_bar_mininterval', type=float, default=0.1, help='Minimum interval')
parser.add_argument('-n', type=int, help="Number of instances to process")
parser.add_argument('--offset', type=int, help="Offset where to start processing")
parser.add_argument('--results_dir', default='results', help="Name of results directory")
parser.add_argument('--no_cuda', action='store_true', help='Disable CUDA')
parser.add_argument("--device_id", type=int, default=0)
parser.add_argument('--seed', type=int, default=1)
parser.add_argument('--ckpt_path', type=str, default='')
parser.add_argument('--path', type=str, default='')
opts = parser.parse_args()
use_cuda = torch.cuda.is_available() and not opts.no_cuda
device_id = opts.device_id
device = torch.device(f"cuda:{device_id}" if use_cuda else "cpu")
opts.device = device
torch.manual_seed(opts.seed)
if opts.path == '':
dataset_path = f'data/vrp/vrp{opts.problem_size}_test_seed1234.pkl'
else:
dataset_path = opts.path
cvrp_lkh_eval(dataset_path, opts)