Exemplo 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 ***"
    from util import Stack
    nodes = Stack()
    closed = dict()
    moves = list()
  
    for var in problem.getSuccessors(problem.getStartState()):
        state = FringeState(var[0])
        state.moves.append(Directions.convToDirection(var[1]))
        nodes.push(state)
    
    closed[problem.getStartState()] = True   

    while(False == nodes.isEmpty()):
        current_node = nodes.pop()
        #print "Current node:", current_node
        
        if(closed.has_key(current_node.pos)):
            continue

        
        if(problem.isGoalState(current_node.pos)):
            print "Goal reached!"
            moves = current_node.moves
            break
        
        for var in problem.getSuccessors(current_node.pos):
            state = FringeState(var[0])
            state.moves= copy.deepcopy(current_node.moves)
            state.moves.append(Directions.convToDirection(var[1]))
            nodes.push(state)
        
        closed[current_node.pos] = True
                
    return moves
Exemplo n.º 2
0
def uniformCostSearch(problem):
    """Search the node of least total cost first."""
    from util import PriorityQueue
    nodes = PriorityQueue()
    closed = dict()
    moves = list()
  
    for var in problem.getSuccessors(problem.getStartState()):
        state = FringeState(var[0])
        state.moves.append(Directions.convToDirection(var[1]))
        state.cost = var[2]
        nodes.push(state, state.cost)
        
    closed[problem.getStartState()] = True   
    
    while(False == nodes.isEmpty()):
        current_node = nodes.pop()
        #print "Current node:", current_node
        
        if(closed.has_key(current_node.pos)):
            continue
                    
        if(problem.isGoalState(current_node.pos)):
            print "Goal reached!"
            moves = current_node.moves
            break
        
        for var in problem.getSuccessors(current_node.pos):
            state = FringeState(var[0])
            state.moves= copy.deepcopy(current_node.moves)
            state.moves.append(Directions.convToDirection(var[1]))
            state.cost = var[2] + current_node.cost
            nodes.push(state, state.cost)
        
        closed[current_node.pos] = True
                
    return moves
Exemplo n.º 3
0
def aStarSearch(problem, heuristic=nullHeuristic):
    """Search the node that has the lowest combined cost and heuristic first."""
    from util import PriorityQueue
    nodes = PriorityQueue()
    closed = dict()
    moves = list()
  
    for var in problem.getSuccessors(problem.getStartState()):
        state = FringeState(var[0])
        state.moves.append(Directions.convToDirection(var[1]))
        state.cost = var[2]
        nodes.push(state, state.cost + heuristic(state.pos,problem))
        
    closed[problem.getStartState()] = True   
    
    while(False == nodes.isEmpty()):
        current_node = nodes.pop()
        #print "Current node:", current_node
        
        if(closed.has_key(current_node.pos)):
            continue
                    
        if(problem.isGoalState(current_node.pos)):
            print "Goal reached!"
            moves = current_node.moves
            break
        
        for var in problem.getSuccessors(current_node.pos):
            state = FringeState(var[0])
            state.moves= copy.deepcopy(current_node.moves)
            state.moves.append(Directions.convToDirection(var[1]))
            state.cost = var[2] + current_node.cost
            nodes.push(state, state.cost + heuristic(state.pos,problem))
        
        closed[current_node.pos] = True
                
    return moves
Exemplo n.º 4
0
def breadthFirstSearch(problem):
    """Search the shallowest nodes in the search tree first."""
    "*** YOUR CODE HERE ***"
    from util import Queue
    nodes = Queue()
    closed = dict()
    moves = list()
  
    
    for var in problem.getSuccessors(problem.getStartState()):
        state = FringeState(var[0])
        state.moves.append(Directions.convToDirection(var[1]))
        nodes.push(state)

    closed[problem.getStartState()] = True   
    
    while(False == nodes.isEmpty()):
        current_node = nodes.pop()
        #print "Current node:", current_node
        
        if(closed.has_key(current_node.pos)):
            continue
                    
        if(problem.isGoalState(current_node.pos)):
            print "Goal reached!"
            moves = current_node.moves
            break
        
        for var in problem.getSuccessors(current_node.pos):
            state = FringeState(var[0])
            state.moves= copy.deepcopy(current_node.moves)
            state.moves.append(Directions.convToDirection(var[1]))
            nodes.push(state)
        
        closed[current_node.pos] = True
                
    return moves