-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTSP.py
More file actions
259 lines (227 loc) · 6.52 KB
/
TSP.py
File metadata and controls
259 lines (227 loc) · 6.52 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import sys
import os
import random
import matplotlib.pyplot as plt
# Points are represented as
# {
# "A":[0.1, 0.3],
# "B":[0.3, 0.5],
# etc
# }
def generateTSP(numdatapoints):
data = {}
dpname = 'A'
for i in range(numdatapoints):
x = random.random()
y = random.random()
data[dpname] = (x,y)
dpname = chr(ord(dpname) + 1)
return data
def plotTSP(data):
for key in data:
pos = data[key]
dot, = plt.plot(pos[0], pos[1], 'ro', label=key)
plt.annotate(key, xy=(pos[0], pos[1]+0.02))
plt.axis([-0.1, 1.1, -0.1, 1.1])
def plotmst(data, mst, col):
for item in mst:
p1 = data[item[0]]
p2 = data[item[1]]
xs = [p1[0], p2[0]]
ys = [p1[1], p2[1]]
line, = plt.plot(xs, ys)
plt.setp(line, color=col)
def plotsolution(data, soln, col):
xs = []
ys = []
for item in soln:
positem = data[item]
xs.append(positem[0])
ys.append(positem[1])
line, = plt.plot(xs, ys)
plt.setp(line, color=col)
def dsqr(data, k1, k2):
p1 = data[k1]
p2 = data[k2]
ds = pow(p2[0]-p1[0], 2) + pow(p2[1] - p1[1],2)
return ds
def dist(data, k1, k2):
return pow(dsqr(data, k1, k2), 0.5)
def treecost(data, subset, apex):
cost = 0
for item in subset:
if item is not apex:
cost = cost + dist(data, apex, item)
return cost
def edge(data, item1, item2):
if item1 < item2:
return (item1, item2, dist(data, item1, item2))
else:
return (item2, item1, dist(data, item1, item2))
def connected(graph, a, b):
visited = []
queue = [a]
while len(queue) > 0:
u = queue[0]
visited.append(u)
if u == b:
return True
queue = queue[1:]
for item in graph:
vert = None
if item[0] == u:
vert = item[1]
elif item[1] == u:
vert = item[0]
if (vert not in visited) and (vert not in queue):
queue.append(vert)
return False
# Kruskal's algorithm
def mst(data, subset):
graph_edges = []
for i in subset:
for j in subset:
if i is not j:
e = edge(data, i, j)
if e not in graph_edges:
graph_edges.append(e)
# Sort graph edges by cost
graph_edges = sorted(graph_edges, key = lambda edge : edge[2])
subset = []
for e in graph_edges:
if not connected(subset, e[0], e[1]):
subset.append(e)
return subset
def costmst(mst):
cost = 0
for item in mst:
cost = cost + item[2]
return cost
def nearestneighbor(data, startnode):
unvisited = set(data.keys())
visited = set()
tour = [startnode]
cpos = startnode
unvisited.remove(startnode)
while len(unvisited) > 0:
nn = None
mindist = 0
# Find nearest neighbor
for item in unvisited:
if nn is None:
nn = item
mindist = dsqr(data, cpos, item)
else:
thisdist = dsqr(data, cpos, item)
if thisdist < mindist:
mindist = thisdist
nn = item
# Set curpos to nn, remove nn from unvisited
tour.append(nn)
unvisited.remove(nn)
visited.add(nn)
cpos = nn
tour.append(startnode)
return tour
def tourcost(data, tour):
if len(tour) <= 1:
return 0
cost = 0
cpos = tour[0]
tour = tour[1:]
for item in tour:
cost = cost + dist(data, cpos, item)
cpos = item
return cost
def astarcost(data, tour):
g = tourcost(data, tour)
h = costmst(mst(data, get_remaining(data, tour) + tour[:1]))
return g + h
def get_remaining(data, tour):
subset = data.keys()
for item in tour:
subset.remove(item)
return subset
def cons_candidates(data, tour):
candidates = []
unvisited = get_remaining(data, tour)
for item in unvisited:
tc = tour + [item]
candidates.append(tc)
return candidates
def astar(data, startnode, stepthru=False):
tour = [startnode]
candidate_list = cons_candidates(data, tour)
while len(candidate_list) > 0:
# print candidate_list
# find minimum cost
mincandidate = None
mincost = -1
for item in candidate_list:
if stepthru:
print "Mincost: " + str(mincost)
print item
print tourcost(data, item)
print astarcost(data, item)
if mincandidate == None:
mincandidate = item
mincost = astarcost(data, item)
else:
thiscost = astarcost(data, item)
if thiscost < mincost:
mincandidate = item
mincost = thiscost
if len(mincandidate) == len(data.keys()):
# We are done
return mincandidate + tour
else:
if stepthru:
print "Picked candidate " + str(mincandidate)
raw_input("Press any key to continue")
candidate_list.remove(mincandidate)
candidate_list = candidate_list + cons_candidates(data, mincandidate)
return None
def domapping(m, i):
while m.has_key(i):
i = m[i]
return i
def partially_mapped_crossover(tour1, tour2):
newtour = []
cutpoint1 = random.randint(1, len(tour1)/2)
cutpoint2 = cutpoint1 + len(tour1)/2 - 1
#print cutpoint1
#print cutpoint2
m = {}
for j in range(cutpoint1, cutpoint2):
m[tour2[j]] = tour1[j]
#print m
newtour.append(domapping(m, tour1[0]))
for i in range(1, len(tour1)-1):
if (i >= cutpoint1) and (i < cutpoint2):
newtour.append(tour2[i])
else:
newtour.append(domapping(m,tour1[i]))
newtour.append(newtour[0])
return newtour
if __name__ == "__main__":
d = generateTSP(8)
#print d
plotTSP(d)
i = raw_input("Choose (1) for nearest neighbor, (2) for A-star, (3) to see the MST, and (4) for genetic algorithm")
nnsol = nearestneighbor(d, 'A')
astsol = astar(d, 'A', False)
f = mst(d, d.keys())
if i == "1":
plotsolution(d, nnsol, 'b')
elif i == "2":
plotsolution(d, astsol, 'r')
elif i == "3":
plotmst(d, f, 'g')
elif i == "4":
a1 = [1,2,3,4,5,6,7,8,1]
a2 = [3,7,5,1,6,8,2,4,3]
print a1
print a2
print partially_mapped_crossover(a1, a2)
plt.title("Traveling salesman")
plt.show()