The idea: plan on a slippery floor

A robot lives on a 4×3 grid. Reaching the goal (4,3) is worth +1, falling into the pit (4,2) costs −1, and both end the game. (2,2) is a wall. The floor is slippery: a move goes where it is aimed with probability 0.8, and slides to the left or the right of that with probability 0.1 each. Bumping into the wall or the edge means staying put. Every move also earns a small living reward r, by default −0.04, so wandering around costs a little.

Left: the 4 by 3 grid with the wall at (2,2), the goal +1 at (4,3), the pit −1 at (4,2) and the start at (1,1). Right: a move aimed north goes north with probability 0.8 and west or east with probability 0.1 each
The grid world: one wall, two exits, and a move that only goes where it is aimed 80% of the time.

The question is not "what is the shortest path" but "what should I do in each cell, given that I might slip". Value iteration answers it by computing a value V(s) for every cell, the reward the robot can expect from there if it plays well, and then reading the best move off the values. Pick a demo and press Run Demo, or use Bellman Update, Sweep and Run to Convergence yourself.

A Markov decision process

The grid is a Markov decision process (MDP): a set of states s (the 9 open cells), actions a (N, E, S, W), transition probabilities P(s'|s,a) (0.8 / 0.1 / 0.1), a reward r for each move and a discount γ between 0 and 1 that makes a reward one step later worth γ times as much. "Markov" means the next state depends only on the current state and action, not on how the robot got there.

The Bellman update

For one cell, try each of the four actions. For each action, list where the robot can end up, weight each neighbour's value by its probability, and add the reward:

Q(s,a) = Σ P(s'|s,a) · [ r + γ · V(s') ]  =  r + γ · Σ P(s'|s,a) · V(s')
V(s)  ←  max over a of Q(s,a)

Q(s,a) is the action value: what the action is worth if the robot plays well afterwards. The new V(s) is the best of the four. At the start every value is 0, so for (3,3) next to the goal: E gives 0.8·1 + 0.1·0 + 0.1·0 = 0.8 and Q = −0.04 + 0.8 = 0.76; N and S each slip into the goal only 10% of the time (Q = 0.06); W gives −0.04. The new value is 0.76 (Demo 1). Next to the pit the sums mix good and bad news: from (3,2), N risks a 0.1 slide into the pit (Demo 2).

Sweeps and convergence

A sweep applies the Bellman update to every cell. The textbook version is synchronous: sweep k+1 reads only the old values Vk and writes Vk+1, so the order of the cells does not matter. After sweep 1 only the cells next to a terminal know about it; after sweep 2 their neighbours do; the values spread outward from the goal one cell per sweep (Demo 3).

Each sweep changes the values less than the one before. The page stops when the largest change in a sweep, Δ, is below ε = 0.001. With r = −0.04 and γ = 1 that takes 19 sweeps, and the values match the ones printed in Russell and Norvig: V(1,1) = 0.705, V(3,3) = 0.918, V(4,1) = 0.387 (the book prints 0.388, the exact limit). The chart on the right shows Δ on a log scale: it falls by a roughly constant factor per sweep. With γ < 1 that is guaranteed: the Bellman update is a contraction, and each sweep shrinks the error by at least a factor γ.

Policy extraction

The values are not the answer yet. The policy π(s) is the action with the largest Q(s,a) under the converged values: one arrow per cell (Extract Policy).

Converged values for r = −0.04 and γ = 1: top row 0.812, 0.868, 0.918, +1; middle row 0.762, wall, 0.660, −1; bottom row 0.705, 0.655, 0.611, 0.387; arrows: top row east, left column north, (3,2) north, bottom row west
After 19 sweeps: (3,2) goes N round the top, and (4,1) goes the long way W instead of passing the pit.

Two cells are interesting. (3,2), right next to the pit, goes N and accepts a 10% chance of slipping into the pit, because the long way round costs more. (4,1) goes W, away from the pit, instead of N along it. With deterministic moves (Demo 4) both risks disappear: every cell takes a shortest path, 6 sweeps suffice, and V(1,1) = 1 − 5 × 0.04 = 0.80.

How the living reward changes the policy

The reward per move decides how much the robot hurries.

r(3,2) next to the pit(4,1) under the pitsweeps
−2 (Demo 5)E: straight into the pitN: straight into the pit too15
−0.4N: shortest way, riskyW12
−0.04 (default)NW: the long way19
−0.01 (Demo 6)W: faces the wall, never slips into the pitS: faces the edge, slips only sideways45
+0.1 (Demo 7)every arrow avoids both exits: living is worth more than leavingnever
Three policies side by side. r = −2: (3,2) points east into the pit and (4,1) north. r = −0.04: (3,2) north and (4,1) west. r = −0.01: (3,2) west into the wall and (4,1) south into the edge
Living hurts a lot (left): end it, even in the pit. Living hurts a little (right): bump into walls rather than risk a slip.

With a very negative r, every move hurts more than the pit does, so cells near the pit jump in (Demo 5). With a slightly negative r, moves are cheap and the pit is the only real danger: (3,2) faces the wall, so a move either bumps (stays) or slips N or S, never E into the pit; (4,1) faces the bottom edge for the same reason (Demo 6). With a positive r and γ = 1, every extra move adds reward, so the best plan is never to finish; the values grow by 0.1 per sweep forever and value iteration never converges (Demo 7).

γ: how far ahead the robot looks

The discount γ makes a reward t steps away worth γt today. With γ = 1 the +1 counts in full however far away it is. With γ = 0.5 (Demo 8), the goal four steps away is worth only 0.54 ≈ 0.06, so far cells barely feel it: V(1,1) = −0.062, most of it the cost of living. A small γ also makes value iteration converge faster (7 sweeps), because the error shrinks by at least a factor γ per sweep. γ < 1 is what guarantees convergence when episodes could go on forever.

Policy iteration

Policy iteration works on the arrows directly. Start with any policy (here: every arrow E). Evaluate it: with the arrows fixed there is no max, just V(s) = r + γ Σ P V(s') for the one chosen action. Improve it: point each arrow at the best Q under those values. Repeat until no arrow changes (Demo 9). Here it needs 4 rounds, but each evaluation is itself a full solve. Both methods end with the same policy. Value iteration is the special case that improves after a single evaluation sweep.

From planning to learning

Value iteration needs the model: it reads P(s'|s,a) and r directly. A real robot often does not know them. Q-Learning learns the same Q values from experience instead, by trying moves and averaging what happens, and ends with the same kind of arrows. The update has the same shape: r + γ · (best value of the next state).

See also Dijkstra's algorithm and Bellman-Ford, which solve the deterministic version of the same equation (shortest paths) and also relax distances until nothing changes, and dynamic programming, of which value iteration is an example. Monte Carlo Tree Search plans by sampling instead of sweeping every state.

An MDP is a Markov chain with actions and rewards; the plain chain is in Markov Chains and Random Walks.

What the page leaves out

In-place (Gauss-Seidel) and asynchronous updates, which use new values within the same sweep and usually converge faster; prioritized sweeping; the bound ε(1−γ)/γ that turns Δ into a guarantee on the policy's loss; rewards that depend on the action or the next state; large or continuous state spaces, where values are approximated by a function (fitted value iteration, deep RL); partially observable MDPs, where the robot does not know which cell it is in; and solving policy evaluation exactly as a linear system.