




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、A算法详解.网络.未知加入时间:2005-1-28 12:55:21点击: 蚤125第一部分:A*算法简介写这篇文章的初衷是应一个网友的要求,当然我也发现现在有关人工智能的中文站点 实在太少,我在这里抛砖引玉,希望大家都来热心的参与。还是说正题,我先拿A*算法开刀,是因为A*在游戏中有它很典型的用法,是人工智 能在游戏中的代表。A*算法在人工智能中是一种典型的启发式搜索算法,为了说清楚A*算法,我看还是 先说说何谓启发式算法。一、何谓启发式搜索算法:在说它之前先提提状态空间搜索。状态空间搜索,如果按专业点的说法就是将问题求 解过程表现为从初始状态到目标状态寻找这个路径的过程。通俗点说,就是在解
2、一个问 题时,找到一条解题的过程可以从求解的开始到问题的结果(好象并不通俗哦)。由于 求解问题的过程中分枝有很多,主要是求解过程中求解条件的不确定性,不完备性造成 的,使得求解的路径很多这就构成了一个图,我们说这个图就是状态空间。问题的求解 实际上就是在这个图中找到一条路径可以从开始到结果。这个寻找的过程就是状态空间 搜索。常用的状态空间搜索有深度优先和广度优先。广度优先是从初始状态一层一层向下 找,直到找到目标为止。深度优先是按照一定的顺序前查找完一个分支,再查找另一个 分支,以至找到目标为止。这两种算法在数据结构书中都有描述,可以参看这些书得到 更详细的解释。前面说的广度和深度优先搜索有一
3、个很大的缺陷就是他们都是在一个给定的状态空 间中穷举。这在状态空间不大的情况下是很合适的算法,可是当状态空间十分大,且不 预测的情况下就不可取了。他的效率实在太低,甚至不可完成。在这里就要用到启发式 搜索了。启发式搜索就是在状态空间中的搜索对每一个搜索的位置进行评估,得到最好的位 置,再从这个位置进行搜索直到目标。这样可以省略大量无畏的搜索路径,提到了效率。 在启发式搜索中,对位置的估价是十分重要的。采用了不同的估价可以有不同的效果。 我们先看看估价是如何表示的。启发中的估价是用估价函数表示的,如:f(n) = g(n) + h(n)其中f(n)是节点n的估价函数,g(n)实在状态空间中从初始
4、节点到n节点的实际代价, h(n)是从n到目标节点最佳路径的估计代价。在这里主要是h(n)体现了搜索的启发信息, 因为g(n)是已知的。如果说详细 点,g(n)代表了搜索的广度的优先趋势。但是当h(n) g(n)时,可以省略g(n),而提高效率。这些就深了,不懂也不影响啦!我们继续看 看何谓A*算法。二、初识A*算法:启发式搜索其实有很多的算法,比如:局部择优搜索法、最好优先搜索法等等。当然 A*也是。这些算法都使用了启发函数,但在具体的选取最佳搜索节点时的策略不同。象 局部择优搜索法,就是在搜索的过程中选取“最佳节点”后舍弃其他的兄弟节点,父亲节 点,而一直得搜索下去。这种搜索的结果很明显,
5、由于舍弃了其他的节点,可能也把最 好的节点都舍弃了,因为求解的最佳节点只是在该阶段的最佳并不一定是全局的最佳。 最好优先就聪明多了,他在搜索时,便没有舍弃节点(除非该节点是死节点)在每一步 的估价中都把当前的节点和以前的节点的估价值比较得到一个“最佳的节点”。这样可以 有效的防止“最佳节点”的丢失。那么A*算法又是一种什么样的算法呢?其实A*算法也 是一种最好优先的算法。只不过要加上一些约束条件罢了。由于在一些问题求解时,我 们希望能够求解出状态空间搜索的最短路径,也就是用最快的方法求解问题,A*就是干 这种事情的!我们先下个定义,如果一个估价函数可以找出最短的路径,我们称之为可 采纳性。A*
6、算法是一个可采纳的最好优先算法。A*算法的估价函数克表示为:f(n) =g(n) + h(n)这里,f(n)是估价函数,g(n)是起点到终点的最短路径值,h(n)是n到目标的最断路 经的启发值。由于这个f(n)其实是无法预先知道的,所以我们用前面的估价函数f(n)做 近似。g(n)代替g(n),但g(n)=g(n)才可(大多数情况下都是满足的,可以不用考虑), h(n)代替h(n),但h(n)g = 0;这是计算h值Node-h = (dx-sx)*(dx-sx) + (dy-sy)*(dy-sy); / should really use sqrt().这是计算f值,即估价值Node-f =
7、 Node-g+Node-h;Node-NodeNum = TileNum(dx, dy);Node-x = dx;Node-y = dy;OPEN-NextNode=Node; / make Open List point to first nodefor (;) 从Open表中取得一个估价值最好的节点BestNode=ReturnBestNode();/如果该节点是目标节点就退出if (BestNode-NodeNum = TileNumDest) / if wevcfound theend, break and finishbreak;/否则生成子节点GenerateSuccessors
8、(BestNode,sx,sy);PATH = BestNode;再看看生成子节点函数GenerateSuccessors:void AstarPathfinder:GenerateSuccessors(NODE *BestNode,int dx, int dy)int x, y;哦!依次生成八个方向的子节点,简单!/ Upper-Leftif ( FreeTile(x=BestNode-x-TILESIZE, y=BestNode-y-TILESIZE) GenerateSucc(BestNode,x,y,dx,dy);/ Upperif ( FreeTile(x=BestNode-x, y
9、=BestNode-y-TILESIZE) )GenerateSucc(BestNode,x,y,dx,dy);/ Upper-Rightif ( FreeTile(x=BestNode-x+TILESIZE, y=BestNode-y-TILESIZE) GenerateSucc(BestNode,x,y,dx,dy);/ Rightif ( FreeTile(x=BestNode-x+TILESIZE, y=BestNode-y)GenerateSucc(BestNode,x,y,dx,dy);/ Lower-Rightif ( FreeTile(x=BestNode-x+TILESIZE
10、, y=BestNode-y+TILESIZE) GenerateSucc(BestNode,x,y,dx,dy);/ Lowerif ( FreeTile(x=BestNode-x, y=BestNode-y+TILESIZE)GenerateSucc(BestNode,x,y,dx,dy);/ Lower-Leftif ( FreeTile(x=BestNode-x-TILESIZE, y=BestNode-y+TILESIZE) GenerateSucc(BestNode,x,y,dx,dy);/ Leftif ( FreeTile(x=BestNode-x-TILESIZE, y=Be
11、stNode-y)GenerateSucc(BestNode,x,y,dx,dy);看看最重要的函数GenerateSucc:void AstarPathfinder:GenerateSucc(NODE *BestNode,int x, int y, int dx, int dy)int g, TileNumS, c = 0;NODE *Old, *Successor;计算子节点的g值g = BestNode-g+1; / g(Successor)=g(BestNode)+cost of gettingfrom BestNode to SuccessorTileNumS = TileNum(x
12、,y); / identification purposes/子节点再Open表中吗?if ( (Old=CheckOPEN(TileNumS) != NULL ) / if equal to NULLthen not in OPEN list, else it returns the Node in Old/若在for( c = 0; c Childc = NULL ) / AddOld to the list of BestNodes Children (or Successors).break;BestNode-Childc = Old;比较Open表中的估价值和当前的估价值(只要比较g
13、值就可以了)if ( g g ) / if our new g value is Parent = BestNode;Old-g = g;Old-f = g + Old-h;else /在 Closed 表中吗?if ( (Old=CheckCLOSED(TileNumS) != NULL ) / if equal toNULL then not in OPEN list, else it returns the Node in Old/若在for( c = 0; cChildc = NULL )/ Add Old to the list of BestNodes Children (or S
14、uccessors).break;BestNode-Childc = Old;比较Closed表中的估价值和当前的估价值(只要比较g值就可以了) if ( g g ) / if our new g value is Parent = BestNode;Old-g = g;Old-f = g + Old-h;/再依次更新Old的所有子节点的估价值PropagateDown(Old); / Since we changed the g value of Old, we need / to propagate this new value downwards, i.e./ do a Depth-Fi
15、rst traversal of the tree!else/不在Open表中也不在Close表中生成新的节点Successor = ( NODE* )calloc(1,sizeof( NODE );Successor-Parent = BestNode;Successor-g = g;Successor-h = (x-dx)*(x-dx) + (y-dy)*(y-dy); / should do sqrt(), but since we dont reallySuccessor-f = g+Successor-h; / care about the distance butjust whic
16、h branch looksSuccessor-x = x; / better this should suffice. Anyayz its faster.Successor-y = y;Successor-NodeNum = TileNumS;/再插入Open表中,同时排序。Insert(Successor); / Insert Successor on OPEN list wrt ffor( c =0; c Childc = NULL )/ Add Old to the list of BestNodes Children (or Successors).break;BestNode-C
17、hildc = Successor;哈哈! A*算法我懂了!当然,我希望你有这样的感觉!不过我还要再说几句。仔细看 看这个程序,你会发现,这个程序和我前面说的伪程序有一些不同,在GenerateSucc函 数中,当子节点在Closed表中时,没有将子节点从Closed表中删除并放入Open表中。 而是直接的重新的计算该节点的所有子节点的估价值(用PropagateDown函数九这样 可以快一些!另当子节点在Open表和Closed表中时,重新的计算估价值后,没有重新 的对Open表中的节点排序,我有些想不通,为什么不排呢?:-(,会不会是一个小小的 BUG。你知道告诉我好吗?Shows how
18、 you can transform an ordinary obstacle into a configuration-space obstacle using the object to be moved and the obstacle to be avoided. Basically , you slide the object around the obstacle, maintaining contact between them at all times, keeping track of one arbitrary tracing point on the moving obj
19、ect as you go. As the tracing point moves around the obstacle , it builds an eight-sided fence. Plainly, there can be no collision between object and obstacle as long as the tracing point stays outside the fence that bounds the configuration-space obstacle associated with the original obstacle.Shows
20、 both of the configuration-space obstacles made from the original triangle and the pentagon and octagon shown in figure 5.6. The lower-left vertex of the triangular robot was used. Evidently , the robot can get through the gap, because the configuration-space obstacles are not large enough to close
21、up the space.It is not entirely clear that the shortest path is through the gap, however. To be sure that it is, you have to search.As yet, there is no net through which to search. The construction of a net is easy, however, for two-dimensional configuration-space problems. To see why it is easy, su
22、ppose that you are at any point in a configuration space. From where you are, you either can see the desired position or you cannot see it. If you can, you need to think no more, for the shortest path is the straight line between you and the desired position.If you cannot see the desired position fr
23、om where you are, then the only move that makes sense is a move to one of the vertexes that you can see. Accordingly, all motion is confined to motion from vertex to vertex, except at the beginning, when motion is from initial position to a vertex, and at the end, when motion is from a vertex to the
24、 desired position. Thus , the initial position, desired position, and vertexes are like the nodes in a net. Because the links between nodes are placed only when there is an unobstructed line of sight between the nodes, the net is call a visibility graph.Illustrates the visibility graph for the robot-motion example, along with the result of performing an A* search to establish the shortest path for the configuration-space robot, a point, through the space of configuration-
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 激光跟踪仪与D扫描技术考核试卷
- 叠拼别墅装饰施工方案
- 比较分析2025年证券从业资格证考试试题及答案
- 2025年【河北省安全员A证】模拟考试题及答案
- 石油开采业的能源转型与碳排放削减考核试卷
- 反不正当竞争考核试卷
- 2024年项目管理专业人士考试重要知识点试题及答案
- 屋面钢模板施工方案
- 2025年关于证券从业资格证的深度探索试题及答案
- 珠宝首饰行业绿色发展策略考核试卷
- 人教版高一下学期期中考试数学试卷及答案(共两套)
- 产科诊疗指南及技术操作规范
- 小学二年级数学三位数加减三位数计算同步练习口算题带答案
- 发展汉语初级口语I-第11课课件
- 免疫规划工作经验
- 海南省海口市2023-2024学年五年级下学期期中综合调研数学试卷(苏教版)
- 第一单元字词过关专题卷-2022-2023学年语文五年级下册(部编版)
- 2024年无人驾驶行业培训资料 - 无人驾驶技术的商业应用与法规管理
- 整本书《中国古代寓言故事》阅读教学设计
- 《太阳照在桑干河上》农村革命与现实生活的冲突
- 电容损耗计算公式(一)
评论
0/150
提交评论