-
Notifications
You must be signed in to change notification settings - Fork 53
/
visualize_tsp.py
55 lines (42 loc) · 1.93 KB
/
visualize_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
import matplotlib.pyplot as plt
def plotTSP(paths, points, num_iters=1):
"""
path: List of lists with the different orders in which the nodes are visited
points: coordinates for the different nodes
num_iters: number of paths that are in the path list
"""
# Unpack the primary TSP path and transform it into a list of ordered
# coordinates
x = []; y = []
for i in paths[0]:
x.append(points[i][0])
y.append(points[i][1])
plt.plot(x, y, 'co')
# Set a scale for the arrow heads (there should be a reasonable default for this, WTF?)
a_scale = float(max(x))/float(100)
# Draw the older paths, if provided
if num_iters > 1:
for i in range(1, num_iters):
# Transform the old paths into a list of coordinates
xi = []; yi = [];
for j in paths[i]:
xi.append(points[j][0])
yi.append(points[j][1])
plt.arrow(xi[-1], yi[-1], (xi[0] - xi[-1]), (yi[0] - yi[-1]),
head_width = a_scale, color = 'r',
length_includes_head = True, ls = 'dashed',
width = 0.001/float(num_iters))
for i in range(0, len(x) - 1):
plt.arrow(xi[i], yi[i], (xi[i+1] - xi[i]), (yi[i+1] - yi[i]),
head_width = a_scale, color = 'r', length_includes_head = True,
ls = 'dashed', width = 0.001/float(num_iters))
# Draw the primary path for the TSP problem
plt.arrow(x[-1], y[-1], (x[0] - x[-1]), (y[0] - y[-1]), head_width = a_scale,
color ='g', length_includes_head=True)
for i in range(0,len(x)-1):
plt.arrow(x[i], y[i], (x[i+1] - x[i]), (y[i+1] - y[i]), head_width = a_scale,
color = 'g', length_includes_head = True)
#Set axis too slitghtly larger than the set of x and y
plt.xlim(min(x)*1.1, max(x)*1.1)
plt.ylim(min(y)*1.1, max(y)*1.1)
plt.show()