请选择 进入手机版 | 继续访问电脑版

0

阿木币

0

精华

182 小时

在线时间

技术大V

Rank: 4

发表于 2020-6-10 09:31:39 4023 浏览 4 回复

[算法之美] 路径优化算法--Dijkstra和A*算法及其Matlab实现

本帖最后由 chasing 于 2020-6-10 09:36 编辑

写在前面的话:只是对两种路径优化算法进行简单的理解和尝试,为后续使用做准备。如果用到,请再次好好理解原理和Matlab源码。本文摘自作者博客 https://blog.csdn.net/qq_15390133 欢迎关注,留言。
参考博客:
1. [用Matlab实现A*算法和Dijkstra算法](https://blog.csdn.net/weixin_43795921/article/details/87945260)
2. [运动规划入门 | 1. 白话Dijkstra,从原理到Matlab实现](https://www.guyuehome.com/5652)
3. [运动规划入门 | 2. 白话A*,从原理到Matlab实现](https://www.guyuehome.com/6560)

直接上干货(参考上述博客可得)首先给出Matlab下的三个脚本文件:

TestScript.m
  1. %
  2. % TestScript for Assignment 1
  3. %

  4. %% Define a small map
  5. % map = false(10);
  6. map = ans;
  7. % Add an obstacle
  8. % map (1:5, 6) = true;
  9. map = logical(map);
  10. start_coords = [1, 1];
  11. dest_coords  = [40, 20];

  12. %%
  13. close all;
  14. %   [route, numExpanded] = DijkstraGrid (map, start_coords, dest_coords);
  15. % Uncomment following line to run Astar
  16.   [route, numExpanded] = AStarGrid (map, start_coords, dest_coords);

  17. %HINT: With default start and destination coordinates defined above, numExpanded for Dijkstras should be 76, numExpanded for Astar should be 23.
复制代码
AStarGrid.m

  1. function [route,numExpanded] = AStarGrid (input_map, start_coords, dest_coords)
  2. % Run A* algorithm on a grid.
  3. % Inputs :
  4. %   input_map : a logical array where the freespace cells are false or 0 and
  5. %   the obstacles are true or 1
  6. %   start_coords and dest_coords : Coordinates of the start and end cell
  7. %   respectively, the first entry is the row and the second the column.
  8. % Output :
  9. %    route : An array containing the linear indices of the cells along the
  10. %    shortest route from start to dest or an empty array if there is no
  11. %    route. This is a single dimensional vector
  12. %    numExpanded: Remember to also return the total number of nodes
  13. %    expanded during your search. Do not count the goal node as an expanded node.

  14. % set up color map for display用一个map矩阵来表示每个点的状态
  15. % 1 - white - clear cell
  16. % 2 - black - obstacle
  17. % 3 - red = visited 相当于CLOSED列表的作用
  18. % 4 - blue  - on list 相当于OPEN列表的作用
  19. % 5 - green - start
  20. % 6 - yellow - destination

  21. cmap = [1 1 1; ...
  22.     0 0 0; ...
  23.     1 0 0; ...
  24.     0 0 1; ...
  25.     0 1 0; ...
  26.     1 1 0; ...
  27.     0.5 0.5 0.5];

  28. colormap(cmap);

  29. % variable to control if the map is being visualized on every
  30. % iteration
  31. drawMapEveryTime = true;

  32. [nrows, ncols] = size(input_map);

  33. % map - a table that keeps track of the state of each grid cell用来上色的
  34. map = zeros(nrows,ncols);

  35. map(~input_map) = 1;   % Mark free cells
  36. map(input_map)  = 2;   % Mark obstacle cells

  37. % Generate linear indices of start and dest nodes将下标转换为线性的索引值
  38. start_node = sub2ind(size(map), start_coords(1), start_coords(2));
  39. dest_node  = sub2ind(size(map), dest_coords(1),  dest_coords(2));

  40. map(start_node) = 5;
  41. map(dest_node)  = 6;

  42. % meshgrid will `replicate grid vectors' nrows and ncols to produce
  43. % a full grid
  44. % type `help meshgrid' in the Matlab command prompt for more information
  45. parent = zeros(nrows,ncols);%用来记录每个节点的父节点

  46. %
  47. [X, Y] = meshgrid (1:ncols, 1:nrows);

  48. xd = dest_coords(1);
  49. yd = dest_coords(2);

  50. % Evaluate Heuristic function, H, for each grid cell
  51. % Manhattan distance用曼哈顿距离作为启发式函数
  52. H = abs(X - xd) + abs(Y - yd);
  53. H = H';
  54. % Initialize cost arrays
  55. f = Inf(nrows,ncols);
  56. g = Inf(nrows,ncols);

  57. g(start_node) = 0;
  58. f(start_node) = H(start_node);

  59. % keep track of the number of nodes that are expanded
  60. numExpanded = 0;

  61. % Main Loop

  62. while true

  63.     % Draw current map
  64.     map(start_node) = 5;
  65.     map(dest_node) = 6;

  66.     % make drawMapEveryTime = true if you want to see how the
  67.     % nodes are expanded on the grid.
  68.     if (drawMapEveryTime)
  69.         image(1.5, 1.5, map);
  70.         grid on;
  71.         axis image;
  72.         drawnow;
  73.     end

  74.     % Find the node with the minimum f value,其中的current是index值,需要转换
  75.     [min_f, current] = min(f(:));

  76.     if ((current == dest_node) || isinf(min_f))
  77.         break;
  78.     end;

  79.     % Update input_map
  80.     map(current) = 3;
  81.     f(current) = Inf; % remove this node from further consideration
  82.     numExpanded=numExpanded+1;
  83.     % Compute row, column coordinates of current node
  84.     [i, j] = ind2sub(size(f), current);

  85.     % *********************************************************************
  86.     % ALL YOUR CODE BETWEEN THESE LINES OF STARS
  87.     % Visit all of the neighbors around the current node and update the
  88.     % entries in the map, f, g and parent arrays
  89.     %
  90.     action=[-1 0; 1 0; 0 -1; 0 1];%上,下,左,右
  91.     for a=1:4
  92.         expand=[i,j]+action(a,:);
  93.         expand1=expand(1,1);
  94.         expand2=expand(1,2);
  95.         %不超出边界,不穿越障碍,不在CLOSED列表里,也不是起点,则进行扩展
  96.         if ( expand1>=1 && expand1<=nrows && expand2>=1 && expand2<=nrows && map(expand1,expand2)~=2 && map(expand1,expand2)~=3 && map(expand1,expand2)~=5)
  97.             if ( g(expand1,expand2)> g(i,j)+1 )
  98.                 g(expand1,expand2)= g(i,j)+1;
  99.                 f(expand1,expand2)= g(expand1,expand2)+H(expand1,expand2);
  100.                 parent(expand1,expand2)=current;
  101.                 map(expand1,expand2)=4;
  102.             end
  103.         end
  104.     end
  105.     %*********************************************************************


  106. end

  107. %% Construct route from start to dest by following the parent links
  108. if (isinf(f(dest_node)))
  109.     route = [];
  110. else
  111.     route = [dest_node];

  112.     while (parent(route(1)) ~= 0)
  113.         route = [parent(route(1)), route];
  114.     end

  115.     % Snippet of code used to visualize the map and the path
  116.     for k = 2:length(route) - 1        
  117.         map(route(k)) = 7;
  118.         pause(0.1);
  119.         image(1.5, 1.5, map);
  120.         grid on;
  121.         axis image;
  122.     end
  123. end

  124. end
复制代码
DijkstraGrid.m

  1. function [route,numExpanded] = DijkstraGrid (input_map, start_coords, dest_coords)
  2. % Run Dijkstra's algorithm on a grid.
  3. % Inputs :
  4. %   input_map : a logical array where the freespace cells are false or 0 and
  5. %   the obstacles are true or 1
  6. %   start_coords and dest_coords : Coordinates of the start and end cell
  7. %   respectively, the first entry is the row and the second the column.
  8. % Output :
  9. %    route : An array containing the linear indices of the cells along the
  10. %    shortest route from start to dest or an empty array if there is no
  11. %    route. This is a single dimensional vector
  12. %    numExpanded: Remember to also return the total number of nodes
  13. %    expanded during your search. Do not count the goal node as an expanded node.


  14. % set up color map for display
  15. % 1 - white - clear cell
  16. % 2 - black - obstacle
  17. % 3 - red = visited
  18. % 4 - blue  - on list
  19. % 5 - green - start
  20. % 6 - yellow - destination

  21. cmap = [1 1 1; ...
  22.         0 0 0; ...
  23.         1 0 0; ...
  24.         0 0 1; ...
  25.         0 1 0; ...
  26.         1 1 0; ...
  27.         0.5 0.5 0.5];

  28. colormap(cmap);

  29. % variable to control if the map is being visualized on every
  30. % iteration
  31. drawMapEveryTime = true;

  32. [nrows, ncols] = size(input_map);

  33. % map - a table that keeps track of the state of each grid cell
  34. map = zeros(nrows,ncols);

  35. map(~input_map) = 1;   % Mark free cells
  36. map(input_map)  = 2;   % Mark obstacle cells

  37. % Generate linear indices of start and dest nodes
  38. start_node = sub2ind(size(map), start_coords(1), start_coords(2));
  39. dest_node  = sub2ind(size(map), dest_coords(1),  dest_coords(2));

  40. map(start_node) = 5;
  41. map(dest_node)  = 6;

  42. % Initialize distance array
  43. distanceFromStart = Inf(nrows,ncols);

  44. % For each grid cell this array holds the index of its parent
  45. parent = zeros(nrows,ncols);

  46. distanceFromStart(start_node) = 0;

  47. % keep track of number of nodes expanded
  48. numExpanded = 0;

  49. % Main Loop
  50. while true

  51.     % Draw current map
  52.     map(start_node) = 5;
  53.     map(dest_node) = 6;

  54.     % make drawMapEveryTime = true if you want to see how the
  55.     % nodes are expanded on the grid.
  56.     if (drawMapEveryTime)
  57.         image(1.5, 1.5, map);
  58.         grid on;
  59.         axis image;
  60.         drawnow;
  61.     end

  62.     % Find the node with the minimum distance
  63.     [min_dist, current] = min(distanceFromStart(:));

  64.     if ((current == dest_node) || isinf(min_dist))
  65.         break;
  66.     end;

  67.     % Update map
  68.     map(current) = 3;         % mark current node as visited
  69.     numExpanded=numExpanded+1;
  70.     % Compute row, column coordinates of current node
  71.     [i, j] = ind2sub(size(distanceFromStart), current);

  72.    % *********************************************************************
  73.     % YOUR CODE BETWEEN THESE LINES OF STARS

  74.     % Visit each neighbor of the current node and update the map, distances
  75.     % and parent tables appropriately.
  76.     action=[-1 0; 1 0; 0 -1; 0 1];%上,下,左,右
  77.     for a=1:4
  78.         expand=[i,j]+action(a,:);
  79.         expand1=expand(1,1);
  80.         expand2=expand(1,2);
  81.         %不超出边界,不穿越障碍,不在CLOSED列表里,则进行扩展
  82.         if ( expand1>=1 && expand1<=nrows && expand2>=1 && expand2<=ncols && map(expand1,expand2)~=2 && map(expand1,expand2)~=3 && map(expand1,expand2)~=5 )
  83. %           if ( expand1>=1 && expand1<=nrows && expand2>=1 && expand2<=ncols && map(expand1,expand2)~=2 && map(expand1,expand2)~=3 && map(expand1,expand2)~=5)
  84.             if ( distanceFromStart(expand1,expand2)> distanceFromStart(i,j)+1 )
  85.                 distanceFromStart(expand1,expand2)= distanceFromStart(i,j)+1;
  86.                 parent(expand1,expand2)=current;
  87.                 map(expand1,expand2)=4;
  88.             end
  89.         end
  90.     end
  91.     distanceFromStart(current) = Inf; % remove this node from further consideration
  92.     %*********************************************************************

  93. end

  94. %% Construct route from start to dest by following the parent links
  95. if (isinf(distanceFromStart(dest_node)))
  96.     route = [];
  97. else
  98.     route = [dest_node];

  99.     while (parent(route(1)) ~= 0)
  100.         route = [parent(route(1)), route];
  101.     end

  102.         % Snippet of code used to visualize the map and the path
  103.     for k = 2:length(route) - 1        
  104.         map(route(k)) = 7;
  105.         pause(0.1);
  106.         image(1.5, 1.5, map);
  107.         grid on;
  108.         axis image;
  109.     end
  110. end

  111. end
复制代码
注: 运行环境,Matlab 2019a 版本,安装 `RTB(Robotic Tool Box)` 工具包,链接地址为,[RTB安装链接](https://petercorke.com/toolboxes/robotics-toolbox/) 。该工具包中可以运行作者大佬写到的matlab/simulink四轴历程,只需要使用指令 `sl_quadrotor` 即可。

                               
登录/注册后可看大图


                               
登录/注册后可看大图

使用方法:
在Matlab 中,使用 makemap(30) 来生成地图,通过鼠标来设置障碍形状。该例子生成了一个`30*30`的方阵,然后直接运行TestScript.m即可。其中要在TestScript.m中选择是采用A*算法,还是Dijkstra算法。同时设置起始点和终点在哪。下图显示得到的A*算法路径优化结果。其中绿色点为起点,黄色点为终点,黑色表示障碍,白色表示空闲,红色表示搜寻过,灰色表示最后规划的路径。

                               
登录/注册后可看大图

下图显示Dijkstra算法的路径优化结果:

                               
登录/注册后可看大图

对应的动态效果已经录屏,下面给出传送门(录屏水印广告请忽略):
1. [A*路径优化算法Matlab实现](
)
2. [Dijkstra路径优化算法matlab实现](
)

通过对比可以看出:A* 算法搜索速度较快,毕竟里面有贪心算法。这在地图较大的场景应用较好。但是A*算法只能得到局部最优解,并不能保证全局最优解。相比之下,Dijkstra算法尽管搜索速度慢,但是是全局最优解。不知道两种方法结合gmapping,hector或者cartographer生成的栅格地图会是什么样的效果。后面期待尝试一下。


扫一扫浏览分享
回复

使用道具 举报

176

阿木币

0

精华

348 小时

在线时间

版主

Rank: 7Rank: 7Rank: 7

发表于 2020-6-10 14:04:56
这博客文章越来越6了啊,视频走起来~~~
回复 点赞

使用道具 举报

0

阿木币

0

精华

182 小时

在线时间

技术大V

Rank: 4

 楼主| 发表于 2020-6-10 20:20:18
maxiou 发表于 2020-6-10 14:04
这博客文章越来越6了啊,视频走起来~~~

回复 点赞

使用道具 举报

131

阿木币

0

精华

270 小时

在线时间

管理员

Rank: 9Rank: 9Rank: 9

发表于 2020-6-13 11:06:39

文章很溜啊 到时候做课程老师啊
我不为己,谁人为我,但我只为己,那我又是谁?
回复 点赞

使用道具 举报

0

阿木币

0

精华

182 小时

在线时间

技术大V

Rank: 4

 楼主| 发表于 2020-6-13 16:10:19
amov_msq 发表于 2020-6-13 11:06
文章很溜啊 到时候做课程老师啊

还是萧齐老师讲的好,我这半吊子水平不行
回复 点赞

使用道具 举报

返回列表
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表