Performance 典型二维阵列路径查找的最快方法是什么?
预期的结果是这样的Performance 典型二维阵列路径查找的最快方法是什么?,performance,algorithm,python-3.x,Performance,Algorithm,Python 3.x,预期的结果是这样的 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 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度的方向,我用的是*,我改变了原算法的一部分,使之更适合我的情况
以下是我的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)
def astar(m、startp、endp):
w、 h=12,12
sx,sy=startp
ex,ey=endp
#[父节点,x,y,g,f]
node=[None,sx,sy,0,abs(exsx)+abs(ey-sy)]
closeList=[节点]
createdList={}
createdList[sy*w+sx]=节点
k=0
while(关闭列表):
节点=closeList.pop(0)
x=节点[1]
y=节点[2]
l=节点[3]+1
k+=1
#找邻居
#让这条路不要太奇怪
如果k&1:
邻居=((x,y+1),(x,y-1),(x+1,y),(x-1,y))
其他:
邻域=((x+1,y)、(x-1,y)、(x,y+1)、(x,y-1))
对于邻居的nx、ny:
如果nx==ex和ny==ey:
路径=[(ex,ey)]
while节点:
append((节点[1],节点[2]))
节点=节点[0]
返回列表(反向(路径))
如果0A*算法对于一个已知的图来说是非常快的(所有边都是已知的,您可以使用一些可接受的启发式算法估计到目标的距离)
A*算法有一些改进,使其更快,但代价是不太优化。最常见的是。其思想是允许算法开发(1+epsilon)*MIN
(其中常规A*只开发MIN)的节点。结果(当然取决于epsilon值)通常是一个更快的解决方案,但找到的路径最多是(1+epsilon)*最优的
另一种可能的优化是从一端做一个*并且从另一端(出口)同时做一个BFS。这种技术被称为,当问题只有一个最终状态时,它通常是提高未加权图性能的好方法。我曾在中解释过一次双向搜索的原理,这不是一个答案,而是一个你可能想要比较你的性能的东西:最短路径的实现。