# rlTD3AgentOptions

Options for TD3 agent

## Description

Use an `rlTD3AgentOptions`

object to specify options for
twin-delayed deep deterministic policy gradient (TD3) agents. To create a TD3 agent, use
`rlTD3Agent`

For more information see Twin-Delayed Deep Deterministic Policy Gradient Agents.

For more information on the different types of reinforcement learning agents, see Reinforcement Learning Agents.

## Creation

### Description

creates an options
object for use as an argument when creating a TD3 agent using all default options. You can
modify the object properties using dot notation.`opt`

= rlTD3AgentOptions

sets option properties using
name-value pairs. For example, `opt`

= rlTD3AgentOptions(`Name,Value`

)`rlTD3AgentOptions('DiscountFactor',0.95)`

creates an option set with a discount factor of `0.95`

. You can specify
multiple name-value pairs. Enclose each property name in quotes.

## Properties

`ExplorationModel`

— Exploration noise model options

`GaussianActionNoise`

object (default) | `OrnsteinUhlenbeckActionNoise`

object

Noise model options, specified as a `GaussianActionNoise`

object or
an `OrnsteinUhlenbeckActionNoise`

object. For more information on noise
models, see Noise Models.

For an agent with multiple actions, if the actions have different ranges and units, it is likely that each action requires different noise model parameters. If the actions have similar ranges and units, you can set the noise parameters for all actions to the same value.

For example, for an agent with two actions, set the standard deviation of each action to a different value while using the same decay rate for both standard deviations.

opt = rlTD3AgentOptions; opt.ExplorationModel.StandardDeviation = [0.1 0.2]; opt.ExplorationModel.StandardDeviationDecayRate = 1e-4;

To use Ornstein-Uhlenbeck action noise, first create a default
`OrnsteinUhlenbeckActionNoise`

object. Then, specify any nondefault
model properties using dot notation.

opt = rlTD3AgentOptions; opt.ExplorationModel = rl.option.OrnsteinUhlenbeckActionNoise; opt.ExplorationModel.StandardDeviation = 0.05;

`TargetPolicySmoothModel`

— Target smoothing noise model options

`GaussianActionNoise`

object

Target smoothing noise model options, specified as a
`GaussianActionNoise`

object. This model helps the policy exploit
actions with high Q-value estimates. For more information on noise models, see Noise Models.

For an agent with multiple actions, if the actions have different ranges and units, it is likely that each action requires different smoothing noise model parameters. If the actions have similar ranges and units, you can set the noise parameters for all actions to the same value.

For example, for an agent with two actions, set the standard deviation of each action to a different value while using the same decay rate for both standard deviations.

opt = rlTD3AgentOptions; opt.TargetPolicySmoothModel.StandardDeviation = [0.1 0.2]; opt.TargetPolicySmoothModel.StandardDeviationDecayRate = 1e-4;

`PolicyUpdateFrequency`

— Number of steps between policy updates

`2`

(default) | positive integer

Number of steps between policy updates, specified as a positive integer.

`ActorOptimizerOptions`

— Actor optimizer options

`rlOptimizerOptions`

object

Actor optimizer options, specified as an `rlOptimizerOptions`

object. It allows you to specify training parameters of
the actor approximator such as learning rate, gradient threshold, as well as the
optimizer algorithm and its parameters. For more information, see `rlOptimizerOptions`

and `rlOptimizer`

.

`CriticOptimizerOptions`

— Critic optimizer options

`rlOptimizerOptions`

object

Critic optimizer options, specified as an `rlOptimizerOptions`

object. It allows you to specify training parameters of
the critic approximator such as learning rate, gradient threshold, as well as the
optimizer algorithm and its parameters. For more information, see `rlOptimizerOptions`

and `rlOptimizer`

.

`TargetSmoothFactor`

— Smoothing factor for target actor and critic updates

`0.005`

(default) | positive scalar less than or equal to 1

Smoothing factor for target actor and critic updates, specified as a positive scalar less than or equal to 1. For more information, see Target Update Methods.

`TargetUpdateFrequency`

— Number of steps between target actor and critic updates

`2`

(default) | positive integer

Number of steps between target actor and critic updates, specified as a positive integer. For more information, see Target Update Methods.

`ResetExperienceBufferBeforeTraining`

— Option for clearing the experience buffer

`true`

(default) | `false`

Option for clearing the experience buffer before training, specified as a logical value.

`SequenceLength`

— Maximum batch-training trajectory length when using RNN

`1`

(default) | positive integer

Maximum batch-training trajectory length when using a recurrent neural network, specified as a positive integer. This value must be greater than `1`

when using a recurrent neural network and `1`

otherwise.

`MiniBatchSize`

— Size of random experience mini-batch

`64`

(default) | positive integer

Size of random experience mini-batch, specified as a positive integer. During each training episode, the agent randomly samples experiences from the experience buffer when computing gradients for updating the critic properties. Large mini-batches reduce the variance when computing gradients but increase the computational effort.

`NumStepsToLookAhead`

— Number of future rewards used to estimate the value of the policy

`1`

(default) | positive integer

Number of future rewards used to estimate the value of the policy, specified as a positive integer. For more information, see [1], Chapter 7.

Note that if parallel training is enabled (that is if an `rlTrainingOptions`

option object in which the
`UseParallel`

property is set to `true`

is
passed to `train`

) then
`NumStepsToLookAhead`

must be set to `1`

,
otherwise an error is generated. This guarantees that experiences are stored
contiguously.

.

`ExperienceBufferLength`

— Experience buffer size

`10000`

(default) | positive integer

Experience buffer size, specified as a positive integer. During training, the agent computes updates using a mini-batch of experiences randomly sampled from the buffer.

`SampleTime`

— Sample time of agent

`1`

(default) | positive scalar | `-1`

Sample time of agent, specified as a positive scalar or as `-1`

. Setting this
parameter to `-1`

allows for event-based simulations.

Within a Simulink^{®} environment, the RL Agent block
in which the agent is specified to execute every `SampleTime`

seconds
of simulation time. If `SampleTime`

is `-1`

, the
block inherits the sample time from its parent subsystem.

Within a MATLAB^{®} environment, the agent is executed every time the environment advances. In
this case, `SampleTime`

is the time interval between consecutive
elements in the output experience returned by `sim`

or
`train`

. If
`SampleTime`

is `-1`

, the time interval between
consecutive elements in the returned output experience reflects the timing of the event
that triggers the agent execution.

`DiscountFactor`

— Discount factor

`0.99`

(default) | positive scalar less than or equal to 1

Discount factor applied to future rewards during training, specified as a positive scalar less than or equal to 1.

## Object Functions

`rlTD3Agent` | Twin-delayed deep deterministic policy gradient reinforcement learning agent |

## Examples

### Create TD3 Agent Options Object

This example shows how to create a TD3 agent option object.

Create an `rlTD3AgentOptions`

object that specifies the mini-batch size.

`opt = rlTD3AgentOptions('MiniBatchSize',48)`

opt = rlTD3AgentOptions with properties: ExplorationModel: [1x1 rl.option.GaussianActionNoise] TargetPolicySmoothModel: [1x1 rl.option.GaussianActionNoise] PolicyUpdateFrequency: 2 ActorOptimizerOptions: [1x1 rl.option.rlOptimizerOptions] CriticOptimizerOptions: [1x2 rl.option.rlOptimizerOptions] TargetSmoothFactor: 0.0050 TargetUpdateFrequency: 2 ResetExperienceBufferBeforeTraining: 1 SequenceLength: 1 MiniBatchSize: 48 NumStepsToLookAhead: 1 ExperienceBufferLength: 10000 SampleTime: 1 DiscountFactor: 0.9900 InfoToSave: [1x1 struct]

You can modify options using dot notation. For example, set the agent sample time to `0.5`

.

opt.SampleTime = 0.5;

## Algorithms

### Noise Models

**Gaussian Action Noise**

A `GaussianActionNoise`

object has the following numeric value
properties.

Property | Description | Default Value (`ExplorationModel` ) | Default Value (`TargetPolicySmoothModel` ) |
---|---|---|---|

`Mean` | Noise mean value | `0` | `0` |

`StandardDeviationDecayRate` | Decay rate of the standard deviation | `0` | `0` |

`StandardDeviation` | Initial value of noise standard deviation | `sqrt(0.1)` | `sqrt(0.2)` |

`StandardDeviationMin` | Minimum standard deviation, which must be less than
`StandardDeviation` | `0.01` | `0.01` |

`LowerLimit` | Noise sample lower limit | `-Inf` | `-0.5` |

`UpperLimit` | Noise sample upper limit | `Inf` | `0.5` |

At each time step `k`

, the Gaussian noise `v`

is
sampled as shown in the following code.

w = Mean + randn(ActionSize).*StandardDeviation(k); v(k+1) = min(max(w,LowerLimit),UpperLimit);

Where the initial value v(1) is defined by the `InitialAction`

parameter. At each sample time step, the standard deviation decays as shown in the
following code.

decayedStandardDeviation = StandardDeviation(k).*(1 - StandardDeviationDecayRate); StandardDeviation(k+1) = max(decayedStandardDeviation,StandardDeviationMin);

**Ornstein-Uhlenbeck Action Noise**

An `OrnsteinUhlenbeckActionNoise`

object has the following numeric value
properties.

Property | Description | Default Value |
---|---|---|

`InitialAction` | Initial value of action | `0` |

`Mean` | Noise mean value | `0` |

`MeanAttractionConstant` | Constant specifying how quickly the noise model output is attracted to the mean | `0.15` |

`StandardDeviationDecayRate` | Decay rate of the standard deviation | `0` |

`StandardDeviation` | Initial value of noise standard deviation | `0.3` |

`StandardDeviationMin` | Minimum standard deviation | `0` |

At each sample time step `k`

, the noise value `v(k)`

is
updated using the following formula, where `Ts`

is the agent sample
time, and the initial value v(1) is defined by the `InitialAction`

parameter.

v(k+1) = v(k) + MeanAttractionConstant.*(Mean - v(k)).*Ts + StandardDeviation(k).*randn(size(Mean)).*sqrt(Ts)

At each sample time step, the standard deviation decays as shown in the following code.

decayedStandardDeviation = StandardDeviation(k).*(1 - StandardDeviationDecayRate); StandardDeviation(k+1) = max(decayedStandardDeviation,StandardDeviationMin);

You can calculate how many samples it will take for the standard deviation to be halved using this simple formula.

halflife = log(0.5)/log(1-StandardDeviationDecayRate);

For continuous action signals, it is important to set the noise standard deviation
appropriately to encourage exploration. It is common to set
`StandardDeviation*sqrt(Ts)`

to a value between 1% and 10% of
your action range.

If your agent converges on local optima too quickly, promote agent exploration by increasing
the amount of noise; that is, by increasing the standard deviation. Also, to increase
exploration, you can reduce the `StandardDeviationDecayRate`

.

## References

[1] Sutton, Richard S., and Andrew G.
Barto. *Reinforcement Learning: An Introduction*. Second edition.
Adaptive Computation and Machine Learning. Cambridge, Mass: The MIT Press, 2018.

## Version History

**Introduced in R2020a**

### R2021a: Properties defining noise probability distribution in the `GaussianActionNoise`

object have changed

The properties defining the probability distribution of the Gaussian action noise model have changed. This noise model is used by TD3 agents for exploration and target policy smoothing.

The

`Variance`

property has been replaced by the`StandardDeviation`

property.The

`VarianceDecayRate`

property has been replaced by the`StandardDeviationDecayRate`

property.The

`VarianceMin`

property has been replaced by the`StandardDeviationMin`

property.

When a `GaussianActionNoise`

noise object saved from a previous
MATLAB release is loaded, the value of `VarianceDecayRate`

is
copied to `StandardDeviationDecayRate`

, while the square root of the
values of `Variance`

and `VarianceMin`

are copied to
`StandardDeviation`

and `StandardDeviationMin`

,
respectively.

The `Variance`

, `VarianceDecayRate`

, and
`VarianceMin`

properties still work, but they are not recommended. To
define the probability distribution of the Gaussian action noise model, use the new property
names instead.

**Update Code**

This table shows how to update your code to use the new property names for
`rlTD3AgentOptions`

object `td3opt`

.

Not Recommended | Recommended |
---|---|

`td3opt.ExplorationModel.Variance = 0.5;` | ```
td3opt.ExplorationModel.StandardDeviation =
sqrt(0.5);
``` |

`td3opt.ExplorationModel.VarianceDecayRate = 0.1;` | ```
td3opt.ExplorationModel.StandardDeviationDecayRate =
0.1;
``` |

`td3opt.ExplorationModel.VarianceMin = 0.1;` | ```
td3opt.ExplorationModel.StandardDeviationMin =
sqrt(0.1);
``` |

### R2021a: Property names defining noise probability distribution in the `OrnsteinUhlenbeckActionNoise`

object have changed

The properties defining the probability distribution of the Ornstein-Uhlenbeck (OU) noise model have been renamed. TD3 agents use OU noise for exploration.

The

`Variance`

property has been renamed`StandardDeviation`

.The

`VarianceDecayRate`

property has been renamed`StandardDeviationDecayRate`

.The

`VarianceMin`

property has been renamed`StandardDeviationMin`

.

The default values of these properties remain the same. When an
`OrnsteinUhlenbeckActionNoise`

noise object saved from a previous
MATLAB release is loaded, the values of `Variance`

,
`VarianceDecayRate`

, and `VarianceMin`

are copied in
the `StandardDeviation`

, `StandardDeviationDecayRate`

,
and `StandardDeviationMin`

, respectively.

The `Variance`

, `VarianceDecayRate`

, and
`VarianceMin`

properties still work, but they are not recommended. To
define the probability distribution of the OU noise model, use the new property names
instead.

**Update Code**

This table shows how to update your code to use the new property names for
`rlTD3AgentOptions`

object `td3opt`

.

Not Recommended | Recommended |
---|---|

`td3opt.ExplorationModel.Variance = 0.5;` | ```
td3opt.ExplorationModel.StandardDeviation =
sqrt(0.5);
``` |

`td3opt.ExplorationModel.VarianceDecayRate = 0.1;` | ```
td3opt.ExplorationModel.StandardDeviationDecayRate =
0.1;
``` |

`td3opt.ExplorationModel.VarianceMin = 0.1;` | ```
td3opt.ExplorationModel.StandardDeviationMin =
sqrt(0.1);
``` |

`td3opt.TargetPolicySmoothModel.Variance = 0.5;` | ```
td3opt.TargetPolicySmoothModel.StandardDeviation =
sqrt(0.5);
``` |

```
td3opt.TargetPolicySmoothModel.VarianceDecayRate =
0.1;
``` | ```
td3opt.TargetPolicySmoothModel.StandardDeviationDecayRate =
0.1;
``` |

`td3opt.TargetPolicySmoothModel.VarianceMin = 0.1;` | ```
td3opt.TargetPolicySmoothModel.StandardDeviationMin =
sqrt(0.1);
``` |

## See Also

## Open Example

You have a modified version of this example. Do you want to open this example with your edits?

## MATLAB Command

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

# Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

## How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

### Americas

- América Latina (Español)
- Canada (English)
- United States (English)

### Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)