コード例 #1
0
ファイル: a_star.py プロジェクト: GeoVa19/ai-examples
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
ファイル: bestfs.py プロジェクト: GeoVa19/ai-examples
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