本文主要是介绍【路径规划】基于matlab GUI多种蚁群算法栅格地图最短路径规划【含Matlab源码 650期】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
✅博主简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,Matlab项目合作可私信。
🍎个人主页:海神之光
🏆代码获取方式:
海神之光Matlab王者学习之路—代码获取方式
⛳️座右铭:行百里者,半于九十。
更多Matlab仿真内容点击👇
Matlab图像处理(进阶版)
路径规划(Matlab)
神经网络预测与分类(Matlab)
优化求解(Matlab)
语音处理(Matlab)
信号处理(Matlab)
车间调度(Matlab)
⛄一、蚁群算法及栅格地图简介
1 蚁群算法
1.1 蚁群算法的提出
蚁群算法(ant colony optimization, ACO),又称蚂蚁算法,是一种用来寻找优化路径的机率型算法。它由Marco Dorigo于1992年在他的博士论文中提出,其灵感来源于蚂蚁在寻找食物过程中发现路径的行为。遗传算法在模式识别、神经网络、机器学习、工业优化控制、自适应控制、生物科学、社会科学等方面都得到应用。
1.2 蚁群算法基本原理
2 栅格地图
2.1 栅格法应用背景
路径规划时首先要获取环境信息, 建立环境地图, 合理的环境表示有利于建立规划方法和选择合适的搜索算法,最终实现较少的时间开销而规划出较为满意的路径。一般使用栅格法在静态环境下建立环境地图。
2.2 栅格法实质
将AGV的工作环境进行单元分割, 将其用大小相等的方块表示出来,这样栅格大小的选取是影响规划算法性能的一个很重要的因素。栅格较小的话,由栅格地图所表示的环境信息将会非常清晰,但由于需要存储较多的信息,会增大存储开销,同时干扰信号也会随之增加,规划速度会相应降低,实时性得不到保证;反之,由于信息存储量少,抗干扰能力有所增强,规划速随之增快,但环境信息划分会变得较为模糊,不利于有效路径的规划。在描述环境信息时障碍物所在区域在栅格地图中呈现为黑色,地图矩阵中标为1,可自由通行区域在栅格地图中呈现为白色,地图矩阵中标为0。路径规划的目的就是在建立好的环境地图中找到一条最优的可通行路径,所以使用栅格法建立环境地图时,栅格大小的合理设定非常关键。
2.3 10乘10的静态环境地图
10乘10的静态环境地图代码
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%建立环境地图%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function DrawMap(map)
n = size(map);
step = 1;
a = 0 : step :n(1);
b = 0 : step :n(2);
figure(1)
axis([0 n(2) 0 n(1)]); %设置地图横纵尺寸
set(gca,'xtick',b,'ytick',a,'GridLineStyle','-',...
'xGrid','on','yGrid','on');
hold on
r = 1;
for(i=1:n(1)) %设置障碍物的左下角点的x,y坐标for(j=1:n(2))if(map(i,j)==1)p(r,1)=j-1;p(r,2)=i-1;fill([p(r,1) p(r,1) + step p(r,1) + step p(r,1)],...[p(r,2) p(r,2) p(r,2) + step p(r,2) + step ],'k');r=r+1;hold onendend
end%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%栅格数字标识%%%%%%%%%%%%%%%%%%%%%%%%%%%%
x_text = 1:1:n(1)*n(2); %产生所需数值.
for i = 1:1:n(1)*n(2)[row,col] = ind2sub([n(2),n(1)],i);text(row-0.9,col-0.5,num2str(x_text(i)),'FontSize',8,'Color','0.7 0.7 0.7');
end
hold on
axis square
建立环境矩阵,1代表黑色栅格,0代表白色栅格,调用以上程序,即可得到上述环境地图。
map=[0 0 0 1 0 0 1 0 0 0;1 0 0 0 0 1 1 0 0 0;0 0 1 0 0 0 1 1 0 0;0 0 0 0 0 0 0 0 0 0;0 0 0 0 0 1 0 0 1 0;1 0 0 0 0 1 1 0 0 0;0 0 0 1 0 0 0 0 0 0;1 1 1 0 0 0 1 0 0 0;0 0 0 0 0 1 1 0 0 0;0 0 0 0 0 1 1 0 0 0;];DrawMap(map); %得到环境地图
2.4 栅格地图中障碍栅格处路径约束
移动体栅格环境中多采用八方向的移动方式,此移动方式在完全可通行区域不存在运行安全问题,当
移动体周围存在障碍栅格时此移动方式可能会发生与障碍物栅格的碰撞问题,为解决此问题加入约束
条件,当在分别与障碍物栅格水平方向和垂直方向的可行栅格两栅格之间通行时,禁止移动体采用对
角式移动方式。
约束条件的加入,实质是改变栅格地图的邻接矩阵,将障碍栅格(数字为“1”的矩阵元素)的对角栅格
设为不可达, 即将对角栅格的距离值改为无穷大。其实现MATLAB代码如下:
代码:
%约束移动体在障碍栅格对角运动
%通过优化邻接矩阵实现
%%%%%%%%%%%%%%%%%% 约束移动体移动方式 %%%%%%%%%%%%%%%%%
function W=OPW(map,W)
% map 地图矩阵 % W 邻接矩阵
n = size(map);
num = n(1)*n(2);
for(j=1:n(1))for(z=1:n(2))if(map(j,z)==1)if(j==1) %若障碍物在第一行if(z==1) %若障碍物为第一行的第一个W(j+1,j+n(2)*j)=Inf;W(j+n(2)*j,j+1)=Inf;elseif(z==n(2)) %若障碍物为第一行的最后一个W(n(2)-1,n(2)+n(1)*j)=Inf;W(n(2)+n(1)*j,n(2)-1)=Inf;else %若障碍物为第一行的其他W(z-1,z+j*n(2))=Inf;W(z+j*n(2),z-1)=Inf;W(z+1,z+j*n(2))=Inf;W(z+j*n(2),z+1)=Inf;endendendif(j==n(1)) %若障碍物在最后一行if(z==1) %若障碍物为最后一行的第一个W(z+n(2)*(j-2),z+n(2)*(j-1)+1)=Inf;W(z+n(2)*(j-1)+1,z+n(2)*(j-2))=Inf;elseif(z==n(2)) %若障碍物为最后一行的最后一个W(n(1)*n(2)-1,(n(1)-1)*n(2))=Inf;W((n(1)-1)*n(2),n(1)*n(2)-1)=Inf;else %若障碍物为最后一行的其他W((j-2)*n(2)+z,(j-1)*n(2)+z-1)=Inf;W((j-1)*n(2)+z-1,(j-2)*n(2)+z)=Inf;W((j-2)*n(2)+z,(j-1)*n(2)+z+1)=Inf;W((j-1)*n(2)+z+1,(j-2)*n(2)+z)=Inf;endendendif(z==1) if(j~=1&&j~=n(1)) %若障碍物在第一列非边缘位置 W(z+(j-2)*n(2),z+1+(j-1)*n(2))=Inf;W(z+1+(j-1)*n(2),z+(j-2)*n(2))=Inf;W(z+1+(j-1)*n(2),z+j*n(2))=Inf;W(z+j*n(2),z+1+(j-1)*n(2))=Inf;endendif(z==n(2))if(j~=1&&j~=n(1)) %若障碍物在最后一列非边缘位置 W((j+1)*n(2),j*n(2)-1)=Inf;W(j*n(2)-1,(j+1)*n(2))=Inf;W(j*n(2)-1,(j-1)*n(2))=Inf;W((j-1)*n(2),j*n(2)-1)=Inf;endendif(j~=1&&j~=n(1)&&z~=1&&z~=n(2)) %若障碍物在非边缘位置W(z+(j-1)*n(2)-1,z+j*n(2))=Inf;W(z+j*n(2),z+(j-1)*n(2)-1)=Inf;W(z+j*n(2),z+(j-1)*n(2)+1)=Inf;W(z+(j-1)*n(2)+1,z+j*n(2))=Inf;W(z+(j-1)*n(2)-1,z+(j-2)*n(2))=Inf;W(z+(j-2)*n(2),z+(j-1)*n(2)-1)=Inf;W(z+(j-2)*n(2),z+(j-1)*n(2)+1)=Inf;W(z+(j-1)*n(2)+1,z+(j-2)*n(2))=Inf;endendendend
end
6 栅格法案例
下面以Djkstra算法为例, 其实现如下:
map=[0 0 0 1 0 0 1 0 0 0;1 0 0 0 0 1 1 0 0 0;0 0 1 0 0 0 1 1 0 0;0 0 0 0 0 0 0 0 0 0;0 0 0 0 0 1 0 0 1 0;1 0 0 0 0 1 1 0 0 0;0 0 0 1 0 0 0 0 0 0;1 1 1 0 0 0 1 0 0 0;0 0 0 0 0 1 1 0 0 0;0 0 0 0 0 1 1 0 0 0;];%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%建立环境矩阵map%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
DrawMap(map); %得到环境地图
W=G2D(map); %得到环境地图的邻接矩阵
W(W==0)=Inf; %邻接矩阵数值处理
W=OPW(map,W); %优化邻接矩阵
[distance,path]=dijkstra(W,1,100);%设置起始栅格,得到最短路径距离以及栅格路径
[x,y]=Get_xy(distance,path,map); %得到栅格相应的x,y坐标
Plot(distance,x,y); %画出路径
运行结果如下:
其中函数程序:
DrawMap(map) 详见建立栅格地图
W=G2D(map) ; 详见建立邻接矩阵
[distance, path] =dijkstra(W, 1, 100) 详见Djk stra算法
[x, y] =Get_xy(distance, path, map) ;
Plot(distance, x, y) ;
⛄二、部分源代码
function varargout = main_GUI_xu(varargin)
% MAIN_GUI_XU MATLAB code for main_GUI_xu.fig
% MAIN_GUI_XU, by itself, creates a new MAIN_GUI_XU or raises the existing
% singleton*.
%
% H = MAIN_GUI_XU returns the handle to a new MAIN_GUI_XU or the handle to
% the existing singleton*.
%
% MAIN_GUI_XU(‘CALLBACK’,hObject,eventData,handles,…) calls the local
% function named CALLBACK in MAIN_GUI_XU.M with the given input arguments.
%
% MAIN_GUI_XU(‘Property’,‘Value’,…) creates a new MAIN_GUI_XU or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before main_GUI_xu_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to main_GUI_xu_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE’s Tools menu. Choose “GUI allows only one
% instance to run (singleton)”.
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help main_GUI_xu
% Last Modified by GUIDE v2.5 22-Mar-2017 15:58:57
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct(‘gui_Name’, mfilename, …
‘gui_Singleton’, gui_Singleton, …
‘gui_OpeningFcn’, @main_GUI_xu_OpeningFcn, …
‘gui_OutputFcn’, @main_GUI_xu_OutputFcn, …
‘gui_LayoutFcn’, [] , …
‘gui_Callback’, []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% — Executes just before main_GUI_xu is made visible.
function main_GUI_xu_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to main_GUI_xu (see VARARGIN)
% Choose default command line output for main_GUI_xu
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes main_GUI_xu wait for user response (see UIRESUME)
% uiwait(handles.figure1);
init_all(handles)
set(handles.edit38,‘string’,‘先选择实验目的,默认选择目的一,即运行一种算法’);
% — Outputs from this function are returned to the command line.
function varargout = main_GUI_xu_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% — Executes on selection change in listbox1.
function listbox1_Callback(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,‘String’)) returns listbox1 contents as cell array
% contents{get(hObject,‘Value’)} returns selected item from listbox1
% — Executes during object creation, after setting all properties.
function listbox1_CreateFcn(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: listbox controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,‘BackgroundColor’), get(0,‘defaultUicontrolBackgroundColor’))
set(hObject,‘BackgroundColor’,‘white’);
end
% — Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% 绘制栅格线和调用figure的屏幕回调函数
global barrier pushbutton1_userdata
pushbutton1_userdata = 1; % 控制设计障碍物按钮是否能够使用
set(handles.edit38,‘string’,‘障碍物设计完成后,请点击输出障碍物按钮,否则容易出错’);
axes(handles.axes1)
cla reset
n_barrier = str2double(get(handles.edit1,‘string’));
barrier = zeros(n_barrier,n_barrier);
axes(handles.axes1);
s.hf = get(handles.axes1,‘parent’);
% 绘制栅格边线
for i=0:1:n_barrier
plot([0,n_barrier],[i,i],‘color’,‘k’);
hold on
axis([0,n_barrier,0,n_barrier])
for j=0:1:n_barrier
plot([i,i],[0,n_barrier],‘color’,‘k’) ;
hold on
axis([0,n_barrier,0,n_barrier])
end
end
% 设置figure的WindowButtonDownFcn属性
set(s.hf,‘WindowButtonDownFcn’,@figure1_windowbuttondownfcn)
% — Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,‘String’)) returns popupmenu1 contents as cell array
% contents{get(hObject,‘Value’)} returns selected item from popupmenu1
global barrier_select start_select goal_select pushbutton1_userdata barrier start goal
barrier_value = get(handles.popupmenu1,‘value’);
cd(‘barrier’)
if barrier_value2
barrier_select = xlsread(‘e_barrier’);
start_select = 20;
goal_select = 295;
pushbutton1_userdata = 0;
elseif barrier_value3
barrier_select = xlsread(‘simple_e’);
start_select = 20;
goal_select = 295;
pushbutton1_userdata = 0;
elseif barrier_value4
barrier_select = xlsread(‘u_barrier’);
start_select = 1;
goal_select = 400;
pushbutton1_userdata = 0;
elseif barrier_value5
barrier_select = xlsread(‘light_u_barrier’);
start_select = 1;
goal_select = 400;
pushbutton1_userdata = 0;
elseif barrier_value6
barrier_select = xlsread(‘right_u_barrier’);
start_select = 1;
goal_select = 400;
pushbutton1_userdata = 0;
elseif barrier_value7
barrier_select = xlsread(‘z_barrier’);
start_select = 49;
goal_select = 369;
pushbutton1_userdata = 0;
elseif barrier_value8
barrier_select = xlsread(‘complex_z’);
start_select = 47;
goal_select = 367;
pushbutton1_userdata = 0;
elseif barrier_value9
barrier_select = xlsread(‘complex_1’);
start_select = 1;
goal_select = 400;
pushbutton1_userdata = 0;
elseif barrier_value10
barrier_select = xlsread(‘complex_2’);
start_select = 1;
goal_select = 400;
pushbutton1_userdata = 0;
elseif barrier_value11
barrier_select = xlsread(‘complex_30’);
start_select = 1;
goal_select = 890;
pushbutton1_userdata = 0;
elseif barrier_value12
barrier_select = xlsread(‘complex_50_1’);
start_select = 1;
goal_select = 2500;
elseif barrier_value13
barrier_select = xlsread(‘complex_50_2’);
start_select = 1;
goal_select = 2500;
pushbutton1_userdata = 0;
elseif barrier_value14
barrier_select = xlsread(‘complex_50_3’);
start_select = 1;
goal_select = 2500;
pushbutton1_userdata = 0;
elseif barrier_value15
barrier_select = xlsread(‘barrier_tmp’);
set(handles.edit18,‘string’,‘请选择起点和终点’)
pushbutton1_userdata = 0;
start = 0;
goal = 0;
end
⛄三、运行结果
⛄四、matlab版本及参考文献
1 matlab版本
2014a
2 参考文献
[1]周东健,张兴国,马海波,李成浩,郭旭.基于栅格地图-蚁群算法的机器人最优路径规划[J].制造业自动化. 2014,36(05)
3 备注
简介此部分摘自互联网,仅供参考,若侵权,联系删除
🍅 仿真咨询
1 各类智能优化算法改进及应用
生产调度、经济调度、装配线调度、充电优化、车间调度、发车优化、水库调度、三维装箱、物流选址、货位优化、公交排班优化、充电桩布局优化、车间布局优化、集装箱船配载优化、水泵组合优化、解医疗资源分配优化、设施布局优化、可视域基站和无人机选址优化
2 机器学习和深度学习方面
卷积神经网络(CNN)、LSTM、支持向量机(SVM)、最小二乘支持向量机(LSSVM)、极限学习机(ELM)、核极限学习机(KELM)、BP、RBF、宽度学习、DBN、RF、RBF、DELM、XGBOOST、TCN实现风电预测、光伏预测、电池寿命预测、辐射源识别、交通流预测、负荷预测、股价预测、PM2.5浓度预测、电池健康状态预测、水体光学参数反演、NLOS信号识别、地铁停车精准预测、变压器故障诊断
3 图像处理方面
图像识别、图像分割、图像检测、图像隐藏、图像配准、图像拼接、图像融合、图像增强、图像压缩感知
4 路径规划方面
旅行商问题(TSP)、车辆路径问题(VRP、MVRP、CVRP、VRPTW等)、无人机三维路径规划、无人机协同、无人机编队、机器人路径规划、栅格地图路径规划、多式联运运输问题、车辆协同无人机路径规划、天线线性阵列分布优化、车间布局优化
5 无人机应用方面
无人机路径规划、无人机控制、无人机编队、无人机协同、无人机任务分配
6 无线传感器定位及布局方面
传感器部署优化、通信协议优化、路由优化、目标定位优化、Dv-Hop定位优化、Leach协议优化、WSN覆盖优化、组播优化、RSSI定位优化
7 信号处理方面
信号识别、信号加密、信号去噪、信号增强、雷达信号处理、信号水印嵌入提取、肌电信号、脑电信号、信号配时优化
8 电力系统方面
微电网优化、无功优化、配电网重构、储能配置
9 元胞自动机方面
交通流 人群疏散 病毒扩散 晶体生长
10 雷达方面
卡尔曼滤波跟踪、航迹关联、航迹融合
这篇关于【路径规划】基于matlab GUI多种蚁群算法栅格地图最短路径规划【含Matlab源码 650期】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!