Esempio n. 1
0
def uniform_cost_search(problem):
    root = Node(problem.initial)
    frontier = PriorityQueue()
    frontier.append(root)
    explored = set()

    while True:
        if len(frontier) == 0:
            return "fail"
        node = frontier.pop()
        if problem.goal_test(node.state):
            return node
        explored.add(node)
        for action in problem.actions(node.state):
            child = node.child_node(problem, action)
            if child.state not in explored:
                print(child.state + " > ")
                frontier.append(child)
            elif child in frontier:
                item = frontier.__getitem__(child)
                if child.path_cost < item.path_cost:
                    pass