使用浮点作为下标

2024-04-25 10:23:37 发布

您现在位置:Python中文网/ 问答频道 /正文

在尝试实现A*搜索导航时,我遇到了一个问题。我的代码在这里:

def shortest_path(M,start,goal):
    #def aStar(graph, current, end):
    openSet = set()
    openHeap = []
    closedSet = set()
    cameFrom = {}

    openSet.add(start)
    print("openSet=", openSet)
    openHeap.append((0, start))
    gScore = {}
    gScore[start] = 0
    print("gScore: ",gScore)
    fScore = {}
    #fscore = {start:heuristic(M.intersections[start], 
               M.intersections[goal])}
    fScore[start] = {start:heuristic(M.intersections[start], 
                     M.intersections[goal])}
    fscore = fScore[start]
    print("fScore: ", fscore)
    while openSet:
        print("Entered While Loop")
        current = heapq.heappop(openHeap)[1]
        print("Current: ", current)
        if current == goal:
            print("current == goal")
            return reconstruct_path(cameFrom, current)
        openSet.remove(current)
        closedSet.add(current)

        print("M.intersections[current]: ", M.intersections[start])
        for neighbor in M.intersections[current]: #tile = neighbor 
            print("for neighbor in M.intersections[current]: ", neighbor)
            if neighbor in closedSet:
                print("neighbor in closedSet")
                continue

                #neighborH = (abs(goal[0] - neighbor[0])+abs(goal[0]-neighbor[0]))*10

            if neighbor not in openSet:
                openSet.add(neighbor)
                print("OpenSet inside if loop =",openSet )
                #if neighbor  not in openSet:
                    #openSet.add(neighbor )
                    #heapq.heappush(openHeap, (neighborH, neighbor ))
                #tile.parent = start
            print("Check Point")
            print("gscore[current]: ", gScore[current])
            print("M.intersections[current]=", M.intersections[current])
            print("M.intersections[neighbor]=", neighbor)
            tentative_gScore = gScore[current] + distance_to(M.intersections[current],neighbor)
            if tentative_gScore >= gScore[neighbor]:
                continue
            cameFrom[neighbor] = current
            gScore[neighbor] = tentative_gScore
            fScore[neighbor] = gScore[neighbor] + heuristic(M.intersections[neighbor], M.intersections[goal])

    print("shortest path called")
    return []

我这里有个错误:

 tentative_gScore = gScore[current] + distance_to(M.intersections[current],neighbor)

'float' object is not subscriptable.

我该怎么处理呢。我知道为什么它给了我这个错误,但我需要一些解决办法,可以帮助我前进。任何新想法都是受欢迎的。你知道吗


Tags: inaddifcurrentstartprintgoalintersections