Skip to content
Lambdian

Value iteration

Solve the Bellman optimality equation by repeated backups; extract the optimal policy greedily.

Animated explanation

The idea

Value iteration skips the separate evaluation step of policy iteration. It simply applies the Bellman optimality backup to every state, over and over: the new value of a state is the best, over actions, of the expected immediate reward plus the discounted value of where you land.

Think of the goal's reward spreading backwards through the grid like ripples, one step per sweep, each step discounted by γ\gamma. Because the backup is a contraction, the values converge to the optimal values V∗V^\ast from any starting guess, and acting greedily on V∗V^\ast is optimal. It is a few lines of code and the cleanest example of dynamic programming in RL.

Why it converges

The backup operator TT satisfies ∥TV−TU∥∞≤γ∥V−U∥∞\lVert TV - TU\rVert_\infty \le \gamma\lVert V-U\rVert_\infty. By the Banach fixed-point theorem it has a unique fixed point (V∗V^\ast) and the iterates approach it geometrically. In practice, stop when the largest change in a sweep is below ε; the greedy policy is usually optimal well before the values have fully converged.

Variants

In-place (Gauss–Seidel) updates use new values immediately and converge faster. Prioritised sweeping updates states whose values are changing most. Asynchronous DP updates states in any order. With function approximation, the same idea becomes fitted value iteration, the ancestor of DQN.

The algorithm, step by step

  1. 1
    Initialise V(s)=0V(s) = 0 for all states.
  2. 2
    Repeat: for every non-terminal state, V(s)←max⁡a∑s′P(s′∣s,a)[r+γV(s′)]V(s) \leftarrow \max_a\sum_{s'}P(s'\mid s,a)[r + \gamma V(s')]; track the largest change Δ.
  3. 3
    Stop when Δ < ε.
  4. 4
    Output the greedy policy π(s)=arg⁡max⁡aQ(s,a)\pi(s) = \arg\max_a Q(s,a).

The maths

Backup

Vk+1(s)=max⁡a∑s′P(s′∣s,a)[r+γVk(s′)]V_{k+1}(s) = \max_a\sum_{s'}P(s'|s,a)[r + \gamma V_k(s')]

Contraction

∥Vk−V∗∥∞≤γk∥V0−V∗∥∞\lVert V_{k} - V^\ast\rVert_\infty \le \gamma^k\lVert V_0 - V^\ast\rVert_\infty

Stopping bound

∥Vk+1−Vk∥∞<ε ⇒ ∥Vk+1−V∗∥∞<εγ1−γ\lVert V_{k+1}-V_k\rVert_\infty < \varepsilon \ \Rightarrow\ \lVert V_{k+1} - V^\ast\rVert_\infty < \frac{\varepsilon\gamma}{1-\gamma}

Strengths and weaknesses

Strengths

  • Very simple; each sweep is cheap
  • Guaranteed convergence to V*
  • Basis for fitted and deep value methods

Weaknesses

  • Needs the full model
  • Many sweeps when γ is close to 1
  • Enumerates the whole state space

Reach for it when…

Known-model planning in small or discretised state spaces; as the reference solution when testing model-free agents.

Check your understanding

Question 1

Q1What is the difference between value iteration and policy iteration?

Question 2

Q2With γ = 0.9, after each sweep the error to V* shrinks by at least…

Question 3

Q3Once V* is known, how do you get the optimal policy?

Lambdian Lab

Value iteration

Using the provided gridworld:

  • bellman_backup(V, s, gamma): max⁡a\max_a of the expected one-step look-ahead.
  • value_iteration(gamma, tol): synchronous sweeps (compute the new V from the old one) until the max change < tol; return (V, sweeps).
  • greedy_policy(V, gamma): dict state → best action (ties → lowest index).
  • bellman_residual(V, gamma): max over states of |backup − V|.
Open the labPython + NumPy runs in your browser. Sign-in keeps your code and attempts.