PIXELBANKv9.1.0
Menu

One Sweep of Iterative Policy Evaluation

Problem Statement

Perform one synchronous sweep of iterative policy evaluation over all states, given the induced transition matrix P (P[s][s']) and reward vector r:

Vk+1(s)=r(s)+γ∑s′P(s′∣s) Vk(s′)V_{k+1}(s) = r(s) + \gamma \sum_{s'} P(s'\mid s)\, V_k(s')

Given the current values V, return the updated values after one sweep (use the old V for all right-hand sides — synchronous). Implement policy_eval_sweep(P, r, gamma, V).

Example:

Input:
policy_eval_sweep([[1.0]], [1.0], 0.9, [0.0])
Output:
[1.0]
Reasoning:
  • Identify the single state s=0s=0 and retrieve its parameters: transition probability P[0][0]=1.0P[0][0] = 1.0, reward r[0]=1.0r[0] = 1.0, discount factor γ=0.9\gamma = 0.9, and current value V[0]=0.0V[0] = 0.0.
  • Calculate the expected value of the next state by summing the products of transition probabilities and current values: E[Vnext]=P[0][0]×V[0]=1.0×0.0=0.0E[V_{next}] = P[0][0] \times V[0] = 1.0 \times 0.0 = 0.0.
  • Apply the Bellman update equation to compute the new value for state 0: Vnew(0)=r[0]+γ×E[Vnext]=1.0+0.9×0.0=1.0V_{new}(0) = r[0] + \gamma \times E[V_{next}] = 1.0 + 0.9 \times 0.0 = 1.0.
  • The final output is [1.0]

Constraints:

  • P is n x n (rows sum to 1), r and V length n.
  • Synchronous update: every state uses the same input V.
  • Return a list of n 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.
One Sweep of Iterative Policy Evaluation - Medium | PixelBank