示例#1
0
def a_star_search(graph, start, goal):
    searchFrontier = PriorityQueue()
    searchFrontier.put(start, heuristic[start])
    currentCost = {}
    currentCost[start] = 0

    path = ""

    while not searchFrontier.empty():
        current = searchFrontier.get()

        # add node in path
        path += current + " "

        if current == goal:
            break

        for next in graph.neighbors(current):
            newCost = currentCost[current] + weights[(current, next)]
            if next not in currentCost or newCost < currentCost[next]:
                currentCost[next] = newCost
                priority = newCost + heuristic[next]  # set priority
                searchFrontier.put(next,
                                   priority)  # add node in priority queue

    return path
示例#2
0
def main():
    selection = int(sys.argv[1])
    # Reads in the board from the random_board.py output
    board = list(map(int, sys.stdin.read().split()))
    board = [[board[0], board[1], board[2]], [board[3], board[4], board[5]],
             [board[6], board[7], board[8]]]
    state = State(board)

    # Setting the x and y position for the blank tile
    for i in range(0, 3):
        for j in range(0, 3):
            if board[i][j] == 0:
                state.setx(i)
                state.sety(j)

    # Initialize the relevent data structures
    curr_node = Node(state, h(state, selection))
    closed = Set()
    frontier = PriorityQueue()
    frontier.push(curr_node)
    V = 0

    # Loops through and creates the search tree
    while objective(curr_node.getstate()) == False:
        closed.add(curr_node.getstate())
        # Creates new states based on the valid_moves returned by the successor
        for valid_state in successor(curr_node.getstate()):
            V += 1
            # Checking that the state we are eveluating has not already been expanded. Only adds to the queue if false
            if closed.isMember(valid_state) == False:
                frontier.push(
                    Node(valid_state, h(valid_state, selection), curr_node))
        curr_node = frontier.pop()

    N = closed.length() + frontier.length()
    d = curr_node.getd()
    print("V=%d" % (V))
    print("N=%d" % (N))
    print("d=%d" % (d))
    print("b=%f" % (N**(1 / d)))
    print()

    # Create an empty list in order to retrieve the path from the goal node's parents
    stack = []
    while curr_node.getid() != 0:
        stack.append(curr_node)
        curr_node = curr_node.getparent()
    print(curr_node.getstate())
    # Prints out the solution path
    for node in reversed(stack):
        if node != None:
            print(node.getstate())
示例#3
0
def bestfs(graph, start, goal):
    searchFrontier = PriorityQueue()
    searchFrontier.put(start, heuristic[start])
    predecessor = {}
    predecessor[start] = None

    path = ""

    while not searchFrontier.empty():
        current = searchFrontier.get()

        path += current + " "

        if current == goal:
            break

        for next in graph.neighbors(current):
            if next not in predecessor:
                priority = heuristic[next]
                searchFrontier.put(next, priority)
                predecessor[next] = current

    return path