## Actor-Critic (AC) Agents

You can use the actor-critic (AC) agent, which uses a model-free, online, on-policy reinforcement learning method, to implement actor-critic algorithms, such as A2C and A3C. The goal of this agent is to optimize the policy (actor) directly and train a critic to estimate the return or future rewards [1].

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

Actor-critic agents can be trained in environments with the following observation and action spaces.

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

Discrete or continuous | Discrete or continuous |

Actor-critic agents use the following actor and critics.

Critic | Actor |
---|---|

Value function critic | Stochastic policy actor |

During training, an actor-critic agent:

Estimates probabilities of taking each action in the action space and randomly selects actions based on the probability distribution.

Interacts with the environment for multiple steps using the current policy before updating the actor and critic properties.

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, an actor-critic agent maintains two function approximators.

Actor

*π*(*A*|*S*;*θ*) — The actor, with parameters*θ*, outputs the conditional probability of taking each action*A*when in state*S*as one of the following:Discrete action space — The probability of taking each discrete action. The sum of these probabilities across all actions is 1.

Continuous action space — The mean and standard deviation of the Gaussian probability distribution for each continuous action.

Critic

*V*(*S*;*ϕ*) — The critic, with parameters*ϕ*, takes observation*S*and returns the corresponding expectation of the discounted long-term reward.

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*).

### Agent Creation

You can create an actor-critic agent with default actor and critics 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 an LSTM layer. To do so, create an agent initialization option object using

`rlAgentInitializationOptions`

.If needed, specify agent options using an

`rlACAgentOptions`

object.Create the agent using an

`rlACAgent`

object.

Alternatively, you can create actor and critic 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 an actor using an

`rlDiscreteCategoricalActor`

(for discrete action spaces) or an`rlContinuousGaussianActor`

(for continuous action spaces) object.Create a critic using an

`rlValueFunction`

object.Specify agent options using an

`rlACAgentOptions`

object.Create the agent using an

`rlACAgent`

object.

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

### Training Algorithm

Actor-critic agents use the following training algorithm. To configure the training
algorithm, specify options using an `rlACAgentOptions`

object.

Initialize the actor

*π*(*A*|*S*;*θ*) with random parameter values*θ*.Initialize the critic

*V*(*S*;*ϕ*) with random parameter values*ϕ*.Generate

*N*experiences by following the current policy. The episode experience sequence is$${S}_{ts},{A}_{ts},{R}_{ts+1},{S}_{ts+1},\dots ,{S}_{ts+N-1},{A}_{ts+N-1},{R}_{ts+N},{S}_{ts+N}$$

Here,

*S*is a state observation,_{t}*A*is an action taken from that state,_{t}*S*is the next state, and_{t+1}*R*is the reward received for moving from_{t+1}*S*to_{t}*S*._{t+1}When in state

*S*, the agent computes the probability of taking each action in the action space using_{t}*π*(*A*|*S*;_{t}*θ*) and randomly selects action*A*based on the probability distribution._{t}*ts*is the starting time step of the current set of*N*experiences. At the beginning of the training episode,*ts*= 1. For each subsequent set of*N*experiences in the same training episode,*ts*=*ts*+*N*.For each training episode that does not contain a terminal state,

*N*is equal to the`NumStepsToLookAhead`

option value. Otherwise,*N*is less than`NumStepsToLookAhead`

and*S*is the terminal state._{N}For each episode step

*t*=*ts*+1,*ts*+2, …,*ts*+*N*, compute the return*G*, which is the sum of the reward for that step and the discounted future reward. If_{t}*S*is not a terminal state, the discounted future reward includes the discounted state value function, computed using the critic network_{ts+N}*V*.$${G}_{t}={\displaystyle \sum _{k=t}^{ts+N}\left({\gamma}^{k-t-1}{R}_{k}\right)}+b{\gamma}^{ts+N-t}V\left({S}_{ts+N};\varphi \right)$$

Here,

*b*is`0`

if*S*is a terminal state and_{ts+N}`1`

otherwise.To specify the discount factor

*γ*, use the`DiscountFactor`

option.Compute the advantage function

*D*._{t}$${D}_{t}={G}_{t}-V\left({S}_{t};\varphi \right)$$

Accumulate the gradients for the actor network by following the policy gradient to maximize the expected discounted reward.

$$d\theta ={\displaystyle \sum _{t=1}^{N}{\nabla}_{{\theta}_{\mu}}}\mathrm{ln}\pi \left(A|{S}_{t};\theta \right)\cdot {D}_{t}$$

Accumulate the gradients for the critic network by minimizing the mean squared error loss between the estimated value function

*V*(*S*;_{t}*ϕ*) and the computed target return*G*across all_{t}*N*experiences. If the`EntropyLossWeight`

option is greater than zero, then additional gradients are accumulated to minimize the entropy loss function.$$d\varphi ={\displaystyle \sum _{t=1}^{N}{\nabla}_{\varphi}}{\left({G}_{t}-V\left({S}_{t};\varphi \right)\right)}^{2}$$

Update the actor parameters by applying the gradients.

$$\theta =\theta +\alpha d\theta $$

Here,

*α*is the learning rate of the actor. Specify the learning rate when you create the actor by setting the`LearnRate`

option in the`rlActorOptimizerOptions`

property within the agent options object.Update the critic parameters by applying the gradients.

$$\varphi =\varphi +\beta d\varphi $$

Here,

*β*is the learning rate of the critic. Specify the learning rate when you create the critic by setting the`LearnRate`

option in the`rlCriticOptimizerOptions`

property within the agent options object.Repeat steps 3 through 9 for each training episode until training is complete.

For simplicity, the actor and critic updates in this algorithm description show a
gradient update using basic stochastic gradient descent. The actual gradient update method
depends on the optimizer you specify using in the `rlOptimizerOptions`

object
assigned to the `rlCriticOptimizerOptions`

property.

## References

[1] Mnih, Volodymyr, Adrià
Puigdomènech Badia, Mehdi Mirza, Alex Graves, Timothy P. Lillicrap, Tim Harley, David
Silver, and Koray Kavukcuoglu. “Asynchronous Methods for Deep Reinforcement Learning.”
*ArXiv:1602.01783 [Cs]*, February 4, 2016.
https://arxiv.org/abs/1602.01783.