查看完整版本: A*算法理论与实践

wice3 2007-4-5 18:47

A*算法理论与实践

[align=center][font=Times New Roman][b][size=12pt][/size][/b][b][font=楷体_GB2312][size=14pt][/size][/font][/b][/font][b][font=楷体_GB2312][size=14pt]启发式搜索算法引论[/size][/font][/b][b][font=楷体_GB2312][size=14pt][font=Times New Roman]------A*[/font][/size][/font][/b][b][font=楷体_GB2312][size=14pt]算法理论与实践[/size][/font][/b][b][font=楷体_GB2312][size=14pt][/size][/font][/b][/align][b][font=楷体_GB2312][size=16pt]            [/size][/font][/b][b][font=Batang][size=12pt]EmilMatthew ([email=EmilMatthew@126.com][color=#0000ff]EmilMatthew@12[font=宋体]6.[/font]com[/color][/email])       [/size][/font][/b][b][font=Batang][size=12pt][/size][/font][/b]
[b][font=楷体_GB2312][size=12pt][  [/size][/font][/b][b][font=楷体_GB2312][size=12pt]类别  ]算法实践 ,人工智能[/size][/font][/b]
[b][font=楷体_GB2312][size=12pt][[/size][/font][/b][b][font=楷体_GB2312][size=12pt]推荐指数]★★★★★[/size][/font][/b]
[b][font=楷体_GB2312][size=12pt][  [/size][/font][/b][b][font=楷体_GB2312][size=12pt]摘要  ]本文介绍了启发式算法中一种重要而有效的算法[/size][/font][/b][b][font=楷体_GB2312][size=12pt][font=Times New Roman]------[/font][/size][/font][/b][b][font=楷体_GB2312][size=12pt]A*[/size][/font][/b][b][font=楷体_GB2312][size=12pt]算法的理论,并给出了寻路问题的交互式实现。[/size][/font][/b][b][font=楷体_GB2312][size=12pt][/size][/font][/b]
[b][font=楷体_GB2312][size=12pt][ [/size][/font][/b][b][font=楷体_GB2312][size=12pt]关键词 ][/size][/font][/b][size=3][font=Times New Roman][b][font=楷体_GB2312][size=12pt]A*[/size][/font][/b][/font][/size][b][font=楷体_GB2312][size=12pt],启发式算法,最优路径,交互式,[/size][/font][/b][b][font=楷体_GB2312][size=12pt][font=Times New Roman]AS2[/font][/size][/font][/b]
[b][font=Arial][size=14pt][/size][/font][/b]
[b][font=Arial][size=14pt]Introduction to Heuristics Search------[/size][/font][/b]
[b][font=Arial][size=14pt]A* Algorithm Principle and Practice[/size][/font][/b]
[b][font=Arial][size=12pt][Classify] Algorithm [/size][/font][/b][b][font=Arial][size=12pt]Implementation ,  Artificial Intelligence[/size][/font][/b][b][font=Arial][size=12pt][/size][/font][/b]
[b][font=Arial][size=12pt][ [/size][/font][/b][b][font=Arial][size=12pt][/size][/font][/b][b][font=Arial][size=12pt]Level[/size][/font][/b][b][font=Arial][size=12pt]] [/size][/font][/b][b][size=12pt]★★★★★[/size][/b][b][font=Arial][size=12pt][/size][/font][/b]
[b][font=Arial][size=12pt][Abstract][/size][/font][/b][b][font=Arial][size=12pt] This article introduces an important and effective heuristics algorithm ------ A* algorithm with its theory and also gives out its interactive implementation of path finding problem.[/size][/font][/b]
[b][font=Arial][size=12pt][Key Words][/size][/font][/b][size=3][b][font=Arial][size=12pt]A* , Heuristics Algorithm , Best Path Finding , Interactive , AS2[/size][/font][/b][/size][b][font=Arial][size=12pt][/size][/font][/b]
[b][size=12pt][font=Times New Roman][/font][/size][/b]
[b][font=楷体_GB2312][size=12pt][font=Times New Roman][/font][/size][/font][/b]
[b][font=楷体_GB2312][size=12pt][1[/size][/font][/b][b][font=楷体_GB2312][size=12pt]历史回顾][/size][/font][/b]
[b][font=楷体_GB2312][size=12pt][font=Times New Roman]       P. E. Hart , N. J. Nilsson [/font][/size][/font][/b][b][font=楷体_GB2312][size=12pt]和[/size][/font][/b][b][font=楷体_GB2312][size=12pt][font=Times New Roman]B. Raphael[/font][/size][/font][/b][b][font=楷体_GB2312][size=12pt]共同发表了一篇在启发式搜索方面有深远影响力的论文:[/size][/font][/b][b][font=楷体_GB2312][size=12pt][/size][/font][/b]
[b][font=楷体_GB2312][size=12pt][font=Times New Roman]“P. E. Hart, N. J. Nilsson, and B. Raphael. A formal basis for the heuristic determination of minimum cost paths in graphs. IEEE Trans. Syst. Sci. and Cybernetics, SSC-4(2):100-107, 1968”[/font][/size][/font][/b][b][font=楷体_GB2312][size=12pt]。从此,一种精巧、高效的算法[/size][/font][/b][b][font=楷体_GB2312][size=12pt][font=Times New Roman]------A*[/font][/size][/font][/b][b][font=楷体_GB2312][size=12pt]算法横空出世了,并在相关领域得到了广泛的应用。[/size][/font][/b]
[b][font=楷体_GB2312][size=12pt]               [img]http://p.blog.csdn.net/images/p_blog_csdn_net/EmilMatthew/0632peter_hart_3.jpg[/img][font=Times New Roman]        [img=120,167]http://p.blog.csdn.net/images/p_blog_csdn_net/EmilMatthew/0632NilsJNilsson.jpg[/img][/font][/size][/font][/b][font=宋体][size=3][color=#000000] [/color][/size][/font]
                                  [color=#3366ff][font=楷体_GB2312][size=10][size=3][font=Times New Roman]Peter E. Hart                 Nils J. Nilsson[/font][/size][/size][/font][/color]

[b][font=楷体_GB2312][size=12pt][2[/size][/font][/b][b][font=楷体_GB2312][size=12pt]从DFS和BFS中来][/size][/font][/b]
[b][font=楷体_GB2312][size=12pt]   A*[/size][/font][/b][b][font=楷体_GB2312][size=12pt]算法的思想来源并不是什么高深莫测的东西,[/size][/font][/b][b][font=楷体_GB2312][size=12pt]事实上,它与我们所熟悉的另外两种搜索策略:[/size][/font][/b][b][font=楷体_GB2312][size=12pt][font=Times New Roman]DFS[/font][/size][/font][/b][b][font=楷体_GB2312][size=12pt](深度优先[/size][/font][/b][b][font=楷体_GB2312][size=12pt][font=Times New Roman]Deep First Search[/font][/size][/font][/b][b][font=楷体_GB2312][size=12pt])和[/size][/font][/b][b][font=楷体_GB2312][size=12pt][font=Times New Roman] BFS([/font][/size][/font][/b][b][font=楷体_GB2312][size=12pt]广度优先[/size][/font][/b][b][font=楷体_GB2312][size=12pt][font=Times New Roman]Breadth First Search)[/font][/size][/font][/b][b][font=楷体_GB2312][size=12pt]有着自然而紧密的联系。[/size][/font][/b][b][font=楷体_GB2312][size=12pt][/size][/font][/b]
[b][font=楷体_GB2312][size=12pt][font=Times New Roman]       [/font][/size][/font][/b][b][font=楷体_GB2312][size=12pt]首先要提一下搜索树的概念,一个可以搜索出某个可行解的问题,如“农夫、白菜、羊、狼”和“八皇后”等,虽然从表面上看上去和“树”这种结构无关,但是整个搜索过程中的可能试探点所行成的搜索空间总可以对应到一颗搜索树上去。所以,将各类形式上不同的搜索问题抽象并统一成为搜索树的形式,为算法的设计与分析带来巨大的方便。[/size][/font][/b][b][font=楷体_GB2312][size=12pt][/size][/font][/b]
[b][font=楷体_GB2312][size=12pt][font=Times New Roman][/font][/size][/font][/b]
[b][font=楷体_GB2312][size=12pt]下面,让我们通过两个[/size][/font][/b][b][font=楷体_GB2312][size=12pt][font=Times New Roman]Flash[/font][/size][/font][/b][b][font=楷体_GB2312][size=12pt]演示来回顾一下[/size][/font][/b][b][font=楷体_GB2312][size=12pt][font=Times New Roman]DFS[/font][/size][/font][/b][b][font=楷体_GB2312][size=12pt]和[/size][/font][/b][b][font=楷体_GB2312][size=12pt][font=Times New Roman]BFS[/font][/size][/font][/b][b][font=楷体_GB2312][size=12pt]的算法思想:[/size][/font][/b][b][font=楷体_GB2312][size=12pt][/size][/font][/b]
[b][font=楷体_GB2312][size=12pt][font=Times New Roman]       DFS[/font][/size][/font][/b][b][font=楷体_GB2312][size=12pt]算法思想演示[/size][/font][/b][b][font=楷体_GB2312][size=12pt][font=Times New Roman]:[/font][/size][/font][/b]
[b][font=楷体_GB2312][size=12pt][font=Times New Roman]                       [/font][/size][/font][/b][b][font=楷体_GB2312][size=12pt][font=Times New Roman] [/font][/size][/font][/b]
[b][font=楷体_GB2312][size=12pt][font=Times New Roman]      [/font][/size][/font][/b]
BFS算法思想演示
                        
      

      重点要在这里说明是:
       DFS和BFS在展开子结点时均属于盲目型搜索,也就是说,它不会选择哪个结点在下一次搜索中更优而去跳转到该结点进行下一步的搜索。在运气不好的情形中,均需要试探完整个解集空间, 显然,只能适用于问题规模不大的搜索问题中。
       那么,作为启发式算法中的A*算法,又比它们高效在哪里呢?
       首先要来谈一下什么是启发式算法。所谓启发式搜索,与DFS和BFS这类盲目型搜索最大的不同,就在于当前搜索结点往下选择下一步结点时,可以通过一个启发函数来进行选择,选择代价最少的结点作为下一步搜索结点而跳转其上(遇到有一个以上代价最少的结点,不妨选距离当前搜索点最近一次展开的搜索点进行下一步搜索)。一个经过仔细设计的启发函数,往往在很快的时间内就可得到一个搜索问题的最优解,对于NP问题,亦可在多项式时间内得到一个较优解。
       A*算法,作为启发式算法中很重要的一种,被广泛应用在最优路径求解和一些策略设计的问题中。而A*算法最为核心的部分,就在于它的一个估值函数的设计上:
[align=center]f(n)=g(n)+h(n)[/align]       其中f(n)是每个可能试探点的估值,它有两部分组成:一部分为g(n),它表示从起始搜索点到当前点的代价(通常用某结点在搜索树中的深度来表示)。另一部分,即h(n),它表示启发式搜索中最为重要的一部分,即当前结点到目标结点的估值,h(n)设计的好坏,直接影响着具有此种启发式函数的启发式算法的是否能称为A*算法。

       一种具有f(n)=g(n)+h(n)策略的启发式算法能成为A*算法的充分条件是:
1)      搜索树上存在着从起始点到终了点的最优路径。
2)      问题域是有限的。
       3)所有结点的子结点的搜索代价值>0。
       4)h(n)=<h*(n) (h*(n)为实际问题的代价值)。
       当此四个条件都满足时,一个具有f(n)=g(n)+h(n)策略的启发式算法能成为A*算法,并一定能找到最优解。([1]P89给出了相关的证明)

对于一个搜索问题,显然,条件1,2,3都是很容易满足的,而
条件4): h(n)<=h*(n)是需要精心设计的,由于h*(n)显然是无法知道的,
所以,一个满足条件4)的启发策略h(n)就来的难能可贵了。不过,对于图的最优路径搜索和八数码问题,有些相关策略h(n)不仅很好理解,而且已经在理论上证明是满足条件4)的,从而为这个算法的推广起到了决定性的作用。不过h(n)距离h*(n)的呈度不能过大,否则h(n)就没有过强的区分能力,算法效率并不会很高。对一个好的h(n)的评价是:h(n)在h*(n)的下界之下,并且尽量接近h*(n).

       当然,估值函数的设计也就就仅仅是f(n)=g(n)+h(n)一种,另外的估值函数“变种”如:f(n)=w*g(n)+(1-w)*h(n) ,f(n)=g(n)+h(n)+h(n-1)针对不同的具体问题亦会有不同的效果。

[3A*算法的核心过程]
让我们先来通过两个Flash演示,来看一下A*算法在具体应用时的搜索树是如何展开的。
            
                                                A* Search搜索树1


                                            
[align=center]A* Search搜索树2[/align]
通过演示,我们可以看出:A*算法最为核心的过程,就在每次选择下一个当前搜索点时,是从所有已探知的但未搜索过点中(可能是不同层,亦可不在同一条支路上),选取f值最小的结点进行展开。而所有“已探知的但未搜索过点”可以通过一个按f值升序的队列(即优先队列)进行排列。这样,在整体的搜索过程中,只要按照类似广度优先的算法框架,从优先队列中弹出队首元素(f值),对其可能子结点计算g、h和f值,直到优先队列为空(无解)或找到终止点为止。
       A*算法与广度优先和深度优先的联系就在于,当g(n)=0时,该算法类似于DFS,当h(n)=0时,该算法类似于BFS , 这一点,可以通过上面的A*搜索树的具体过程中将h(n)设为0或将g(n)设为0而得到。

       A*算法实现框架:
       重要数据解释:
       Open Table  :存放所有已探知的但未搜索过点的优先队列。
       Closed Table :存在搜索过的点的数组,提取最优路径时有用。
Start Node   :起始点。
Target Node  :终止点。
C Node     :当前点。

       算法框架如下:
1.Init start node , add it to open table
While not reach target node && open table is unNull
       2.a) Get the head node in open table->c node
       2.b) Finding all c node’s possible child node.
       2.c) Calculate each child node’s f value.
       2.d) Remove c node from open table , add it to closed table.
        2.e) Add all c node’s child nodes to open table in an undescend sequence.
Wend
3.Ouput Search Result
      
       算法的实现部分参附录1(C Version),附录2(AS2 Version)
       提取最优路径的算法并不复杂,虽然在CLOSE表中会有许多无效的搜索点,但是最优路径上各结点的下标一定是按照CLOSE表中下标的升序排列的。因此,只要在CLOSE表中,将下标从终止点向起始点移动,若CLOSE[i+1]与CLOSE没有关联,则剔除CLOSE。

[3A*算法在路径最优问题的交互式示例]
       路径最优问题,简单来说,就是在两个结点之间找一条最短路径。有的朋友不禁要问,这个问题不是已经有Dijkstra算法可以解决了吗?此话不假,但是不要忘了Dijkstra算法的复杂度是O(n^2),一旦结点很多并且需要实时计算的话,Dijkstra就无法满足要求了。而A*来处理这类有需要实时要求的问题则显得游刃有余。
       在路径最优问题中,用来作为启发函数关键部分的h(n)其实很容易选,那便是当前结点至最终结点的距离,这个距离既可以是Hamilton距离(|x1-x2|+|y1-y2|),亦可以是Euclid 距离(直线距离)。都可以在较快的速度下达到问题的最优解。
       下面给出在Flash平台下,用AS2编制的交互式A*算法寻路问题的三个示例程序:(限于Flash的处理性能,当你点了一个目标点后,需要等卡通人物行进到该点时方可点下一点)
                                               

                                                            A* Test1
                                               


[align=center]A* Test2[/align]                                                            A* Test3


注:上面所示的三个Flash的最优路径搜索有时会出现走弯路的现象,系我这里的最优路径提取算法设计不佳所致(A*算法本身并没有错误),相应的改进请参考:[url=http://blog.csdn.net/EmilMatthew/archive/2006/10/31/1359172.aspx]A*算法最优解提取算法[/url]

      
[4]小结
       本文不仅对A*算法的思想来源及核心部分作了较为详细的介绍,更重要的是,给出了A*算法在网络平台下的一种交互式实现,这一点具有创新意义。
       这个程序的实现,不仅给我自己带去了快乐,相信也带给了更多朋友一点小小的启发,对此,我深感颀慰。当然,这个程序还可以在时实交互性上做得更好些,这是值得努力的方向。还有,A*算法本身亦有许多改进之处,如应对“陷井”时,如何更快的脱离,避免无畏的搜索,都是值得去展开的方向。

[参考文献与网站]
[0] 林尧瑞、马少平著,人工智能导论,清华大学出版社,2001.
[1] Nils J.Nilsson著,郑扣根等译,人工智能,机械工业出版社,2003
[2] [url=http://ai.stanford.edu/users/nilsson/trweb/tr.html]http://ai.stanford.edu/users/nilsson/trweb/tr.html[/url]
[3] [url=http://www.crc.ricoh.com/~hart/]http://www.crc.ricoh.com/~hart/[/url]
[4][url=http://i11www.iti.uni-karlsruhe.de/~fschulz/shortest-paths/]http://i11www.iti.uni-karlsruhe.de/~fschulz/shortest-paths/[/url]
程序完成日:06/10/15
文章完成日:06/10/17

附录[0]
A*算法核心部分之C语言版本:
       void parseMap_AStar()
       {            
                            float tempG,tempF;
                            EPQ_DataType    tempPoint;
                            int   noFindingFlag;
                            int   i;
                            int   moreThanOneBlock;
                            int   sIndex;
                            currentPoint=startPoint;
                                                                             
                            //early check
                            enQueue_EPQ(testQueue,currentPoint,LESS_THAN_VAL);
                     if(currentPoint->x==targetPoint->x&&
currentPoint->y==targetPoint->y)
                                          goto LABEL_FOUNDED;
                           
                            //Init for output data
                            CTCIndex=0;
                            NAIndex=0;
                            noFindingFlag=0;
                            moreThanOneBlock=0;
                            sIndex=0;

                            //core algorithm
                            while(!isEmpty_EPQ(testQueue))
                            {                                 
                               currentPoint=deQueue_EPQ(testQueue);
                                                  
                               //For natrual A* path finding answer output        
                                                                                     CLOSETable[CTCIndex].x=currentPoint->pX;
                               CLOSETable[CTCIndex].y=currentPoint->pY;
                                                                    NATable[NAIndex]=CLOSETable[CTCIndex];
                               CTCIndex++;NAIndex++;
                                                                                                               
              
                     //from current point ,look for the possible point to move.
                     //start from west ,clockwise
                     tempG=currentPoint->g+STEP_G;
                                                        
//west                                             
if(currentPoint->x>1&&
mapData[(int)currentPoint->y][(int)currentPoint->x-1]!=BLOCKED_GROUND&&
mapData[(int)currentPoint->y][(int)currentPoint->x-1]!=LABELED)
{            
                        //target pos checkif(currentPoint->x-1==targetPoint->x&&
currentPoint->y==targetPoint->y)
{currentPoint->pX=currentPoint->x;
currentPoint->pY=currentPoint->y;
currentPoint->x=currentPoint->x-1;
                                                                                                                                                                        goto LABEL_FOUNDED;
                            }
                                                                      tempF=calDis(currentPoint->y,currentPoint->x-1);
                                                                      tempPoint=createAPoint(currentPoint->x-1,currentPoint->y);
       tempPoint->pX=currentPoint->x;
       tempPoint->pY=currentPoint->y;
                                                                                                  
                                                                      setAStarValue(tempPoint,tempF,tempG);
                                                                                                                                            enQueue_EPQ(testQueue,tempPoint,LESS_THAN_VAL);
                                                                                                                              mapData[(int)currentPoint->y][(int)currentPoint->x-1]=LABELED;
}
                                          
//north
if(currentPoint->y>1&&
mapData[(int)currentPoint->y-1][(int)currentPoint->x]!=BLOCKED_GROUND&&
mapData[(int)currentPoint->y-1][(int)currentPoint->x]!=LABELED)
{                                                   
       //target pos check                                                               if(currentPoint->x==targetPoint->x&&
currentPoint->y-1==targetPoint->y)
{                                                                             currentPoint->pX=currentPoint->x;
              currentPoint->pY=currentPoint->y;
              currentPoint->y=currentPoint->y-1;   
              goto LABEL_FOUNDED;
       }
                                                               tempF=calDis(currentPoint->y-1,currentPoint->x);
                                                                                                                                     tempPoint=createAPoint(currentPoint->x,currentPoint->y-1);
       tempPoint->pX=currentPoint->x;
tempPoint->pY=currentPoint->y;
                                                                                                                       setAStarValue(tempPoint,tempF,tempG);
                                                                                    enQueue_EPQ(testQueue,tempPoint,LESS_THAN_VAL);
                                                                                                                       mapData[(int)currentPoint->y-1][(int)currentPoint->x]=LABELED;
}
                                                        
//east                                             
if(currentPoint->x<mapWidth-1&&
mapData[(int)currentPoint->y][(int)currentPoint->x+1]!=BLOCKED_GROUND&&
mapData[(int)currentPoint->y][(int)currentPoint->x+1]!=LABELED)
{
       //target pos check                                                        if(currentPoint->x+1==targetPoint->x&&
currentPoint->y==targetPoint->y)
       {
              currentPoint->pX=currentPoint->x;
              currentPoint->pY=currentPoint->y;
              currentPoint->x=currentPoint->x+1;
                                                                     
              goto LABEL_FOUNDED;
       }
                                                               tempF=calDis(currentPoint->y,currentPoint->x+1);                                          tempPoint=createAPoint(currentPoint->x+1,currentPoint->y);
       tempPoint->pX=currentPoint->x;
       tempPoint->pY=currentPoint->y;
                                                                                                                                     setAStarValue(tempPoint,tempF,tempG);                                                                     
       enQueue_EPQ(testQueue,tempPoint,LESS_THAN_VAL);
                                                                     
       mapData[(int)currentPoint->y][(int)currentPoint->x+1]=LABELED;
}
                                                        
//south                                                  
if(currentPoint->y<mapHeight-1&&
mapData[(int)currentPoint->y+1][(int)currentPoint->x]!=BLOCKED_GROUND&&
mapData[(int)currentPoint->y+1][(int)currentPoint->x]!=LABELED)
{
       //target pos check                                                        if(currentPoint->x==targetPoint->x&&
currentPoint->y+1==targetPoint->y)
{                                                      
currentPoint->pX=currentPoint->x;
currentPoint->pY=currentPoint->y;
currentPoint->y=currentPoint->y+1;
                                                                                                                          goto LABEL_FOUNDED;
         }
                                                        tempF=calDis(currentPoint->y+1,currentPoint->x);
                                                                                    tempPoint=createAPoint(currentPoint->x,currentPoint->y+1);
       tempPoint->pX=currentPoint->x;
       tempPoint->pY=currentPoint->y;                                                               
                                                                                                                                     setAStarValue(tempPoint,tempF,tempG);
                                                                                                                       enQueue_EPQ(testQueue,tempPoint,LESS_THAN_VAL);
                                                                                                                              mapData[(int)currentPoint->y+1][(int)currentPoint->x]=LABELED;
}                                 
}
                                                               
LABEL_FOUNDED:
                     CLOSETable[CTCIndex].x=currentPoint->pX;
                     CLOSETable[CTCIndex].y=currentPoint->pY;
                     NATable[NAIndex]=CLOSETable[CTCIndex];
                     CTCIndex++;NAIndex++;
                     CLOSETable[CTCIndex].x=currentPoint->x;
                     CLOSETable[CTCIndex].y=currentPoint->y;
                     NATable[NAIndex]=CLOSETable[CTCIndex];
                     CTCIndex++;NAIndex++;
      
                     //PROCESS OF ANSTable.
                     ANSTable[0]=CLOSETable[CTCIndex-1];
                     ANSIndex=1;
                           
                     i=CTCIndex-2;
do
{
       //ARBITARY OF NEAR BY POINT
if(
(CLOSETable.x-1==ANSTable[ANSIndex-1].x&&CLOSETable.y==ANSTable[ANSIndex-1].y)||
(CLOSETable.x==ANSTable[ANSIndex-1].x&&CLOSETable.y-1==ANSTable[ANSIndex-1].y)||
(CLOSETable.x+1==ANSTable[ANSIndex-1].x&&CLOSETable.y==ANSTable[ANSIndex-1].y)||
(CLOSETable.x==ANSTable[ANSIndex-1].x&&CLOSETable.y+1==ANSTable[ANSIndex-1].y))
              {
                                   ANSTable[ANSIndex]=CLOSETable;
                                   ANSIndex++;      
               }
                                    
              i--;
}while(!(CLOSETable.x==startPoint->x&&CLOSETable.y==startPoint->y));
                     
              //Output CLOSE table and NATRUAL table … …
       }
附录[1]
A*算法核心部分之AS2语言版本:
function AStarSearch():Void
{
              if(_root.gStartX==_root.gEndX&&_root.gStartY==_root.gEndY)
                     return;
              else
              {      
                       AStarQueue=new eQueue();
                           
                       targetNode=new eNode();
                       currentNode=new eNode();
                       tempNode=new eNode();
                                   
                                                
                            //a)data init
                            NAIndex=0;
                            CTCIndex=0;
                            FrameNAIndex=0;
                            ParTableIndex=0;
      
                            STEP_G=0.5;
                           
                            noFindingFlag=0;
                            moreThanOneBlock=0;
                            sIndex=0;
                                   
                                                        
                            targetNode.setXY(_root.gEndX,_root.gEndY);
                            currentNode.setXY(_root.gStartX,_root.gStartY);
                           
                            _root.gMapArr[_root.gStartY][_root.gStartX]=LABELED;
                           
                            currentNode.pX=-1;currentNode.pY=-1;
                              currentNode.setG_H(0,Cal_Cost(currentNode,targetNode));
                           
                            AStarQueue.enQueue_AStar(currentNode);
                           
//b)core algorithm
                           
                            while(!AStarQueue.isEmpty())
                            {
                                          
                                   currentNode=AStarQueue.deQueue();
                                                         
                                   //For natrual A* path finding answer output        
                                                               
                               CLOSETable[CTCIndex]=new eNode();
                               CLOSETable[CTCIndex].x=currentNode.x;
                                   CLOSETable[CTCIndex].y=currentNode.y;
                                   CTCIndex++;
                                                               
                                   ParTable[ParTableIndex]=new eNode();
                                   ParTable[ParTableIndex].x=currentNode.pX;
                                   ParTable[ParTableIndex].y=currentNode.pY;
                                   ParTableIndex++;
                                                               
                            //from current point ,look for the possible point to move.
                            //start from west ,clockwise
                                   tempG=currentNode.mG+STEP_G;
                                                        
//west                                      
if(currentNode.x>1&&
_root.gMapArr[currentNode.y][currentNode.x-1]!=BLOCKED_GROUND&&
_root.gMapArr[currentNode.y][currentNode.x-1]!=LABELED)
       {                                                                                   
//target pos check
                                                                      if(currentNode.x-1==targetNode.x&¤tNode.y==targetNode.y)
              {
                     currentNode.pX=currentNode.x;
                     currentNode.pY=currentNode.y;
                     currentNode.x=currentNode.x-1;
                                                                         break;
              }
                                                               
              tempNode=new eNode();
                                          
      tempNode.setXY(currentNode.x-1,currentNode.y);
                                                                             
              tempH=Cal_Cost(targetNode,currentNode);
              tempNode.pX=currentNode.x;
              tempNode.pY=currentNode.y;
                                                                                                  
              tempNode.setG_H(tempG,tempH);
                                                                                                                                                   AStarQueue.enQueue_AStar(tempNode);
                                                                                                                                                _root.gMapArr[currentNode.y][currentNode.x-1]=LABELED;
       }
                                          
//north                                                   
if(currentNode.y>1&&
_root.gMapArr[currentNode.y-1][currentNode.x]!=BLOCKED_GROUND&&
_root.gMapArr[currentNode.y-1][currentNode.x]!=LABELED)
{
       //target pos check                                                        if(currentNode.x==targetNode.x&¤tNode.y-1==targetNode.y)
       {
                     currentNode.pX=currentNode.x;
                     currentNode.pY=currentNode.y;
                     currentNode.y=currentNode.y-1;
                                                                                    
                     break;
       }

       tempNode=new eNode();
                                                                      tempNode.setXY(currentNode.x,currentNode.y-1);
                                                                     
                                                                      tempH=Cal_Cost(targetNode,tempNode);
       tempNode.pX=currentNode.x;
       tempNode.pY=currentNode.y;                                                                 
       tempNode.setG_H(tempG,tempH);
                                                                                                                                     AStarQueue.enQueue_AStar(tempNode);
                                                                                                                                            _root.gMapArr[currentNode.y-1][currentNode.x]=LABELED;
}
                                                        
//east
                                   
if(currentNode.x<_root.gColNum-1&&
_root.gMapArr[currentNode.y][currentNode.x+1]!=BLOCKED_GROUND&&_root.gMapArr[currentNode.y][currentNode.x+1]!=LABELED)
{
                                                                     
       //target pos check                                                               if(currentNode.x+1==targetNode.x&¤tNode.y==targetNode.y)
       {
                            currentNode.pX=currentNode.x;
                            currentNode.pY=currentNode.y;
                                                                                                          currentNode.x=currentNode.x+1;
                                                                                                                              
                            break;
       }

tempNode=new eNode();                                                             tempNode.setXY(currentNode.x+1,currentNode.y);                                    tempH=Cal_Cost(targetNode,tempNode);
       tempNode.pX=currentNode.x;
       tempNode.pY=currentNode.y;
                                                                                                                                    
       tempNode.setG_H(tempG,tempH);
                                                                                                                                            AStarQueue.enQueue_AStar(tempNode);
                                                                                                                                            _root.gMapArr[currentNode.y][currentNode.x+1]=LABELED;
                                                                     
}
                                                        
//south        
                                                               
                                                
if(currentNode.y<_root.gRowNum-1&&
_root.gMapArr[currentNode.y+1][currentNode.x]!=BLOCKED_GROUND&&
_root.gMapArr[currentNode.y+1][currentNode.x]!=LABELED)
       {
              //target pos check                                                        if(currentNode.x==targetNode.x&¤tNode.y+1==targetNode.y)
              {
                                   currentNode.pX=currentNode.x;
                                currentNode.pY=currentNode.y;
                                   currentNode.y=currentNode.y+1;
                                                                                                                              
                                   break;
              }
                     
                tempNode=new eNode();                              
tempNode.setXY(currentNode.x,currentNode.y+1);
           tempH=Cal_Cost(tempNode,targetNode);
              
tempNode.pX=currentNode.x;
              tempNode.pY=currentNode.y;
                                                                                                  
              tempNode.setG_H(tempG,tempH);
                                                                                                                                    
              AStarQueue.enQueue_AStar(tempNode);
                                                                                                                                                   _root.gMapArr[currentNode.y+1][currentNode.x]=LABELED;
       }
}

//FINISHED_LABEL:
                                   CLOSETable[CTCIndex]=new eNode();
                                   CLOSETable[CTCIndex].x=currentNode.x;
                                   CLOSETable[CTCIndex].y=currentNode.y;
                                   CTCIndex++;
                                                               
                                   ParTable[ParTableIndex]=new eNode();
                                   ParTable[ParTableIndex].x=currentNode.pX;
                                   ParTable[ParTableIndex].y=currentNode.pY;
                                   ParTableIndex++;
                     
                     
                     //PROCESS OF NATable.
                     NATable[0]=new eNode();
                     NATable[0]=CLOSETable[CTCIndex-1];
                     NAIndex=1;
                     
                     trace3("NATable[0]"+"("+String(NATable[0].x)+","+String(NATable[0].y)+")\n");
                     
                     //Assertion  
                     if(CTCIndex!=ParTableIndex)
                     {
                                   trace3("in a star search:CTCIndex!=ParTableIndex");
                                   return;
                     }      
                     
                     i=CTCIndex-1;
                     
                     do
                     {
                                   NATable[NAIndex]=new eNode();
                                   NATable[NAIndex]=ParTable;  
                                   NAIndex++;
                                   i--;
                                   
                                   if(i==0)//normal successful exit pos
                                          break;
                                   
                     while(!(NATable[NAIndex-1].x==CLOSETable.x&&
NATable[NAIndex-1].y==CLOSETable.y))
                                   {
                                          i--;
                                   }
                     
                                   if(i==0)//special condition exit pos
                                   {
                                          NATable[NAIndex]=new eNode();
                                          NATable[NAIndex]=CLOSETable;   
                                          NAIndex++;
                                          
                                          break;
                                   }
                     }while(true);      

                     var timeCount:Number=0;
                     var midObj:eNode;
                     
                     if(NAIndex%2==0)
                            timeCount=NAIndex/2;
                     else
                            timeCount=(NAIndex-1)/2;
                     
                     for(i=0;i<timeCount;i++)
                     {
                                   midObj=new eNode();      
                                   midObj=NATable;
                                   NATable=NATable[NAIndex-1-i];
                                   NATable[NAIndex-1-i]=midObj;
                     }
                           
              }
}


[源码下载、源程序下载]
源程序内容列表:
Code1:C语言版本源码
Code2:AS2版本源码
[url=http://emilmatthew.51.net/EmilPapers/0632AStarSearch/code1.rar]http://emilmatthew.51.net/EmilPapers/0632AStarSearch/code1.rar[/url]
[url=http://emilmatthew1984.googlepages.com/0633code2.rar]http://emilmatthew1984.googlepages.com/0633code2.rar[/url]

若直接点击无法下载(或浏览),请将下载(或浏览)的超链接粘接至浏览器地( 推荐MYIE或GREENBORWSER)址栏后按回车.若不出意外,此时应能下载.
若下载中出现了问题,请参考:
[url=http://blog.csdn.net/emilmatthew/archive/2006/04/08/655612.aspx]http://blog.csdn.net/emilmatthew/archive/2006/04/08/655612.aspx[/url]
[i][i][i][i][i][i][i][i][i][i][i][i][i][i][i][i][i][i][i][size=3][font=Times New Roman][/font][/size][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i]

[[i] 本帖最后由 wice3 于 2007-4-5 18:59 编辑 [/i]]
页: [1]
查看完整版本: A*算法理论与实践