当前位置:   article > 正文

AStar算法(MATLAB实现)_astar matlab

astar matlab

A*算法简介:

A*(A-Star)算法是一种静态路网中求解最短路径最有效的直接搜索方法,也是解决许多搜索问题的有效算法。算法中的距离估算值与实际值越接近,最终搜索速度越快。

结合Dijkstra算法与BFS算法的优点,得到的就是A星算法,A*算法是一种启发式搜索算法,它是在状态空间中的搜索,首先对每一个搜索的位置进行评估,得到最好的位置,再从这个位置进行搜索直到目标。这样可以省略大量无谓的搜索路径,提高了效率。在启发式搜索中,对位置的估价是十分重要的。采用了不同的估价可以有不同的效果。公式表示为: f(n)=g(n)+h(n),其中, f(n) 是从初始状态经由状态n到目标状态的代价估计,g(n) 是在状态空间中从初始状态到状态n的实际代价,h(n) 是从状态n到目标状态的最佳路径的估计代价,对于路径搜索问题,状态就是图中的节点,代价就是距离。h(n)的选取:保证找到最短路径(最优解的)条件,关键在于估价函数f(n)的选取(或者说h(n)的选取)。我们以d(n)表达状态n到目标状态的距离,那么h(n)的选取大致有如下三种情况:

(1)如果h(n)< d(n)到目标状态的实际距离,这种情况下,搜索的点数多,搜索范围大,效率低。但能得到最优解。

(2)如果h(n)=d(n),即距离估计h(n)等于最短距离,那么搜索将严格沿着最短路径进行, 此时的搜索效率是最高的。

(3)如果 h(n)>d(n),搜索的点数少,搜索范围小,效率高,但不能保证得到最优解。

  

此外:,如果h(n)为0,则只g(n)起作用,A*变成Dijkstra算法,保证找到最短路径。如果h(n)总是低于(或等于)从目标移动n到目标的成本,则保证 A* 找到最短路径。越低h(n),节点 A* 扩展得越多,速度越慢。如果h(n)正好等于从n到目标的移动成本,那么 A* 将只遵循最佳路径,而不会扩展其他任何东西,使其非常快。尽管您不能在所有情况下都做到这一点,但您可以在某些特殊情况下做到这一点。很高兴知道给定完美的信息,A* 将表现完美。如果h(n)有时大于从移动n到目标的成本,则不能保证 A* 找到最短路径,但它可以运行得更快。在另一个极端,如果h(n)相对于 非常高g(n),则仅h(n)起作用,并且 A* 变成贪婪的最佳优先搜索。

A*算法伪代码:

       把起始格添加到 "开启列表"

do{

        寻找开启列表中F值最低的格子, 我们称它为当前格.

        把它切换到关闭列表.

        对当前格相邻的8格中的每一个

        if (它不可通过 || 已经在 "关闭列表" 中) {

                什么也不做.

        }

        if (它不在开启列表中) {

                把它添加进 "开启列表", 把当前格作为这一格的父节点, 计算这一格的 FGH

        }

        if (它已经在开启列表中) {

                if (用 G 值为参考检查新的路径是否更好, 更低的G值意味着更好的路径) {

                        把这一格的父节点改成当前格, 并且重新计算这一格的 GF 值.

                }

        }

} while( 目标格已经在 "开启列表", 这时候路径被找到)

如果开启列表已经空了, 说明路径不存在.

最后从目标格开始, 沿着每一格的父节点移动直到回到起始格, 这就是路径.

代码:

  1. %% 单次路径规划
  2. clear all;
  3. %% 初始化智能体
  4. fig = figure(); % 创建图形窗口
  5. hold on;
  6. % 设置区域边界
  7. Estart_x = 0; Estart_y = 0;
  8. Weight = 80; High = 80;
  9. dL = 1; %设置网格大小
  10. axis([Estart_x-5 Weight 0 High]);
  11. axis equal; %设置屏幕高宽比,使得每个坐标轴的具有均匀的刻度间隔
  12. %添加模糊刻度
  13. set(gca,'XMinorTick','on')
  14. set(gca,'YMinorTick','on')
  15. xlabel('X(m)'); ylabel('Y(m)');
  16. grid minor %在刻度线之间添加次网格线
  17. rectangle('position',[Estart_x,Estart_y,Weight,High],'LineWidth',2); %画边界
  18. %% 区域网格化
  19. xL = 0:dL:Weight;
  20. yL = 0:dL:High;
  21. [XL , YL] = meshgrid(xL,yL); %获得网格化后的X矩阵和Y矩阵
  22. C = zeros(size(XL)); %存储网格的高度值
  23. %--------------------%
  24. C(1,:) = 100; C(:,1) = 100;
  25. C(end,:) = 100; C(:,end) = 100;
  26. %% 设置障碍物覆盖值
  27. %--------------------%
  28. a1(1:21) = 20;
  29. C((20:40),20)=80;
  30. plot((20:40),a1,'k-','LineWidth',2);
  31. %--------------------%
  32. a2(1:30) = 30;
  33. C((1:29),30)=80;
  34. plot((0:29),a2,'k-','LineWidth',2);
  35. %--------------------%
  36. a3(1:20) = 50;
  37. C(50,(31:50))=80;
  38. plot(a3,(31:50),'k-','LineWidth',2);
  39. %--------------------%
  40. a4(1:21) = 60;
  41. C((40:60),60)=80;
  42. plot((40:60),a4,'k-','LineWidth',2);
  43. %--------------------%
  44. a5(1:21) = 25;
  45. C(25,(40:60))=80;
  46. plot(a5,(40:60),'k-','LineWidth',2);
  47. close_list=0;open_list=0; %初始化节点容器
  48. start_x=70;start_y=3; %设置起点位置
  49. end_x=5;end_y=70; %设置终点位置
  50. i=1;
  51. %本节点x坐标
  52. father_node(:,1)=start_x;
  53. %本节点y坐标
  54. father_node(:,2)=start_y;
  55. %本节点f(n)
  56. father_node(:,3)=norm([father_node(:,1),father_node(:,2)]-[start_x,start_y])+(father_node(:,1)-end_x)^2+(father_node(:,2)-end_y)^2;
  57. %本节点g(n)
  58. father_node(:,4)=norm([father_node(:,1),father_node(:,2)]-[start_x,start_y]);
  59. %父节点y坐标
  60. father_node(:,5)=start_x;
  61. %父节点y坐标
  62. father_node(:,6)=start_y;
  63. close_list=father_node;
  64. %返回8个邻居节点
  65. [N,index]=land(father_node(:,1),father_node(:,2),start_x,start_y,end_x,end_y,Estart_x,Estart_x+Weight,Estart_y,Estart_x+High,C,close_list);
  66. last_open_list = sortrows(N,3); %排序并存入临时open_list
  67. father_node=last_open_list(1,:); %f(n)最小邻居设为父节点
  68. last_open_list(1,:)=[]; %删除临时open_list中最小f(n)节点
  69. cend=1;
  70. while close_list(cend,1)~=end_x||close_list(cend,2)~=end_y
  71. cend=cend+1;
  72. x=father_node(1,1); %返回f(n)最小节点坐标
  73. y=father_node(1,2);
  74. [N,index]=land(father_node(:,1),father_node(:,2),start_x,start_y,end_x,end_y,Estart_x,Estart_x+Weight,Estart_y,Estart_x+High,C,close_list);
  75. for i=1:index
  76. a=N(i,1)==father_node(1,1);
  77. b=N(i,1)==father_node(1,2);
  78. f=N(i,1);
  79. g=N(i,1);
  80. if a>0&&b>0
  81. if father_node(1,3)>g
  82. father_node(1,1)=N(i,1);
  83. father_node(1,1)=N(i,2);
  84. father_node(1,1)=N(i,3);
  85. father_node(1,1)=N(i,4);
  86. father_node(1,1)=N(i,5);
  87. father_node(1,1)=N(i,6);
  88. end
  89. else
  90. last_open_list(i,:)=N(i,:);
  91. plot(N(i,1),N(i,2),'bs'); %正方形(表示待检测节点)
  92. end
  93. end
  94. last_open_list = sortrows(last_open_list,3);
  95. close_list(cend,1)=father_node(1,1);
  96. close_list(cend,2)=father_node(1,2);
  97. close_list(cend,3)=father_node(1,3);
  98. close_list(cend,4)=father_node(1,4);
  99. close_list(cend,5)=father_node(1,5);
  100. close_list(cend,6)=father_node(1,6);
  101. father_node=last_open_list(1,:);
  102. last_open_list(1,:)=[];
  103. end
  104. plot(close_list(:,1),close_list(:,2),'r*'); %当前节点标绿色*
  105. function [N,index]=land(x,y,start_x,start_y,end_x,end_y,xmin,xmax,ymin,ymax,C,close_list)
  106. index=0;
  107. %上邻域
  108. if judge(x,y+1,xmin,xmax,ymin,ymax,C,close_list)==1
  109. index=index+1;
  110. N(index,1)=x;
  111. N(index,2)=y+1;
  112. N(index,3)=norm([x,y+1]-[start_x,start_y])+(x-end_x)^2+(y+1-end_y)^2;
  113. N(index,4)=norm([x,y+1]-[start_x,start_y]);
  114. N(index,5)=x;
  115. N(index,6)=y;
  116. end
  117. %下邻域
  118. if judge(x,y-1,xmin,xmax,ymin,ymax,C,close_list)==1
  119. index=index+1;
  120. N(index,1)=x;
  121. N(index,2)=y-1;
  122. N(index,3)=norm([x,y-1]-[start_x,start_y])+(x-end_x)^2+(y-1-end_y)^2;
  123. N(index,4)=norm([x,y-1]-[start_x,start_y]);
  124. N(index,5)=x;
  125. N(index,6)=y;
  126. end
  127. %左邻域
  128. if judge(x-1,y,xmin,xmax,ymin,ymax,C,close_list)==1
  129. index=index+1;
  130. N(index,1)=x-1;
  131. N(index,2)=y;
  132. N(index,3)=norm([x-1,y]-[start_x,start_y])+(x-1-end_x)^2+(y-end_y)^2;
  133. N(index,4)=norm([x-1,y]-[start_x,start_y]);
  134. N(index,5)=x;
  135. N(index,6)=y;
  136. end
  137. %右邻域
  138. if judge(x+1,y,xmin,xmax,ymin,ymax,C,close_list)==1
  139. index=index+1;
  140. N(index,1)=x+1;
  141. N(index,2)=y;
  142. N(index,3)=norm([x+1,y]-[start_x,start_y])+(x+1-end_x)^2+(y-end_y)^2;
  143. N(index,4)=norm([x+1,y]-[start_x,start_y]);
  144. N(index,5)=x;
  145. N(index,6)=y;
  146. end
  147. %左上邻域
  148. if judge(x-1,y+1,xmin,xmax,ymin,ymax,C,close_list)==1
  149. index=index+1;
  150. N(index,1)=x-1;
  151. N(index,2)=y+1;
  152. N(index,3)=norm([x-1,y+1]-[start_x,start_y])+(x-1-end_x)^2+(y+1-end_y)^2;
  153. N(index,4)=norm([x-1,y+1]-[start_x,start_y]);
  154. N(index,5)=x;
  155. N(index,6)=y;
  156. end
  157. %右上邻域
  158. if judge(x+1,y+1,xmin,xmax,ymin,ymax,C,close_list)==1
  159. index=index+1;
  160. N(index,1)=x+1;
  161. N(index,2)=y+1;
  162. N(index,3)=norm([x+1,y+1]-[start_x,start_y])+(x+1-end_x)^2+(y+1-end_y)^2;
  163. N(index,4)=norm([x+1,y+1]-[start_x,start_y]);
  164. N(index,5)=x;
  165. N(index,6)=y;
  166. end
  167. %左下邻域
  168. if judge(x-1,y-1,xmin,xmax,ymin,ymax,C,close_list)==1
  169. index=index+1;
  170. N(index,1)=x-1;
  171. N(index,2)=y-1;
  172. N(index,3)=norm([x-1,y-1]-[start_x,start_y])+(x-1-end_x)^2+(y-1-end_y)^2;
  173. N(index,4)=norm([x-1,y-1]-[start_x,start_y]);
  174. N(index,5)=x;
  175. N(index,6)=y;
  176. end
  177. %右下邻域
  178. if judge(x+1,y-1,xmin,xmax,ymin,ymax,C,close_list)==1
  179. index=index+1;
  180. N(index,1)=x+1;
  181. N(index,2)=y-1;
  182. N(index,3)=norm([x+1,y-1]-[start_x,start_y])+(x+1-end_x)^2+(y-1-end_y)^2;
  183. N(index,4)=norm([x+1,y-1]-[start_x,start_y]);
  184. N(index,5)=x;
  185. N(index,6)=y;
  186. end
  187. end
  188. function ruselt=judge(x,y,xmin,xmax,ymin,ymax,C,close_list)
  189. if x<xmin||x>xmax||y<ymin||y>ymax
  190. ruselt=0;
  191. return
  192. end
  193. if C(x,y)==100||C(x,y)==80
  194. ruselt=0;
  195. return
  196. end
  197. [i,j]=size(close_list);
  198. for ii=1:i
  199. if close_list(ii,1)==x&&close_list(ii,2)==y
  200. ruselt=0;
  201. return
  202. end
  203. end
  204. ruselt=1;
  205. end

参考:

A星(A*、A Star)路径规划算法详解(附MATLAB代码)

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号