PIXELBANKv9.1.0
Menu

State Value from Action Values

Problem Statement

The state value under a policy is the policy-weighted average of action values:

Vπ(s)=∑aπ(a∣s) Qπ(s,a)V^\pi(s) = \sum_a \pi(a\mid s)\, Q^\pi(s, a)

Given the policy distribution pi and action values q (same length, aligned by action), implement state_value(pi, q).

Example:

Input:
state_value([0.5, 0.5], [2.0, 4.0])
Output:
3.0
Reasoning:
  • Identify the policy probabilities Ï€\pi and action values QQ from the input: Ï€=[0.5,0.5]\pi = [0.5, 0.5] and Q=[2.0,4.0]Q = [2.0, 4.0].
  • Calculate the weighted contribution for the first action by multiplying its probability by its value: 0.5×2.0=1.00.5 \times 2.0 = 1.0.
  • Calculate the weighted contribution for the second action similarly: 0.5×4.0=2.00.5 \times 4.0 = 2.0.
  • Sum these individual contributions to compute the policy-weighted average state value: 1.0+2.0=3.01.0 + 2.0 = 3.0.
  • The final output is 3.0

Constraints:

  • len(pi) == len(q), 1 <= len <= 1000
  • pi is a valid distribution (sums to 1).
  • Return a float.
🔒

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.