## Soft Actor-Critic (SAC) Agents

The soft actor-critic (SAC) algorithm is a model-free, online, off-policy, actor-critic reinforcement learning method. The SAC algorithm computes an optimal policy that maximizes both the long-term expected reward and the entropy of the policy. The policy entropy is a measure of policy uncertainty given the state. A higher entropy value promotes more exploration. Maximizing both the expected cumulative long term reward and the entropy helps to balance between exploitation and exploration of the environment.

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

The implementation of the SAC agent in Reinforcement Learning Toolbox™ software uses two Q-value function critics, which prevents overestimation of the value function. Other implementations of the SAC algorithm use an additional value function critic.

SAC agents can be trained in environments with the following observation and action spaces.

Observation Space | Action Space |
---|---|

Discrete or continuous | Continuous |

SAC agents use the following actor and critic.

Critics | Actor |
---|---|

Q-value function critics
| Stochastic policy actor |

During training, a SAC agent:

Updates the actor and critic properties at regular intervals during learning.

Estimates the mean and standard deviation of a Gaussian probability distribution for the continuous action space, then randomly selects actions based on the distribution.

Updates an entropy weight term that balances the expected return and the entropy of the policy.

Stores past experience using a circular experience buffer. The agent updates the actor and critic using a mini-batch of experiences randomly sampled from the buffer.

If the `UseExplorationPolicy`

option of the agent is set to
`false`

the action with maximum likelihood is always used in `sim`

and `generatePolicyFunction`

. As a result, the simulated agent and generated policy
behave deterministically.

If the `UseExplorationPolicy`

is set to `true`

the
agent selects its actions by sampling its probability distribution. As a result the policy is
stochastic and the agent explores its observation space.

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

### Actor and Critic Function Approximators

To estimate the policy and value function, a SAC agent maintains the following function approximators.

Stochastic actor

*π*(*A*|*S*;*θ*) — The actor, with parameters*θ*, outputs the mean ans standard deviation of conditional Gaussian probability of taking each continuous action*A*when in state*S*.One or two Q-value critics

*Q*(_{k}*S*,*A*;*ϕ*) — The critics, each with parameters_{k}*ϕ*, take observation_{k}*S*and action*A*as inputs and return the corresponding expectation of the value function, which includes both the long-term reward and entropy.One or two target critics

*Q*(_{tk}*S*,*A*;*ϕ*) — To improve the stability of the optimization, the agent periodically sets the target critic parameters_{tk}*ϕ*to the latest corresponding critic parameter values. The number of target critics matches the number of critics._{tk}

When you use two critics,
*Q*_{1}(*S*,*A*;*ϕ*_{1})
and
*Q*_{2}(*S*,*A*;*ϕ*_{2}),
each critic can have different structures. When the critics have the same structure, they
must have different initial parameter values.

Each critic
*Q _{k}*(

*S*,

*A*;

*ϕ*) and corresponding target critic

_{k}*Q*(

_{tk}*S*,

*A*;

*ϕ*) must have the same structure and parameterization.

_{tk}For more information on creating actors and critics for function approximation, see Create Policies and Value Functions.

During training, the agent tunes the parameter values in *θ*. After
training, the parameters remain at their tuned value and the trained actor function
approximator is stored in
*π*(*A*|*S*).

#### Action Generation

The actor in a SAC agent generates mean and standard deviation outputs. To select an action, the actor first randomly selects an unbounded action from a Gaussian distribution with these parameters. During training, the SAC agent uses the unbounded probability distribution to compute the entropy of the policy for the given observation.

If the action space of the SAC agent is bounded, the actor generates bounded actions
by applying *tanh* and *scaling* operations to the
unbounded action.

### Agent Creation

You can create and train SAC agents at the MATLAB^{®} command line or using the **Reinforcement Learning Designer** app. For
more information on creating agents using **Reinforcement Learning Designer**, see
Create Agents Using Reinforcement Learning Designer.

At the command line, you can create a SAC agent with default actor and critic based on the observation and action specifications from the environment. To do so, perform the following steps.

Create observation specifications for your environment. If you already have an environment interface object, you can obtain these specifications using

`getObservationInfo`

.Create action specifications for your environment. If you already have an environment interface object, you can obtain these specifications using

`getActionInfo`

.If needed, specify the number of neurons in each learnable layer or whether to use a recurrent neural network. To do so, create an agent initialization option object using

`rlAgentInitializationOptions`

.If needed, specify agent options using an

`rlSACAgentOptions`

object.Create the agent using an

`rlSACAgent`

object.

Alternatively, you can create actor and critics and use these objects to create your agent. In this case, ensure that the input and output dimensions of the actor and critic match the corresponding action and observation specifications of the environment.

Create a stochastic actor using an

`rlContinuousGaussianActor`

object. For SAC agents, in order to properly scale the mean values to the desired action range, the actor network must not contain a`tanhLayer`

and`scalingLayer`

as last two layers in the output path for the mean values. However, in order to ensure non-negativity of the standard deviation values, the actor network must contain a`reluLayer`

as a last layer in the output path for the standard deviation values.Create one or two critics using

`rlQValueFunction`

objects.Specify agent options using an

`rlSACAgentOptions`

object.Create the agent using an

`rlSACAgent`

object.

SAC agents do not support actors and critics that use recurrent deep neural networks as function approximators.

For more information on creating actors and critics for function approximation, see Create Policies and Value Functions.

### Training Algorithm

SAC agents use the following training algorithm, in which they periodically update their
actor and critic models and entropy weight. To configure the training algorithm, specify
options using an `rlSACAgentOptions`

object. Here, *K* = 2 is the number of critics and *k* is the critic
index.

Initialize each critic

*Q*(_{k}*S*,*A*;*ϕ*) with random parameter values_{k}*ϕ*, and initialize each target critic with the same random parameter values: $${\varphi}_{tk}={\varphi}_{k}$$._{k}Initialize the actor

*π*(*S*;*θ*) with random parameter values*θ*.Perform a warm start by taking a sequence of actions following the initial random policy in

*π*(*S*). For each action, store the experience in the experience buffer. To specify the number of warm up actions, use the`NumWarmStartSteps`

option.For each training time step:

For the current observation

*S*, select action*A*using the policy in*π*(*S*;*θ*).Execute action

*A*. Observe the reward*R*and next observation*S'*.Store the experience (

*S*,*A*,*R*,*S'*) in the experience buffer.Sample a random mini-batch of

*M*experiences (*S*,_{i}*A*,_{i}*R*,_{i}*S'*) from the experience buffer. To specify_{i}*M*, use the`MiniBatchSize`

option.Every

*D*time steps, update the parameters of each critic by minimizing the loss_{C}*L*across all sampled experiences. To specify_{k}*D*, use the_{C}`CriticUpdateFrequency`

option.$${L}_{k}=\frac{1}{2M}{\displaystyle \sum _{i=1}^{M}{\left({y}_{i}-{Q}_{k}\left({S}_{i},{A}_{i};{\varphi}_{k}\right)\right)}^{2}}$$

If

*S'*is a terminal state, the value function target_{i}*y*is equal to the experience reward_{i}*R*. Otherwise, the value function target is the sum of_{i}*R*, the minimum discounted future reward from the critics, and the weighted entropy._{i}$${y}_{i}={R}_{i}+\gamma *\underset{k}{\mathrm{min}}\left({Q}_{tk}\left({S}_{i}\text{'},{A}_{i}\text{'};{\varphi}_{tk}\right)\right)-\alpha \mathrm{ln}\pi \left({S}_{i}\text{'};\theta \right)$$

Here:

*A'*is the bounded action derived from the unbounded output of the actor_{i}*π*(*S'*)._{i}*γ*is the discount factor, which you specify using the`DiscountFactor`

option.$$-\alpha \mathrm{ln}\pi \left(S;\theta \right)$$ is the weighted policy entropy for the bounded output of the actor when in state

*S*.*α*is the entropy loss weight, which you specify using the`EntropyLossWeight`

option.

Every

*D*time steps, update the actor parameters by minimizing the following objective function. To set_{A}*D*, use the_{A}`PolicyUpdateFrequency`

option.$${J}_{\pi}=\frac{1}{M}{\displaystyle \sum _{i=1}^{M}\left(-\underset{k}{\mathrm{min}}\left({Q}_{k}\left({S}_{i},{A}_{i};{\varphi}_{k}\right)\right)+\alpha \mathrm{ln}\pi \left({S}_{i};\theta \right)\right)}$$

Every

*D*time steps, also update the entropy weight by minimizing the following loss function._{A}$${L}_{\alpha}=\frac{1}{M}{\displaystyle \sum _{i=1}^{M}\left(-\alpha \mathrm{ln}\pi \left({S}_{i};\theta \right)-\alpha \mathscr{H}\right)}$$

Here,

*ℋ*is the target entropy, which you specify using the`EntropyWeightOptions.TargetEntropy`

option.Every

*D*steps, update the target critics depending on the target update method. To specify_{T}*D*, use the_{T}`TargetUpdateFrequency`

option. For more information, see Target Update Methods.Repeat steps 4 through 8

*N*times, where_{G}*N*is the number of gradient steps, which you specify using the_{G}`NumGradientStepsPerUpdate`

option.

### Target Update Methods

SAC agents update their target critic parameters using one of the following target update methods.

Smoothing — Update the target critic parameters at every time step using smoothing factor

*τ*. To specify the smoothing factor, use the`TargetSmoothFactor`

option.$${\varphi}_{tk}=\tau {\varphi}_{k}+\left(1-\tau \right){\varphi}_{tk}$$

Periodic — Update the target critic parameters periodically without smoothing (

`TargetSmoothFactor = 1`

). To specify the update period, use the`TargetUpdateFrequency`

parameter.$${\varphi}_{tk}={\varphi}_{k}$$

Periodic smoothing — Update the target parameters periodically with smoothing.

To configure the target update method, create an `rlSACAgentOptions`

object, and set the `TargetUpdateFrequency`

and
`TargetSmoothFactor`

parameters as shown in the following table.

Update Method | `TargetUpdateFrequency` | `TargetSmoothFactor` |
---|---|---|

Smoothing (default) | 1 | Less than 1 |

Periodic | Greater than 1 | 1 |

Periodic smoothing | Greater than 1 | Less than 1 |

## References

[1] Haarnoja, Tuomas, Aurick Zhou, Kristian Hartikainen, George Tucker, Sehoon Ha, Jie Tan, Vikash Kumar, et al. "Soft Actor-Critic Algorithms and Application." Preprint, submitted January 29, 2019. https://arxiv.org/abs/1812.05905.