dijkstra法路径规划matlab

Dijkstra法路径规划matlab

  • 前言
  • 一、dijkstra算法是什么?
  • 二、Dijkstra算法路径规划matlab程序
  • 总结

前言

随着无人驾驶时代的来临,路径规划技术也越来越为人所熟知,本文简单介绍了Dijkstra算法,并给出了其matlab程序


一、dijkstra算法是什么?

Dijkstra’s algorithm,可译为迪杰斯特拉算法,亦可不音译而称为Dijkstra算法,是由荷兰计算机科学家Edsger W. Dijkstra 在1956年发现的算法,并于3年后在期刊上发表。Dijkstra算法使用类似广度优先搜索的方法解决赋权图的单源最短路径问题,但需要注意绝大多数的Dijkstra算法不能有效处理带有负权边的图。Dijkstra算法在计算机科学的人工智能等领域也被称为均一开销搜索,并被认为是最良优先搜索的一个特例。
具体运行步骤可参考数据结构–Dijkstra算法最清楚的讲解。

二、Dijkstra算法路径规划matlab程序

Dijkstara算法路径规划GitHub地址https://github.com/Say-Hello2y/Path-planning-for-robots
程序包括七个子程序,运行时一定要把matlab工作目录调到程序所在目录,运行时只需运行debug即可出来结果。
下面分别介绍各个程序的作用:
debug.m :为运行程序,里面定义了起始点和终止点以及其他必要的准备,并将规划出的路线在栅格地图上画出来。

clear;
clc;
close all;
[map]=creMap()%Map采用的是按要求生成18-20个障碍物,每个障碍物占6-16格
start=[1 100]%起点的坐标
goal=[100 1]%终点的坐标
Dmap=G2D(map);%生成栅格地图的邻接矩阵
PlotGrid(map,start,goal)%画出二维栅格地图
[distance,route]=Dijkstras(Dmap,9901,100)%由算法得到最短距离和具体路先
[x y]=Get_xy(distance,route,map);%将具体路线用栅格坐标表示出来
Plot(distance,x,y)%在栅格图上画出具体路线

creMap.m :创造地图矩阵

function [Map]=creMap()
%地图矩阵生成函数,生成的是100*100的地图矩阵
Map=ones(100)
for i=1:randi([19 21]) %障碍物个数
p=randi([1 90]);
q=randi([1 90]);
j1=randi([2 4]);
j2=randi([3 4]);
Map(p:p+j1,q:q+j2)=0;
end
end

G2D.m:由于Dijkstra为基于图的算法,此程序用于将栅格地图转换为邻接矩阵。

%%%%%%%%%%%%%%%%%%%%%%%%得到环境地图的邻接矩阵%%%%%%%%%%%%%%%%%%%%%%%%
function W=G2D(map)
l=size(map);
W=zeros(l(1)*l(2),l(1)*l(2));
for i=1:l(1)for j=1:l(2)if map(i,j)==1for m=1:l(1)for n=1:l(2)if map(m,n)==1im=abs(i-m);jn=abs(j-n);if im+jn==1||(im==1&&jn==1)W((i-1)*l(2)+j,(m-1)*l(2)+n)=(im+jn)^0.5;endendendendendend
endW(W==0)=Inf;     %不相邻栅格距离值为Inf(无穷大),将对角元素也设置为了Inf,在Dijkstra算法中使用无影响。
end

Get_xy.m: 由dijkstra算法生成的路径点和地图计算出具体的栅格地图路线。

%%%%%%%%%%%%%%%%%%%%%根据栅格标识,得到相应的x,y坐标%%%%%%%%%%%%%%%%%%%%%function [x y]=Get_xy(distance,path,map)
% distance 路径长度 若不可达路径不执行该函数程序
% path 路径节点向量
% map  栅格地图信息
if(distance~=Inf)                %判断路径是否可达n = size(map);N = size(path,2);for(i=1:N)     %根据栅格标识,得到相应的x,y坐标if rem(path(i),n(2))==0x(i)=n(2);y(i)= floor(path(i)/n(2));elsex(i)= rem(path(i),n(2));y(i)= floor(path(i)/n(2))+1;endend
elsex = 0;y = 0;
end
4

Plot.m :画出具体的路线

%%%%%%%%%%%%%%%%%%%%%绘制具体路径%%%%%%%%%%%%%%%%%%%%%
function  Plot(distance,x,y)
if(distance~=Inf)            %判断路径是否可达plot(x+0.5,y+0.5,'r');axis image xyhold on
elsedisp('路径不可达');hold on
end

Dijkstra.m:算法的核心,利用基于图的Dijkstra算法计算两点之间的最短路径,函数输入起始点,终止点以及邻接矩阵输出两点之间最短距离以及路径点,需要注意的是这里输入的起始点为转化成图后的点,在本算法中为按照列的顺序排列,及起始点在栅格地图中若为[1 100]则对应第9901个节点。

%%%%%%%%%%%%%%%%%%%%%dijkstraË㷨ʵÏÖ%%%%%%%%%%%%%%%%%%%%%
function [Cost, Route] = Dijkstras( Graph, SourceNode, TerminalNode )
%Dijkstras.m Given a graph with distances from node to node calculates the
%optimal route from the Source Node to the Terminal Node as defined by the
%inputs.
%
% Inputs:
%
% Graph - A graph of the costs from each node to each other also known as
% an adjacency matrix. In the context of this project it is a matrix of the
% distances from cities within North Carolina to each other. You can mark
% unconnected nodes as such by assigning them values of Inf. For instance,
% if nodes 3 & 5 are unconnected then Graph(3,5)=Graph(5,3)=Inf;
%
% SourceNode - The index of the node (city) which you wish to travel from
%
% TerminalNode - The index of the node (city) which you wish to travel to
%
% Outputs:
%
% Cost - The cost to travel from the source node to the terminal node.
% There are several metrics this could be but in the case of route planning
% which I am working on this is distance in miles.
%
% Route - A vector containing the indices of the nodes traversed when going
% from the source node to the terminal node. Always begins with the source
% node and will always end at the terminal node unless the algorithm fails
% to converge due to there not existing a valid path between the source
% node and the terminal node. % Check for valid parameters
if size(Graph,1) ~= size(Graph,2)fprintf('The Graph must be a square Matrix\n');return;
elseif min(min(Graph)) < 0fprintf('Dijkstras algorithm cannot handle negative costs.\n')fprintf('Please use Bellman-Ford or another alternative instead\n');return;
elseif SourceNode < 1 && (rem(SourceNode,1)==0) && (isreal(SourceNode)) && (SourceNode <= size(Graph,1))fprintf('The source node must be an integer within [1, sizeofGraph]\n');return;
elseif TerminalNode < 1 && (rem(TerminalNode,1)==0) && isreal(TerminalNode) && (TerminalNode <= size(Graph,1))fprintf('The terminal node must be an integer within [1, sizeofGraph]\n');return;
end% Special Case so no need to waste time doing initializations
if SourceNode == TerminalNodeCost = Graph(SourceNode, TerminalNode);Route = SourceNode;return;
end% Set up a cell structure so that I can store the optimal path from source
% node to each node in this structure. This structure stores the
% antecedents so for instance if there is a path to B through A-->C-->D-->B
% you will see [A,C,D] in cell{B} (as well as a bunch of filler 0's after
% that)
PathToNode = cell(size(Graph,1),1);% Initialize all Node costs to infinity except for the source node
NodeCost = Inf.*ones(1,size(Graph,1));
NodeCost(SourceNode) = 0;% Initialize the Current Node to be the Source Node
CurrentNode = SourceNode;% Initialize the set of Visited and Unvisited Nodes
VisitedNodes = SourceNode;
UnvisitedNodes = 1:size(Graph,2);
UnvisitedNodes = UnvisitedNodes(UnvisitedNodes ~= VisitedNodes);while (CurrentNode ~= TerminalNode)% Extract the Costs/Path Lengths to each node from the current nodeCostVector = Graph(CurrentNode, :);% Only look at valid neighbors ie. those nodes which are unvisitedUnvisitedNeighborsCostVector = CostVector(UnvisitedNodes);% Extract the cost to get to the Current NodeCurrentNodeCost = NodeCost(CurrentNode);% Extract the path to the current nodePathToCurrentNode = PathToNode{CurrentNode};% Iterate through the Unvisited Neighbors assigning them a new tentative costfor i = 1:length(UnvisitedNeighborsCostVector)if UnvisitedNeighborsCostVector(i) ~= Inf % Only Check for update if non-infinitetempCost = CurrentNodeCost + UnvisitedNeighborsCostVector(i); % The tentative cost to get to the neighbor through the current node% Compare the tentative cost to the currently assigned cost and% assign the minimumif tempCost < NodeCost(UnvisitedNodes(i))NewPathToNeighbor = [PathToCurrentNode(PathToCurrentNode~=0) CurrentNode]; % The new path to get to the neighborNewPath = [NewPathToNeighbor zeros(1,size(Graph,1)-size(NewPathToNeighbor,2))];PathToNode{UnvisitedNodes(i)}(:) = NewPath;NodeCost(UnvisitedNodes(i)) = tempCost;endendend% Search for the smallest cost remaining that is in the unvisited setRemainingCosts = NodeCost(UnvisitedNodes);[MIN, MIN_IND] = min(RemainingCosts);% If the smallest remaining cost amongst the unvisited set of nodes is% infinite then there is no valid path from the source node to the% terminal node. if MIN == Inffprintf('There is no valid path from the source node to the');fprintf('terminal node. Please check your graph.\n')return;end% Update the Visited and Unvisited NodesVisitedNodes = [VisitedNodes CurrentNode];CurrentNode = UnvisitedNodes(MIN_IND);UnvisitedNodes = UnvisitedNodes(UnvisitedNodes~=CurrentNode);
endRoute = PathToNode{TerminalNode};
Route = Route(Route~=0);
Route = [Route TerminalNode];
Cost = NodeCost(TerminalNode);
end

PlotGrid.m:画出由creMap创造出的栅格地图。

%%%%%%%%%%%%%%%%%%%%%地图绘制函数%%%%%%%%%%%%%%%%%%%%%
function PlotGrid(map,start,goal)
n = size(map,1);
b = map;
b(end+1,end+1) = 0;
figure;
colormap([0 0 0;1 1 1])
pcolor(b); % 赋予栅格颜色
set(gca,'XTick',10:10:n,'YTick',10:10:n);  % 设置坐标
axis image xy% displayNum(n);%显示栅格中的数值.text(start(1)+0.5,start(2)+0.5,'START','Color','red','FontSize',10);%显示start字符
text(goal(1)+0.5,goal(2)+0.5,'GOAL','Color','red','FontSize',10);%显示goal字符hold on
%pin strat goal positon
scatter(start(1)+0.5,start(2)+0.5,'MarkerEdgeColor',[1 0 0],'MarkerFaceColor',[1 0 0], 'LineWidth',1);%start point
scatter(goal(1)+0.5,goal(2)+0.5,'MarkerEdgeColor',[0 1 0],'MarkerFaceColor',[0 1 0], 'LineWidth',1);%goal pointhold on
end

总结

Dijkstra算法作为经典的基于图的最短路径算法被广泛用于路径规划上,同样著名的有Astar算法,人工势场法,遗传算法,蚁群算法,强化学习算法等,其中强化学习路径规划的python代码可见于我的另一篇博客中强化学习路径规划,需要注意的是这些上述的路径规划方法都没考虑到机器人或车辆的动力学模型,如果有这方面需要的话建议学习ROS或者Carla,ros与Carla之间可以相互连接实现更好的仿真效果,且ROS与Carla都为开源软件,使用免费,ROS自带的路径规划程序用的就是Dijkstra算法。

Dijkstra法路径规划matlab相关推荐

  1. 人工势场法路径规划算法(APF)

       本文主要对人工势场法路径规划算法进行介绍,主要涉及人工势场法的简介.引力和斥力模型及其推导过程.人工势场法的缺陷及改进思路.人工势场法的Python与MATLAB开源源码等方面    一.人工势 ...

  2. 3.蚁群算法求解格栅地图路径规划matlab代码

    往期: 1.Dijkstra算法求解格栅地图路径matlab代码_墨叔叔的博客-CSDN博客 2.A*搜索算法原理及matlab代码_墨叔叔的博客-CSDN博客 一.蚁群算法原理 原理:蚁群系统(An ...

  3. 灭火机器人路径规划matlab_机器人路径规划MATLAB源码

    机器人路径规划MATLAB源码 基于遗传算法的机器人路径规划 MATLAB 源码算法的思路如下:取各障碍物顶点连线的中点为路径点,相互连接各路径点,将机器人移动的起点和终点限制在各路径点上,利用 Di ...

  4. 25-混合A星算法Hybrid_Astar路径规划MATLAB代码

    资源: Hybrid-Astar(混合A星算法)路径规划MATLAB代码-电子商务文档类资源-CSDN文库 主要内容: 以车辆的运动学模型为节点,以当前点到终点的Astar距离和RS距离两者最大的距离 ...

  5. matlab三维路径规划,【路径规划】基于A星算法的三维路径规划matlab源码

    %% 该函数用于演示基于A_Star算法的三维路径规划算法 %% 清空环境 clc clear %% 数据初始化 %下载数据 starttime=cputime; load HeightData z ...

  6. 人工势场路径规划-matlab代码

    一.人工势场算法原理 人工势场法是广泛应用于机器人.智能车领域中的一种路径规划算法,其原理是将智能车在行驶环境中的运动转化为智能车在人为设定的抽象势场中的运动,抽象势场由引力.斥力两大势场组成. 将引 ...

  7. 【 无错版】基于蚁群算法的机器人路径规划matlab程序代码实现

    文章目录 1. 按 2. 介绍 3. matlab实现 3.1. 代码 3.2. 效果 1. 按 网上有发的这个算法的错误版的,不知道发的意义何在,是在误人子弟吗???在此对其行为表示强烈的谴责. 错 ...

  8. 遗传算法之路径规划matlab代码(栅格地图)含详细注释

    遗传算法本人在另一篇博文中已经有记载,本次将遗传算法用于路径规划的代码记录于此,用于大家一起学习 一起进步,如果有用,欢迎点赞. 1.基于遗传算法的栅格法机器人路径规划main.m % 基于遗传算法的 ...

  9. 【三维路径规划】基于RRT实现三维路径规划matlab源码

    对RRT算法感兴趣,是因为我对它不仅在二维平面上适用,在高维空间也能使用而感到心动,最近比较忙,下周或者什么时候要要用代码亲自实现一下. 路径规划都有哪些方法呢?比较流行的有:图搜索.势场法.RRT ...

  10. 【路径规划】基于蚁群算法求解机器人栅格地图路径规划matlab代码

    1 简介 通过栅格法建立栅格地图作为机器人路径规划的工作环境,采用蚁群算法作为机器人路径搜索的规则.将所有机器人放置于初始位置.经过NC次无碰撞迭代运动找到最优路径.到达目标位置.为防止机器人在路径搜 ...

最新文章

  1. Oracle数据库的逻辑结构和存储层次
  2. 理解矩阵的掩码操作 使用opencv锐化图片
  3. ARMS V4.3发布,应用监控全新支持内存快照分析,全息排查等功能。
  4. 分支管理---创建与合并分支
  5. zabbix-agent端自定义监控项(free -m)服务器内存使用率
  6. java jar包收集
  7. python中编函数_在python中编写函数
  8. 输入域html,我可以让HTML输入表单域成为可选的吗?
  9. 7 款 Python 开源框架的优劣总结
  10. Linux中brk()系统调用,sbrk(),mmap(),malloc(),calloc()的异同【转】
  11. Java 并发编程之读写锁 ReentrantReadWriteLock
  12. LaTex问题解决集[2]:解决插入Visio图片有多余边框和白边的问题
  13. liveness探测mysql_Kubernetes 服务中 Liveness 和 Readiness 探测
  14. makefile 目标:依赖文件写法
  15. 移动 APP 的测试方法,以及移动专项测试的思路与方法
  16. Linux 异常:The following signatures couldn‘t be verified because the public key is not available
  17. python人机猜拳_python实现人机猜拳小游戏
  18. Java开发必学:Java开发进大厂面试必备技能,技术总监都拍手叫好
  19. 【JVM源码解析】模板解释器解释执行Java字节码指令(上)
  20. 科学家无法解释的灵异现象转》

热门文章

  1. 通州区机器人比赛活动总结_2018年通州区中小学生机器人智能大赛开赛
  2. Unity3D四、This application has requested the Runtime to terminate it in an unusual way。Please contact
  3. 数学领域著名的“哥德巴赫猜想”的大致意思是:任何一个大于2的偶数总能表示为两个素数之和。比如:24=5+19,其中5和19都是素数。
  4. “双反”压制中国光伏海外生根 新兴市场前景广阔
  5. kmeans集群算法(cluster-reuters)
  6. C语言—输入数字查看星期
  7. IDEA 中启用 lombok
  8. 用贝叶斯估计法推出朴素贝叶斯法中的慨率估计公式
  9. 香港中文大学计算机辅助翻译课程,港中文翻译(MA in Translation)专业申请解析...
  10. ansible剧本的使用