Python Forum

Full Version: Heuristic for GBFS Maze
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Not really sure this could be called homework as there aren't any assignments, but it feels like a homework-ish question so figure I'd post it here. This is part of a self paced Edx class with youtube videos.

I'm trying to learn how to set up heuristics for the Greedy Best First Search.

Basically the idea is to call the python file and the maze file from the command line.

Ie.. typing on the command line something like this:
python "maze - DFS - CLI.py" "maze.txt"
the text file is literally just a few number signs denoting walls in the maze.

maze.txt would look like this. Sorry about picture, but I couldn't get the blank spaces to show up properly.

This maze example is 7 rows by 12 Columns.

[Image: maze-example.png]

Here is the code for a working example of the depth first search (provided by the class).


My question is about my attempt to modify the above code to add a heuristic to follow the Greedy Best First Search algorithm.

I tried to modify the solve function as follows

    def solve(self):
        """Finds a solution to maze, if one exists."""

        # Keep track of number of states explored
        self.num_explored = 0

        # Initialize frontier to just the starting position
        start = Node(state=self.start, parent=None, action=None)
        frontier = StackFrontier() #Initialize object as Class Stack Frontier
        frontier.add(start)

        # Initialize an empty explored set
        self.explored = set()

        # Keep looping until solution found
        while True:

            # If nothing left in frontier, then no path
            if frontier.empty():
                raise Exception("no solution")

            # Choose a node from the frontier - Removes the Current node and sets it to explored.
            node = frontier.remove()
            self.num_explored += 1 #Update the Number of states explored. Ie.. first loop removes initial state and increments to 1 from zero.

            # If node is the goal, then we have a solution
            if node.state == self.goal:
                actions = [] #Initialize Action List so we can backtrack the steps taken to reach the goal
                cells = [] #Initialize Cells List so we can Back Track steps taken to reach the goal. 
                while node.parent is not None:
                    actions.append(node.action) #Fills Actions List with the Actions taken. LIFO
                    cells.append(node.state) #Fills Cells List with Cells taken. LIFO
                    node = node.parent #Continues working backwards until there are no parents. Ie.. start state is reached.
                actions.reverse() #Reverses List so we can see the solution from first step
                cells.reverse() #Reverses List so we can see the solution from first step
                self.solution = (actions, cells) #saves the solution to the object
                return

            # Mark node as explored, no need to re-examine if the node is checked in the future.
            self.explored.add(node.state)

            #Make an array to store manhattan distance values
            MD = []
            #row, col = node.state
            #print("row ", row)
            #print("column ", col)
            goal_row, goal_col = self.goal
            #print("maze goal row ", goal_row)
            #print("maze goal col ", goal_col)
                    
            # Add neighbors to frontier - Look at all the neighboring Cells
            for action, state in self.neighbors(node.state):
                #If the state is not in the frontier and if its not in the explored set, then add to the frontier as a child.
                if not frontier.contains_state(state) and state not in self.explored:
                    child = Node(state=state, parent=node, action=action)
                    row, col = node.state
                    MD.append([abs(goal_row - row) + abs(goal_col - col), state, node, action])
            
            MD.sort() #sort the lowest path cost to be first
            child = Node(state = MD[0][1], parent = MD[0][2], action = MD[0][3])
            frontier.add(child) #add the best node to the frontier based on the sorted list above?
Here's my full broken code if you want to see it also.

I'm not sure what I'm doing wrong, but I'm open to suggestions or better approaches to adding the heuristic into this type of maze solving problem.

Thanks again for the help!