Esempio n. 1
0
def depthFirstSearch(problem):
    """
    Search the deepest nodes in the search tree first.

    Your search algorithm needs to return a list of actions that reaches the
    goal. Make sure to implement a graph search algorithm.

    To get started, you might want to try some of these simple commands to
    understand the search problem that is being passed in:

    print "Start:", problem.getStartState()
    print "Is the start a goal?", problem.isGoalState(problem.getStartState())
    print "Start's successors:", problem.getSuccessors(problem.getStartState())
    """
    "*** YOUR CODE HERE ***"
    node = Node(problem.getStartState())
    if problem.isGoalState(problem.getStartState()): return node.solution()
    frontier = util.Stack()
    frontier.push(node)
    explored = set()
    while not frontier.isEmpty():
        node = frontier.pop()
        if problem.isGoalState(node.state): return node.solution()
        explored.add(node.state)
        for child in node.expand(problem):
            if child.state not in explored:
                frontier.push(child)
    return None
Esempio n. 2
0
def bestFirstSearch(problem, heuristic=nullHeuristic):
    node = Node(problem.getStartState())
    if problem.isGoalState(problem.getStartState()): return node.solution()
    frontier = util.PriorityQueue()
    frontier.update(node, heuristic(node.state, problem))
    explored = set()
    while not frontier.isEmpty():
        node = frontier.pop()
        if problem.isGoalState(node.state): return node.solution()
        explored.add(node.state)
        for child in node.expand(problem):
            if (child.state not in explored) and (child not in frontier.heap):
                frontier.update(child, heuristic(child.state, problem))
    return None
Esempio n. 3
0
def aStarSearch(problem, heuristic=nullHeuristic):
    """Search the node that has the lowest combined cost and heuristic first."""
    "*** YOUR CODE HERE ***"
    node = Node(problem.getStartState())
    if problem.isGoalState(problem.getStartState()): return node.solution()
    frontier = util.PriorityQueue()
    frontier.update(node, node.path_cost + heuristic(node.state, problem))
    explored = set()
    while not frontier.isEmpty():
        node = frontier.pop()
        if problem.isGoalState(node.state): return node.solution()
        explored.add(node.state)
        for child in node.expand(problem):
            if (child.state not in explored) and (child not in frontier.heap):
                frontier.update(
                    child, child.path_cost + heuristic(child.state, problem))
    return None
Esempio n. 4
0
def breadthFirstSearch(problem):
    """Search the shallowest nodes in the search tree first."""
    "*** YOUR CODE HERE ***"
    node = Node(problem.getStartState())
    if problem.isGoalState(problem.getStartState()): return node.solution()
    frontier = util.Queue()
    frontier.push(node)
    explored = set()
    while not frontier.isEmpty():
        node = frontier.pop()
        if problem.isGoalState(node.state): return node.solution()
        explored.add(node.state)
        for child in node.expand(problem):
            if (child.state not in explored) and (child.state
                                                  not in frontier.list):
                if problem.isGoalState(child.state): return child.solution()
                frontier.push(child)
    return None