liveBook · Manning

Advantage function

As explained in Optimization Algorithms: AI techniques for design, planning, and control problems

An advantage is the estimated improvement in expected future reward from taking an action in a state compared with the average action in that state.

When an agent chooses an action, its reward alone does not say whether the choice was good: some states offer better prospects than others. The advantage function compares the expected future reward from taking an action with the expected future reward from the state under the current policy. In shorthand, A(s, a) = Q(s, a) − V(s). A positive advantage favors that action relative to the state’s baseline; a negative one disfavors it. section 12.1.2 section 12.1.4

In a policy-gradient update, an estimated advantage gives feedback on an action sampled from the policy. Positive estimates increase the probability of choosing that action in similar states; negative estimates decrease it. In advantage actor-critic (A2C), the actor produces action probabilities, while the critic supplies value estimates that serve as a baseline for this feedback. section 12.1.2 section 12.1.4

A CartPole step

For CartPole, the actor chooses between pushing left and pushing right. A one-step advantage estimate is reward + (1 − done) × γ × V(next state) − V(current state). If the episode ends, the next-state term vanishes; otherwise, the discount factor γ weights its estimated future value. The implementation minimizes the mean squared advantage to update the critic, then uses the sampled action’s negative log probability times a detached advantage to update the actor. It tracks episode rewards to see whether balancing improves. section 12.3

advantage = reward + (1-
 done)*gamma*critic(torch.from_numpy(next_state).float()) - 
    critic(torch.from_numpy(state).float())       #1
  
total_reward += reward                              #2
state = next_state                                  #3
#1 Compute the advantage function.
#2 Update the total reward.
#3 Move to the next state.

Inspect the one-step calculation to see how the reward, terminal flag, discount factor, and two critic estimates combine. section 12.3

The estimate depends on the policy and the experiences collected under it. Repeatedly updating a policy on one batch can move it far from the policy that generated those experiences, making the advantage estimate inaccurate. Proximal policy optimization (PPO) addresses large policy shifts with penalty- or clipping-based variants. An advantage is useful feedback, but its reliability still matters when choosing how far to update. section 12.1.5

How Optimization Algorithms: AI techniques for design, planning, and control problems covers it

How deep it goes
Defines Q(s, a) − V(s), explains its effect on action probabilities, and works through a one-step CartPole estimate.
Examples
Uses CartPole’s left-or-right actions, a one-step advantage calculation, actor and critic losses, and episode-reward tracking.
What it assumes
Builds on states, actions, rewards, policies, and value functions while defining advantage as a comparison with the state baseline.
What you can do after
Build it from scratch in the shown A2C CartPole setup to compute advantages and update an actor and critic.
How it connects
Links policy gradients, state-value and action-value functions, actor-critic methods, and proximal policy optimization.

Also explained in

Compare how 3 books explain advantage function →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Optimization Algorithms: AI techniques for design, planning, and control problems. The section links point to the parts of the book it draws on.