【发布时间】:2015-07-28 21:48:08
【问题描述】:
我尝试基于 tile-graph(图像栅格数据作为图形)制作一个 A-star Pathfinding 程序,其中每个像素值表示为成本。到目前为止,这是我的 A-star 函数脚本:
def A_star(h,c,dx,dy,u,s_id,e_id,Op,Cl,Prt,CC,o,ht,w):
Op.append(s_id)
while e_id not in Op :
if Op == [ ] :
break
candidate = { }
for i in Op :
d = {i : CC[i]}
candidate.update(d)
o = min(candidate, key=candidate.get)
Cl.append(o)
Op.remove(o)
adjacent_list = adjacent_cell(o,dx,dy )
for p in adjacent_list :
if p in Cl:
adjacent_list = filter(lambda i: i != p, adjacent_list)
elif p not in Op :
Op.append(p)
d = {p : o }
Prt.update(d)
d = {p : F(p,o,h,u,w,c,dx,e_id,ht,CC)}
CC.update(d)
elif id in Op :
f1 = F(p,o,h,u,w,c,dx,e_id,ht,CC)
f2 = F(p,Prt[p],h,u,w,c,dx,e_id,ht,CC)
if f1 < f2 :
d = {p : o }
Prt.update(d)
d = {id : F(p,o,h,u,w,c,dx,e_id,ht,CC)}
CC.update(d)
return Prt
我的示例代码和输入数据可以在这里下载 https://drive.google.com/open?id=0B2zjTfTukbMvaV9BalU4ZGx4MjQ
如果我使用 DEM 的小分辨率 25x29 ( srtm1.tif ),则此程序运行良好。但是如果我尝试使用大分辨率的 DEM,例如 1228 x 972 ( dem,asc )。这个程序花了很长时间来计算一个完整的细胞。我来宾说问题出在我尝试使用循环进行迭代的循环进度上(第 110 行,A-star 函数)。
while e_id not in Op :
是否有任何解决方案可以让我的代码运行得更快? 各种帮助、建议、评论、解决方案将不胜感激..
【问题讨论】:
-
而且这段代码还需要gdal和numpy库
标签: python-2.7 optimization nested-loops a-star