-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtsp_starter.py
132 lines (101 loc) · 3.7 KB
/
tsp_starter.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
'''
TSP starter code. Make this your own!
Run with: ipython tsp_starter.py
'''
import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial.distance import euclidean
plt.ion() # turn interactive mode on
def read_cities(filepath):
'''
Load a TSP dataset.
This function works for loading CSV files generated
by the data/make_data.py script.
'''
cities = np.loadtxt(filepath, delimiter=',')
return cities
def score_solution(cities, solution):
'''
Calculate the total distance traveled by the given solution.
This function scores a TSP solution by computing the total
distance the salesperson would travel. Lower is better!
The 'solution' array must contain indices into the 'cities'
array. Also, the 'solution' array must visit each city exactly
once!
'''
if len(solution) != len(cities):
raise Exception(('Invalid solution: len(solution) is {}, ' + \
'but it should be {}.').format(len(solution), len(cities)))
if set(solution) != set(range(len(cities))):
raise Exception('Invalid solution: The solution does not ' + \
'visit each city exactly once!')
dist = 0.0
for i in range(len(solution)):
p_prev = cities[solution[i-1]]
p_here = cities[solution[i]]
dist += euclidean(p_prev, p_here)
return dist
def create_figure():
'''
Creates a figure which `visualize_solution()` will draw onto.
'''
fig, axes = plt.subplots(1, 2, figsize=(15, 7))
return fig, axes
def visualize_solution(cities, solution, fig=None, axes=None, block=True):
'''
Visualize the solution in a 2D plot.
The 'cities' and 'solution' arguments are the same
as to the `score_solution()` function.
'''
dist = score_solution(cities, solution) if len(solution) == len(cities) else float('NaN')
if fig is None or axes is None:
fig, axes = create_figure()
ax1, ax2 = axes
fig.suptitle('Total Distance: {}'.format(dist), fontsize=20)
ax1.clear()
ax1.scatter(cities[:,0], cities[:,1])
if len(solution) == len(cities):
path = np.hstack((solution, solution[0])) # <-- the salesperson has to return home!
else:
path = solution
ax2.clear()
ax2.plot(cities[path,0], cities[path,1])
ax2.scatter(cities[:,0], cities[:,1])
if block:
while plt.fignum_exists(fig.number):
plt.pause(0.001)
else:
plt.pause(0.001)
def tsp_solver_silly(cities, new_best_solution_func = None):
'''
This TSP solver is super silly.
This solver simply randomizes several solutions then
keeps the one which is best.
'''
best_dist = float("inf")
best_solution = None
for i in range(1000):
solution = np.arange(len(cities))
np.random.shuffle(solution)
dist = score_solution(cities, solution)
if dist < best_dist:
best_dist = dist
best_solution = solution
if new_best_solution_func:
new_best_solution_func(solution)
return best_solution
if __name__ == '__main__':
cities = read_cities('data/tiny.csv')
show_progress = False
if not show_progress:
solution = tsp_solver_silly(cities)
visualize_solution(cities, solution)
else:
fig, axes = create_figure()
# Closure over cities, fig, and axes:
def visualize_wrapper(solution, is_final=False):
print ('FINAL SOLUTION:' if is_final else 'Best so far:'), \
score_solution(cities, solution), solution
visualize_solution(cities, solution, fig, axes, block=is_final)
solution = tsp_solver_silly(cities, visualize_wrapper)
visualize_wrapper(solution, True)