示例#1
0
    def __init__(self, maze, mode):
        """
        Inputs:
            maze: a MazeClass.Maze instance
            mode: one of "BFS", "DFS", or "RND"
        """
        self.mz = maze #just made a new, shorter name. 
        self.free = {}
        self.parents = {}
        self.walls = {}
        self.start = maze.start
        self.end = maze.goal

        if mode == "DFS":
            self.frontier = LIFO_Queue()

        elif mode == "BFS":
            self.frontier = FIFO_Queue()

        elif mode == "RND":
            self.frontier = Random_Queue()
示例#2
0
    def __init__(self, maze, mode):
        """
        Inputs:
            maze: a MazeClass.Maze instance
            mode: one of "BFS", "DFS", or "RND"
        """
        self.mz = maze  #just made a new, shorter name.
        self.free = {}
        self.parents = {}
        self.walls = {}
        self.start = maze.start
        self.end = maze.goal

        if mode == "DFS":
            self.frontier = LIFO_Queue()

        elif mode == "BFS":
            self.frontier = FIFO_Queue()

        elif mode == "RND":
            self.frontier = Random_Queue()
示例#3
0
class SearchAgent:
    """Core class for this assignment.
    Stores a MazeClass.Maze instance and the results of searching that maze.
    After a SearchAgent is initialized, the search() method should be run to
    look for start-->goal paths in the maze. The display methods will then
    show information about the search results."""
    def __init__(self, maze, mode):
        """
        Inputs:
            maze: a MazeClass.Maze instance
            mode: one of "BFS", "DFS", or "RND"
        """
        self.mz = maze  #just made a new, shorter name.
        self.free = {}
        self.parents = {}
        self.walls = {}
        self.start = maze.start
        self.end = maze.goal

        if mode == "DFS":
            self.frontier = LIFO_Queue()

        elif mode == "BFS":
            self.frontier = FIFO_Queue()

        elif mode == "RND":
            self.frontier = Random_Queue()

    def search(self):
        """Searches from start until it reaches goal or proves that it can't.
        On return, all visited states will appear in self.walls or self.free,
        and self.parents will reflect how each visited state was reached. The
        type of queue used to store the frontier determines which states are
        visited in which order.
        search() takes no inputs and has no return value.
        """
        #thank you for the pseudocode!
        self.frontier.add(self.start)
        self.parents[self.start] = None

        while len(self.frontier) != 0:
            self.curr = self.frontier.get()
            if self.mz.is_wall(self.curr):
                self.walls[self.curr] = None

            else:
                self.free[self.curr] = None
                if self.curr == self.end:
                    return

                for i in self.mz.neighbors(self.curr):
                    if i not in self.parents:
                        self.parents[i] = self.curr
                        self.frontier.add(i)

    def path_to(self, state):
        """Returns a list of (row, col) pairs along a path from start-->state.
        Should only be called after search(). If state has not been reached,
        an empty list is returned.
        """
        ret = []
        if self.curr != self.end:
            return ret

        #follows trace from end to start, bc curr is at the end
        self.trace = self.curr
        while self.trace != self.start:
            ret.append(self.trace)
            self.trace = self.parents[self.trace]
        ret.append(self.trace)
        return ret

    def display_path(self):
        """Prints the maze with a path from start-->goal if one was found.
        Should only be called after search()."""
        rep = u""
        path = self.path_to(self.mz.goal)
        for r in range(self.mz.rows):
            for c in range(self.mz.cols):
                rep += self._path_char((r, c), path) + " "
            rep += "\n"
        print rep

    def _path_char(self, state, path):
        """Helper function for display_path()."""
        if state in path:
            return PATH
        return self.mz._display_char(self.curr)

    def display_parents(self):
        """Prints a representation of the agent's knowledge of the maze.
        Should be called after search(). Visited free cells are represented by
        an arrow pointing in the direction of their parent. Unvisited cells
        are displayed as blanks."""
        rep = u""
        for r in range(self.mz.rows):
            for c in range(self.mz.cols):
                rep += self._parent_char((r, c)) + " "
            rep += "\n"
        print rep

    def _parent_char(self, state):
        """Helper function for display_parents()."""
        if state == self.mz.start:
            return EMPTY
        if state in self.walls:
            return WALL
        if state in self.free:
            p = self.parents[state]
            if p[0] < state[0]:
                return UP
            if p[0] > state[0]:
                return DOWN
            if p[1] < state[1]:
                return LEFT
            if p[1] > state[1]:
                return RIGHT
        return UNKNOWN
示例#4
0
class SearchAgent:
    """Core class for this assignment.
    Stores a MazeClass.Maze instance and the results of searching that maze.
    After a SearchAgent is initialized, the search() method should be run to
    look for start-->goal paths in the maze. The display methods will then
    show information about the search results."""
    def __init__(self, maze, mode):
        """
        Inputs:
            maze: a MazeClass.Maze instance
            mode: one of "BFS", "DFS", or "RND"
        """
        self.mz = maze #just made a new, shorter name. 
        self.free = {}
        self.parents = {}
        self.walls = {}
        self.start = maze.start
        self.end = maze.goal

        if mode == "DFS":
            self.frontier = LIFO_Queue()

        elif mode == "BFS":
            self.frontier = FIFO_Queue()

        elif mode == "RND":
            self.frontier = Random_Queue()

    def search(self):
        """Searches from start until it reaches goal or proves that it can't.
        On return, all visited states will appear in self.walls or self.free,
        and self.parents will reflect how each visited state was reached. The
        type of queue used to store the frontier determines which states are
        visited in which order.
        search() takes no inputs and has no return value.
        """
        #thank you for the pseudocode!
        self.frontier.add(self.start)
        self.parents[self.start] = None

        while len(self.frontier) != 0:
            self.curr = self.frontier.get()
            if self.mz.is_wall(self.curr):
                self.walls[self.curr] = None

            else:
                self.free[self.curr] = None
                if self.curr == self.end:
                    return

                for i in self.mz.neighbors(self.curr):
                    if i not in self.parents:
                        self.parents[i] = self.curr
                        self.frontier.add(i)


    def path_to(self, state):
        """Returns a list of (row, col) pairs along a path from start-->state.
        Should only be called after search(). If state has not been reached,
        an empty list is returned.
        """
        ret = []
        if self.curr != self.end:
            return ret

        #follows trace from end to start, bc curr is at the end
        self.trace = self.curr
        while self.trace!= self.start:
            ret.append(self.trace)
            self.trace= self.parents[self.trace]
        ret.append(self.trace)            
        return ret


    def display_path(self):
        """Prints the maze with a path from start-->goal if one was found.
        Should only be called after search()."""
        rep = u""
        path = self.path_to(self.mz.goal)
        for r in range(self.mz.rows):
            for c in range(self.mz.cols):
                rep += self._path_char((r,c), path) + " "
            rep += "\n"
        print rep

    def _path_char(self, state, path):
        """Helper function for display_path()."""
        if state in path:
            return PATH
        return self.mz._display_char(self.curr)
        
    def display_parents(self):
        """Prints a representation of the agent's knowledge of the maze.
        Should be called after search(). Visited free cells are represented by
        an arrow pointing in the direction of their parent. Unvisited cells
        are displayed as blanks."""
        rep = u""
        for r in range(self.mz.rows):
            for c in range(self.mz.cols):
                rep += self._parent_char((r,c)) + " "
            rep += "\n"
        print rep

    def _parent_char(self, state):
        """Helper function for display_parents()."""
        if state == self.mz.start:
            return EMPTY
        if state in self.walls:
            return WALL
        if state in self.free:
            p = self.parents[state]
            if p[0] < state[0]:
                return UP
            if p[0] > state[0]:
                return DOWN
            if p[1] < state[1]:
                return LEFT
            if p[1] > state[1]:
                return RIGHT
        return UNKNOWN