PIXELBANKv9.1.0
Menu

Q-Values from State Values

Problem Statement

Given state values V, recover the action values for one state. Each action a provides reward[a] and a next-state distribution P[a] (P[a][s']):

Q(s,a)=reward[a]+Ξ³βˆ‘sβ€²P[a][sβ€²] V(sβ€²)Q(s,a) = reward[a] + \gamma \sum_{s'} P[a][s']\, V(s')

Implement q_from_v(reward, P, gamma, V) returning the list of Q-values, one per action.

Example:

Input:
q_from_v([1.0, 0.0], [[1.0, 0.0], [0.0, 1.0]], 0.9, [0.0, 10.0])
Output:
[1.0, 9.0]
Reasoning:
  • Action 0 Calculation: For the first action, the immediate reward is 1.01.0. The expected value of the next state is computed by weighting the state values V=[0.0,10.0]V = [0.0, 10.0] by the transition probabilities P[0]=[1.0,0.0]P[0] = [1.0, 0.0], resulting in 1.0Γ—0.0+0.0Γ—10.0=0.01.0 \times 0.0 + 0.0 \times 10.0 = 0.0.
  • Action 0 Q-Value: The Q-value is the sum of the immediate reward and the discounted expected future value: 1.0+0.9Γ—0.0=1.01.0 + 0.9 \times 0.0 = 1.0.
  • Action 1 Calculation: For the second action, the immediate reward is 0.00.0. The expected value of the next state is computed using the transition probabilities P[1]=[0.0,1.0]P[1] = [0.0, 1.0], resulting in 0.0Γ—0.0+1.0Γ—10.0=10.00.0 \times 0.0 + 1.0 \times 10.0 = 10.0.
  • Action 1 Q-Value: The Q-value is the sum of the immediate reward and the discounted expected future value: 0.0+0.9Γ—10.0=9.00.0 + 0.9 \times 10.0 = 9.0.
  • The final output is [1.0, 9.0]

Constraints:

  • len(reward) == len(P) (one row per action).
  • Each P[a] aligns with V and sums to 1.
  • 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.
Q-Values from State Values - Medium | PixelBank