【问题标题】:How to accelerate my python looping process for A-star pathfinding?如何为 A-star 寻路加速我的 python 循环过程?
【发布时间】: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


【解决方案1】:

一些优化会是这样的:

def A_star(h,c,dx,dy,u,s_id,e_id,Op,Cl,Prt,CC,o,ht,w):
    Op.append(s_id) 
    continu = e_id not in Op
    while continu : 
        if Op == []:
            break
        candidate = {i:CC[i] for i in Op }  # HERE
        o = min(candidate.values())  # HERE
        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)
                if p==e_id:
                   continu=False # this will stop while from looping!
                Prt[p]=o  # HERE
                CC[p]=F(p,o,h,u,w,c,dx,e_id,ht,CC)  # HERE
            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 :  
                    Prt[p]=o  # HERE
                    CC[id]= F(p,o,h,u,w,c,dx,e_id,ht,CC)  # HERE
    return Prt

使用dict.update 不是为了设置一项!请改用dict[key]=value

另一个帮助是检查p == e_id 何时添加,如果它添加到Opwhile 将停止!

【讨论】:

    猜你喜欢
    • 2011-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多