-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTSP.py
More file actions
188 lines (153 loc) · 6.31 KB
/
Copy pathTSP.py
File metadata and controls
188 lines (153 loc) · 6.31 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
from A_star_TSP import search
from pylab import *
def Distance(R1, R2, Rmap_grid):
distance_between_two = search(Rmap_grid, R1, R2)
# print "distance_between_two, init, goal"
# print "R1, R2 , distance_between_two----------", R1, R2, distance_between_two
return distance_between_two
def TotalDistance(city, R, Rmap_grid):
# print "R, city"
# print R--- will print set of key points
# print city [0, 1, 2, 3, 4, 5, 6]
dist = 0
for i in range(len(city) - 1):
dist += Distance(R[city[i]], R[city[i + 1]], Rmap_grid)
print "Dist:", R[city[i]], R[city[i + 1]], Distance(R[city[i]], R[city[i + 1]], Rmap_grid)
dist += Distance(R[city[-1]], R[city[0]], Rmap_grid)
return dist
def reverse(city, n):
nct = len(city)
nn = (1 + ((n[1] - n[0]) % nct)) / 2
for j in range(nn):
k = (n[0] + j) % nct
l = (n[1] - j) % nct
(city[k], city[l]) = (city[l], city[k]) # swapping
PtList = []
def Plot(city, R, dist, grid):
Pt = [R[city[i]] for i in range(len(city))]
Pt += [R[city[0]]]
Pt = array(Pt)
title('Total distance=' + str(dist))
print "-------Pt-----"
print Pt
PtList = np.array(Pt).tolist()
print "-------Pt LIST-----"
print PtList
plot(Pt[:, 0], Pt[:, 1], '-o')
# show()
def Plot2(city, R, dist, grid, f_degree, b_degree, r_degree, l_degree):
Pt = [R[city[i]] for i in range(len(city))]
Pt += [R[city[0]]]
Pt = array(Pt)
title('Total distance=' + str(dist))
print "-------Pt-----"
print Pt
PtList = np.array(Pt).tolist()
plot(Pt[:, 0], Pt[:, 1], '-o')
# show()
print "PATH MAP__________________", PtList
# ------------------ setting the closest entrance as start---------------------------
cleanList = []
[cleanList.append(x) for x in PtList if x not in cleanList]
PtList = cleanList
closestPoint = min(PtList)
closestPointIndex = PtList.index(closestPoint)
print "closestPointIndex: ", closestPointIndex
newList = []
for i in range(closestPointIndex):
newList.append(PtList[i])
for i in range(closestPointIndex):
PtList.remove(PtList[0])
PtList.extend(newList)
PtList.append(PtList[0])
print "--------------------------------new PATH MAP -------------"
print PtList
goal_init = PtList
print "Initial Goal---------", goal_init[0]
from TurtleGUI_TSP import draw as drawGui
from TurtleGUI_TSP import search as searchGui
drawGui(grid, goal_init, len(grid), len(grid[0]), 10)
for i in range(len(PtList)):
initialPoint= PtList[0]
init = goal_init[i]
goal = goal_init[i + 1]
searchGui(init, goal, 10, grid,f_degree, b_degree, r_degree, l_degree, initialPoint)
goal_init.append(init)
goal_init.append(goal)
Rmap = []
# edit 2
# def printRamp(Rmap_grid):
# for kr in range(len(Rmap_grid)):
# for kc in range(len(Rmap_grid[0])):
# if Rmap_grid[kr][kc] == 2:
# # print "Two in ",kr, kc
# Rmap.append([kr, kc])
# Rmap_grid[kr][kc] = 0
#
# print "Rmap", Rmap
# def main(Rmap_grid,f_degree, b_degree, r_degree, l_degree):
def main(Rmap_grid,f_degree, b_degree, r_degree, l_degree, assignedArea):
# printRamp(Rmap_grid)
Rmap=assignedArea
print "Rmap Grid"
for kr in range(len(Rmap_grid)):
print Rmap_grid[kr]
init = Rmap[0]
goal = Rmap[1]
print "init, goal: ", init, goal
# Rmap =[[1,1],[5,2],[8,9],[2,4],[3,3]]
# Rmap = [[0, 0], [0, 4], [2, 1], [3, 5], [4, 2]]
ncity = len(Rmap) # Number of cities to visit
maxTsteps = 1 # Temperature is lowered not more than maxTsteps
Tstart = 0.2 # Starting temperature - has to be high enough
fCool = 0.9 # Factor to multiply temperature at each cooling step
maxSteps = 10 * ncity # Number of steps at constant temperature
maxAccepted = 10 * ncity # Number of accepted steps at constant temperature
Preverse = 0.5 # How often to choose reverse/transpose trial move
# Choosing city coordinates
R = [] # coordinates of cities are choosen randomly
for i in range(ncity):
# R.append([rand(), rand()])
R.append(Rmap[i])
# R.append([Rmap[i]])
# print Rmap[i]
R = array(R)
city = range(ncity)
# Distance of the travel at the beginning
dist = TotalDistance(city, R, Rmap_grid)
# Stores points of a move
n = zeros(6, dtype=int)
nct = len(R) # number of cities
T = Tstart # temperature
Plot(city, R, dist, Rmap_grid)
for t in range(maxTsteps): # Over temperature
accepted = 0
for i in range(maxSteps): # At each temperature, many Monte Carlo steps
while True: # Will find two random cities sufficiently close by
# Two cities n[0] and n[1] are choosen at random
n[0] = int((nct) * rand()) # select one city
n[1] = int((nct - 1) * rand()) # select another city, but not the same
if (n[1] >= n[0]):
n[1] += 1
if (n[1] < n[0]):
(n[0], n[1]) = (n[1], n[0]) # swap, because it must be: n[0]<n[1]
nn = (n[0] + nct - n[1] - 1) % nct # number of cities not on the segment n[0]..n[1]
if nn >= 3: break
# We want to have one index before and one after the two cities
# The order hence is [n2,n0,n1,n3]
n[2] = (n[0] - 1) % nct # index before n0
n[3] = (n[1] + 1) % nct # index after n2
# What would be the cost to reverse the path between city[n[0]]-city[n[1]]?
de = Distance(R[city[n[2]]], R[city[n[1]]], Rmap_grid) + Distance(R[city[n[3]]], R[city[n[0]]],Rmap_grid) - Distance(
R[city[n[2]]], R[city[n[0]]],Rmap_grid) - Distance(R[city[n[3]]], R[city[n[1]]],Rmap_grid)
if de < 0 or exp(-de / T) > rand(): # Metropolis
accepted += 1
dist += de
reverse(city, n)
if accepted > maxAccepted: break
print "T=%10.5f , distance= %10.5f " % (T, dist)
T *= fCool # The system is cooled down
if accepted == 0: break # If the path does not want to change any more, we can stop
Plot2(city, R, dist, Rmap_grid, f_degree, b_degree, r_degree, l_degree)
if __name__ == '__main__':
main()