-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathastar.py
302 lines (241 loc) · 6.54 KB
/
astar.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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import Queue
import math
def rowInRange(x):
if (x >= 0 and x < getTotalRow()):
return True
else:
return False
def colInRange(y):
if (y >= 0 and y < getTotalCol()):
return True
else:
return False
def canGo(posX, posY, visited):
if not rowInRange(posX):
return False
if not colInRange(posY):
return False
if map[posX][posY] == -1:
return False
if visited[posX][posY]:
return False
return True
def removeNoise(line):
line = line.replace("\n", "")
line = line.replace(" ", "")
return line
def getTotalRow():
row = 0
f = open('map', 'r')
line = f.readline()
while line != '':
row += 1
line = f.readline()
return row
def getTotalCol():
f = open('map', 'r')
line = f.readline()
line = removeNoise(line)
TOTALCOL = len(line)
f.close()
return TOTALCOL
def getMap():
TOTALROW = getTotalRow()
TOTALCOL = getTotalCol()
map = [[-1 for x in range(TOTALCOL)] for x in range(TOTALROW)]
currentRow = TOTALROW - 1
currentCol = 0
f = open('map', 'r')
line = f.readline()
while line != "":
line = removeNoise(line)
for x in range(len(line)):
ch = line[x]
if (ch == 'X'):
map[currentRow][x] = -1
elif (ch == '.'):
map[currentRow][x] = 0
else:
map[currentRow][x] = ch
currentRow -= 1
line = f.readline()
f.close()
return map
# return the location of the character : A, B, or C
def getLocation(map,ch):
for x in range(getTotalRow()):
for y in range(getTotalCol()):
if (map[x][y] == ch):
xLoc = x
yLoc = y
return (xLoc,yLoc)
# return the distance of two points using admissible heuristic function
# pos,des - a tuple in (x,y) format
def getAdmissibleDistance(pos, des, cost=0):
posX = pos[0]
posY = pos[1]
desX = des[0]
desY = des[1]
xDiff = (desX - posX) ** 2
yDiff = (desY - posY) ** 2
diff = xDiff + yDiff
diff = math.pow(diff, 0.5)
cost += diff
return cost
def getNonAdmissibleDistance(pos, des, cost=0):
posX = pos[0]
posY = pos[1]
desX = des[0]
desY = des[1]
xDiff = math.fabs(desX - posX)
yDiff = math.fabs(desY - posY)
diff = xDiff + yDiff
cost += diff
cost += 100
return cost
def computeDistance(func,pos,des):
return func(pos,des)
# test the condition and put the node
# put the node into the queue if possible
# i.e. there is such a node, the node free (not -1)
# and the node is not visited before
def testAndPutQueue(curPos, neighbourPos, des, cost, func, que, visited):
posX = neighbourPos[0]
posY = neighbourPos[1]
if (canGo(posX, posY, visited)):
prio = func(neighbourPos,des, cost)
member = (prio, neighbourPos, curPos[0], curPos[1])
que.put(member)
def countDistance (parent, des):
distance = 0
path = str(des)
nextPtX = des[0]
nextPtY = des[1]
while (True):
nextPt = parent[nextPtX][nextPtY]
if (nextPt == (-1,-1)):
break
path = str(nextPt) + path
distance += 1
nextPtX = nextPt[0]
nextPtY = nextPt[1]
#print "working : @ " + str(nextPt)
return distance
def constructPath (parent, des):
path = str(des)
nextPtX = des[0]
nextPtY = des[1]
while (True):
nextPt = parent[nextPtX][nextPtY]
if (nextPt == (-1,-1)):
break
path = str(nextPt) + "->" + path
nextPtX = nextPt[0]
nextPtY = nextPt[1]
#print "working : @ " + str(nextPt)
return path
def aStar(pos, des, path, func):
map = getMap()
currentX = pos[0]
currentY = pos[1]
que = Queue.PriorityQueue()
member = (0, pos, -1, -1)
que.put(member)
visited = [[False for x in range(getTotalCol())] for x in range(getTotalRow())]
# 2D array to hold where the path comes from
# ex. 2-5-8, 2 is 5 parent and 5 is 8 parent
parent = [[(-2,-2) for x in range(getTotalCol())] for x in range(getTotalRow())]
cost = 0
while (1):
element = que.get()
pos = element[1] # second element is the current position (prio,pos)
posX = int(pos[0])
posY = int(pos[1])
# do no visit again visted node
if (visited[posX][posY]):
continue
# increment cost
cost += 1
path = path + str(pos)
visited[posX][posY] = True
parentX = element[2]
parentY = element[3]
parent[posX][posY] = (parentX, parentY)
if ( (posX,posY) == des ):
#print parent
#return path
return constructPath(parent, des)
# make a copy of own location
posX = int(pos[0])
posY = int(pos[1])
curPos = (posX, posY)
# compute bottom left
posX = int(pos[0]) - 1
posY = int(pos[1]) - 1
neighbourPos = (posX, posY)
testAndPutQueue(curPos, neighbourPos, des, cost, func, que, visited)
# compute central left
parentPosX = int(pos[0])
parentPosY = int(pos[1])
posX = int(pos[0]) - 1
posY = int(pos[1])
neighbourPos = (posX, posY)
testAndPutQueue(curPos, neighbourPos, des, cost, func, que, visited)
# compute top left
posX = int(pos[0]) - 1
posY = int(pos[1]) + 1
neighbourPos = (posX, posY)
testAndPutQueue(curPos, neighbourPos, des, cost, func, que, visited)
# compute bottom
posX = int(pos[0])
posY = int(pos[1]) - 1
neighbourPos = (posX, posY)
testAndPutQueue(curPos, neighbourPos, des, cost, func, que, visited)
# compute top
posX = int(pos[0])
posY = int(pos[1]) + 1
neighbourPos = (posX, posY)
testAndPutQueue(curPos, neighbourPos, des, cost, func, que, visited)
# compute bottom right
posX = int(pos[0]) + 1
posY = int(pos[1]) - 1
neighbourPos = (posX, posY)
testAndPutQueue(curPos, neighbourPos, des, cost, func, que, visited)
# compute central right
posX = int(pos[0]) + 1
posY = int(pos[1])
neighbourPos = (posX, posY)
testAndPutQueue(curPos, neighbourPos, des, cost, func, que, visited)
# compute top right
posX = int(pos[0]) + 1
posY = int(pos[1]) + 1
neighbourPos = (posX, posY)
testAndPutQueue(curPos, neighbourPos, des, cost, func, que, visited)
if (que.empty()):
return "No Path\n" + "Path so far: " + path
return "Error"
map = getMap()
bLoc = getLocation(map,'B')
aLoc = getLocation(map, 'A')
cLoc = getLocation(map, 'C')
dLoc = getLocation(map, 'D')
# A-C Admissible 28.0
# A-C Non-Admissible 39.0
result = aStar(aLoc, bLoc,"",getAdmissibleDistance)
print "A->B Admissible Result : "
print result
result = aStar(aLoc, bLoc,"",getNonAdmissibleDistance)
print "A->B Non Admissible Result : "
print result
result = aStar(aLoc, cLoc,"",getAdmissibleDistance)
print "A->C Admissible Result : "
print result
result = aStar(aLoc, cLoc,"",getNonAdmissibleDistance)
print "A->C Non Admissible Result : "
print result
result = aStar(aLoc, dLoc,"",getAdmissibleDistance)
print "A->D Admissible Result : "
print result
result = aStar(aLoc, dLoc,"",getNonAdmissibleDistance)
print "A->D Non Admissible Result : "
print result