python - 如何在 A 星算法中添加更多的起点和目标点?

标签 python shortest-path path-finding a-star

下面是A*算法的Python代码A*它在 2D 简单环境中查找从起点 (0,0)目标点 (4,5) 的路径。我可以在同一环境中添加另一个起点和目标点(即同一环境上的 2 个起点和 2 个目标点)并一起查看它们的路径并查看算法对于起点和目标点以外的行为如何?我可以为我的代码或想法获得任何帮助吗?

下图为操作过程

Multiple points

下面是 A* 算法的 Python 代码

from __future__ import print_function
import random

grid = [[0, 1, 0, 0, 0, 0],
        [0, 1, 0, 0, 0, 0],#0 are free path whereas 1's are obstacles
        [0, 1, 0, 0, 0, 0],
        [0, 1, 0, 0, 1, 0],
        [0, 0, 0, 0, 1, 0]]


init = [0, 0]
goal = [len(grid)-1, len(grid[0])-1] #all coordinates are given in format [y,x] 
cost = 1


#the cost map which pushes the path closer to the goal
heuristic = [[0 for row in range(len(grid[0]))] for col in range(len(grid))]
for i in range(len(grid)):    
    for j in range(len(grid[0])):            
        heuristic[i][j] = abs(i - goal[0]) + abs(j - goal[1])




#the actions we can take
delta = [[-1, 0 ], # go up
         [ 0, -1], # go left
         [ 1, 0 ], # go down
         [ 0, 1 ]] # go right


#function to search the path
def search(grid,init,goal,cost,heuristic):

    closed = [[0 for col in range(len(grid[0]))] for row in range(len(grid))]# the referrence grid
    closed[init[0]][init[1]] = 1
    action = [[0 for col in range(len(grid[0]))] for row in range(len(grid))]#the action grid

    x = init[0]
    y = init[1]
    g = 0

    f = g + heuristic[init[0]][init[0]]
    cell = [[f, g, x, y]]

    found = False  # flag that is set when search is complete
    resign = False # flag set if we can't find expand

    while not found and not resign:
        if len(cell) == 0:
            resign = True
            return "FAIL"
        else:
            cell.sort()#to choose the least costliest action so as to move closer to the goal
            cell.reverse()
            next = cell.pop()
            x = next[2]
            y = next[3]
            g = next[1]
            f = next[0]


            if x == goal[0] and y == goal[1]:
                found = True
            else:
                for i in range(len(delta)):#to try out different valid actions
                    x2 = x + delta[i][0]
                    y2 = y + delta[i][1]
                    if x2 >= 0 and x2 < len(grid) and y2 >=0 and y2 < len(grid[0]):
                        if closed[x2][y2] == 0 and grid[x2][y2] == 0:
                            g2 = g + cost
                            f2 = g2 + heuristic[x2][y2]
                            cell.append([f2, g2, x2, y2])
                            closed[x2][y2] = 1
                            action[x2][y2] = i
    invpath = []
    x = goal[0]
    y = goal[1]
    invpath.append([x, y])#we get the reverse path from here
    while x != init[0] or y != init[1]:
        x2 = x - delta[action[x][y]][0]
        y2 = y - delta[action[x][y]][1]
        x = x2
        y = y2
        invpath.append([x, y])

    path = []
    for i in range(len(invpath)):
        path.append(invpath[len(invpath) - 1 - i])
    print("ACTION MAP")
    for i in range(len(action)):
        print(action[i])

    return path

a = search(grid,init,goal,cost,heuristic)
for i in range(len(a)):
    print(a[i]) 

最佳答案

您可以在列表中包含(初始,目标)坐标对的元组,您可以使用相同的网格对其进行迭代。

init_goal_pairs = [(init_0,goal_0),(init_1,goal_1), (init_2,goal_2), (init_3,goal_3)]

然后,你可以这样做

for init, goal in init_goal_pairs
    path = search(grid,init,goal,cost,heuristic)
    print(path)

关于python - 如何在 A 星算法中添加更多的起点和目标点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59509383/

相关文章:

python - 填充现有 numpy 数组和创建新数组之间的性能差异

python - 如何选择两列来绘制数据框?

python - 带数组的傅里叶变换

algorithm - 为 A* 算法寻找启发式方法有哪些好方法?

c++ - 贝尔曼福特邻接矩阵的单源最短路径未检测到负循环

c++ - 了解 FRINGE 搜索伪代码

python - 继续阅读NUL

sql - 如何在飞行路线表中获得最短路径

algorithm - "Face-up"纸牌算法

c++ - 没有对角线移动的 Dijkstra 算法