Example #1
0
def aStarSearch(problem, heuristic=nullHeuristic):
    '''
    Pay clos attention to util.py- specifically, args you pass to member functions. 

    Key Point: If a node is already present in the queue with higher path cost, 
    you'll update its cost (Similar to pseudocode in figure 3.14 of your textbook.). Be careful, 
    autograder cannot catch this bug.

    '''
    "Start of Your Code"
    # pass
    "End of Your Code"
    from util import PriorityQueue

    A_queue = PriorityQueue()

    visited = []
    path = []

    if problem.isGoalState(problem.getStartState()):
        return []

    element = (problem.getStartState(), [])
    cost = problem.getCostOfActions(element[1]) + heuristic(
        element[0], problem)
    A_queue.push(element, cost)

    while (True):
        if A_queue.isEmpty():
            return []

        ((xy, path), c) = A_queue.pop()
        visited.append(xy)
        check = xy, path

        if problem.isGoalState(xy):
            return path

        succ = problem.getSuccessors(xy)
        if succ:
            for child in succ:
                if child[0] not in visited and (child[0] not in (
                        data[2][0] for data in A_queue.heap)):

                    newPath = path + [child[1]]
                    element = (child[0], newPath)
                    cost = problem.getCostOfActions(element[1]) + heuristic(
                        element[0], problem)
                    A_queue.push(element, cost)

                elif child[0] not in visited and (child[0] in (
                        data[2][0] for data in A_queue.heap)):
                    i = 0
                    for data in A_queue.heap:
                        if data[2][0] == child[0]:
                            i -= 1
                            oldprice = problem.getCostOfActions(data[2][1])

                    newprice = problem.getCostOfActions(path + [child[1]])

                    if oldprice > newprice:
                        newPath = path + [child[1]]
                        element = (child[0], newPath)
                        cost = problem.getCostOfActions(
                            element[1]) + heuristic(element[0], problem)
                        A_queue.Update_priority((child[0], newPath), cost, i,
                                                A_queue.count)
Example #2
0
def uniformCostSearch(problem):
    """Search the node of least total cost first.
       You may need to pay close attention to util.py.
       Useful Reminder: Note that problem.getSuccessors(node) returns "step_cost". 

       Key Point: If a node is already present in the queue with higher path cost, 
       you'll update its cost. (Similar to pseudocode in figure 3.14 of your textbook.). Be careful, 
       autograder cannot catch this bug. 
    """

    "Start of Your Code"
    # pass
    "End of Your Code"

    from util import PriorityQueue

    UCS_queue = PriorityQueue()

    visited = []
    path = []

    if problem.isGoalState(problem.getStartState()):
        return []

    UCS_queue.push((problem.getStartState(), []), 0)

    while (True):

        if UCS_queue.isEmpty():
            return []

        ((xy, path), c) = UCS_queue.pop()
        visited.append(xy)
        check = xy, path

        if problem.isGoalState(xy):
            return path

        succ = problem.getSuccessors(xy)
        if succ:
            for child in succ:
                if child[0] not in visited and (child[0] not in (
                        data[2][0] for data in UCS_queue.heap)):

                    newPath = path + [child[1]]
                    pri = problem.getCostOfActions(newPath)

                    UCS_queue.push((child[0], newPath), pri)
                elif child[0] not in visited and (child[0] in (
                        data[2][0] for data in UCS_queue.heap)):
                    i = 0
                    for data in UCS_queue.heap:
                        if data[2][0] == child[0]:
                            i -= 1
                            oldprice = problem.getCostOfActions(data[2][1])

                    newprice = problem.getCostOfActions(path + [child[1]])

                    if oldprice > newprice:
                        newPath = path + [child[1]]
                        UCS_queue.Update_priority((child[0], newPath),
                                                  newprice, i, UCS_queue.count)