-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsp.py
153 lines (117 loc) · 3.67 KB
/
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
"""
tsp.py
10-29-2018
jack skrable
"""
import annealing as an
import output as out
import utils
import random
import cProfile
from datetime import datetime, timezone
from timeit import default_timer as timer
# init global
CITIES = []
OUTPUT = {}
DT = datetime.now()
# Get path for output results
ts = DT.strftime('%Y%m%d%H%M%S')
PATH = 'results/' + ts + '/'
def run(tour, algorithm, report, iterations):
# Function to run a given algorithm
global OUTPUT
alg = an.anneal if algorithm == 'sa' else mcmc
# Init results
results = {}
start = timer()
# Check reporting switch
if report:
run = cProfile.run("'"+alg+"(tour,iterations)'")
else:
run = alg(tour, iterations)
# Close timer and get duration
end = timer()
dur = end - start
# Split return tuple
tour = run[0]
OUTPUT.update({'alpha': run[1]})
efforts = run[2]
# Plot significant partial efforts
for i in range(len(efforts)):
plotname = PATH + 'partial_' + str("%03d" % i)
partial = utils.State(efforts[i]['tour'],
efforts[i]['temp'],
an.cost(efforts[i]['tour']),
0.5,
True,
plotname)
out.plot_tsp(partial)
# Update results
results.update({algorithm: {
'tour': tour,
'duration': dur,
'cost': an.cost(tour)
}
})
OUTPUT.update(results[algorithm])
return results
def solve(tour, sa, mcmc, report, iterations):
# Function to solve problem
results = {}
# Use both and compare
if sa and mcmc:
results.update(run(tour, 'sa', report, iterations))
results.update(run(tour, 'mcmc', report, iterations))
# Use simulated annealing
elif sa:
results.update(run(tour, 'sa', report, iterations))
# Use markov chain monte carlo
elif mcmc:
results.update(run(tour, 'mcmc', report, iterations))
return results
def main():
# Main function. Parses arguments, initializes random problem, solves problem.
args = utils.arg_parser()
OUTPUT.update({'size': args.size,
'iterations': args.iterations
})
# Create initial tour visiting each city in order of creation
tour = []
n = args.size
for i in range(n):
i = utils.City(i, random.randint(0, n**2), random.randint(0, n**2))
tour.append(i)
# Display or save initial problem
problem = utils.State(tour,
1.0,
0,
0,
True,
PATH)
out.plot_tsp(problem)
# Randomize tour
# TODO Add a heuristic here???
# Start with a better tour???
random.shuffle(tour)
print('initial cost of random tour is ' + str(an.cost(tour)))
print('optimizing tour...')
results = solve(tour, args.sa, args.mcmc, args.report, args.iterations)
print('cost of solved tour is ' + str(results['sa']['cost']))
print('time to solve was ' + str(results['sa']['duration']) + ' seconds')
# Display or save solved tour
solution = utils.State(results['sa']['tour'],
0,
results['sa']['cost'],
1,
True,
PATH)
out.plot_tsp(solution)
# Get timestamp for output file
ts = DT.strftime('%Y-%m-%d %H:%M:%S')
OUTPUT.update({'timestamp': ts})
print('writing results...')
out.write_results('run_stats', OUTPUT)
out.animate(PATH)
print('complete')
if __name__ == '__main__':
main()