 # 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)-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))] for col in range(len(grid))]
closed[init][init] = 1

x = init
y =init
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
y = next
g = next

if x == goal and y == goal:
found = next
print next
else:
for i in range(len(delta)):
x2 = x + delta[i]
y2 = y + delta[i]
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