AI Search Program Doesn’t Output Search Matrix Results

Wrote and ran an AI search program to run a search from start until the end or result is found. However, when I run it, I do not get the search result instead I get fail and none. Any idea what could be the cause of the issue would be much appreciated

#-- begin snippet: js hide: false console: true babel: false -->

#<!-- language: py -->

grid = [[0, 0, 1, 0, 0, 0],
[0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 1, 0],
[0, 0, 1, 1, 1, 0],
[0, 0, 0, 0, 1, 0]]
init = [0, 0]
goal = [len(grid)-1, len(grid[0])-1]
cost = 1

delta = [[-1, 0], # go up
[0,-1], # go left
[1, 0], # go down
[0, 1]] # go right

delta_name = [’^’, ‘<’, ‘v’, ‘>’]

def search():
closed = [[0 for row in range(len(grid[0]))] for col in range(len(grid))]
closed[init[0]][init[1]] = 1

x = init[0]
y =init[1]
g = 0

open = [[g, x, y]]

found = False
resign = False

while found is False and resign is False:
if len(open) == 0:
resign = True
print ‘fail’

else:
open.sort()
open.reverse()
next = open.pop()

x = next[3]
y = next[4]
g = next[1]

if x == goal[0] and y == goal[1]:
found = next
print next
else:
for i in range(len(delta)):
x2 = x + delta[i][0]
y2 = y + delta[i][1]
if x2 >= 0 and x2 < len(grid) and y2 >= 0 and y2 < len(grid):
if closed[x2][y2] == 0 and grid[x2][y2] == 0:
g2 = g + cost

open.append([g2, x2, y2])
closed[x2][y2] = 1
print search()

<!-- end snippet -->

1 Like