Train DDPG Agent to Control Double Integrator System
This example shows how to train a deep deterministic policy gradient (DDPG) agent to control a second-order dynamic system modeled in MATLAB®.
For more information on DDPG agents, see Deep Deterministic Policy Gradient Agents. For an example showing how to train a DDPG agent in Simulink®, see Train DDPG Agent to Swing Up and Balance Pendulum.
Double Integrator MATLAB Environment
The reinforcement learning environment for this example is a second-order double-integrator system with a gain. The training goal is to control the position of a mass in the second-order system by applying a force input.
For this environment:
The mass starts at an initial position between –4 and 4 units.
The force action signal from the agent to the environment is from –2 to 2 N.
The observations from the environment are the position and velocity of the mass.
The episode terminates if the mass moves more than 5 m from the original position or if .
The reward , provided at every time step, is a discretization of :
is the state vector of the mass.
is the force applied to the mass.
is the weights on the control performance; .
is the weight on the control effort; .
For more information on this model, see Load Predefined Control System Environments.
Create Environment Interface
Create a predefined environment interface for the double integrator system.
env = rlPredefinedEnv("DoubleIntegrator-Continuous")
env = DoubleIntegratorContinuousAction with properties: Gain: 1 Ts: 0.1000 MaxDistance: 5 GoalThreshold: 0.0100 Q: [2x2 double] R: 0.0100 MaxForce: Inf State: [2x1 double]
env.MaxForce = Inf;
The interface has a continuous action space where the agent can apply force values from -
Inf to the mass.
Obtain the observation and action information from the environment interface.
obsInfo = getObservationInfo(env); numObservations = obsInfo.Dimension(1); actInfo = getActionInfo(env); numActions = numel(actInfo);
Fix the random generator seed for reproducibility.
Create DDPG Agent
A DDPG agent approximates the long-term reward, given observations and actions, using a critic value function representation. To create the critic, first create a deep neural network with two inputs (the state and action) and one output. For more information on creating a neural network value function representation, see Create Policies and Value Functions.
statePath = featureInputLayer(numObservations,'Normalization','none','Name','state'); actionPath = featureInputLayer(numActions,'Normalization','none','Name','action'); commonPath = [concatenationLayer(1,2,'Name','concat') quadraticLayer('Name','quadratic') fullyConnectedLayer(1,'Name','StateValue','BiasLearnRateFactor',0,'Bias',0)]; criticNetwork = layerGraph(statePath); criticNetwork = addLayers(criticNetwork,actionPath); criticNetwork = addLayers(criticNetwork,commonPath); criticNetwork = connectLayers(criticNetwork,'state','concat/in1'); criticNetwork = connectLayers(criticNetwork,'action','concat/in2');
View the critic network configuration.
Specify options for the critic representation using
criticOpts = rlOptimizerOptions('LearnRate',5e-3,'GradientThreshold',1);
Create the critic representation using the specified neural network and options. You must also specify the action and observation info for the critic, which you obtain from the environment interface. For more information, see
critic = rlQValueFunction(criticNetwork,obsInfo,actInfo,'ObservationInputNames','state','ActionInputNames','action');
A DDPG agent decides which action to take, given observations, using an actor representation. To create the actor, first create a deep neural network with one input (the observation) and one output (the action).
Construct the actor in a similar manner to the critic.
actorNetwork = [ featureInputLayer(numObservations,'Normalization','none','Name','state') fullyConnectedLayer(numActions,'Name','action','BiasLearnRateFactor',0,'Bias',0)]; actorNetwork = dlnetwork(actorNetwork); actorOpts = rlOptimizerOptions('LearnRate',1e-04,'GradientThreshold',1); actor = rlContinuousDeterministicActor(actorNetwork,obsInfo,actInfo);
To create the DDPG agent, first specify the DDPG agent options using
agentOpts = rlDDPGAgentOptions(... 'SampleTime',env.Ts,... 'ActorOptimizerOptions',actorOpts,... 'CriticOptimizerOptions',criticOpts,... 'ExperienceBufferLength',1e6,... 'MiniBatchSize',32); agentOpts.NoiseOptions.Variance = 0.3; agentOpts.NoiseOptions.VarianceDecayRate = 1e-6;
Create the DDPG agent using the specified actor representation, critic representation, and agent options. For more information, see
agent = rlDDPGAgent(actor,critic,agentOpts);
To train the agent, first specify the training options. For this example, use the following options.
Run at most 1000 episodes in the training session, with each episode lasting at most 200 time steps.
Display the training progress in the Episode Manager dialog box (set the
Plotsoption) and disable the command line display (set the
Stop training when the agent receives a moving average cumulative reward greater than –66. At this point, the agent can control the position of the mass using minimal control effort.
For more information, see
trainOpts = rlTrainingOptions(... 'MaxEpisodes', 5000, ... 'MaxStepsPerEpisode', 200, ... 'Verbose', false, ... 'StopTrainingCriteria','AverageReward',... 'StopTrainingValue',-66);
You can visualize the double integrator environment by using the
plot function during training or simulation.
Train the agent using the
train function. Training this agent is a computationally intensive process that takes several hours to complete. To save time while running this example, load a pretrained agent by setting
false. To train the agent yourself, set
doTraining = false; if doTraining % Train the agent. trainingStats = train(agent,env,trainOpts); else % Load the pretrained agent for the example. load('DoubleIntegDDPG.mat','agent'); end
Simulate DDPG Agent
simOptions = rlSimulationOptions('MaxSteps',500); experience = sim(env,agent,simOptions);
totalReward = sum(experience.Reward)
totalReward = -65.9933