v9.1.1
Menu

Advantage from Q and V

mediumPROReturns, Policies, and Value FunctionsReinforcement LearningRL 1: Foundations

Problem Statement

The advantage of an action measures how much better it is than the state's average:

Aπ(s,a)=Qπ(s,a)−Vπ(s)A^\pi(s, a) = Q^\pi(s, a) - V^\pi(s)

Given action values q and the policy pi at a state, compute V = sum_a pi[a] q[a] and return the list of advantages A[a] = q[a] - V. Implement advantages(pi, q).

Example

Input
advantages([0.5, 0.5], [2.0, 4.0])
Output
[-1.0, 1.0]
Reasoning
  • Compute the state value VV by taking the weighted sum of the action values using the policy probabilities: V=0.5×2.0+0.5×4.0=1.0+2.0=3.0V = 0.5 \times 2.0 + 0.5 \times 4.0 = 1.0 + 2.0 = 3.0.
  • Calculate the advantage for the first action by subtracting the state value from its specific action value: A[0]=2.0−3.0=−1.0A[0] = 2.0 - 3.0 = -1.0.
  • Calculate the advantage for the second action by subtracting the state value from its specific action value: A[1]=4.0−3.0=1.0A[1] = 4.0 - 3.0 = 1.0.
  • The final output is [-1.0, 1.0]

Constraints

  • len(pi) == len(q), valid distribution.
  • Under any valid policy, sum_a pi[a] * A[a] is 0.
  • Return a list of floats.

Learn this in

More from RL 1: Foundations

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.