예제 #1
0
def a_star_search(problem, fringe):
    # If there is no node on fringe, make one
    if len(fringe.queue) <= 0:
        initial_node = Node(problem)
        fringe.insert(initial_node)

    # If nodes don't have path cost for A* Search, give them one
    for node in fringe.queue:
        if node.a_star_cost == None:
            node.a_star_cost = a_star_evaluator(node)

    # Form a temporary priority queue
    a_star_fringe = Queue()
    a_star_fringe.insert_all(fringe.queue)

    # Sort them according to lowest path cost
    bubble_sort(a_star_fringe, False)

    # Iterate over the sorted nodes, to check visually
    for nodes in a_star_fringe.queue:
        print('Node: ' + str(nodes.a_star_cost) + ' Depth: ' + str(nodes.depth) + ' Action: ' + str(nodes.action))

    # Now do the good stuff
    # Get the first node in A* fringe and then pop it on the main fringe
    greed_node = a_star_fringe.pop()
    node = fringe.pop_specific(greed_node)

    # If the node is the goal, return it as goal; otherwise expand it
    if goal_test(node.state) == True:
        return True, solution(node), node
    else:
        if node.state.connor.death == False:
            fringe.insert_all(expand(node))
        return False, fringe, node
예제 #2
0
def best_fs(problem, fringe):
    # If there is no node on fringe, make one
    if len(fringe.queue) <= 0:
        initial_node = Node(problem)
        fringe.insert(initial_node)

    # If nodes don't have path cost for Greedy BFS, give them one
    for node in fringe.queue:
        if node.greed_cost == None:
            node.greed_cost = best_fs_evaluator(node)

    # Form a temporary priority queue
    best_fs_fringe = Queue()
    best_fs_fringe.insert_all(fringe.queue)

    # Sort them according to lowest path cost
    bubble_sort(best_fs_fringe)

    # Now do the good stuff
    # Get the first node in Greedy BFS fringe and then pop it on the main fringe
    greed_node = best_fs_fringe.pop()
    node = fringe.pop_specific(greed_node)

    # If the node is the goal, return it as goal; otherwise expand it
    if goal_test(node.state) == True:
        return True, solution(node), node
    else:
        if node.state.connor.death == False:
            fringe.insert_all(expand(node))
        return False, fringe, node