print ans def mazetest1(): m = Maze(smallMazeText) ans = [((r, c), m.isPassable((r, c))) for r in xrange(1, 4) for c in xrange(2, 4)] print m print ans def mazetest2(): m = Maze(smallMazeText) ans = [((r, c), m.isPassable((r, c))) for r in xrange(-1, 1) for c in xrange(7, 10)] print m print ans def mstest(): m = Maze(mediumMazeText) c = maze_successors(m)(m.start) ans = list(sorted(c)) print ans small = Maze(hugeMazeText) goaltest = lambda (r, c): (r, c) == small.goal print len(search(maze_successors(small), small.start, goaltest, dfs=False))
# 13. Assume the start state is G and the goal state is C. Enter the path found # by depth-first search with dynamic programming: # GHEBSAC # 14. How many states were visited during this search? # 8 # # 15. How many nodes were expanded during the search? # 6 # 16. Enter the name of a state that was visited more than once by depth-first # without DP. # D # 17. Enter the maximum number of states that can be visited by ANY depth-first # search with DP in map1. # 12 # 18. The path found by depth-first search with and without DP should generally # be the same path. # False def map1successors(s,a): if len(map1[s]) > a: return map1[s][a] else: return s print search.search('G',lambda x: x == 'C',range(4),lambda s, a:map1successors(s,a),depthFirst=True,DP=True)
def getPath(worldname, world): if worldname == 'dl2World': return search.search(MazeSearchNode(world, world.start, None), lambda state: state==world.goal) else: return [(15,4), (17,8), (13,12), (11,8), (9,4), (5,8), (7,12)]
def breadth_first_search(successors, start_state, goal_test): return search(successors, start_state, goal_test)
def mazetest(): m = Maze(largeMazeText) ans = [m.width,m.height,m.start,m.goal] print m print ans def mazetest1(): m = Maze(smallMazeText) ans = [((r,c), m.isPassable((r,c))) for r in xrange(1,4) for c in xrange(2,4)] print m print ans def mazetest2(): m = Maze(smallMazeText) ans = [((r,c), m.isPassable((r,c))) for r in xrange(-1,1) for c in xrange(7,10)] print m print ans def mstest(): m = Maze(mediumMazeText) c = maze_successors(m)(m.start) ans = list(sorted(c)) print ans small = Maze(hugeMazeText) goaltest = lambda (r,c): (r,c) == small.goal print len(search(maze_successors(small), small.start, goaltest, dfs=False))
def getPath(maze): path = [] a = search(mazeSuccessors(maze), maze.start, goalFind(maze), dfs = False) for item in a: path.append(maze.indicesToPoint(item)) return path
:param state: parent state :param action: action move :return: ''' if action < len(map1[state]): return map1[state][action] else: return state actions = range(max([len(map1[s]) for s in map1])) path = search.search('A', lambda x: x == 'G', actions, map1successor, depthFirst=False, DP=False, maxNodes=10000) print(path) # with dynamic path = search.search('A', lambda x: x == 'G', actions, map1successor, depthFirst=False, DP=True, maxNodes=10000) print(path) path = search.search('G',
ans[i][j].append((i, j+1)) return lambda loc: ans[loc[0]][loc[1]] # TEST CASES # # set up lists of strings to represent the four test mazes small_maze_text = [line.strip() for line in open('small_maze.txt').readlines()] medium_maze_text = [line.strip() for line in open('medium_maze.txt').readlines()] large_maze_text = [line.strip() for line in open('large_maze.txt').readlines()] huge_maze_text = [line.strip() for line in open('huge_maze.txt').readlines()] # Your code here to run a search on a test maze small_maze = Maze(small_maze_text) medium_maze = Maze(medium_maze_text) large_maze = Maze(large_maze_text) huge_maze = Maze(huge_maze_text) small = search(make_maze_successors(small_maze), small_maze.start, lambda x: x == small_maze.goal, False) print(len(small)) medium = search(make_maze_successors(medium_maze), medium_maze.start, lambda x: x == medium_maze.goal, False) print(len(medium)) large = search(make_maze_successors(large_maze), large_maze.start, lambda x: x == large_maze.goal, False) print(len(large)) huge = search(make_maze_successors(huge_maze), huge_maze.start, lambda x: x == huge_maze.goal, False) print(len(huge))
if (i != 0 and j == 0) or (i == 0 and j != 0): tup = (row+i, col+j) if self._isNode(tup) and not self._isBacktracking(tup): children.append(MazeSearchNode(self.maze, tup, self)) return children def _isNode(self, tup): return self.maze.isPassable(tup) def _isBacktracking(self, tup): return self.parent != None and self.parent.state == tup def getPath(self): path = [] node = self while node.parent != None: path.append(node.state) node = node.parent return path def reachedGoal(state, goal): return state == goal if __name__ == '__main__': m = Maze(largeMazeText) f = lambda state: reachedGoal(state, m.goal) result = search(MazeSearchNode(m, m.start, None), f) print result print len(result) + 1
# set up lists of strings to represent the four test mazes small_maze_text = [line.strip() for line in open('small_maze.txt').readlines()] medium_maze_text = [ line.strip() for line in open('medium_maze.txt').readlines() ] large_maze_text = [line.strip() for line in open('large_maze.txt').readlines()] huge_maze_text = [line.strip() for line in open('huge_maze.txt').readlines()] # Your code here to run a search on a test maze small_maze = Maze(small_maze_text) medium_maze = Maze(medium_maze_text) large_maze = Maze(large_maze_text) huge_maze = Maze(huge_maze_text) small = search(make_maze_successors(small_maze), small_maze.start, lambda x: x == small_maze.goal, False) print(len(small)) medium = search(make_maze_successors(medium_maze), medium_maze.start, lambda x: x == medium_maze.goal, False) print(len(medium)) large = search(make_maze_successors(large_maze), large_maze.start, lambda x: x == large_maze.goal, False) print(len(large)) huge = search(make_maze_successors(huge_maze), huge_maze.start, lambda x: x == huge_maze.goal, False) print(len(huge))