💥💥💞💞欢迎来到本博客❤️❤️💥💥
🏆博主优势:🌞🌞🌞博客内容尽量做到思维缜密,逻辑清晰,为了方便读者。
⛳️座右铭:行百里者,半于九十。
📋📋📋本文目录如下:🎁🎁🎁
目录
💥1 概述
路径规划是指确定从起始点到目标点之间最佳路径的过程,通常涉及到考虑到环境、约束条件和优化目标等因素。强化学习是一种通过与环境交互来学习决策策略的机器学习方法,它试图通过最大化累积奖励来达到某个目标。 首先,需要定义问题的状态空间,即机器人可能处于的各种环境状态。这可能涉及到机器人当前的位置、姿态、目标位置等信息。 确定机器人可以采取的动作集合,这些动作将影响机器人的状态转移。例如,机器人在某个位置可以选择向前、向后、向左或向右移动等动作。 定义在每个状态下采取每个动作后机器人所获得的奖励。奖励的设计应该考虑到优化目标,例如最短路径、最小碰撞风险等。通过将路径规划与强化学习结合,机器人能够更加智能地选择路径,并在不断的交互中学习和优化决策策略,从而实现更加灵活、高效的路径规划。
📚2 运行结果
部分代码:
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
🎉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.