填写列表以通过 python 使用 A*(a-star)算法

问题描述 投票:0回答:1

我想使用 A* 算法在具有非加权节点的地图中找到最快的方法。

我的地图是一个嵌套列表,仅包含 5 个不同的值: 0 代表墙壁,2、3 和 4 代表不同类型的方块 1 代表空旷的空间(你只能在空旷的空间“行走”)

这是地图示例:

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0]
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0]
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0]
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0]
[0, 0, 0, 0, 1, 1, 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 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

注意:* 项是源,列表中的“$$$”项是目的地。 (我想从*转到“$$$”)

我阅读了这个网站对 A* 算法的解释,但我仍然不确定如何编写我的 python 函数。

从网站上,我能够理解的是,我必须逐步(从源点开始)将我的块列表转换为方向;左、右、上、下(如果块值为 1)。然后,一旦到达目标块,我应该停下来并按照指示返回源块;这会给我最好的路径。

目标: 我想创建一个输入嵌套列表的函数:

  1. 从源点(*)开始,在列表中填写方向。
  2. 然后,一旦到达目的地点,就按照之前填写的方向从目的地点返回源点
  3. 返回包含找到的路径的点列表

注意:我是寻路算法的新手(但不是Python),所以请给我一个可读的答案。

python path-finding a-star
1个回答
0
投票

尽管我认为这可能不是做你想做的事情的最佳方式,但我仍然可以尝试回答你的问题: 给定一个包含地图的嵌套列表,您希望使用 A-star 算法找到两点之间的最佳路径,因为您知道只能在值为 1 的块上“行走”

你还想使用“方向”,我猜这可能是四个基本方向(上、下、左、右)

了解了这一切,我认为您希望该功能如下:

  1. 从源点开始,递归地将附近每一个值为1的项目替换为“方向”(上、下、左、右),这样你可以从地图上的任何点沿着箭头走,它仍然会回到源点。到达目的地就停下来
  2. 从目的地点出发,按照箭头返回源点,将每一项替换为某个符号(可能是星号“*”)

注意:告诉我您是否希望我更改目标列表中的任何内容

在开始程序之前,这是您给出的列表(您可以更改它)

mapList = [ # change this nested list to whatever your map is
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
    [0, 0, 1, 1, 1, 1, 1, 1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0, 0],
    [0, 0, 1, 1, 3, 3, 3, 3, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 0, 0],
    [0, 0, 1, 1, 3, 3, 3, 3, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 0, 0],
    [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 0, 0],
    [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 0, 0],
    [0, 0, 1, 1, 3, 3, 3, 3, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 0, 0],
    [0, 0, 1, 1, 3, 3, 3, 3, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 0, 0],
    [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 0, 0],
    [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
]
  1. 将每个值为 1 的项目替换为“方向”:
sourceRow = 6 # value of the source row, change it according to your needs
sourceColumn = 2 # same thing
sourceSymbol = "Source" # You can change this to whatever you want
mapList[sourceRow][sourceColumn] = sourceSymbol

destRow = 2
destColumn = 15
destinationSymbol = "$$$" # same
mapList[destRow][destColumn] = destinationSymbol

up = "^" # the signs don't really matter
down = "v"
right = ">"
left = "<"


tempList = [(sourceRow, sourceColumn)]


def fillWithDirections(tempPoint: tuple):
    # tempPoint[0] is the row index, tempPoint[1] is the column index

    # List of all the positions to test
    positionToTestList = [
        (tempPoint[0]+1, tempPoint[1]), # this is the point directly below tempPoint
        (tempPoint[0]-1, tempPoint[1]), # this is the point directly above tempPoint
        (tempPoint[0], tempPoint[1]+1), # this is the point directly to the right of tempPoint
        (tempPoint[0], tempPoint[1]-1), # this is the point directly to the left of tempPoint
    ]

    for posTuple in positionToTestList:
        # looping through the 4 possible positions (up, down, left, right) and test if we can fill it with a "direction"
        
        if mapList[posTuple[0]][posTuple[1]] == destinationSymbol:
            # we reached the destination !!!, so end the program
            return 1

        if mapList[posTuple[0]][posTuple[1]] == 1:
            # This means that the current item is "fillable" with "directions"
            
            hasBeenFilled = True
            # fill with up, down, left, right
            if posTuple[0] == tempPoint[0] + 1:
                # means we are currently "below" the tempPoint, so fill with "direction up"
                mapList[posTuple[0]][posTuple[1]] = up
            elif posTuple[0] == tempPoint[0] - 1:
                # means we are currently "above" the tempPoint, so fill with "direction down"
                mapList[posTuple[0]][posTuple[1]] = down
            elif posTuple[1] == tempPoint[1] + 1:
                # means we are currently "to the right" the tempPoint, so fill with "direction left"
                mapList[posTuple[0]][posTuple[1]] = left
            elif posTuple[1] == tempPoint[1] - 1:
                # means we are currently "to the left" the tempPoint, so fill with "direction right"
                mapList[posTuple[0]][posTuple[1]] = right
            else:
                hasBeenFilled = False
            
            if hasBeenFilled:
                # once the we filled the point with the corresponding "direction", let's add it to tempList
                # so that we will eventually test the point
                tempList.append((posTuple[0], posTuple[1]))




while tempList: # run this while there are still points to test in tempList

    tempPoint = tempList.pop(0) # take out the first item of tempList

    # if the function returned anything, it means that it hit the destination point
    if fillWithDirections(tempPoint):
        # if so, break out of the while loop as it doesn't matter to continue
        break

注意:我们现在已经用“方向”填充了嵌套列表,直到到达目的地点

  1. 沿着箭头从目的地回到源点,并将路径上的每个项目替换为星号“*”
# This is the list of all the positions surrounding the destination point
positionOfDestToTestList = [
    (destRow+1, destColumn),
    (destRow-1, destColumn),
    (destRow, destColumn+1),
    (destRow, destColumn-1),
]


tempPosTuple = None
# This is the list of all the "directions" possible (">", "<", ...)
listOfSigns = [up, down, right, left]

for posToTest in positionOfDestToTestList:
    # If one of the four surrounding items is a "direction"
    if mapList[posToTest[0]][posToTest[1]] in listOfSigns:
        # Then set tempPosTuple equal to the corresponding surrounding point
        tempPosTuple = posToTest

while tempPosTuple:
    currentItem = mapList[tempPosTuple[0]][tempPosTuple[1]]

    # If we reached the source point
    if currentItem == sourceSymbol:
        tempPosTuple = None
        break
    
    # Because the point is on the path, replace the "direction" by a star "*"
    mapList[tempPosTuple[0]][tempPosTuple[1]] = '*'
    # Because the point is on the path, replace the "direction" by a star "*"
    mapList[tempPosTuple[0]][tempPosTuple[1]] = '*'

    # change tempPosTuple according to the "direction" it is (ex: column + 1 if arrow is to the right)
    if currentItem == up:
        tempPosTuple = (tempPosTuple[0]-1, tempPosTuple[1])
    elif currentItem == down:
        tempPosTuple = (tempPosTuple[0]+1, tempPosTuple[1])
    elif currentItem == left:
        tempPosTuple = (tempPosTuple[0], tempPosTuple[1]-1)
    elif currentItem == right:
        tempPosTuple = (tempPosTuple[0], tempPosTuple[1]+1)


应该就是这样!

您现在拥有一张包含星星“*”的地图,表示最快路径

我希望这有帮助

© www.soinside.com 2019 - 2024. All rights reserved.