Skip to content

Commit 88c881d

Browse files
committed
Changed Filename from A*.py to a_star.py
1 parent b2d165d commit 88c881d

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

Graphs/a_star.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
2+
grid = [[0, 1, 0, 0, 0, 0],
3+
[0, 1, 0, 0, 0, 0],#0 are free path whereas 1's are obstacles
4+
[0, 1, 0, 0, 0, 0],
5+
[0, 1, 0, 0, 1, 0],
6+
[0, 0, 0, 0, 1, 0]]
7+
8+
'''
9+
heuristic = [[9, 8, 7, 6, 5, 4],
10+
[8, 7, 6, 5, 4, 3],
11+
[7, 6, 5, 4, 3, 2],
12+
[6, 5, 4, 3, 2, 1],
13+
[5, 4, 3, 2, 1, 0]]'''
14+
15+
init = [0, 0]
16+
goal = [len(grid)-1, len(grid[0])-1] #all coordinates are given in format [y,x]
17+
cost = 1
18+
19+
#the cost map which pushes the path closer to the goal
20+
heuristic = [[0 for row in range(len(grid[0]))] for col in range(len(grid))]
21+
for i in range(len(grid)):
22+
for j in range(len(grid[0])):
23+
heuristic[i][j] = abs(i - goal[0]) + abs(j - goal[1])
24+
if grid[i][j] == 1:
25+
heuristic[i][j] = 99 #added extra penalty in the heuristic map
26+
27+
28+
#the actions we can take
29+
delta = [[-1, 0 ], # go up
30+
[ 0, -1], # go left
31+
[ 1, 0 ], # go down
32+
[ 0, 1 ]] # go right
33+
34+
35+
#function to search the path
36+
def search(grid,init,goal,cost,heuristic):
37+
38+
closed = [[0 for col in range(len(grid[0]))] for row in range(len(grid))]# the referrence grid
39+
closed[init[0]][init[1]] = 1
40+
action = [[0 for col in range(len(grid[0]))] for row in range(len(grid))]#the action grid
41+
42+
x = init[0]
43+
y = init[1]
44+
g = 0
45+
f = g + heuristic[init[0]][init[0]]
46+
cell = [[f, g, x, y]]
47+
48+
found = False # flag that is set when search is complete
49+
resign = False # flag set if we can't find expand
50+
51+
while not found and not resign:
52+
if len(cell) == 0:
53+
resign = True
54+
return "FAIL"
55+
else:
56+
cell.sort()#to choose the least costliest action so as to move closer to the goal
57+
cell.reverse()
58+
next = cell.pop()
59+
x = next[2]
60+
y = next[3]
61+
g = next[1]
62+
f = next[0]
63+
64+
65+
if x == goal[0] and y == goal[1]:
66+
found = True
67+
else:
68+
for i in range(len(delta)):#to try out different valid actions
69+
x2 = x + delta[i][0]
70+
y2 = y + delta[i][1]
71+
if x2 >= 0 and x2 < len(grid) and y2 >=0 and y2 < len(grid[0]):
72+
if closed[x2][y2] == 0 and grid[x2][y2] == 0:
73+
g2 = g + cost
74+
f2 = g2 + heuristic[x2][y2]
75+
cell.append([f2, g2, x2, y2])
76+
closed[x2][y2] = 1
77+
action[x2][y2] = i
78+
invpath = []
79+
x = goal[0]
80+
y = goal[1]
81+
invpath.append([x, y])#we get the reverse path from here
82+
while x != init[0] or y != init[1]:
83+
x2 = x - delta[action[x][y]][0]
84+
y2 = y - delta[action[x][y]][1]
85+
x = x2
86+
y = y2
87+
invpath.append([x, y])
88+
89+
path = []
90+
for i in range(len(invpath)):
91+
path.append(invpath[len(invpath) - 1 - i])
92+
print "ACTION MAP"
93+
for i in range(len(action)):
94+
print action[i]
95+
96+
return path
97+
98+
a = search(grid,init,goal,cost,heuristic)
99+
for i in range(len(a)):
100+
print a[i]
101+

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy