One-Step Advantage: Better or Worse Than Expected?

Reinforcement LearningEasy+10 XP+10

One-Step Advantage: Better or Worse Than Expected?

"This action earned a reward of 10" means nothing on its own. Was 10 good? If the agent expected 12 from that state, it was a disappointment. The advantage answers the only question that matters for learning: did this turn out better or worse than expected?

The one-step version compares what actually happened against the critic's prediction for the state you were in:

At=rt+γV(st+1)−V(st)A_t = r_t + \gamma V(s_{t+1}) - V(s_t)At​=rt​+γV(st+1​)−V(st​)

Task: write advantages(rewards, values, gamma) returning one advantage per step, each rounded to 4 decimal places.

  • rewards[t] is the reward at step t; values[t] is the critic's estimate for the state at step t. Both lists are the same length.
  • The episode ends after the last step, so there is no values[t+1] for it. Treat the value after the final state as 0 — a terminal state has no future worth anything.
  • Advantages are commonly negative, and that's the useful half: a negative advantage means the policy should make that action less likely next time.

This is why policy-gradient methods keep a critic at all. Multiplying the gradient by a raw return pushes up every action that preceded any positive reward, including the bad ones in a lucky episode. Multiplying by the advantage pushes up only the actions that beat the baseline — far less noise, far faster learning.

Examples

rewards = [1.0, 1.0, 1.0]
values = [0.5, 0.5, 0.5]
gamma = 0.9
returns [0.95, 0.95, 0.5]
rewards = [0.0, 10.0]
values = [1.0, 2.0]
gamma = 1.0
returns [1.0, 8.0]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run