randall davis, csail 1 Today What’s better than S 3 ? F 3 : your Free Food Finder R 3 : Representations, representations, reps… Efficiency consequences
Today’s task Think of F 3 as a game Moving around on a grid But the grid contents can change randall davis, csail 2
The game as graph search Nodes: current state of the entire game List of lists of strings Overall board, each row as a sublist Each string describes the contents of a square board1 = [["S", " ", " ", " ", "F "]] board2 = [ ["F", “ ", " ", " ", " "], ["W", "W", "S", "W", "F"], [" ", " ", " ", "W", " "], ] Goal state? randall davis, csail 3
Maze Description Language randall davis, csail 4 _______ _______ |L |_ __|___ | | L L _ L_|_|___L|| |||__| _|_ | |_| |L ___L | L_L_|| L__L__ ||L|_ |_| L | |__||| | _|| |L_L _ LL_||L || L__ L L _ |_L_||| |_____| |L__| _| L L _ |_||_ | L | | ___L LL |LL || |||___ |_L_ |L| ||L |__|L_ ||| | |L |L |_| |||||| L_L_ |L _||||||| L____L_|L_LL__L| | L |
find_path randall davis, csail 5 def find_path ( neighbors_function , start, goal_test ): """ Return the shortest path through a graph from a given starting state to any state that satisfies a given goal condition. Parameters: * neighbors_function (state) is a function which returns a list of legal neighbor states * start is the starting state for the search * goal_test (state) is a function which returns True if the given state is a goal state for the search, and False otherwise . Returns: a tuple of states showing the shortest path from start to a state satisfying goal_test (state), or None there none NOTE state representation is up to the caller, but it must be hashable . """
find_path randall davis, csail 6 if goal_test (start): return (start,) agenda = [(start,)] visited = {start} while agenda: this_path = agenda.pop (0) terminal_state = this_path [-1] for neighbor in neighbors_function ( terminal_state ): if neighbor not in visited: new_path = this_path + (neighbor,) if goal_test (neighbor): return new_path agenda.append ( new_path ) visited.add (neighbor)