v9.1.1
Menu

Value Iteration Bellman Error

mediumPRODynamic ProgrammingReinforcement LearningRL 1: Foundations

Problem Statement

Value iteration converges when the largest change across states drops below a threshold. Given the values before and after a sweep, compute the max-norm (sup-norm) difference:

∥Vnew−Vold∥∞=max⁡s∣Vnew(s)−Vold(s)∣\|V_{new} - V_{old}\|_\infty = \max_s |V_{new}(s) - V_{old}(s)|

Implement bellman_error(v_old, v_new) returning a float. An empty pair returns 0.0.

Example

Input
bellman_error([0.0, 0.0], [0.1, -0.3])
Output
0.3
Reasoning
  • Verify that the input lists are non-empty to ensure the calculation proceeds, as an empty state space would default the error to 0.00.0.
  • Pair the corresponding values from the old and new value functions to evaluate the change at each state: (0.0,0.1)(0.0, 0.1) and (0.0,−0.3)(0.0, -0.3).
  • Compute the absolute difference for the first state to measure the magnitude of change: ∣0.1−0.0∣=0.1|0.1 - 0.0| = 0.1.
  • Compute the absolute difference for the second state: ∣−0.3−0.0∣=0.3|-0.3 - 0.0| = 0.3.
  • Determine the max-norm by selecting the largest of these differences, which represents the maximum convergence error: max⁡(0.1,0.3)=0.3\max(0.1, 0.3) = 0.3.
  • The final output is 0.3

Constraints

  • len(v_old) == len(v_new).
  • Return the maximum absolute per-state difference.

More from RL 1: Foundations

PRO

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

Sign in to run your code against the test cases.