Main Content

Actor-Critic (AC) Agent

Actor-critic (AC) agents implement actor-critic algorithms such as A2C and A3C, which are on-policy policy-gradient reinforcement learning methods for environments with a discrete or continuous action space. The actor-critic agent directly optimizes a stochastic policy and uses a value function critic to estimate the value of the policy [1]. The AC agent is similar to a REINFORCE policy gradient agent with a baseline, with the difference that AC uses a fully bootstrapping critic (that is, it used to update the value estimate of a state based on the value estimates of subsequent states). You can use the, which uses to implement actor-critic algorithms, such as A2C and A3C.

For continuous action spaces and with custom networks, this agent does not automatically enforce the constraints set by the action specification. In this case, you must enforce action space constraints within the environment. The default agent for continuous action spaces automatically enforces constraints on the action.

In Reinforcement Learning Toolbox™, a REINFORCE actor-critic agent is implemented by an rlACAgent object.

Note

AC agents do not generally have functional advantages with respect to more recent agents such as PPO and are provided mostly for educational purposes.

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 SpaceAction Space
Discrete, continuous, or hybrid.Discrete or continuous

Actor-critic agents use the following actor and critics.

CriticActor

Value function critic V(S), which you create using rlValueFunction

Stochastic policy actor π(S), which you create using rlDiscreteCategoricalActor (for discrete action spaces) or rlContinuousGaussianActor (for continuous action spaces)

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.

Note

The UseExplorationPolicy option affects only simulation and deployment; it does not affect training. When you train an agent using train, the agent always uses its exploration policy independently of the value of this property.

Actor and Critic Used by the AC Agent

To estimate the policy and value function, an actor-critic agent maintains two function approximators.

  • Stochastic 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 — A vector containing the probabilities of taking each discrete action. The sum of these probabilities across all actions is 1.

    • Continuous action space — Two vectors containing the means and standard deviations, respectively, of the Gaussian distribution for each continuous action.

    The agent samples these probability distributions to obtain the action vector.

During training, the actor tunes the parameter values in θ to improve the policy. Similarly, during training, the critic tunes the parameter values in ϕ to improve its value function estimation. After training, the parameters remain at their tuned values in the actor and critic internal to the trained agent.

For more information on actors and critics, see Create Actors, Critics, and Policy Objects.

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

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

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

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

  4. If needed, specify agent options using an rlACAgentOptions object. Alternatively, you can skip this step and modify the agent options later using dot notation.

  5. Create the agent using rlACAgent.

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.

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

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

  3. Create an approximation model for your actor. For continuous action spaces, this model must be a neural network object. For discrete action spaces, you also have the option of using a custom basis function with initial parameter values.

  4. Create an actor using rlDiscreteCategoricalActor (for discrete action spaces) or rlContinuousGaussianActor (for continuous action spaces). Use the model you created in the previous step as a first input argument.

  5. Create an approximation model for your critic using either a custom basis function with initial parameter values or a neural network object.

  6. Create a critic using rlValueFunction. Use the model you created in the previous step as a first input argument.

  7. Specify agent options using an rlACAgentOptions object. Alternatively, you can skip this step and modify the agent options later using dot notation.

  8. Create the agent using rlACAgent.

For more information on creating actors and critics for function approximation, see Create Actors, Critics, and Policy Objects.

AC Agent Initialization

When you create an AC agent, the actor and critic initialize as follows.

  • The actor π(A|S;θ) uses random parameter values in θ.

  • The critic V(S;ϕ) uses parameter values in ϕ.

The agent uses this initial actor and critic parameters at the beginning of the first training session. For each subsequent training session, the actor and critic retain the parameters from the previous session.

AC Training Algorithm

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

  1. Generate up to N+1 experiences (within the current episode) by following the current policy.

    1. At the beginning of each episode, get the initial observation S1 from the environment.

    2. For the current observation St, select the action At using the policy in π(A|S;θ).

    3. Execute action At. Observe the reward Rt+1 and the next observation St+1.

    4. Store the experience (St,At,Rt+1,St+1).

    The episode experience sequence is

    Sts,Ats,Rts+1,Sts+1,,Sts+N1,Ats+N1,Rts+N,Sts+N

    Here, 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 value of the NumStepsToLookAhead option. Otherwise, N is less than NumStepsToLookAhead and SN is the terminal state.

  2. For each episode step t = ts, ts+1, …, ts+N, compute the return Gt, which is the sum of the reward for that step and the discounted future reward. If Sts+N is not a terminal state, the discounted future reward includes the discounted state value function, computed using the critic network V.

    Gt=k=t+1ts+N(γkt1Rk)+bγts+NtV(Sts+N;ϕ)

    Here, b is 0 if Sts+N is a terminal state and 1 otherwise.

    To specify the discount factor γ, use the DiscountFactor option.

  3. Compute the advantage function Dt.

    Dt=GtV(St;ϕ)

  4. Accumulate the gradients for the actor network by following the policy gradient to maximize the expected discounted cumulative long-term reward.

    dθ=t=1Nθμlnπ(A|St;θ)Dt

    Here,

    • t(θ,St) is the entropy loss and w is the entropy loss weight factor, specified using the EntropyLossWeight option. For more information on entropy loss, see Entropy Loss.

    • π(Ai|Si;θ) indicates one of the following quantities:

      • For a discrete action space — The probability of taking the discrete action Ai, given observation Si, and parameters θ.

      • For a continuous action space — The value of the joint probability density function of the action, calculated for the action vector Ai and the observation Si, given parameters θ:

        π(Ai|S;θ)=j=1Nfj(Aj,i|Si;θ)

        Here, fj is the probability density function for the dimension j of the action vector, and Ai,j is the jth element of Ai, which has dimension N.

  5. Accumulate the gradients for the critic network by minimizing the mean squared error loss between the estimated value function V (St;ϕ) and the computed target return Gt across all N experiences.

    dϕ=t=1Nϕ(GtV(St;ϕ))2

  6. Update the actor parameters by applying the gradients.

    θ=θ+αdθ

    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.

  7. Update the critic parameters by applying the gradients.

    ϕ=ϕ+βdϕ

    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.

  8. 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 in the rlOptimizerOptions object assigned to the rlCriticOptimizerOptions property.

Entropy Loss

To promote agent exploration, you can subtract an entropy loss term wi(θ,Si) from the actor loss function, where w is the entropy loss weight and i(θ,Si) is the entropy.

The entropy value is higher when the agent is more uncertain about which action to take next. Therefore, maximizing the entropy loss term (minimizing the negative entropy loss) increases the agent uncertainty, thus encouraging exploration. To promote additional exploration, which can help the agent move out of local optima, you can specify a larger entropy loss weight.

For a discrete action space, the agent uses the following entropy value. In this case, the actor outputs the probability of taking each possible discrete action.

i(θ,Si)=id(θ,Si)=k=1Pπ(Ak|Si;θ)lnπ(Ak|Si;θ)

Here:

  • P is the number of possible discrete actions.

  • π(Ak|Si;θ) is the probability of taking action Ak when in state Si following the current policy.

For a continuous action space, the agent uses the following entropy value. In this case, the actor outputs the mean and standard deviation of the Gaussian distribution for each continuous action.

i(θ,Si)=ic(θ,Si)=12k=1NCln(2πeσk,i2)

Here:

  • C is the number of continuous actions output by the actor.

  • σk,i is the standard deviation for action k when in state Si following the current policy.

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, June 16, 2016. https://doi.org/10.48550/arXiv.1602.01783.

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

See Also

Objects

Topics