-
Notifications
You must be signed in to change notification settings - Fork 18
/
Run2optHeuristicsFix.py
98 lines (81 loc) · 3.34 KB
/
Run2optHeuristicsFix.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
import argparse
import os
import istarmap
from DataGenerator import TSPDataset
from tqdm import tqdm
import pandas as pd
import new_mp_utils as mp_utils
import multiprocessing as mp
import time
parser = argparse.ArgumentParser()
parser.add_argument('--heuristic',
default=None, type=str, help='Heuristic to run')
parser.add_argument('--test_size',
default=512, type=int, help='Test data size')
parser.add_argument('--batch_size',
default=512, type=int, help='batch size')
parser.add_argument('--n_points',
type=int, default=100, help='Number of points in TSP')
parser.add_argument('--n_steps',
default=1000,
type=int, help='Number of steps to run')
parser.add_argument('--test_from_data',
default=True,
action='store_true', help='read TSP data')
parser.add_argument('--data_dir', type=str, default='data')
parser.add_argument('--gat_data_path', type=str, default='')
args = parser.parse_args()
# Heuristics handling
heuristics_list = ['best_improvement',
'first_improvement',
'best_improvement_restart',
'first_improvement_restart']
if args.heuristic is None:
raise Exception('Heuristics cannot be empty.')
elif args.heuristic not in heuristics_list:
raise Exception('Heuristic not implemented.')
if args.heuristic == 'best_improvement':
args.n_steps = 0
f = mp_utils.heuristic_2opt_bi
elif args.heuristic == 'first_improvement':
args.n_steps = 0
f = mp_utils.heuristic_2opt_fi
elif args.heuristic == 'best_improvement_restart':
f = mp_utils.heuristic_2opt_bi_restart
elif args.heuristic == 'first_improvement_restart':
f = mp_utils.heuristic_2opt_fi_restart
# Data handling
if args.test_from_data:
if args.gat_data_path != '':
# test_data
pass
else:
test_data = TSPDataset(dataset_fname=os.path.join(args.data_dir,
'TSP{}-data-test.json'
.format(args.n_points)),
num_samples=args.test_size, seed=1234)
else:
raise Exception('Heuristic will only run for saved data.')
num_cpus = os.cpu_count()
result = []
start = time.perf_counter()
for i in range(0, len(test_data), args.batch_size):
batch = test_data[i:i+args.batch_size]
steps = [args.n_steps for _ in range(len(batch))]
with mp.Pool(num_cpus) as p:
if args.heuristic in ['best_improvement', 'first_improvement']:
r = list(tqdm(p.imap(f, batch), total=len(batch)))
else:
r = list(tqdm(p.istarmap(f, zip(batch, steps)), total=len(batch)))
result.extend(r)
finish = time.perf_counter()
print("Finished in {:.2f} secs".format(finish-start))
df = pd.DataFrame(result, columns=["Cost"])
print(df)
df.to_csv(args.data_dir+'/heuristics/'
+ args.heuristic
+ 'aaa-TSP{}-test-steps{}-testsize{}-batchsize{}.csv'.format(args.n_points,
args.n_steps,
args.test_size,
args.batch_size),
index=False)