PIXELBANKv9.1.0
Menu

Full Value Iteration to Convergence

Problem Statement

Run value iteration to convergence on a finite MDP and return the optimal values. The MDP is given as mdp[s] = list of actions, each {"reward": r, "probs": [...], "next": [...]}. Start from all-zero values and repeat synchronous optimality sweeps until the max-norm change is below theta, or max_iters is reached.

V(s)=max⁡a[r+γ∑iprobsi V(nexti)]V(s) = \max_a \left[ r + \gamma \sum_i probs_i\, V(next_i) \right]

Implement value_iteration(mdp, gamma, theta, max_iters) returning the optimal values (list of floats, rounded is not required).

Example:

Input:
value_iteration([[{"reward":0.0,"probs":[1.0],"next":[0]}]], 0.9, 1e-9, 1000)
Output:
[0.0]
Reasoning:
  • Initialization: The MDP contains a single state (n=1n=1). The value function VV is initialized to all zeros, so V[0]=0.0V[0] = 0.0.
  • First Iteration (Sweep): For state 0, the only available action has a reward of 0.00.0 and transitions to state 0 with probability 1.01.0. The Q-value is calculated as Q=0.0+0.9×(1.0×V[0])=0.0+0.9×0.0=0.0Q = 0.0 + 0.9 \times (1.0 \times V[0]) = 0.0 + 0.9 \times 0.0 = 0.0. Since this is the only action, the new value for state 0 is new_V[0]=0.0\text{new\_V}[0] = 0.0.
  • Convergence Check: The maximum change in values is Δ=∣0.0−0.0∣=0.0\Delta = |0.0 - 0.0| = 0.0. This change is compared against the threshold θ=10−9\theta = 10^{-9}. Since 0.0<10−90.0 < 10^{-9}, the convergence criterion is met immediately after the first iteration.
  • Termination: The loop breaks because the values have stabilized (the change is below the threshold). The algorithm does not need to perform further iterations.
  • The final output is [0.0]

Constraints:

  • Synchronous updates (a full sweep uses the previous sweep's values).
  • Stop when max_s |V_new - V_old| < theta or after max_iters sweeps.
  • 0 <= gamma < 1 guarantees convergence.
🔒

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.
Full Value Iteration to Convergence - Hard | PixelBank