# rlTRPOAgentOptions

## Description

Use an `rlTRPOAgentOptions`

object to specify options for trust
region policy optimization (TRPO) agents. To create a TRPO agent, use `rlTRPOAgent`

.

For more information on TRPO agents, see Trust Region Policy Optimization (TRPO) Agent.

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

## Creation

### Description

creates an
`opt`

= rlTRPOAgentOptions`rlPPOAgentOptions`

object for use as an argument when creating a TRPO
agent using all default settings. You can modify the object properties using dot
notation.

creates the options set opt and sets its properties using one
or more name-value arguments. For example,
`opt`

= rlTRPOAgentOptions(`Name=Value`

)`rlTRPOAgentOptions(DiscountFactor=0.95)`

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

. You can specify multiple name-value
arguments.

## Properties

`SampleTime`

— Sample time of agent

`1`

(default) | positive scalar | `-1`

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

.

Within a MATLAB^{®} environment, the agent is executed every time the environment advances,
so, `SampleTime`

does not affect the timing of the agent
execution.

Within a Simulink^{®} environment, the RL Agent block
that uses the agent object executes every `SampleTime`

seconds of
simulation time. If `SampleTime`

is `-1`

the block
inherits the sample time from its input signals. Set `SampleTime`

to
`-1`

when the block is a child of an event-driven subsystem.

**Note**

Set `SampleTime`

to a positive scalar when the block is not
a child of an event-driven subsystem. Doing so ensures that the block executes
at appropriate intervals when input signal sample times change due to model
variations.

Regardless of the type of environment, the time interval between consecutive elements
in the output experience returned by `sim`

or
`train`

is
always `SampleTime`

.

If `SampleTime`

is `-1`

, for Simulink environments, the time interval between consecutive elements in the
returned output experience reflects the timing of the events that trigger the RL Agent block
execution, while for MATLAB environments, this time interval is considered equal to
`1`

.

This property is shared between the agent and the agent options object within the agent. Therefore, if you change it in the agent options object, it gets changed in the agent, and vice versa.

**Example: **`SampleTime=-1`

`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.

**Example: **`DiscountFactor=0.9`

`EntropyLossWeight`

— Entropy loss weight

`0.01`

(default) | scalar value between `0`

and `1`

Entropy loss weight, specified as a scalar value between `0`

and `1`

. A higher entropy loss weight value promotes agent
exploration by applying a penalty for being too certain about which action
to take. Doing so can help the agent move out of local optima.

When gradients are computed during training, an additional gradient component is computed for minimizing the entropy loss. For more information, see Entropy Loss.

**Example: **`EntropyLossWeight=0.02`

`ExperienceHorizon`

— Number of steps used to calculate the advantage

`512`

(default) | positive integer

Number of steps used to calculate the advantage, specified as a positive integer. For more information, see the agent training algorithm.

**Example: **`ExperienceHorizon=1024`

`MiniBatchSize`

— Mini-batch size

`128`

(default) | positive integer

Mini-batch size used for each learning epoch, specified as a positive integer. When the agent uses a recurrent neural network, `MiniBatchSize`

is treated as the training trajectory length.

The `MiniBatchSize`

value must be less than or equal to the `ExperienceHorizon`

value.

**Example: **`MiniBatchSize=256`

`NumEpoch`

— Number of times an agent learns over a data set of experiences

`1`

(default) | positive integer

Number of times an agent learns over a data set of experiences, specified as a
positive integer. This value defines the number of passes over a data set that has a
minimum length specified by the `LearningFrequency`

property.

**Example: **`NumEpoch=2`

`MaxMiniBatchPerEpoch`

— Maximum number of mini-batches used for learning during a single epoch

`100`

(default) | positive integer

Maximum number of mini-batches used for learning during a single epoch, specified as a positive integer.

For on-policy agents that support this property (PPO and TRPO), the actual number of
mini-batches used for learning depends on the length of aggregated trajectories, it has
a lower bound of
`LearningFrequency`

/`MiniBatchSize`

and an
upper bound of `MaxMiniBatchPerEpoch`

.

This value also specifies the maximum number of gradient steps per learning iteration
because the maximum number of gradient steps is equal to the
`MaxMiniBatchPerEpoch`

value multiplied by the
`NumEpoch`

value. For PPO and TRPO agents, it is good practice to
set this value to an arbitrarily high number to ensure all data is used for
training.

**Example: **`MaxMiniBatchPerEpoch=500`

`LearningFrequency`

— Minimum number of environment interactions between learning iterations

`-1`

(default) | positive integer

Minimum number of environment interactions between learning iterations, specified as a
positive integer or `-1`

. This value defines how many new data samples
need to be generated before learning. For on-policy agents that support this property
(PPO and TRPO), set `LearningFrequency`

to an integer multiple of
`MiniBatchSize`

. The default value of `-1`

indicates that 10*`MiniBatchSize`

samples are collected before
learning. Set this property to a lower value (for example,
2*`MiniBatchSize`

) if you want the agent to learn more
frequently.

**Example: **`LearningFrequency=4`

`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`

.

**Example: **```
CriticOptimizerOptions =
rlOptimizerOptions(LearnRate=5e-3)
```

`ConjugateGradientDamping`

— Conjugate gradient damping factor

`0.01`

(default) | nonnegative scalar

Conjugate gradient damping factor for numerical stability, specified as a nonnegative scalar.

**Example: **`ConjugateGradientDamping=0.05`

`KLDivergenceLimit`

— Upper limit for KL divergence

`0.01`

(default) | positive scalar

Upper limit for the Kullback-Leibler (KL) divergence between the old policy and the current policy, specified as a positive scalar.

**Example: **`KLDivergenceLimit=0.02`

`NumIterationsConjugateGradient`

— Maximum number of iterations for conjugate gradient descent

`10`

(default) | positive integer

Maximum number of iterations for conjugate gradient descent, specified as positive integer.

**Example: **`NumIterationsConjugateGradient=30`

`NumIterationsLineSearch`

— Number of iterations for line search

`10`

(default) | positive integer

Number of iterations for line search, specified as a positive integer.

Typically, the default value works well for most cases.

**Example: **`NumIterationsLineSearch=5`

`ConjugateGradientResidualTolerance`

— Conjugate gradient residual tolerance factor

`1e-8`

(default) | positive scalar

Conjugate gradient residual tolerance, specified as a positive scalar. Once the residual for the conjugate gradient algorithm is below this tolerance, the algorithm stops.

Typically, the default value works well for most cases.

**Example: **`ConjugateGradientResidualTolerance=2e-8`

`AdvantageEstimateMethod`

— Method for estimating advantage values

`"gae"`

(default) | `"finite-horizon"`

Method for estimating advantage values, specified as one of the following:

`"gae"`

— Generalized advantage estimator`"finite-horizon"`

— Finite horizon estimation

For more information on these methods, see the training algorithm information in Proximal Policy Optimization (PPO) Agent.

**Example: **`AdvantageEstimateMethod="finite-horizon"`

`GAEFactor`

— Smoothing factor for generalized advantage estimator

`0.95`

(default) | scalar value between `0`

and `1`

Smoothing factor for generalized advantage estimator, specified as a scalar value between `0`

and `1`

, inclusive. This option applies only when the `AdvantageEstimateMethod`

option is `"gae"`

**Example: **`GAEFactor=0.97`

`NormalizedAdvantageMethod`

— Method for normalizing advantage function

`"none"`

(default) | `"current`

| `"moving"`

Method for normalizing advantage function values, specified as one of the following:

`"none"`

— Do not normalize advantage values`"current"`

— Normalize the advantage function using the mean and standard deviation for the current mini-batch of experiences.`"moving"`

— Normalize the advantage function using the mean and standard deviation for a moving window of recent experiences. To specify the window size, set the`AdvantageNormalizingWindow`

option.

In some environments, you can improve agent performance by normalizing the advantage function during training. The agent normalizes the advantage function by subtracting the mean advantage value and scaling by the standard deviation.

**Example: **`NormalizedAdvantageMethod="moving"`

`AdvantageNormalizingWindow`

— Window size for normalizing advantage function

`1e6`

(default) | positive integer

Window size for normalizing advantage function values, specified as a positive integer. Use this option when the `NormalizedAdvantageMethod`

option is `"moving"`

.

**Example: **`AdvantageNormalizingWindow=1e5`

`InfoToSave`

— Options to save additional agent data

structure (default)

Options to save additional agent data, specified as a structure containing a
field named `Optimizer`

.

You can save an agent object in one of the following ways:

Using the

`save`

commandSpecifying

`saveAgentCriteria`

and`saveAgentValue`

in an`rlTrainingOptions`

objectSpecifying an appropriate logging function within a

`FileLogger`

object

When you save an agent using any method, the fields in the
`InfoToSave`

structure determine whether the
corresponding data is saved with the agent. For example, if you set the
`Optimizer`

field to `true`

,
then the actor and critic optimizers are saved along with the agent.

You can modify the `InfoToSave`

property only after the
agent options object is created.

**Example: **`options.InfoToSave.Optimizer=true`

`Optimizer`

— Option to save actor and critic optimizers

`false`

(default) | `true`

Option to save the actor and critic optimizers,
specified as a logical value. If you set the
`Optimizer`

field to
`false`

, then the actor and
critic optimizers (which are hidden properties of
the agent and can contain internal states) are not
saved along with the agent, therefore saving disk
space and memory. However, when the optimizers
contain internal states, the state of the saved
agent is not identical to the state of the original
agent.

**Example: **`true`

## Object Functions

`rlTRPOAgent` | Trust region policy optimization (TRPO) reinforcement learning agent |

## Examples

### Create TRPO Agent Options Object

Create a TRPO agent options object, specifying the discount factor.

opt = rlTRPOAgentOptions(DiscountFactor=0.9)

opt = rlTRPOAgentOptions with properties: SampleTime: 1 DiscountFactor: 0.9000 EntropyLossWeight: 0.0100 ExperienceHorizon: 512 MiniBatchSize: 128 NumEpoch: 1 MaxMiniBatchPerEpoch: 100 LearningFrequency: -1 CriticOptimizerOptions: [1x1 rl.option.rlOptimizerOptions] ConjugateGradientDamping: 0.1000 KLDivergenceLimit: 0.0100 NumIterationsConjugateGradient: 10 NumIterationsLineSearch: 10 ConjugateGradientResidualTolerance: 1.0000e-08 AdvantageEstimateMethod: "gae" GAEFactor: 0.9500 NormalizedAdvantageMethod: "none" AdvantageNormalizingWindow: 1000000 InfoToSave: [1x1 struct]

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

.

opt.SampleTime = 0.1;

## Version History

**Introduced in R2021b**

### R2024a: The `NumEpoch`

property changed behavior and default value

The `NumEpoch`

property changed behavior. Previously, this property
defined the number of passes over a data set with minimum length of
`ExperienceHorizon`

, when calculating the gradient. Now, it defines the
number of passes over a data set with minimum length of
`LearningFrequency`

.

### R2022a: Simulation and deployment: `UseDeterministicExploitation`

will be removed

The property `UseDeterministicExploitation`

of the
`rlTRPOAgentOptions`

object will be removed in a future release. Use the
`UseExplorationPolicy`

property of `rlTRPOAgent`

instead.

Previously, you set `UseDeterministicExploitation`

as follows.

Force the agent to always select the action with maximum likelihood, thereby using a greedy deterministic policy for simulation and deployment.

agent.AgentOptions.UseDeterministicExploitation = true;

Allow the agent to select its action by sampling its probability distribution for simulation and policy deployment, thereby using a stochastic policy that explores the observation space.

agent.AgentOptions.UseDeterministicExploitation = false;

Starting in R2022a, set `UseExplorationPolicy`

as follows.

Force the agent to always select the action with maximum likelihood, thereby using a greedy deterministic policy for simulation and deployment.

agent.UseExplorationPolicy = false;

Allow the agent to select its action by sampling its probability distribution for simulation and policy deployment, thereby using a stochastic policy that explores the observation space.

agent.UseExplorationPolicy = true;

Similarly to `UseDeterministicExploitation`

,
`UseExplorationPolicy`

affects only simulation and deployment; it does
not affect training.

## See Also

### Objects

## MATLAB 命令

您点击的链接对应于以下 MATLAB 命令：

请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。

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)