-
Notifications
You must be signed in to change notification settings - Fork 21
/
test.py
72 lines (57 loc) · 1.91 KB
/
test.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
import time
import torch
from torch.distributions import Categorical, kl
from net import Net
from aco import ACO
from utils import *
torch.manual_seed(1234)
EPS = 1e-10
device = 'cpu'
def infer_instance(model, demands, n_ants, t_aco_diff):
if model:
model.eval()
pyg_data = gen_pyg_data(demands, device)
heu_vec = model(pyg_data)
heu_mat = heu_vec.reshape((n_node+1, n_node+1)) + EPS
aco = ACO(
demand=demands,
n_ants=n_ants,
heuristic=heu_mat,
device=device
)
else:
aco = ACO(
demand=demands,
n_ants=n_ants,
device=device
)
results = torch.zeros(size=(len(t_aco_diff),), device=device)
for i, t in enumerate(t_aco_diff):
best_cost = aco.run(t)
results[i] = best_cost
return results
@torch.no_grad()
def test(dataset, model, n_ants, t_aco):
_t_aco = [0] + t_aco
t_aco_diff = [_t_aco[i+1]-_t_aco[i] for i in range(len(_t_aco)-1)]
sum_results = torch.zeros(size=(len(t_aco_diff),), device=device)
start = time.time()
for demands in dataset:
results = infer_instance(model, demands, n_ants, t_aco_diff)
sum_results += results
end = time.time()
return sum_results / len(dataset), end-start
n_ants = 20
t_aco = [1, 5, 10, 20]
n_node = 120
test_list = load_test_dataset(n_node, device)
net = Net().to(device)
net.load_state_dict(torch.load(f'../pretrained/bpp/bpp{n_node}.pt', map_location=device))
avg_aco_best, duration = test(test_list, net, n_ants, t_aco)
print('total duration: ', duration)
for i, t in enumerate(t_aco):
print("T={}, average obj. is {}.".format(t, avg_aco_best[i]))
avg_aco_best, duration = test(test_list, None, n_ants, t_aco)
print('total duration: ', duration)
for i, t in enumerate(t_aco):
print("T={}, average obj. is {}.".format(t, avg_aco_best[i]))