MATLAB强化学习实战(七) 在Simulink中训练DDPG控制倒立摆系统

此示例显示了如何训练深度确定性策略梯度(DDPG)智能体去控制以Simscape™Multibody™建模的倒立摆系统。

matlab 版本 2020b
在这里插入图片描述

倒立摆的Simscape模型

此示例的强化学习环境是一根杆,该杆连接到手推车上未操纵的关节上,该关节沿着无摩擦的轨道移动。 训练的目标是使杆立起,而只需花费最小的控制力就不会摔倒。

打开模型

mdl = 'rlCartPoleSimscapeModel';
open_system(mdl)

在这里插入图片描述
倒立摆系统是使用Simscape Multibody建模的。

在这里插入图片描述
对于该模型:

  1. 向上的平衡杆位置为0弧度,向下的悬挂位置为pi弧度。
  2. 从agent到环境的力作用信号是15到15 N,
  3. 从环境中观察到的是手推车的位置和速度,以及杆角的正弦、余弦和导数。
  4. 如果车从原来的位置移动超过3.5米,则该episode终止。
  5. 在每个时间步提供的奖励 r t r_t rt
    在这里插入图片描述

其中:

  1. θ t \theta_t θt是从杆的直立位置开始的位移角。
  2. x t x_t xt是从小车中心位置移动的位置。
  3. u t − 1 u_{t-1} ut1是前一个时间步骤的控制工作。
  4. B是一个标志(1或0),指示车是否越界。

有关此模型的更多信息,请参见加载预定义的Simulink环境

创建环境接口

为杆创建预定义的环境接口。

env = rlPredefinedEnv('CartPoleSimscapeModel-Continuous')

在这里插入图片描述
该接口具有连续的作用空间,智能体可以在其中向极点施加从–15到15 N的可能扭矩值。

从环境接口获取观察和动作信息。

obsInfo = getObservationInfo(env);
numObservations = obsInfo.Dimension(1);
actInfo = getActionInfo(env);

以秒为单位指定模拟时间Tf和智能体采样时间Ts。

Ts = 0.02;
Tf = 25;

固定随机发生器种子的重现性。

rng(0)

创建DDPG智能体

DDPG智能体使用评论者价值函数表示法,根据给定的观察和操作来估算长期奖励。 要创建评论者,首先要创建一个具有两个输入(状态和动作)和一个输出的深度神经网络。 动作路径的输入大小为[1 1 1],因为智能体可以将动作作为一个力值施加到环境。 有关创建深度神经网络值函数表示的更多信息,请参见创建策略和值函数表示

statePath = [
    featureInputLayer(numObservations,'Normalization','none','Name','observation')
    fullyConnectedLayer(128,'Name','CriticStateFC1')
    reluLayer('Name','CriticRelu1')
    fullyConnectedLayer(200,'Name','CriticStateFC2')];

actionPath = [
    featureInputLayer(1,'Normalization','none','Name','action')
    fullyConnectedLayer(200,'Name','CriticActionFC1','BiasLearnRateFactor',0)];

commonPath = [
    additionLayer(2,'Name','add')
    reluLayer('Name','CriticCommonRelu')
    fullyConnectedLayer(1,'Name','CriticOutput')];

criticNetwork = layerGraph(statePath);
criticNetwork = addLayers(criticNetwork,actionPath);
criticNetwork = addLayers(criticNetwork,commonPath);
    
criticNetwork = connectLayers(criticNetwork,'CriticStateFC2','add/in1');
criticNetwork = connectLayers(criticNetwork,'CriticActionFC1','add/in2');

查看评论者网络配置。

figure
plot(criticNetwork)

在这里插入图片描述
使用rlRepresentationOptions指定评论者表示的选项。

criticOptions = rlRepresentationOptions('LearnRate',1e-03,'GradientThreshold',1);

使用指定的深度神经网络和选项创建评论者表示。 您还必须指定评论者的操作和观察信息,这些信息已经从环境界面中获得。 有关更多信息,请参见rlQValueRepresentation

critic = rlQValueRepresentation(criticNetwork,obsInfo,actInfo,...
    'Observation',{
    
    'observation'},'Action',{
    
    'action'},criticOptions);

DDPG智能体使用写得者表示来决定在给定的观察结果下要采取哪种操作。 要创建行动者,首先要创建一个具有一个输入(观察)和一个输出(动作)的深度神经网络。

以类似于评论者的方式构造行动者。 有关更多信息,请参见rlDeterministicActorRepresentation

actorNetwork = [
    featureInputLayer(numObservations,'Normalization','none','Name','observation')
    fullyConnectedLayer(128,'Name','ActorFC1')
    reluLayer('Name','ActorRelu1')
    fullyConnectedLayer(200,'Name','ActorFC2')
    reluLayer('Name','ActorRelu2')
    fullyConnectedLayer(1,'Name','ActorFC3')
    tanhLayer('Name','ActorTanh1')
    scalingLayer('Name','ActorScaling','Scale',max(actInfo.UpperLimit))];

actorOptions = rlRepresentationOptions('LearnRate',5e-04,'GradientThreshold',1);

actor = rlDeterministicActorRepresentation(actorNetwork,obsInfo,actInfo,...
    'Observation',{
    
    'observation'},'Action',{
    
    'ActorScaling'},actorOptions);

要创建DDPG智能体,请首先使用rlDDPGAgentOptions指定DDPG智能体选项。

agentOptions = rlDDPGAgentOptions(...
    'SampleTime',Ts,...
    'TargetSmoothFactor',1e-3,...
    'ExperienceBufferLength',1e6,...
    'MiniBatchSize',128);
agentOptions.NoiseOptions.Variance = 0.4;
agentOptions.NoiseOptions.VarianceDecayRate = 1e-5;

然后,使用指定的行动者表示,评论者表示和智能体选项创建行动者。 有关更多信息,请参见rlDDPGAgent

agent = rlDDPGAgent(actor,critic,agentOptions);

训练智能体

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

  1. 每个训练episode 最多运行2000个episode ,每个episode 最多持续ceil(Tf / Ts)个时间步长。

  2. 在“Episode Manager”对话框中显示训练进度(设置“Plots ”选项)并禁用命令行显示(将“Verbose ”选项设置为false)。

  3. 当智能体连续五个episode 获得的平均累积奖励大于–400时,请停止训练。 在这一点上,智能体可以用最少的控制力量快速地将立杆平衡在直立位置。

  4. 为累积奖励大于–400的每个episode 保存智能体的副本。

有关更多信息,请参见rlTrainingOptions

maxepisodes = 2000;
maxsteps = ceil(Tf/Ts);
trainingOptions = rlTrainingOptions(...
    'MaxEpisodes',maxepisodes,...
    'MaxStepsPerEpisode',maxsteps,...
    'ScoreAveragingWindowLength',5,...
    'Verbose',false,...
    'Plots','training-progress',...
    'StopTrainingCriteria','AverageReward',...
    'StopTrainingValue',-400,...
    'SaveAgentCriteria','EpisodeReward',...
    'SaveAgentValue',-400);

使用训练功能训练智能体。 培训此智能体过程需要大量的计算,并且需要几个小时才能完成。 为了节省运行本示例的时间,请通过将doTraining设置为false来加载预训练的智能体。 要自己训练智能体,请将doTraining设置为true

doTraining = false;

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

在这里插入图片描述

DDPG智能体仿真

要验证训练后的智能体的表现,请在杆状环境中对其进行仿真。 有关智能体模拟的更多信息,请参见rlSimulationOptionssim

simOptions = rlSimulationOptions('MaxSteps',500);
experience = sim(env,agent,simOptions);

在这里插入图片描述

bdclose(mdl)

猜你喜欢

转载自blog.csdn.net/wangyifan123456zz/article/details/109606175