【问题标题】:what is the fastest way of a typical 2d array path finding?典型的二维阵列寻路的最快方法是什么?
【发布时间】:2012-11-29 04:52:17
【问题描述】:

一个预期的结果是这样的。

0 0 0 1 0 * * * 0 0 0 0
0 0 0 0 1 * 1 * * 0 0 0
0 0 1 1 1 * 1 0 * 0 0 0
0 * * 0 1 * 1 0 * 0 0 0
0 * 1 1 1 * 1 0 * * 0 0
0 * 0 1 1 * 1 0 0 * 0 0
0 * 0 0 1 * 1 0 0 * 0 0
0 * * * * * 1 0 0 * * 0
0 0 0 0 0 0 1 0 0 0 * 0
0 0 0 0 0 0 1 0 0 0 * 0
0 0 0 0 0 0 1 0 0 0 * 0
0 0 0 0 0 0 1 0 0 0 * *

您只能在 4 个方向上行走,没有 45 度方向,我使用 A*,我更改了部分原始算法以更适合我的情况。

这是我的 python 代码:

我运行了 1000 次。

成本为1.4s~1.5s

def astar(m,startp,endp):
    w,h = 12,12
    sx,sy = startp
    ex,ey = endp
    #[parent node, x, y,g,f]
    node = [None,sx,sy,0,abs(ex-sx)+abs(ey-sy)] 
    closeList = [node]
    createdList = {}
    createdList[sy*w+sx] = node
    k=0
    while(closeList):
        node = closeList.pop(0)
        x = node[1]
        y = node[2]
        l = node[3]+1
        k+=1
        #find neighbours 
        #make the path not too strange
        if k&1:
            neighbours = ((x,y+1),(x,y-1),(x+1,y),(x-1,y))
        else:
            neighbours = ((x+1,y),(x-1,y),(x,y+1),(x,y-1))
        for nx,ny in neighbours:
            if nx==ex and ny==ey:
                path = [(ex,ey)]
                while node:
                    path.append((node[1],node[2]))
                    node = node[0]
                return list(reversed(path))            
            if 0<=nx<w and 0<=ny<h and m[ny][nx]==0:
                if ny*w+nx not in createdList:
                    nn = (node,nx,ny,l,l+abs(nx-ex)+abs(ny-ey))
                    createdList[ny*w+nx] = nn
                    #adding to closelist ,using binary heap
                    nni = len(closeList)
                    closeList.append(nn)
                    while nni:
                        i = (nni-1)>>1
                        if closeList[i][4]>nn[4]:
                            closeList[i],closeList[nni] = nn,closeList[i]
                            nni = i
                        else:
                            break


    return 'not found'

m = ((0,0,0,1,0,0,0,0,0,0,0,0),
     (0,0,0,0,1,0,1,0,0,0,0,0),
     (0,0,1,1,1,0,1,0,0,0,0,0),
     (0,0,0,0,1,0,1,0,0,0,0,0),
     (0,0,1,1,1,0,1,0,0,0,0,0),
     (0,0,0,1,1,0,1,0,0,0,0,0),
     (0,0,0,0,1,0,1,0,0,0,0,0),
     (0,0,0,0,0,0,1,0,0,0,0,0),
     (0,0,0,0,0,0,1,0,0,0,0,0),
     (0,0,0,0,0,0,1,0,0,0,0,0),
     (0,0,0,0,0,0,1,0,0,0,0,0),
     (0,0,0,0,0,0,1,0,0,0,0,0)
     )

t1 = time.time()
for i in range(1000):
    result = astar(m,(2,3),(11,11))
print(time.time()-t1) 
cm = [list(x[:]) for x in m]


if isinstance(result, list):
    for y in range(len(m)):
        my = m[y]
        for x in range(len(my)):
            for px,py in result:
                if px==x and py ==y:
                    cm[y][x] = '*'

for my in cm:
    print(' '.join([str(x) for x in my]))

exit(0)

告诉我你现在是否知道更快或最快的方法。

【问题讨论】:

  • 不是一个答案,但您可能想将您的性能与以下内容进行比较:networkx.lanl.gov 最短路径的实现。

标签: performance algorithm python-3.x


【解决方案1】:

A* 算法对于已知图来说非常快(所有边都是已知的,您可以使用一些可接受的启发式方法估计到目标的距离)。

A* 算法有一些改进,以不太理想为代价使其更快。最常见的是A*-Epsilon (AKA bounded A*)。这个想法是允许算法开发 (1+epsilon)*MIN 的节点(其中常规 A* 仅开发 MIN)。结果(当然取决于 epsilon 值)通常是更快的解决方案,但找到的路径最多为(1+epsilon) * OPTIMAL。


另一种可能的优化是从一端执行 A* - 从另一端(“出口”)同时执行 BFS。这种技术称为 bi-directional search - 当问题具有单一最终状态时,通常是提高未加权图性能的好方法。我曾经在this thread中尝试过解释双向搜索的原理

【讨论】:

    猜你喜欢
    • 2012-04-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-21
    • 2016-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多