Пример #1
0
def aStarSearch(problem, heuristic):
    """
    Search the node that has the lowest combined cost and heuristic first.
    """

    fringe = PriorityQueue()
    visited = list()
    fringe.push((problem.startingState(), ()),
                heuristic(problem.startingState(), problem))
    while not fringe.isEmpty():
        curr = fringe.pop()
        if problem.isGoal(curr[0]):
            return list(curr[1])
        if not curr[0] in visited:
            visited.append(curr[0])
            nextSteps = problem.successorStates(curr[0])
            for i in nextSteps:
                path = list(curr[1])
                path.append(i[1])
                next = (i[0], tuple(path))
                if not i[0] in visited:
                    path = list(curr[1])
                    path.append(i[1])
                    fringe.push(
                        next,
                        problem.actionsCost(path) + heuristic(i[0], problem))
    return None
Пример #2
0
def uniformCostSearch(problem):
    fringe = PriorityQueue()
    fringe.push((problem.startingState(), [], 0), 0)
    visited = []
    while not fringe.isEmpty():
        state, currentPath, cost = fringe.pop()
        if problem.isGoal(state):
            return currentPath
        if state in visited:
            continue
        visited.append(state)
        for nextState in problem.successorStates(state):
            fringe.push((nextState[0], currentPath + [nextState[1]],
                         cost + nextState[2]), cost)
    return None
Пример #3
0
def aStarSearch(problem, heuristic):
    """
    Search the node that has the lowest combined cost and heuristic first.
    """

    # *** Your Code Here ***
    q = PriorityQueue()
    v = []
    n = problem.startingState()
    flag = False
    if len(n) == 2:  # problem 4 or 7
        flag = True
    actions = []
    q.push([n, actions], 0)

    while True:
        if q.isEmpty():
            return None
        (n, alist) = q.pop()
        actions = alist
        if problem.isGoal(n):
            return actions
        if not flag:
            visit = (n[0], n[2])
        else:
            visit = n
        if visit not in v:
            for item in problem.successorStates(n):
                child = item[0]
                dir = item[1]
                actions.append(dir)
                if flag:
                    item = (child[0], child[1])
                    vis = item
                else:
                    vis = (item[0], item[2])
                cost = problem.actionsCost(actions) + heuristic(item, problem)
                if vis not in v:
                    q.push([item, actions.copy()], cost)
                actions.pop(len(actions) - 1)
            if flag:
                visited = (n[0], n[1])
            else:
                visited = (n[0], n[2])
            v.append(visited)
Пример #4
0
def aStarSearch(problem, heuristic):
    """
    Search the node that has the lowest combined cost and heuristic first.
    """
    # *** Your Code Here ***
    explored = []
    path = []
    fringe = PriorityQueue()
    memSuccessorStates = {}
    fringe.push((problem.startingState(), 'SELF', 0), 0)
    while (not fringe.isEmpty()):
        currentNode = fringe.pop()
        path.append(currentNode)
        successorStates = problem.successorStates(currentNode[0])
        states = []
        for state, _, _ in successorStates:
            states.append(state)
        memSuccessorStates[currentNode[0]] = states
        for successor in successorStates:
            if (successor[0] not in explored):
                if (problem.isGoal(successor[0])):
                    deleteNodes = []
                    includeNode = path[-1]
                    for j in range(1, len(path)):
                        revIndex = len(path) - j - 1
                        currNode = path[revIndex]
                        if (includeNode[0] in memSuccessorStates[currNode[0]]):
                            includeNode = path[revIndex]
                        else:
                            deleteNodes.append(path[revIndex])
                    solution = []
                    for node in path:
                        if (node not in deleteNodes):
                            if (node[1] != 'SELF'):
                                solution.append(node[1])
                    solution.append(successor[1])
                    return (solution)
                fringe.push(successor,
                            successor[2] + heuristic(successor[0], problem))
                explored.append(successor[0])
    return ([])
    raise NotImplementedError()
Пример #5
0
def uniformCostSearch(problem):
    """
    Search the node of least total cost first.
    """
    # *** Your Code Here ***
    q = PriorityQueue()
    v = []
    n = problem.startingState()
    flag = False
    if len(n) == 2:  # problem 4 or 7
        flag = True
    actions = []
    q.push([n, actions], 0)

    while True:
        if q.isEmpty():
            return None
        (n, alist) = q.pop()
        actions = alist
        if problem.isGoal(n):
            return actions
        if not flag:
            visit = (n[0], n[2])
        else:
            visit = n
        if visit not in v:
            v.append(visit)
            for item in problem.successorStates(n):
                child = item[0]
                dir = item[1]
                actions.append(dir)
                if flag:
                    item = child
                    vis = child
                else:
                    vis = (item[0], item[2])
                cost = problem.actionsCost(actions)
                if vis not in v:
                    q.push([item, actions.copy()], cost)
                actions.pop(len(actions) - 1)
Пример #6
0
def uniformCostSearch(problem):
    """
    Search the node of least total cost first.
    """

    fringe = PriorityQueue()
    visited = list()
    fringe.push((problem.startingState(), None, None, list()), 0)
    while not fringe.isEmpty():
        curr = fringe.pop()
        if problem.isGoal(curr[0]):
            return curr[3]
        if not curr[0] in visited:
            visited.append(curr[0])
            nextSteps = problem.successorStates(curr[0])
            for i in nextSteps:
                if not i[0] in visited:
                    path = list(curr[3])
                    path.append(i[1])
                    fringe.push((i[0], i[1], i[2], path),
                                problem.actionsCost(path))
    return None
def uniformCostSearch(problem):
    """
    Search the node of least total cost first.
    """

    # *** Your Code Here ***
    visited = []
    queue = PriorityQueue()
    neighbors = []

    queue.push(((problem.startingState()), []), 0)

    while not queue.isEmpty():

        CurNode, path = queue.pop()

        if problem.isGoal(CurNode) is True:
            return path

        # print("Current Node: %s" % (str(CurNode)))
        # print("Is the start a goal?: %s" % (problem.isGoal(CurNode)))
        # print("Start's successors: %s" % (problem.successorStates(CurNode)))

        if CurNode in visited:
            continue

        visited.append(CurNode)
        neighbors = problem.successorStates(CurNode)

        for leaf in neighbors:
            if leaf[0] in visited:
                continue
            else:
                total_cost = problem.actionsCost(path + [leaf[1]]) + leaf[2]
                queue.push((leaf[0], path + [leaf[1]]), total_cost)

    raise NotImplementedError()
def aStarSearch(problem, heuristic):
    """
    Search the node that has the lowest combined cost and heuristic first.
    """

    # *** Your Code Here ***
    visited = []
    queue = PriorityQueue()
    neighbors = []

    queue.push(((problem.startingState()), []), 0)

    while not queue.isEmpty():

        CurNode, path = queue.pop()

        if problem.isGoal(CurNode) is True:
            return path

        # print("Current Node: %s" % (str(CurNode)))
        # print("Is the start a goal?: %s" % (problem.isGoal(CurNode)))
        # print("Start's successors: %s" % (problem.successorStates(CurNode)))

        if CurNode in visited:
            continue

        visited.append(CurNode)
        neighbors = problem.successorStates(CurNode)

        for leaf in neighbors:
            if leaf[0] in visited:
                continue
            else:
                total_cost = problem.actionsCost(path + [leaf[1]]) + heuristic(
                    leaf[0], problem)
                queue.push((leaf[0], path + [leaf[1]]), total_cost)