基于时序差分学习(TD Learning)的路径规划仿真,包含SARSA和Q-learning两种算法对比
代码块
%% 环境参数设置
gridSize = [12,4]; % 迷宫尺寸
startPos = [1,1]; % 起点坐标
goalPos = [12,4]; % 终点坐标
trapPositions = [3:12,2]; % 陷阱区域(第2列除起点外)
%% 奖励函数设计
rewardMatrix = -ones(gridSize);
rewardMatrix(goalPos(1),goalPos(2)) = 100; % 终点奖励
rewardMatrix(trapPositions(:,1),trapPositions(:,2)) = -100; % 陷阱惩罚
复制成功
代码块
function nextState = stateTransition(state, action)
% 动作映射:1-上, 2-下, 3-左, 4-右
switch action
case 1
nextState = state + [-1,0];
case 2
nextState = state + [1,0];
case 3
nextState = state + [0,-1];
case 4
nextState = state + [0,1];
end
% 边界处理
nextState(1) = max(1, min(gridSize(1), nextState(1)));
nextState(2) = max(1, min(gridSize(2), nextState(2)));
end
复制成功
代码块
%% SARSA参数设置
alpha = 0.1; % 学习率
gamma = 0.9; % 折扣因子
epsilon = 0.1; % 探索率
numEpisodes = 500;
%% 初始化Q表
Q = zeros(gridSize(1), gridSize(2), 4); % 4个动作
%% 训练循环
for ep = 1:numEpisodes
state = startPos;
action = epsilonGreedy(Q, state, epsilon);
while ~isequal(state, goalPos)
nextState = stateTransition(state, action);
nextAction = epsilonGreedy(Q, nextState, epsilon);
% SARSA更新
reward = getReward(state, nextState);
Q(state(1),state(2),action) = Q(state(1),state(2),action) + ...
alpha*(reward + gamma*Q(nextState(1),nextState(2),nextAction) - Q(state(1),state(2),action));
state = nextState;
action = nextAction;
end
end
复制成功
代码块
function action = epsilonGreedy(Q, state, epsilon)
if rand < epsilon
action = randi(4); % 随机探索
else
[~,action] = max(Q(state(1),state(2),:)); % 利用最优策略
end
end
复制成功
代码块
%% Q-learning参数设置
alpha = 0.1;
gamma = 0.9;
epsilon = 0.1;
%% 初始化Q表
Q = zeros(gridSize(1), gridSize(2), 4);
%% 训练循环
for ep = 1:numEpisodes
state = startPos;
while ~isequal(state, goalPos)
action = epsilonGreedy(Q, state, epsilon);
nextState = stateTransition(state, action);
reward = getReward(state, nextState);
% Q-learning更新
Q(state(1),state(2),action) = Q(state(1),state(2),action) + ...
alpha*(reward + gamma*max(Q(nextState(1),nextState(2),:)) - Q(state(1),state(2),action));
state = nextState;
end
end
复制成功
参考代码 时序差分学习做路径规划的仿真 www.youwenfan.com/contentbic/50855.html
代码块
%% 路径可视化函数
function plotPath(Q, algorithmName)
[~,path] = findOptimalPath(Q, startPos, goalPos);
figure;
imagesc(1:gridSize(2),1:gridSize(1),rewardMatrix);
hold on;
plot(path(:,2),path(:,1),'r-o','LineWidth',2);
title([algorithmName '最优路径']);
colorbar;
end
%% 路径搜索函数
function [path, cost] = findOptimalPath(Q, start, goal)
path = start;
current = start;
cost = 0;
while ~isequal(current, goal)
[~,action] = max(Q(current(1),current(2),:));
current = stateTransition(current, action);
path = [path; current];
cost = cost + 1;
end
end
复制成功
指标SARSAQ-learning平均路径长度18.2步15.7步收敛速度300轮250轮陷阱触发率0%12%计算效率0.8s/轮0.6s/轮
代码块
% 经验回放缓冲区
replayBuffer = struct('state',{}, 'action',{});
replaySize = 1000;
% 更新时随机采样
batch = replayBuffer(randperm(length(replayBuffer), min(32,length(replayBuffer))));
复制成功
代码块
% 目标网络参数更新
targetQ = Q;
updateInterval = 100;
if mod(ep, updateInterval) == 0
targetQ = 0.95*Q + 0.05*targetQ; % 软更新
end
复制成功
代码块
% 随机移动陷阱
if rand < 0.05
trapPositions(randperm(size(trapPositions,1),1),:) = trapPositions(randperm(size(trapPositions,1),1),:) + [-1,0];
end
复制成功
代码块
% 双目标奖励函数
reward = 0.7*getReward(state,nextState) + 0.3*(1 - pathLengthPenalty);
复制成功
代码块
figure;
imagesc(1:gridSize(2),1:gridSize(1),squeeze(mean(Q,1)));
hold on;
plot(startPos(2),startPos(1),'go','MarkerSize',10);
plot(goalPos(2),goalPos(1),'ro','MarkerSize',10);
title('Q值分布热力图');
colorbar;
复制成功
代码块
figure;
plot(1:numEpisodes, avgReward, 'b-o', 1:numEpisodes, successRate, 'r--x');
xlabel('训练轮次'); ylabel('性能指标');
legend('平均奖励','成功率');
复制成功
代码块
TD_PathPlanning/
├── src/
│ ├── env_setup.m % 环境建模
│ ├── sarsa_agent.m % SARSA算法
│ ├── qlearning_agent.m % Q-learning算法
│ └── utils.m % 工具函数
├── examples/
│ ├── basic_simulation.m % 基础仿真
│ └── dynamic_obstacles.m % 动态障碍物案例
├── data/
│ ├── maze_map.mat % 地图数据
│ └── training_logs.mat % 训练记录
└── visualize/
├── plot_path.m % 路径可视化
└── plot_training.m % 训练曲线绘制
复制成功
免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删