路径规划与强化学习结合的Matlab实现
创作时间:
作者:
@小白创作中心
路径规划与强化学习结合的Matlab实现
引用
CSDN
1.
https://blog.csdn.net/m0_64583023/article/details/139570010
💥1 概述
路径规划是指确定从起始点到目标点之间最佳路径的过程,通常涉及到考虑到环境、约束条件和优化目标等因素。强化学习是一种通过与环境交互来学习决策策略的机器学习方法,它试图通过最大化累积奖励来达到某个目标。
首先,需要定义问题的状态空间,即机器人可能处于的各种环境状态。这可能涉及到机器人当前的位置、姿态、目标位置等信息。
确定机器人可以采取的动作集合,这些动作将影响机器人的状态转移。例如,机器人在某个位置可以选择向前、向后、向左或向右移动等动作。
定义在每个状态下采取每个动作后机器人所获得的奖励。奖励的设计应该考虑到优化目标,例如最短路径、最小碰撞风险等。
通过将路径规划与强化学习结合,机器人能够更加智能地选择路径,并在不断的交互中学习和优化决策策略,从而实现更加灵活、高效的路径规划。
📚2 运行结果
🎉3 参考文献
文章中一些内容引自网络,会注明出处或引用为参考文献,难免有未尽之处,如有不妥,请随时联系删除。
[1]梁凯冲,赵治国,颜丹姝.基于动态运动基元的车辆高速公路换道轨迹规划[J/OL].机械工程学报:1-15[2024-06-09].http://kns.cnki.net/kcms/detail/11.2187.TH.20240603.0841.002.html.
[2]贾瑞,强颖,赵锋.基于机器视觉的图书机器人取书路径控制方法研究[J/OL].计算机测量与控制:1-9[2024-06-09].http://kns.cnki.net/kcms/detail/11.4762.tp.20240603.1039.002.html.
🌈4 Matlab代码实现
function [ Qtable] = QLearningFunction( name )
model = xlsread(name)
% initial Q tables for up, right, down, left
Q1 = zeros(size(model)); %up
Q2 = zeros(size(model)); %right
Q3 = zeros(size(model)); %down
Q4 = zeros(size(model)); %left
Qtable = zeros(size(model));
% parameters for updating Q
gamma = 0.8;
alpha = 0.9;
epsilon = 0.9;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%updating Q%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
K = 10000; % number of the episodes to update the Q
for i = 1:K
%%%%%%%%%%%%%%%%% 0ne episode %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
s = 1 ; % number of the steps in each iteration
% for each episode select a random start point
currentState = [randi([1 10],1,1) randi([1 10],1,1)];
if model( currentState(1) , currentState(2) ) == -1
currentState = [2 2];
end
while s <= 150 % 150 is the max number of steps in each episode
s = s+1;
if model( currentState(1) , currentState(2) ) == 100 % agent has reached to the goal
break
end
% update all the four Qs in the current state
a = currentState(1);
b = currentState(2);
% select action based on the epsilon policy to find the next state
r = rand;
r2 = rand;
QState = [Q1(a,b); Q2(a,b); Q3(a,b); Q4(a,b)];
action = nextAction(epsilon, QState, r, r2);
if action == 1
% update Q1
[NS1,NS2] = nextStep(a,b, 1, 1);
QNS = [Q1(NS1,NS2) ; Q2(NS1,NS2) ; Q3(NS1,NS2) ; Q4(NS1,NS2) ];
Q1(a,b) = Q1(a,b) + alpha*( model(NS1,NS2) + gamma* (max(QNS)) - Q1(a,b));
elseif action == 2
% update Q2
[NS1,NS2] = nextStep(a,b, 2, 1);
QNS = [Q1(NS1,NS2) ; Q2(NS1,NS2) ; Q3(NS1,NS2) ; Q4(NS1,NS2) ];
Q2(a,b) = Q2(a,b) + alpha*( model(NS1,NS2) + gamma* (max(QNS)) - Q2(a,b));
elseif action == 3
% update Q3
[NS1,NS2] = nextStep(a,b, 3, 1);
QNS = [Q1(NS1,NS2) ; Q2(NS1,NS2) ; Q3(NS1,NS2) ; Q4(NS1,NS2) ];
Q3(a,b) = Q3(a,b) + alpha*( model(NS1,NS2) + gamma* (max(QNS)) - Q3(a,b));
elseif action == 4
% update Q4
[NS1,NS2] = nextStep(a,b, 4, 1);
QNS = [Q1(NS1,NS2) ; Q2(NS1,NS2) ; Q3(NS1,NS2) ; Q4(NS1,NS2) ];
Q4(a,b) = Q4(a,b) + alpha*( model(NS1,NS2) + gamma* (max(QNS)) - Q4(a,b));
end
Qtable(a,b) = max([Q1(a,b),Q2(a,b),Q3(a,b),Q4(a,b)]);
% update the next position of the agent
if model(NS1,NS2)~= -1
currentState = [NS1, NS2];
end
end % end of one episode
end % end of the 1000 episodes of training
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for x= 0:9
for y = 0:9
rectangle('Position',[x y 1 1],'FaceColor',[.5 .5 .5],'EdgeColor',[.192,.192,.192] )
if model(9-y+1,x+1) == 0
rectangle('Position',[x y 1 1],'FaceColor',[1 1 1],'EdgeColor',[.192,.192,.192] )
t = text(x+.5,y+.9, num2str(round(Q1(9-y+1,x+1))))
t.FontSize = 7;
t.FontWeight = 'bold';
t = text(x+.7,y+.5, num2str(round(Q2(9-y+1,x+1))))
t.FontSize = 7;
t.FontWeight = 'bold';
t = text(x+.5,y+.1, num2str(round(Q3(9-y+1,x+1))))
t.FontSize = 7;
t.FontWeight = 'bold';
t = text(x+.1,y+.5, num2str(round(Q4(9-y+1,x+1))))
t.FontSize = 7;
t.FontWeight = 'bold';
end
if model(9-y+1,x+1) == -1
rectangle('Position',[x y 1 1],'FaceColor',[139/255,69/255,19/255] ,'EdgeColor',[.192,.192,.192])
end
end
rectangle('Position',[8 1 1 1],'FaceColor','r' ,'EdgeColor',[.192,.192,.192])
t = text(8.1, 1.5, 'Goal')
t.FontSize = 10; % make the text larger
t.FontWeight = 'bold';
end
end
热门推荐
银行定期存款自动转存利率怎么确定?
体味重怎么去除
维特根斯坦:何谓精英
通货膨胀与利率:怎样影响投资决策?
健身减脂期必读:科学饮食规划助你轻松减脂塑型
轻松去除黑眼圈的6个方法
晚上没睡好如何快速消除黑眼圈
AI时代已来:凯文·凯利揭示的12个未来趋势,你准备好了吗?
破执守真,重审个人价值
委员履职故事|陈怡平:科研的真正目的就是为人民服务
实测!高德Call车快 Uber杜绝拣客
乌龙球算不算分?篮球与足球中的乌龙球判罚规则详解
如何辨别真实快递单号?一文详解防诈骗指南
使用公积金买二套房的条件和材料有哪些
八字月柱天干七杀:命运解析与影响
《黑神话悟空》二郎神部分台词及出处介绍
《难念的经》:一首武侠主题曲的经典密码
AI 技术的核心本质是什么 背后的技术原理有哪些
凝汽器结垢的危害与在线化学清洗技术应用
怎么判断甲流
长债基金VS短债基金,怎么选?
程序员的代码书写如何可以精简
水质二价铁的检测方法(鉴别水中二价铁离子的试剂是什么)
高中数学建模有哪些有趣的课题
离别之际,祝福朋友的神仙诗句
冻品产品仓库如何管理
如何判断投资平台的合法性?这些平台的风险如何评估?
浅析多元化教学在生物教学中的应用
多家民营银行下调存款利率,留给“存款特种兵”们的空间不多了
利率"倒挂"!五年期收益竟不敌三年?钱该放哪儿?