Exemple #1
0
def uniformCostSearch(problem):
    "Search the node of least total cost first. "
    
    fringe = util.PriorityQueue()
    paths = util.PriorityQueue()
    costs = util.PriorityQueue()
    discovered = []
    fringe.push(problem.getStartState(), 0)
    paths.push([], 0)
    costs.push(0, 0)
    
    while not fringe.isEmpty():
        
        current_state = fringe.pop()
        current_path = paths.pop()
        cost = costs.pop()
        discovered.append(current_state)
        
        if problem.isGoalState(current_state)==True:
            return current_path
        
        children = problem.getSuccessors(current_state)
        
        for successor in children:
            if successor[0] not in discovered:
                copy = current_path[:]
                copy.append(successor[1])
                costs.push(successor[2] + cost, successor[2] + cost)
                paths.push(copy, successor[2] + cost)
                fringe.push(successor[0], successor[2] + cost)
    return None
Exemple #2
0
def depthFirstSearch(problem):
    """
    Search the nodes in the search tree with a depth-first search algorithm and
    returns the first accepted path to the goal. 
    This is implemented with a graph search algorithm.

    Author - Shandheap Shanmuganathan
    """
    
    # Uses two stacks to store the path to the state
    # as well as the state that should be discovered
    fringe = util.Stack()
    paths = util.Stack()
    discovered = []
    fringe.push(problem.getStartState())
    paths.push([])
    
    # While there are states still to be explored
    while not fringe.isEmpty():
        current_state = fringe.pop()
        current_path = paths.pop()
        discovered.append(current_state)
        # If the algorithm finds a goal, then it immediately
        # returns it
        if problem.isGoalState(current_state):
            return current_path
        children = problem.getSuccessors(current_state)
        for successor in children:
            if successor[0] not in discovered:
                copy = current_path[:]
                copy.append(successor[1])
                paths.push(copy)
                fringe.push(successor[0])
    return None
Exemple #3
0
def breadthFirstSearch(problem):
    """
    Search the shallowest nodes in the search tree first.
    """
    fringe = util.Queue()
    paths = util.Queue()
    discovered = []
    fringe.push(problem.getStartState())
    paths.push([])
    
    while not fringe.isEmpty():
        current_state = fringe.pop()
        current_path = paths.pop()
        discovered.append(deepcopy(current_state))
        if problem.isGoalState(current_state)==True:
            return current_path
        children = problem.getSuccessors(current_state)
        for successor in children:
            if successor[0] not in discovered:
                if type(successor[0][0]) == tuple and successor[0][0] in problem.corners:
                    if problem.corners[problem.current_corner] == successor[0][0]:
                        if problem.goal[problem.current_corner] == False:
                            problem.goal[problem.current_corner] = True
                            fringe = util.Queue()
                            paths = util.Queue()
                            problem.current_corner += 1
                discovered.append(deepcopy(successor[0]))
                copy = current_path[:]
                copy.append(successor[1])
                paths.push(copy)
                fringe.push(successor[0])
    return None
Exemple #4
0
def uniformCostSearch(problem):
    '''
    Search the node of least total cost first and returns the shortest possible accepted path. 
    This is implemented with a graph search algorithm.

    Author - Shandheap Shanmuganathan
    '''
    
    fringe = util.PriorityQueue()
    paths = util.PriorityQueue()
    costs = util.PriorityQueue()
    discovered = []
    fringe.push(problem.getStartState(), 0)
    paths.push([], 0)
    costs.push(0, 0)
    
    while not fringe.isEmpty():
        current_state = fringe.pop()
        current_path = paths.pop()
        cost = costs.pop()
        discovered.append(current_state)
        if problem.isGoalState(current_state):
            return current_path
        children = problem.getSuccessors(current_state)
        for successor in children:
            if successor[0] not in discovered:
                copy = current_path[:]
                copy.append(successor[1])
                costs.push(successor[2] + cost, successor[2] + cost)
                paths.push(copy, successor[2] + cost)
                fringe.push(successor[0], successor[2] + cost)
    return None
Exemple #5
0
def copy_of(board):
	"""
	Returns a copy of a board
	"""
	copy = []
	for row_num in range(10):
		row = []
		for col_num in range(10):
			row.append(board[row_num][col_num])
		copy.append(row)
	return copy
Exemple #6
0
def copy_of(board):
    """
	Returns a copy of a board
	"""
    copy = []
    for row_num in range(10):
        row = []
        for col_num in range(10):
            row.append(board[row_num][col_num])
        copy.append(row)
    return copy
Exemple #7
0
def aStarSearch(problem, heuristic = nullHeuristic):
    '''
    Search the node that has the lowest combined cost and heuristic first.
    Implemented with a graph search algorithm. Uses nullHeuristic by default
    if no other heuristic is specified.

    Author - Shandheap Shanmuganathan
    '''
    
    fringe = util.PriorityQueue()
    paths = util.PriorityQueue()
    aStarCosts = util.PriorityQueue()
    discovered = []
    heuristicCost = heuristic(problem.getStartState(), problem)
    fringe.push(problem.getStartState(), heuristicCost)
    paths.push([], heuristicCost)
    aStarCosts.push(heuristicCost, heuristicCost)
    
    while not fringe.isEmpty():
        current_state = fringe.pop()
        current_path = paths.pop()
        cost = aStarCosts.pop()
        while current_state in discovered:
            current_state = fringe.pop()
            current_path = paths.pop()
            cost = aStarCosts.pop()
        # Makes a deep copy so that the entry in list discovered 
        # is not altered by changes to current_state
        discovered.append(deepcopy(current_state))              
        if problem.isGoalState(current_state):
            return current_path
        children = problem.getSuccessors(current_state)
        for successor in children:
            if successor[0] not in discovered:
                if type(successor[0][0]) == tuple and successor[0][0] in problem.corners:
                    if problem.corners[problem.current_corner] == successor[0][0]:
                        if problem.goal[problem.current_corner] == successor[0][0]:
                            problem.goal[problem.current_corner] = True
                            fringe = util.PriorityQueue()
                            paths = util.PriorityQueue()
                            aStarCosts = util.PriorityQueue()
                            problem.current_corner += 1
                copy = current_path[:]
                copy.append(successor[1])
                heuristicCost = heuristic(successor[0], problem)
                aStarCosts.push(successor[2] + cost, successor[2] + cost + heuristicCost)
                paths.push(copy, successor[2] + cost + heuristicCost)
                fringe.push(successor[0], successor[2] + cost + heuristicCost)
    return None
Exemple #8
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())
    """
  
    fringe = util.Stack()
    paths = util.Stack()
    discovered = []
    fringe.push(problem.getStartState())
    paths.push([])
    
    
    while not fringe.isEmpty():
        
        current_state = fringe.pop()
        current_path = paths.pop()
        discovered.append(current_state)
        
        if problem.isGoalState(current_state)==True:
            return current_path
        
        children = problem.getSuccessors(current_state)
        
        for successor in children:
            if successor[0] not in discovered:
                copy = current_path[:]
                copy.append(successor[1])
                paths.push(copy)
                fringe.push(successor[0])
    return None
Exemple #9
0
def aStarSearch(problem, heuristic = nullHeuristic):
    "Search the node that has the lowest combined cost and heuristic first."
    fringe = util.PriorityQueue()
    paths = util.PriorityQueue()
    aStarCosts = util.PriorityQueue()
    discovered = []
    heuristicCost = heuristic(problem.getStartState(), problem)
    fringe.push(problem.getStartState(), heuristicCost)
    paths.push([], heuristicCost)
    aStarCosts.push(heuristicCost, heuristicCost)
    
    while not fringe.isEmpty():
        current_state = fringe.pop()
        current_path = paths.pop()
        cost = aStarCosts.pop()
        while current_state in discovered:
            current_state = fringe.pop()
            current_path = paths.pop()
            cost = aStarCosts.pop()
        discovered.append(deepcopy(current_state))
        if problem.isGoalState(current_state)==True:
            return current_path
        children = problem.getSuccessors(current_state)
        for successor in children:
            if successor[0] not in discovered:
                if type(successor[0][0]) == tuple and successor[0][0] in problem.corners:
                    if problem.corners[problem.current_corner] == successor[0][0]:
                        if problem.goal[problem.current_corner] == successor[0][0]:
                            problem.goal[problem.current_corner] = True
                            fringe = util.PriorityQueue()
                            paths = util.PriorityQueue()
                            aStarCosts = util.PriorityQueue()
                            problem.current_corner += 1
                copy = current_path[:]
                copy.append(successor[1])
                heuristicCost = heuristic(successor[0], problem)
                aStarCosts.push(successor[2] + cost, successor[2] + cost + heuristicCost)
                paths.push(copy, successor[2] + cost + heuristicCost)
                fringe.push(successor[0], successor[2] + cost + heuristicCost)
    return None
Exemple #10
0
def breadthFirstSearch(problem):
    """
    Search the shallowest nodes in the search tree first and returns the shortest possible accepted path. 
    This is implemented with a graph search algorithm.

    Author - Shandheap Shanmuganathan
    """
    fringe = util.Queue()
    paths = util.Queue()
    discovered = []
    fringe.push(problem.getStartState())
    paths.push([])
    
    while not fringe.isEmpty():
        current_state = fringe.pop()
        current_path = paths.pop()
        # Makes a deep copy so that the entry in list discovered 
        # is not altered by changes to current_state
        discovered.append(deepcopy(current_state))          
        if problem.isGoalState(current_state):
            return current_path
        children = problem.getSuccessors(current_state)
        for successor in children:
            if successor[0] not in discovered:
                if type(successor[0][0]) == tuple and successor[0][0] in problem.corners:
                    if problem.corners[problem.current_corner] == successor[0][0]:
                        if problem.goal[problem.current_corner] == False:
                            problem.goal[problem.current_corner] = True
                            fringe = util.Queue()
                            paths = util.Queue()
                            problem.current_corner += 1
                discovered.append(deepcopy(successor[0]))
                copy = current_path[:]
                copy.append(successor[1])
                paths.push(copy)
                fringe.push(successor[0])
    return None