路径规划与强化学习结合的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
热门推荐
甘肃天水必吃十大美食,你吃过多少种?
今夕复何夕:岁月流转与人生感悟
揭秘《甄嬛传》:甄嬛与果郡王私情被皇帝发现始末
主要涉及4个地方!城中村如何改出新气象,留住老记忆?丨两会关注
2025 Quant金融工程排名大洗牌:巴鲁克跌落神坛,佐治亚理工成最大黑马!
都什么原因能引起呕吐
冯诺依曼体系结构和操作系统原理详解
豆腐的这3种搭配,补钙护心抗氧化
帝国和风暴斗篷间的选择建议
这10大视频素材网站,能为短视频创作者提供很大助力
什么是后窗加热?原理、优势及使用注意事项全解析
物业客服管家提升的10个要点
减内脏脂肪的6个最佳方法,让腰围下降10cm!
《破密》:无名英雄的荧屏揭秘与青春叙事
入境韩国时禁止携带的物品
听英文歌曲轻松提高英语听力
中国地理独有的多样性,为发展特色旅游提供巨大空间
万亿医药健康产业崛起
鬼谷八荒重塑肉身功能详解:如何复活重要NPC并延续剧情
龙龟的寓意及摆放要求
公司到底赚不赚钱?看这5大指标
90后女硕士“弹唱”古诗词直播间受热捧,想让更多人爱上古琴吟诵
数字浪潮下的历史教学新探索 —— 西安市育才中学学科大练兵活动纪实
英国本科成绩C等级详解:分数范围、重要性及发展建议
甲状腺结节有哪些危害
了解乌龙茶——一种独特的茶叶品种(乌龙茶的种类、特点、产地及营养价值)
贵州五天自由行全攻略:黄果树瀑布、荔波小七孔、西江苗寨、梵净山游记
制定虚拟货币投资策略:从入门到精通
三类医疗器械注册检测:确保高风险医疗器械安全有效
缓存:提升计算机性能的关键技术