【路径规划】基于强化学习Q-Learing实现栅格地图路径规划matlab源码

512 阅读5分钟

1 模型

提出了一种基于强化学习的机器人路径规划算法,该算法将激光雷达所获取的移动机器人周围障碍物信息与目标点所在方位信息离散成有限个状态,进而合理地设计环境模型与状态空间数目;设计了一种连续的报酬函数,使得机器人采取的每一个动作都能获得相应的报酬,提高了算法训练效率.最后在Gazebo中建立仿真环境,对该智能体进行学习训练,训练结果验证了算法的有效性;同时在实际机器人上进行导航实验,实验结果表明该算法在实际环境中也能够完成导航任务.

2 部分代码

function varargout =PathPlanning(varargin)
% 移动机器人路径规划仿真平台接口:仿真平台提供了机器人工作环境的仿真界面,利用inf=load('inf'),sp=inf.StartPoint,
% EP=inf.EndPoint,WS=inf.env得到机器人工作环境的出发点、目标点位置及障碍物位置信息,工作空间边界及障碍物区域设置为1,自由空间
%设置为0。
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                  'gui_Singleton',  gui_Singleton, ...
                  'gui_OpeningFcn', @Simulation_OpeningFcn, ...
                  'gui_OutputFcn',  @Simulation_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 GridSimulation is made visible.
function Simulation_OpeningFcn(hObject, eventdata, handles, varargin)

set(handles.StartPoint,'Enable','off')
set(handles.EndPoint,'Enable','off')
set(handles.Obstacle,'Enable','on')
set(handles.Start,'Enable','on')
set(handles.OldEnv,'Enable','on')       %“开始运行”按钮禁用
set(handles.MainFigure,'WindowButtonDownFcn','PathPlanning(''MainFigure_WindowButtonDownFcn'',gcbo,[],guidata(gcbo))');
%set(handles.MainFigure,'WindowButtonUpFcn','PathPlanning(''MainFigure_WindowButtonUpFcn'',gcbo,[],guidata(gcbo))');
function MainFigure_WindowButtonDownFcn(hObject, eventdata, handles)
% hObject   handle to MainFigure (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   structure with handles and user data (see GUIDATA)
   inf=load('inf'); 
   ws=inf.env;
   Pos=get(handles.MainAxes,'CurrentPoint');
   Pos=round(Pos);
   XPos=Pos(1,1);YPos=Pos(1,2);       %当前点坐标
   X=[XPos-.5,XPos-.5,XPos+.5,XPos+.5];
   Y=[YPos-.5,YPos+.5,YPos+.5,YPos-.5];
   fill(X,Y,[0 0 0])                   %画障碍物
   text(13-.2,12,'B','color',[1 1 1]);
   text(7-.2,8,'A','color',[1 1 1]);
 % for i=XPos-1:XPos+1
  %     for j=YPos-1:YPos+1
 %         if((i>0)&(i<=XLim))           %防止出现环境矩阵元素下标为零
 %             if((j>0)&(j<=YLim))
                   ws(XPos,YPos)=1;
 %             end
 %         end
 %     end
 %end
  env=ws;

f = frame2im(f);
imwrite(f, fpath);
saveas(newFig,'filename.eps')
close(newFig)
%移动机器人路径规划仿真平台程序 END END END END END END END END END END END END END END END END END END END
% --------------------------------------------------------------------
function MainFigure_WindowButtonMotionFcn(hObject, eventdata, handles)
% hObject   handle to MainFigure (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   structure with handles and user data (see GUIDATA)    
%h=get(handles.MainFigure,'SelectionType')

function RobotRadius_CreateFcn(hObject, eventdata, handles)
% hObject   handle to RobotRadius (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   empty - handles not created until after all CreateFcns called

% Hint: edit 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


function mainfig_CreateFcn(hObject, eventdata, handles)
% hObject   handle to mainfig (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   empty - handles not created until after all CreateFcns called
% --- Executes during object creation, after setting all properties.
function MainFigure_CreateFcn(hObject, eventdata, handles)
% hObject   handle to MainFigure (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   empty - handles not created until after all CreateFcns called
% --- Executes on button press in Start.







% --- Executes during object creation, after setting all properties.
function RobotSingleLength_CreateFcn(hObject, eventdata, handles)
% hObject   handle to RobotSingleLength (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   empty - handles not created until after all CreateFcns called

% Hint: edit 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 during object creation, after setting all properties.
function SensorMaxValue_CreateFcn(hObject, eventdata, handles)
% hObject   handle to SensorMaxValue (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   empty - handles not created until after all CreateFcns called

% Hint: edit 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 during object creation, after setting all properties.
function RobotVelocity_CreateFcn(hObject, eventdata, handles)
% hObject   handle to RobotVelocity (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   empty - handles not created until after all CreateFcns called

% Hint: edit 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



function Sensor1Length_Callback(hObject, eventdata, handles)
% hObject   handle to Sensor1Length (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of Sensor1Length as text
%       str2double(get(hObject,'String')) returns contents of Sensor1Length as a double


% --- Executes during object creation, after setting all properties.
function Sensor1Length_CreateFcn(hObject, eventdata, handles)
% hObject   handle to Sensor1Length (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   empty - handles not created until after all CreateFcns called

% Hint: edit 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




function Sensor2Length_Callback(hObject, eventdata, handles)
% hObject   handle to Sensor2Length (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of Sensor2Length as text
%       str2double(get(hObject,'String')) returns contents of Sensor2Length as a double


% --- Executes during object creation, after setting all properties.
function Sensor2Length_CreateFcn(hObject, eventdata, handles)
% hObject   handle to Sensor2Length (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   empty - handles not created until after all CreateFcns called

% Hint: edit 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


function Sensor3Length_Callback(hObject, eventdata, handles)
% hObject   handle to Sensor3Length (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of Sensor3Length as text
%       str2double(get(hObject,'String')) returns contents of Sensor3Length as a double


% --- Executes during object creation, after setting all properties.
function Sensor3Length_CreateFcn(hObject, eventdata, handles)
% hObject   handle to Sensor3Length (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   empty - handles not created until after all CreateFcns called

% Hint: edit 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


ange in popupmenu10.
function popupmenu10_Callback(hObject, eventdata, handles)
% hObject   handle to popupmenu10 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns popupmenu10 contents as cell array
%       contents{get(hObject,'Value')} returns selected item from popupmenu10


% --- Executes during object creation, after setting all properties.
function popupmenu10_CreateFcn(hObject, eventdata, handles)
% hObject   handle to popupmenu10 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   empty - handles not created until after all CreateFcns called

% Hint: popupmenu 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




% --------------------------------------------------------------------
%function Handbook_Callback(hObject, eventdata, handles)
% hObject   handle to Handbook (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   structure with handles and user data (see GUIDATA)

3 仿真结果

4 参考文献

[1]童亮, 王准. 强化学习在机器人路径规划中的应用研究[J]. 计算机仿真, 2013, 30(012):351-355.

[2]张福海, 李宁, 袁儒鹏,等. 基于强化学习的机器人路径规划算法[J]. 华中科技大学学报:自然科学版, 2018, 46(12):65-70.

部分理论引用网络文献,若有侵权联系博主删除。

5 MATLAB代码与数据下载地址

见博客主页