PIXELBANKv9.1.0
Menu

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.
πŸ”’

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

0/0
Run code to see test results.
Advantage from Q and V - Medium | PixelBank