5.1.9 · D2Reinforcement Learning Foundations

Visual walkthrough — Dynamic programming (value - policy iteration)

2,335 words11 min readBack to topic

This page builds the central engine of dynamic programming — the value iteration update — one picture at a time. We start with nothing but a grid, a walking agent, and the idea of "how good is it to stand here?" By the end you will have watched a number we will call grow, step by step, from a blank board into the true "distance-to-goal" map. That settled map has a name we will earn in Step 3 — the optimal value — but for now, think of it simply as the number written on each square.

Prerequisites we lean on (and re-explain visually): Bellman-equations, Markov Decision Processes, and the Contraction-mapping-theorem. The parent is Dynamic Programming (Value/Policy Iteration).


Step 1 — What does a "value" even mean? Draw the walker

WHAT. Picture a board. A tiny walker stands on a square. We label squares by (column, row) starting from the top-left: column runs left-to-right, row runs top-to-bottom. So the walker's home top-left corner is and the goal bottom-right is . Every square holds a single number I will call — read it as "if I start here and play well forever, how much total reward do I collect?" The letter just means "which square" (the state).

WHY. Before any formula, we need one honest question: how good is it to be standing here? Everything else is machinery for filling in that number correctly. In our grid the only reward is for every step you take, and stepping onto the goal ends the game. So "good" means "close to the goal" — few steps left — and will end up being (roughly) minus the number of steps to the exit.

Edge case — walking into a wall. What if the walker tries to move off the board (e.g. "left" from column )? We use the standard rule: the move is blocked, the walker stays on the same square, and it still pays the step cost. So a wall-bump keeps you put but costs you — never a free move.

PICTURE. The board below with coordinates. The goal square is special: standing on it, the game is already over, so its value is pinned at forever. Every other square starts life as a blank too — we simply don't know better yet.

Figure — Dynamic programming (value - policy iteration)

Step 2 — One move, taken apart: reward now + value of where you land

WHAT. Focus on a single square and a single action (say "move right"). Taking that action does exactly two things: it hands you an immediate reward , and it drops you on a new square whose value is . The worth of that one action is:

WHY. This is the whole trick of dynamic programming: a long journey's value = this one step + the value of everything after this one step. We never sum an infinite future by hand; we borrow it from , which already stores it. This "borrow the rest from the neighbour" idea is the Bellman backbone.

WHY the (and why not just add?). (gamma) is the discount factor, a number with ; here . It shrinks future rewards a little each step. We need it for two reasons: (1) rewards far away should matter slightly less, and (2) — the deep one — multiplying by a number smaller than is exactly what makes the whole update a contraction, guaranteeing it settles to one answer. Without the numbers could spiral forever.

PICTURE. One square, one arrow to its neighbour, the two pieces and labelled on the arrow.

Figure — Dynamic programming (value - policy iteration)

Step 3 — Four actions, one winner: the

WHAT. From square you don't have just one action — you have up, down, left, right. Each gives its own "reward now + discounted next value". We compute all four and keep the best one. Write the updated guess as — the fresh number we are about to write into square , computed from the old numbers still on the board:

Reading it left to right:

  • ::: the brand-new value we compute for square this sweep; on the right is still the old value from the previous sweep.
  • ::: try every action , keep the largest result — "pick my best move."
  • ::: average over where action might land me. is the MDP dynamics: the chance that action from gives reward and lands me on . In our deterministic grid this probability is just for the one square you actually move to (or the same square, if you bump a wall), elsewhere — so the sum collapses to a single term.
  • ::: the value of that landing, from Step 2.
  • ::: overwrite the old guess with this fresh, better number.

WHY the and not an average? We are hunting for the optimal value now we can define it: is how good square is if you always play perfectly from here on. A perfect player never dilutes their best move with worse ones, so we take the maximum, not the policy-weighted average. (Averaging over a fixed policy would instead give , the value of that policy — that's policy evaluation, a different goal.) Repeating this -update is what drives toward .

PICTURE. The four candidate values fanning out from one square; the winning arrow glows, the losers fade.

Figure — Dynamic programming (value - policy iteration)

Step 4 — Sweep 1: information leaks out of the goal

WHAT. Now apply the Step 3 rule to every square, once. This is one sweep. Start from the blank board where every .

On sweep 1, only the squares touching the goal learn anything real. From the square just left of the goal, the "move right" action gives and lands on the goal whose value is : Every other square also updates to (each action costs , all neighbours still read ), so after sweep 1 the whole board is except the goal at .

WHY. Value has to propagate. On the first pass, no square except the goal's neighbours has any way to "see" that the goal exists — everyone's neighbours are still zeros. So the useful signal lives only one ring out from the goal. This is the maze-solved-backwards intuition made literal.

PICTURE. The board after sweep 1: a rim of around a goal.

Figure — Dynamic programming (value - policy iteration)

Step 5 — Sweep 2 and beyond: the wave spreads

WHAT. Run the same rule again. Now a square two steps from the goal has a neighbour worth , so its best move gives The information ripples one ring further outward each sweep. The far corner is six steps from the goal, so its optimal value settles at roughly (the discounted cost of the six-step best path, ), while any given sweep keeps nudging it toward that limit.

WHY. Each sweep lets value cross exactly one more square, like a wave. Because , each ring's number is a shrunken copy of the ring before it — the numbers stay finite and the whole map converges.

PICTURE. Three boards side by side: sweep 1, sweep 2, sweep 3 — watch the dark "far" values crawl inward.

Figure — Dynamic programming (value - policy iteration)

Step 6 — Why it always stops: the contraction squeeze

WHAT. Define the error as the single biggest gap between our current guess and the true answer over all squares. This "worst gap over all states" is the sup-norm (also called the max-norm or infinity-norm), written . One sweep guarantees: Every sweep multiplies the worst gap by . Ten sweeps shrink it by ; a hundred sweeps by .

WHY this exact bound? The and the sum can each only shrink or preserve differences, and then multiplies everything by less than one. That is the definition of a contraction mapping, and such maps have exactly one fixed point — the true — that they always march toward. So value iteration cannot get stuck, oscillate, or find a wrong answer.

PICTURE. Error-versus-sweep: a curve diving toward zero, each step a factor shorter than the last.

Figure — Dynamic programming (value - policy iteration)

Step 7 — Reading the policy off the finished map

WHAT. Once stops changing, we have . To act optimally you never need to plan ahead again — at each square just step toward the highest-valued neighbour: Here means "the action that achieves the max" — not the value itself, but which move won.

WHY. The value map is a hill sloping up toward the goal. The optimal policy is simply "always walk uphill." This is the free lunch of value iteration: solve for the numbers once, and the arrows fall out for every square at once. The same greedy read-off feeds model-free cousins like Q-learning and SARSA, and contrasts with policy-gradient methods which skip the value map entirely.

PICTURE. The converged value board with a red arrow on every square pointing to its best neighbour — a clean flow to the goal.

Figure — Dynamic programming (value - policy iteration)

The one-picture summary

Figure — Dynamic programming (value - policy iteration)

The whole story in one frame: blank board → the "" one-step rule → pick the best action with → sweep after sweep the value wave fills the board → error shrinks by each time → read off the arrows.

Blank board V equals 0

One step value r plus gamma V next

Take max over 4 actions

Sweep every square once

Error shrinks by gamma

V star settled

Read arrows argmax

Recall Feynman retelling — say it like a story

Imagine a flooded room where the water is the "goodness" spilling out from the exit. At first the whole floor is dry (all zeros) except the exit. Each tick of the clock, goodness seeps one tile further from the exit, but a little weaker each time it moves (that's the ). Every tile keeps asking its neighbours, "which of you is the best to walk toward, counting the one step it costs me?" and copies that best neighbour's soaked-in value minus the step cost. Even bumping a wall costs a step and lands you back where you were, so wandering never pays. Because each hop weakens the number by a fixed fraction, the seeping settles into a stable map — a hill whose peak is the exit. To escape from anywhere, you don't think: you just walk uphill. That walk-uphill rule is the optimal policy, and the settled hill is .

Recall

What does the in value iteration accomplish? ::: It picks the best action at every square, which silently improves the policy on each sweep so we converge straight to the optimal value (not a fixed policy's ). Why does value iteration always converge? ::: Because the update is a contraction in the sup-norm with factor : each sweep multiplies the worst error by , and a contraction has exactly one fixed point, . After convergence, how do you act? ::: Greedily — at each state take the action pointing to the highest-valued neighbour, .