强化学习工具箱(Matlab)

1、Get Started

1.1、MDP环境下训练强化学习智能体

MDP环境如下图在这里插入图片描述

  1. 每个圆圈代表一个状态
  2. 每个状态都有上或下的选择
  3. 智能体从状态 1 开始
  4. 智能体接收的奖励值为图中状态转移的值
  5. 训练目标是最大化累计奖励

(1)创建 MDP 环境

创建一个具有 8 个状态和 2 个动作( " up " 和 " down " )的MDP模型。

MDP = createMDP(8,["up";"down"]);

要对上图中状态转换进行建模,需修改 MDP 的状态转换矩阵和奖励矩阵。默认情况下,这些矩阵均为零。有关创建 MDP 模型和 MDP 对象属性的更多信息,请参见 createMDP

修改 MDP 的状态转换和奖励矩阵,例如,在以下命令中:

  1. 前两行指定了通过执行动作 1( " up " )从状态 1 到状态 2 的转移,以及此转移对应的奖励 +3
  2. 接下来的两行指定了通过执行操作2( " down " )从状态 1 到状态 3 的转移,以及此转移对应的奖励 +1
MDP.T(1,2,1) = 1;
MDP.R(1,2,1) = 3;
MDP.T(1,3,2) = 1;
MDP.R(1,3,2) = 1;

同理,指定图中其余的状态转移和对应奖励。

% State 2 transition and reward
MDP.T(2,4,1) = 1;
MDP.R(2,4,1) = 2;
MDP.T(2,5,2) = 1;
MDP.R(2,5,2) = 1;
% State 3 transition and reward
MDP.T(3,5,1) = 1;
MDP.R(3,5,1) = 2;
MDP.T(3,6,2) = 1;
MDP.R(3,6,2) = 4;
% State 4 transition and reward
MDP.T(4,7,1) = 1;
MDP.R(4,7,1) = 3;
MDP.T(4,8,2) = 1;
MDP.R(4,8,2) = 2;
% State 5 transition and reward
MDP.T(5,7,1) = 1;
MDP.R(5,7,1) = 1;
MDP.T(5,8,2) = 1;
MDP.R(5,8,2) = 9;
% State 6 transition and reward
MDP.T(6,7,1) = 1;
MDP.R(6,7,1) = 5;
MDP.T(6,8,2) = 1;
MDP.R(6,8,2) = 1;
% State 7 transition and reward
MDP.T(7,7,1) = 1;
MDP.R(7,7,1) = 0;
MDP.T(7,7,2) = 1;
MDP.R(7,7,2) = 0;
% State 8 transition and reward
MDP.T(8,8,1) = 1;
MDP.R(8,8,1) = 0;
MDP.T(8,8,2) = 1;
MDP.R(8,8,2) = 0;

指定状态 7 和状态 8 为 MDP 的终端状态。

MDP.TerminalStates = ["s7";"s8"];

创建强化学习 MDP 环境。

env = rlMDPEnv(MDP);

通过重置函数指定环境的初始状态始终为状态 1,该函数在每次训练或模拟开始时调用。创建一个匿名函数句柄,将初始状态设置为 1。

env.ResetFcn = @() 1;

设置随机数种子。

rng(0)

(2) 创建 Q-Learning 智能体

首先使用 MDP 环境中的观察和动作创建 Q table,将学习率设置为1。

obsInfo = getObservationInfo(env);
actInfo = getActionInfo(env);
qTable = rlTable(obsInfo, actInfo);
qFunction = rlQValueFunction(qTable, obsInfo, actInfo);
qOptions = rlOptimizerOptions(LearnRate=1);

然后使用 Q table 创建一个 Q-learning 智能体,遵循 ϵ \epsilon ϵ-贪婪策略。有关创建 Q-learning 智能体的更多信息,请参阅 rlQAgentrlQAgentOptions

agentOpts = rlQAgentOptions;
agentOpts.DiscountFactor = 1;
agentOpts.EpsilonGreedyExploration.Epsilon = 0.9;
agentOpts.EpsilonGreedyExploration.EpsilonDecay = 0.01;
agentOpts.CriticOptimizerOptions = qOptions;
qAgent = rlQAgent(qFunction,agentOpts); %#ok<NASGU> 

(3)训练 Q-Learning 智能体

训练智能体首先需要指定训练选项。对于此示例,使用以下选项:

  1. 最多训练 500 回合,每回合最多持续 50 个时间步长
  2. 当智能体在连续 30 回合内的平均累积奖励 >10,停止训练

更多信息请参见 rlTrainingOptions

trainOpts = rlTrainingOptions;
trainOpts.MaxStepsPerEpisode = 50;
trainOpts.MaxEpisodes = 500;
trainOpts.StopTrainingCriteria = "AverageReward";
trainOpts.StopTrainingValue = 13;
trainOpts.ScoreAveragingWindowLength = 30;

使用函数 Train 训练智能体,大该需要几分钟完成。为了节省时间,可以通过将 doTraining 设置为 false 来加载预训练的智能体。若要自己训练智能体,将 doTraining 设置为 true 。

doTraining = false;

if doTraining
    % Train the agent.
    trainingStats = train(qAgent,env,trainOpts); %#ok<UNRCH> 
else
    % Load pretrained agent for the example.
    load("genericMDPQAgent.mat","qAgent"); 
end

在这里插入图片描述

(4)验证 Q-Learning 结果

为了验证训练结果,使用函数 sim 在 MDP 环境中模拟智能体。智能体成功地找到了最佳路径,并获得了 13 的累积奖励。

Data = sim(qAgent,env);
cumulativeReward = sum(Data.Reward)

==> cumulativeReward = 13

由于折扣系数设置为 1,因此经过训练的智能体的 Q table 中的值即真实的回报。

QTable = getLearnableParameters(getCritic(qAgent));
QTable{1}

==> ans = 8x2 single matrix

   12.9912   11.6621
    8.2141    9.9950
   10.8645    4.0414
    4.8017   -1.6150
    5.1975    8.9975
    5.8058   -0.2353
         0         0
         0         0
TrueTableValues = [13,12;5,10;11,9;3,2;1,9;5,1;0,0;0,0]
==> TrueTableValues = 8×2

    13    12
     5    10
    11     9
     3     2
     1     9
     5     1
     0     0
     0     0

1.2、在网格世界中训练智能体(Q-learning 和 SARSA)

网格世界描述如下:

  1. 网格世界大小为 5 × 5 5\times5 5×5,有四个可能的动作(North=1,South=2,East=3,West=4)
  2. 智能体从网格(2,1)出发
  3. 如果智能体到达网格(5,5),则它将获得奖励 10
  4. 网格世界中包含从网格(2,4)到网格(4,4)的跳跃,奖励为 5
  5. 智能体会被障碍物(黑色网格区域)阻挡
  6. 所有其他动作将获得奖励 -1
    在这里插入图片描述

(1)创建网格世界环境

env = rlPredefinedEnv("BasicGridWorld");

通过创建一个充值函数来指定智能体的初始状态始终为 [ 2 , 1 ] [2,1] [2,1],该函数在每次训练和模拟开始调用。

从位置[1,1]开始对状态进行编号。状态编号随着您向下移动第一列,然后向下移动后续每一列而增加。因此,创建一个匿名函数句柄,将初始状态设置为2。

固定随机数种子

rng(0)

(2)创建 Q-Learning 智能体

首先创建 Q table,然后将学习率设置为1。

qTable = rlTable(getObservationInfo(env),getActionInfo(env));
qRepresentation = rlQValueRepresentation(qTable,getObservationInfo(env),getActionInfo(env));
qRepresentation.Options.LearnRate = 1;

接下来,使用 Q-table 创建 Q-learning 智能体,并设定 ϵ \epsilon ϵ-贪婪策略。更多信息请参见 rlQAgentrlQAgentOptions

agentOpts = rlQAgentOptions;
agentOpts.EpsilonGreedyExploration.Epsilon = .04;
qAgent = rlQAgent(qRepresentation,agentOpts);

(3)训练 Q-Learning 智能体

指定以下训练选项:

  • 最多训练 200 回合,每个回合最多持续 50 个时间步长
  • 当智能体连续 20 回合平均累计奖励 >10 时,停止训练

更多的信息请参见 rlTrainingOptions

trainOpts = rlTrainingOptions;
trainOpts.MaxStepsPerEpisode = 50;
trainOpts.MaxEpisodes= 200;
trainOpts.StopTrainingCriteria = "AverageReward";
trainOpts.StopTrainingValue = 11;
trainOpts.ScoreAveragingWindowLength = 30;

使用函数 Train 来训练 Q-learning 智能体,训练可能需要几分钟才能完成。

为了在运行此示例时节省时间,可以将 doTraining 设置为 false 来加载预训练的智能体。若要自己训练智能体,可以将 doTraining 设置为 true 。

doTraining = false;

if doTraining
    % Train the agent.
    trainingStats = train(qAgent,env,trainOpts);
else
    % Load the pretrained agent for the example.
    load('basicGWQAgent.mat','qAgent')
end

回合管理器窗口打开并显示训练进度。
在这里插入图片描述

(4)验证 Q-Learning 结果

plot(env)
env.Model.Viewer.ShowTrace = true;
env.Model.Viewer.clearTrace;

使用函数 sim 模拟环境中的智能体。

sim(qAgent,env)

在这里插入图片描述

(5)创建和训练 SARSA 智能体

使用与 Q-learning 智能体相同的 Q table 和 ϵ \epsilon ϵ-贪婪策略。更多信息请参见 rlSARSAAgentrlSARSAAgentOptions

agentOpts = rlSARSAAgentOptions;
agentOpts.EpsilonGreedyExploration.Epsilon = 0.04;
sarsaAgent = rlSARSAAgent(qRepresentation,agentOpts);

使用函数 Train 训练 SARSA 智能体,训练可能需要几分钟才能完成。

为了在运行此示例时节省时间,可以将 doTraining 设置为 false 来加载预训练的智能体。若要自己训练智能体,可以将 doTraining 设置为 true 。

doTraining = false;

if doTraining
    % Train the agent.
    trainingStats = train(sarsaAgent,env,trainOpts);
else
    % Load the pretrained agent for the example.
    load('basicGWSarsaAgent.mat','sarsaAgent')
end

在这里插入图片描述

(6)验证 SARSA 结果

plot(env)
env.Model.Viewer.ShowTrace = true;
env.Model.Viewer.clearTrace;

在环境中对智能体进行仿真。

sim(sarsaAgent,env)

在这里插入图片描述

SARSA 智能体找到与 Q-learning 智能体相同的解决方案。

1.3、创建 Simulink 环境并训练智能体

这个例子展示了如何将水箱 Simulink 模型中的 PI 控制器转换为强化学习深度确定性策略梯度(DDPG)智能体。有关在 MATLAB 中训练 DDPG 智能体的示例,请参阅 Compare DDPG Agent to LQR Controller

(1)水箱模型

该模型的目标是控制水箱中的水位。有关水箱模型的更多信息,请参阅 watertank Simulink Model
在这里插入图片描述
修改原始模型:

  1. 删除PID控制器
  2. 插入强化学习智能体模块
  3. 计算状态观测矢量 [ ∫   e   d t e h ] T \left[\int\ e\ \mathrm{dt}\quad e\quad h\right]^T [ e dteh]T,其中 h h h 是水箱中的水位, e = r − h e=r−h e=rh r r r 是参考水位
  4. 设置奖励: r e w a r d = 10   (   ∣ e ∣ <   0.1 )   − 1   (   ∣ e ∣   ≥   0.1   )   − 100   (   h ≤ 0   ∣ ∣   h ≥ 20 ) \mathrm{reward}=10\ (\ \mid e \mid<\ 0.1)\ -1\ (\ \mid e \mid\ \geq\ 0.1\ )\ -100\ (\ h\leq0\ ||\ h\geq20) reward=10 ( e∣< 0.1) 1 ( e  0.1 ) 100 ( h0 ∣∣ h20)
  5. 设置终端信号,例如仿真在 h ≤ 0 h≤0 h0 h ≥ 20 h≥20 h20 时停止。

生成的模型是 r l w a t e r t a n k . s l x \mathrm{rlwatertank.slx} rlwatertank.slx。有关此模型和更改的详细信息,请参见创建自定义 Simulink 环境

open_system("rlwatertank")

在这里插入图片描述

(2)创建环境

环境模型的创建包括以下内容:

定义状态观测明细 o b s I n f o \mathrm{obsInfo} obsInfo 和动作明细 a c t I n f o \mathrm{actInfo} actInfo

% Observation info
obsInfo = rlNumericSpec([3 1],...
    LowerLimit=[-inf -inf 0  ]',...
    UpperLimit=[ inf  inf inf]');

% Name and description are optional and not used by the software
obsInfo.Name = "observations";
obsInfo.Description = "integrated error, error, and measured height";

% Action info
actInfo = rlNumericSpec([1 1]);
actInfo.Name = "flow";

创建环境对象。

env = rlSimulinkEnv("rlwatertank","rlwatertank/RL Agent",...
    obsInfo,actInfo);

设置一个自定义重置函数,使模型的参考值随机化。

env.ResetFcn = @(in)localResetFcn(in);

指定仿真时间 T f T_f Tf和智能体采样时间 T s T_s Ts(单位为秒)。

Ts = 1.0;
Tf = 200;

设置随机数种子。

rng(0)

(3)创建 Critic

DDPG 智能体使用参数化动作价值函数来评估策略,它以当前状态观测和动作作为输入,输出单个标量(估计的未来折扣累积奖励)。

要在 critic 中对参数化动作价值函数进行建模,使用具有两个输入层(一个用于 o b s I n f o \mathrm{obsInfo} obsInfo 指定的状态观测通道,另一个用于 a c t I n f o \mathrm{actInfo} actInfo 指定的动作通道)和一个输出层( 返回标量值 )的神经网络。

将每条网络路径定义为层对象数组。为每个路径的输入和输出层指定名称。通过这些名称,可以连接各路径,然后将网络输入层和输出层与相应的环境通道明确关联。从 o b s I n f o \mathrm{obsInfo} obsInfo a c t I n f o \mathrm{actInfo} actInfo 明细中获取状态观测空间和动作空间的维度。

% Observation path
obsPath = [
    featureInputLayer(obsInfo.Dimension(1),Name="obsInLyr")
    fullyConnectedLayer(50)
    reluLayer
    fullyConnectedLayer(25,Name="obsPathOutLyr")
    ];

% Action path
actPath = [
    featureInputLayer(actInfo.Dimension(1),Name="actInLyr")
    fullyConnectedLayer(25,Name="actPathOutLyr")
    ];

% Common path
commonPath = [
    additionLayer(2,Name="add")
    reluLayer
    fullyConnectedLayer(1,Name="QValue")
    ];

criticNetwork = layerGraph();
criticNetwork = addLayers(criticNetwork,obsPath);
criticNetwork = addLayers(criticNetwork,actPath);
criticNetwork = addLayers(criticNetwork,commonPath);

criticNetwork = connectLayers(criticNetwork, ...
    "obsPathOutLyr","add/in1");
criticNetwork = connectLayers(criticNetwork, ...
    "actPathOutLyr","add/in2");

查看 Critic 网络配置。

figure
plot(criticNetwork)

在这里插入图片描述
将网络转换为 dlnetwork 对象并汇总其属性。

criticNetwork = dlnetwork(criticNetwork);
summary(criticNetwork)

==>  Initialized: true

     Number of learnables: 1.5k

     Inputs:
        1   'obsInLyr'   3 features
        2   'actInLyr'   1 features

指定的深度神经网络、环境明细对象、网络输入与状态观测和动作通道相关联的名称,创建 Critic 对象。

critic = rlQValueFunction(criticNetwork, ...
    obsInfo,actInfo, ...
    ObservationInputNames="obsInLyr", ...
    ActionInputNames="actInLyr");

有关 Q-value 函数的更多信息,请参阅 rlQValueFunction

用随机输入的状态观测和动作来检查 Critic。

getValue(critic, ...
    {rand(obsInfo.Dimension)}, ...
    {rand(actInfo.Dimension)})

==> ans = single
        -0.1631

有关创建 Critics 的详细信息,请参见 Create Policies and Value Functions

(4)创建 Actor

DDPG 智能体在连续动作空间上使用参数化的确定性策略,该策略由连续的确定性 Actor 学习。

Actor 将当前状态观测作为输入,返回动作作为输出。

对 Actor 进行建模,需使用具有一个输入层(如 o b s I n f o \mathrm{obsInfo} obsInfo 所指定,接收环境状态观测通道的内容)和一个输出层(如 a c t I n f o \mathrm{actInfo} actInfo 所指定,将动作返回到环境动作通道)的神经网络。

将网络定义为层对象数组。

actorNetwork = [
    featureInputLayer(obsInfo.Dimension(1))
    fullyConnectedLayer(3)
    tanhLayer
    fullyConnectedLayer(actInfo.Dimension(1))
    ];

将网络转换为 dlnetwork 对象并汇总其属性。

actorNetwork = dlnetwork(actorNetwork);
summary(actorNetwork)

==> Initialized: true

    Number of learnables: 16

    Inputs:
       1   'input'   3 features

使用指定的深度神经网络、环境明细对象以及与状态观测通道相关联的网络输入名称,创建 Actor 对象。

actor = rlContinuousDeterministicActor(actorNetwork,obsInfo,actInfo);

更多信息,请参阅 rlContinuousDeterministicActor

用随机输入状态观测数据检查 Actor。

getAction(actor,{rand(obsInfo.Dimension)})

==> ans = 1x1 cell array
        {[-0.3408]}

(5)创建 DDPG 智能体

使用指定的 Actor 和 Critic 对象创建 DDPG 智能体。

agent = rlDDPGAgent(actor,critic);

更多信息,请参阅 rlDDPGAgent

指定智能体、Actor 和 Critic 。

agent.SampleTime = Ts;

agent.AgentOptions.TargetSmoothFactor = 1e-3;
agent.AgentOptions.DiscountFactor = 1.0;
agent.AgentOptions.MiniBatchSize = 64;
agent.AgentOptions.ExperienceBufferLength = 1e6; 

agent.AgentOptions.NoiseOptions.Variance = 0.3;
agent.AgentOptions.NoiseOptions.VarianceDecayRate = 1e-5;

agent.AgentOptions.CriticOptimizerOptions.LearnRate = 1e-03;
agent.AgentOptions.CriticOptimizerOptions.GradientThreshold = 1;
agent.AgentOptions.ActorOptimizerOptions.LearnRate = 1e-04;
agent.AgentOptions.ActorOptimizerOptions.GradientThreshold = 1;

或者,也可以使用 rlDDPGAgentOptions 对象指定智能体选项。

用随机输入状态观测结果检查智能体。

getAction(agent,{rand(obsInfo.Dimension)})

==> ans = 1x1 cell array
        {[-0.7926]}

(6)训练智能体

首先要指定训练选项。本例中使用以下选项:

  1. 每次训练最多运行 5000 回合。指定每回合最多持续 ceil ( T f / T s T_f/T_s Tf/Ts) ( 即 200 )个时间步
  2. 在 "回合管理器 "对话框中显示训练进度(设置 Plots 选项),并禁用命令行显示( 将 Verbose 选项设为 false )
  3. 当智能体在连续 20 个回合中获得的平均累积奖励 > 800 时,停止训练。此时,智能体可以控制水箱中的水位

更多信息,请参阅 rlTrainingOptions

trainOpts = rlTrainingOptions(...
    MaxEpisodes=5000, ...
    MaxStepsPerEpisode=ceil(Tf/Ts), ...
    ScoreAveragingWindowLength=20, ...
    Verbose=false, ...
    Plots="training-progress",...
    StopTrainingCriteria="AverageReward",...
    StopTrainingValue=800);

使用函数 train 训练智能体。训练是一个计算密集型过程,需要几分钟才能完成。为节省运行此示例的时间,请将 doTraining 设为 false,以加载预训练过的智能体。要自己训练代理,可将 doTraining 设为 true。

doTraining = false;

if doTraining
    % Train the agent.
    trainingStats = train(agent,env,trainOpts);
else
    % Load the pretrained agent for the example.
    load("WaterTankDDPG.mat","agent")
end

在这里插入图片描述

(7)验证 DDPG 智能体

根据模型仿真验证 DDPG 智能体。由于重置函数会随机化参考值,因此要固定随机数种子,以确保可重复性。

rng(1)

在环境中仿真,并将输出返回。

simOpts = rlSimulationOptions(MaxSteps=ceil(Tf/Ts),StopOnError="on");
experiences = sim(env,agent,simOpts);

在这里插入图片描述

(8)本地重置函数

function in = localResetFcn(in)

% Randomize reference signal
blk = sprintf("rlwatertank/Desired \nWater Level");
h = 3*randn + 10;
while h <= 0 || h >= 20
    h = 3*randn + 10;
end
in = setBlockParameter(in,blk,Value=num2str(h));

% Randomize initial height
h = 3*randn + 10;
while h <= 0 || h >= 20
    h = 3*randn + 10;
end
blk = "rlwatertank/Water-Tank System/H";
in = setBlockParameter(in,blk,InitialCondition=num2str(h));

end
  • 22
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值