Planning with a Model

Reinforcement learning, part 2: dynamic programming, policy iteration and value iteration

Author

Published

Oct. 2026

PDF

Introduction

In the previous part, we laid the foundations of reinforcement learning: you now have the tools to describe a decision problem and to say what an optimal solution to it is.

This second part is about planning with a model: evaluating a policy, or finding the best one, in an environment with rules we know. This applies to problems such as video games, chess or Go, and it is a crucial step before learning from experience, without a model, which is the real goal of reinforcement learning.

Where Part 1 left us

Part 1, Modeling the World gave us the basics: an agent in state StS_t takes an action AtA_t, receives a reward Rt+1R_{t+1} and lands in St+1S_{t+1}, according to the dynamics of the Markov Decision Process (MDP), p(s′,r∣s,a)p(s', r \mid s, a).

The agent wants to maximize the return, recursively defined by the relation Gt=Rt+1+γ Gt+1G_t = R_{t+1} + \gamma\, G_{t+1}, or in full, as the discounted sum of all the rewards to come:

Gt=Rt+1+γ Rt+2+γ2Rt+3+⋯=∑k=0∞γkRt+k+1G_t = R_{t+1} + \gamma\, R_{t+2} + \gamma^2 R_{t+3} + \dots = \sum_{k=0}^{\infty} \gamma^k R_{t+k+1}

To evaluate how good a state or a state-action pair is, we defined helper functions: the value functions score states and actions under a policy π\pi,

vπ(s)≐Eπ[Gt∣St=s],qπ(s,a)≐Eπ[Gt∣St=s,At=a],v_\pi(s) \doteq \mathbb{E}_\pi[G_t \mid S_t = s], \qquad q_\pi(s, a) \doteq \mathbb{E}_\pi[G_t \mid S_t = s, A_t = a],

and their optimal counterparts v∗(s)=max⁡πvπ(s)v_*(s) = \max_\pi v_\pi(s) and q∗(s,a)=max⁡πqπ(s,a)q_*(s, a) = \max_\pi q_\pi(s, a). The state values satisfy the Bellman equations, one per state:

vπ(s)=∑aπ(a∣s)∑s′,rp(s′,r∣s,a) [r+γ vπ(s′)]v∗(s)=max⁡a∑s′,rp(s′,r∣s,a) [r+γ v∗(s′)]\begin{aligned} v_\pi(s) &= \sum_a \pi(a \mid s) \sum_{s', r} p(s', r \mid s, a)\,\big[r + \gamma\, v_\pi(s')\big] \\ v_*(s) &= \max_a \sum_{s', r} p(s', r \mid s, a)\,\big[r + \gamma\, v_*(s')\big] \end{aligned}

On the four-state altitude map, Part 1 solved these by hand, as a system of equations.

This page teaches you how to solve them on any finite MDP, with algorithms that only need the model pp and the recursive structure of these equations. We will study:

  1. Dynamic programming and policy evaluation: computing the value of a policy.
  2. Policy iteration: finding the best policy in three steps: evaluate, improve, repeat.
  3. Value iteration: a shortcut to the optimal behavior: optimize directly, one sweep at a time.
  4. Taking a step back: the pattern behind all three, generalized policy iteration, and why the optimal policy is not unique.

Part 3 will remove the model: Part 3.1, Learning from Experience, with Monte Carlo methods and temporal-difference learning, then Part 3.2, Learning to Act, with SARSA and Q-learning, learn the same values from sampled interactions only. Everything they do is an approximation at scale of what this page computes exactly.

1. Evaluating a policy via dynamic programming

To be able to find an optimal policy, we first need to be able to compare policies. Policy evaluation is the computation of the value function of a policy, in every state. When a model is available, the main technique to do so is to use dynamic programming.

1.1 What dynamic programming is

Dynamic programming (DP) solves a complex problem by breaking it into simpler subproblems, storing their solutions, and reusing them instead of computing them again. Here, “dynamic” refers to the sequential, temporal side of the problem. It applies when a problem has two properties:

MDPs have both. The Bellman equation is the decomposition link: the value of a state is one step plus the value of the next state. And the value function is the cache: one number per state, computed once, reused by every state that leads there.

The first property has a name in this setting, the principle of optimality:

Principle of optimality

A policy is optimal from a state ss if and only if it is optimal from every state s′s' it can reach from ss.

Once the hiker of Part 1 has reached AA, whatever happened before, the best the hiker can do is to behave optimally from AA. The problem splits into a first step and a smaller problem from the next state, which is exactly the Bellman optimality equation.

DP assumes full knowledge of the MDP: the states, the actions, the dynamics pp and γ\gamma. It is a planning method, used for both problems of section 4.3 of Part 1:

1.2 Iterative policy evaluation

The Bellman expectation equation gives ∣S∣|\mathcal{S}| linear equations for the ∣S∣|\mathcal{S}| values vπ(s)v_\pi(s). We could solve the system directly, but that costs about ∣S∣3|\mathcal{S}|^3 operations, too much for large state spaces.

Instead, we turn the equation into an update rule, and apply it again and again. Starting from any v0v_0, for instance 00 everywhere, and computing, for every state,

vk+1(s)=∑aπ(a∣s)∑s′,rp(s′,r∣s,a) [r+γ vk(s′)]v_{k+1}(s) = \sum_{a} \pi(a \mid s) \sum_{s', r} p(s', r \mid s, a)\, \big[ r + \gamma\, v_k(s') \big]

One pass over all the states is a sweep. Each sweep replaces the value of every state by a one-step look-ahead on the current values, a Bellman backup, and the sequence v0,v1,v2,…v_0, v_1, v_2, \dots converges to vπv_\pi. In practice, we stop when the largest change of a sweep, Δ\Delta, falls below a small threshold θ\theta:

Algorithm Iterative policy evaluation

Input: a policy π\pi, a threshold θ>0\theta > 0

  • v(s)←0v(s) \leftarrow 0 for every state ss
  • repeat
    • Δ←0\Delta \leftarrow 0
    • for each state ss
      • vold←v(s)v_{\text{old}} \leftarrow v(s)
      • v(s)←∑aπ(a∣s)∑s′,rp(s′,r∣s,a)[r+γ v(s′)]v(s) \leftarrow \sum_a \pi(a \mid s) \sum_{s', r} p(s', r \mid s, a) \big[ r + \gamma\, v(s') \big]
      • Δ←max⁡(Δ,∣v(s)−vold∣)\Delta \leftarrow \max\big(\Delta, |v(s) - v_{\text{old}}|\big)
  • until Δ<θ\Delta < \theta

One sweep costs about ∣S∣2∣A∣|\mathcal{S}|^2 |\mathcal{A}| operations: every state, every action, every next state, but we don’t know how many sweeps are required to converge.

Main example of this lesson: A 4×44 \times 4 grid with states 00 to 1515, numbered row by row; the corners 00 and 1515 are terminal. Four actions, up, down, left, right, deterministic; an action that would leave the grid leaves the agent in place. Every step costs r=−1r = -1, γ=1\gamma = 1, and the policy we study is the equiprobable random one, π(a∣s)=14\pi(a \mid s) = \frac14.

The gridworld of Example 4.1
  • States 0 to 15, row by row; the corners 0 and 15 are terminal
  • ↑↓Actions up, down, left, right, deterministic (state 9)
  • ↺An action that would leave the grid leaves the agent in place (state 7, right)
  • −1Reward r = −1 on every step, γ = 1: the return of an episode is minus its number of steps
  • ¼Policy π: the random one, each action with probability ¼
Grey: the two terminal corners. The arrows show the four actions from state 9, and, from state 7, a move right that would leave the grid and leaves the agent in place.

Starting the policy evaluation from v0=0v_0 = 0: after one sweep, every non-terminal state is worth −1-1, the reward of one step, whatever happens later. For state 11, next to the terminal corner:

v1(1)=14[(−1+v0(1))+(−1+v0(5))+(−1+v0(0))+(−1+v0(2))]=−1\begin{aligned} v_1(1) &= \tfrac14 \big[ (-1 + v_0(1)) + (-1 + v_0(5)) \\ &\qquad + (-1 + v_0(0)) + (-1 + v_0(2)) \big] = -1 \end{aligned}

After two sweeps, the states next to a terminal corner are worth −1.75-1.75 (one of their four moves ends the episode) and the others −2-2. The values keep spreading from the corners, sweep after sweep, up to convergence:

The random policy, evaluated sweep by sweep
Top: the values vk of the random policy after k sweeps (two arrays), on one color scale for every grid, darker when more negative; the grey corners are terminal. Bottom: the same values, each grid shaded against itself, from its highest to its lowest value. The numbers keep falling up to k = ∞, but from k = 4 the bottom grids barely change: the order between the states is final.

To what values does vπ(s)v_\pi(s) converge? Take a few seconds to think about it.

Answer Open once you have a guess

vπ(s)v_\pi(s) is minus the expected number of steps a random walker needs to fall into a corner: 2222 steps on average from the far corners, although they are only 33 steps from a terminal state.

The values take a long time to fully settle: about 220220 sweeps to get within 10−410^{-4} of the true vπv_\pi, the far corners going from −4-4 after 44 sweeps to −22-22. However, note that the comparisons between states settle much faster: from k=4k = 4, every state that is better than another under vπv_\pi is already better under vkv_k. Keep this in mind for sections 2 and 3.

These large negative values say little about the task: they measure the cost of acting at random. Finding the optimal cost is the job of the next step: improving the policy.

2. Policy iteration: evaluate, improve, repeat

Evaluating a policy is a great step forward, but our true goal is to find the best policy, or one of the best: this is what policy iteration is about.

2.1 Improving a policy

Once we know vπv_\pi, we can measure what happens if, in one state ss, we make a different choice and take a different action aa than the policy would, then follow π\pi again. That is exactly qπ(s,a)q_\pi(s, a), and it follows from vπv_\pi in one step:

qπ(s,a)=∑s′,rp(s′,r∣s,a) [r+γ vπ(s′)]q_\pi(s, a) = \sum_{s', r} p(s', r \mid s, a)\, \big[ r + \gamma\, v_\pi(s') \big]

On the gridworld of section 1.2, under the random policy, moves are deterministic so the sum shrinks and qπ(s,a)=−1+vπ(sa′)q_\pi(s, a) = -1 + v_\pi(s'_a), where sa′s'_a is the cell reached by aa. Moving down from state 1111 ends the episode, and a terminal state is worth 00: qπ(11,down)=−1+0=−1q_\pi(11, \text{down}) = -1 + 0 = -1. Moving down from state 77 lands in state 1111: qπ(7,down)=−1+(−14)=−15q_\pi(7, \text{down}) = -1 + (-14) = -15.

Same first action, but very different values, because qπq_\pi scores one step of your choice followed by the random policy: from 1111, that one step ends the episode; from 77, it hands the agent back to 1414 more random steps, on average.

qπ(7,down)=−1⏟one step,your choice+vπ(11)⏟then π from 11until a corner=−1+(−14)=−15q_\pi(7, \text{down}) = \underbrace{-1}_{\substack{\text{one step,} \\ \text{your choice}}} + \underbrace{v_\pi(11)}_{\substack{\text{then } \pi \text{ from } 11 \\ \text{until a corner}}} = -1 + (-14) = -15

The figure below starts with a simpler policy than the random one, up, then left, whose path is easy to follow: there, qπ(7,down)=−6q_\pi(7, \text{down}) = -6. Switch it to random to find the −15-15 above, as the average of many episodes.

One action of your choice, then the policy
The values vπ of a policy π. By default, π is a fixed policy, easy to follow: up, then left along the top row (small arrows). The orange arrow is the action a, chosen once; from there, the walker follows π (green trail) until it reaches a corner. qπ(s, a) is −1 for the chosen step plus vπ of the cell reached. Switch π to random for the policy of the text: every episode is then different, and qπ is the average of their returns.

The figure already holds the idea of this section. Under the random policy, state 77 is worth vπ(7)=−20v_\pi(7) = -20; going down first, then acting at random, is worth qπ(7,down)=−15q_\pi(7, \text{down}) = -15.

One better choice, made once, saves five steps on average. In general, if qπ(s,a)>vπ(s)q_\pi(s, a) > v_\pi(s), taking aa in ss is better than following π\pi there (both are expected returns from ss). Changing the policy in that one state should help, but is this always the case, in any order we proceed? The policy improvement theorem says that it is, even when we change it in every state at once.

Policy improvement theorem

Let π\pi and π′\pi' be two deterministic policies such that, for every state ss,

qπ(s,π′(s))≥vπ(s).q_\pi(s, \pi'(s)) \ge v_\pi(s).

Then π′\pi' is at least as good as π\pi: vπ′(s)≥vπ(s)v_{\pi'}(s) \ge v_\pi(s) for every ss.

The natural candidate here is what we call the greedy policy, which picks in every state the action with the best return one step ahead:

π′(s)=arg⁡max⁡aqπ(s,a)=arg⁡max⁡a∑s′,rp(s′,r∣s,a) [r+γ vπ(s′)]\begin{aligned} \pi'(s) &= \arg\max_a q_\pi(s, a) \\ &= \arg\max_a \sum_{s', r} p(s', r \mid s, a)\, \big[ r + \gamma\, v_\pi(s') \big] \end{aligned}

It satisfies the hypothesis of the theorem by construction. In every state, vπ(s)v_\pi(s) is an average of the action values, weighted by the policy, and a max is at least an average:

qπ(s,π′(s))=max⁡aqπ(s,a)  ≥  ∑aπ(a∣s) qπ(s,a)=vπ(s)q_\pi(s, \pi'(s)) = \max_a q_\pi(s, a) \;\ge\; \sum_a \pi(a \mid s)\, q_\pi(s, a) = v_\pi(s)

So greedifying never hurts.

When does it stop helping? Only when the policy is already optimal:

Optional A stable policy is optimal: why greedifying changes nothing only at the optimum Math needed Compute: arithmetic, a small linear system, applying a formula of the pageend of high school Reason: expectations, inequalities, an argument in words: no new tools, but a chain of reasoningfirst year of university, with a first course in probability Prove: induction, limits, norms, fixed points: a proof, which can be skipped on a first readingabout two years of university math The level says which math is needed, not how long it takes.

If π′\pi' is only as good as π\pi, vπ′=vπv_{\pi'} = v_\pi, write the Bellman equation of vπ′v_{\pi'}, one step with π′\pi', then replace vπ′v_{\pi'} by vπv_\pi:

vπ(s)=vπ′(s)=∑s′,rp(s′,r∣s,π′(s)) [r+γ vπ(s′)]=max⁡aqπ(s,a)v_\pi(s) = v_{\pi'}(s) = \sum_{s', r} p(s', r \mid s, \pi'(s))\, \big[ r + \gamma\, v_\pi(s') \big] = \max_a q_\pi(s, a)

In every state, vπ(s)=max⁡a∑s′,rp(s′,r∣s,a) [r+γ vπ(s′)]v_\pi(s) = \max_a \sum_{s', r} p(s', r \mid s, a)\, [r + \gamma\, v_\pi(s')]: vπv_\pi satisfies the Bellman optimality equation, so vπ=v∗v_\pi = v_*, and π\pi was already optimal.

Exercise 2.1 Proof of the policy improvement theorem Math needed Compute: arithmetic, a small linear system, applying a formula of the pageend of high school Reason: expectations, inequalities, an argument in words: no new tools, but a chain of reasoningfirst year of university, with a first course in probability Prove: induction, limits, norms, fixed points: a proof, which can be skipped on a first readingabout two years of university math The level says which math is needed, not how long it takes.

Let π\pi and π′\pi' be two deterministic policies with qπ(s,π′(s))≥vπ(s)q_\pi(s, \pi'(s)) \ge v_\pi(s) in every state. The idea is to unroll the hypothesis: apply it at ss, then at the next state, then at the one after, each time taking one more step with π′\pi' instead of π\pi.

  1. Show that qπ(s,π′(s))=Eπ′[Rt+1+γ vπ(St+1)∣St=s]q_\pi(s, \pi'(s)) = \mathbb{E}_{\pi'}\big[ R_{t+1} + \gamma\, v_\pi(S_{t+1}) \mid S_t = s \big]. Why can the expectation be taken under π′\pi', although qπq_\pi is defined with π\pi?

  2. Apply the hypothesis at St+1S_{t+1}, and show that vπ(s)≤Eπ′[Rt+1+γRt+2+γ2vπ(St+2)∣St=s]v_\pi(s) \le \mathbb{E}_{\pi'}\big[ R_{t+1} + \gamma R_{t+2} + \gamma^2 v_\pi(S_{t+2}) \mid S_t = s \big].

  3. Show by induction that, for every n≥1n \ge 1,

    vπ(s)≤Eπ′[Rt+1+γRt+2+⋯+γn−1Rt+n+γnvπ(St+n)∣St=s]v_\pi(s) \le \mathbb{E}_{\pi'}\big[ R_{t+1} + \gamma R_{t+2} + \cdots + \gamma^{n-1} R_{t+n} + \gamma^n v_\pi(S_{t+n}) \mid S_t = s \big]
  4. Let n→∞n \to \infty and conclude that vπ(s)≤vπ′(s)v_\pi(s) \le v_{\pi'}(s). What is needed for the last term to vanish?

Hint

Question 1: qπ(s,a)q_\pi(s, a) fixes the first action, then follows π\pi. Rt+1R_{t+1} and St+1S_{t+1} only depend on that first action, and drawing it from the deterministic π′\pi' means taking π′(s)\pi'(s).

Hint

Question 2: in the expectation of question 1, bound vπ(St+1)v_\pi(S_{t+1}) by qπ(St+1,π′(St+1))q_\pi(S_{t+1}, \pi'(S_{t+1})), and use question 1 again, at St+1S_{t+1}. The inequality survives because γ≥0\gamma \ge 0.

Hint

Question 4: the tail is γn Eπ′[vπ(St+n)]\gamma^n\, \mathbb{E}_{\pi'}[v_\pi(S_{t+n})]. When is vπv_\pi bounded? What if γ=1\gamma = 1?

Checkpoint what you should get

Each application of the hypothesis hands one more step to π′\pi'; when γn Eπ′[vπ(St+n)]→0\gamma^n\, \mathbb{E}_{\pi'}[v_\pi(S_{t+n})] \to 0, the right-hand side tends to Eπ′[Gt∣St=s]=vπ′(s)\mathbb{E}_{\pi'}[G_t \mid S_t = s] = v_{\pi'}(s).

Solution

The whole chain, one line per application of the hypothesis:

vπ(s)≤qπ(s,π′(s))=Eπ′[Rt+1+γ vπ(St+1)∣St=s]≤Eπ′[Rt+1+γ qπ(St+1,π′(St+1))∣St=s]=Eπ′[Rt+1+γRt+2+γ2vπ(St+2)∣St=s]  ⋮≤Eπ′[Rt+1+γRt+2+γ2Rt+3+⋯∣St=s]=vπ′(s)\begin{aligned} v_\pi(s) &\le q_\pi(s, \pi'(s)) \\ &= \mathbb{E}_{\pi'}\big[ R_{t+1} + \gamma\, v_\pi(S_{t+1}) \mid S_t = s \big] \\ &\le \mathbb{E}_{\pi'}\big[ R_{t+1} + \gamma\, q_\pi(S_{t+1}, \pi'(S_{t+1})) \mid S_t = s \big] \\ &= \mathbb{E}_{\pi'}\big[ R_{t+1} + \gamma R_{t+2} + \gamma^2 v_\pi(S_{t+2}) \mid S_t = s \big] \\ &\ \ \vdots \\ &\le \mathbb{E}_{\pi'}\big[ R_{t+1} + \gamma R_{t+2} + \gamma^2 R_{t+3} + \cdots \mid S_t = s \big] = v_{\pi'}(s) \end{aligned}
  1. The second line is where π′\pi' enters: qπ(s,π′(s))q_\pi(s, \pi'(s)) conditions on the action π′(s)\pi'(s), which is the same as drawing the first action from π′\pi'. After that first step, vπ(St+1)v_\pi(S_{t+1}) still follows π\pi.
  2. The third line applies the hypothesis at St+1S_{t+1}, and multiplying by γ≥0\gamma \ge 0 keeps the inequality. The fourth line is question 1 at St+1S_{t+1}: the second action is now drawn from π′\pi' too.
  3. Each repetition of questions 1 and 2 replaces γkvπ(St+k)\gamma^k v_\pi(S_{t+k}) by γkRt+k+1+γk+1vπ(St+k+1)\gamma^k R_{t+k+1} + \gamma^{k+1} v_\pi(S_{t+k+1}), with one more action drawn from π′\pi': this is the induction step.
  4. The sum of the rewards tends to the return GtG_t of π′\pi', whose expectation is vπ′(s)v_{\pi'}(s), if the tail γn Eπ′[vπ(St+n)]\gamma^n\, \mathbb{E}_{\pi'}[v_\pi(S_{t+n})] vanishes: true for γ<1\gamma < 1 with bounded rewards, since vπv_\pi is then bounded, or, with γ=1\gamma = 1, when π′\pi' ends the episode with probability 1, since the value of a terminal state is 00.
What to notice the take-away

Each line hands one more step from π\pi to π′\pi', and the hypothesis guarantees that each hand-over can only help. Nothing requires π′\pi' to be greedy: any policy that does at least as well as π\pi one step ahead, in every state, does at least as well for good.

2.2 Policy iteration

Policy iteration alternates the two steps until the policy stops changing:

  1. Evaluate the current policy: compute vπv_\pi, with the sweeps of section 1.2.
  2. Improve it: make it greedy with respect to vπv_\pi.
  3. Repeat until the policy is stable: π′=π\pi' = \pi.
Algorithm Policy iteration
  • initialize π\pi arbitrarily
  • repeat
    • policy evaluation
      • repeat for each state ss
        • v(s)←∑aπ(a∣s)∑s′,rp(s′,r∣s,a)[r+γ v(s′)]v(s) \leftarrow \sum_a \pi(a \mid s) \sum_{s', r} p(s', r \mid s, a) \big[ r + \gamma\, v(s') \big]
      • until convergence
    • policy improvement
      • πold←π\pi_{\text{old}} \leftarrow \pi
      • for each state ss
        • π(s)←argmax⁡a∑s′,rp(s′,r∣s,a)[r+γ v(s′)]\pi(s) \leftarrow \operatorname{argmax}_a \sum_{s', r} p(s', r \mid s, a) \big[ r + \gamma\, v(s') \big]
  • until π=πold\pi = \pi_{\text{old}}
Policy iteration, with the line being run highlighted, and a schematic run in two pictures. Top, the chain of evaluations (E) and improvements (I). Bottom, the space of value functions and policies: every evaluation brings the values onto the line v = vπ, sweep after sweep; every improvement brings the policy onto the line π = greedy(v), in one step. The two lines meet at (v*, π*).

In words: each policy is strictly better than the previous one, unless it is already optimal. And policy iteration always stops: a finite MDP has a finite number of deterministic policies, so policy iteration reaches an optimal one in a finite number of iterations: v∗v_*, which is unique, and an optimal policy, which may not be.

On the gridworld of section 1.2, it goes fast. The greedy policy with respect to the values of the random policy is already optimal: walk to the nearest corner. Better still, it is optimal long before the evaluation has converged: the greedy policy of v3v_3 already is, although v3v_3 is far from vπv_\pi. The greedy step only reads the ranking of the neighbors of each state, not the values themselves, nor the order of all the states: that is why it settles at k=3k = 3, one sweep before the full order of section 1.2.

One improvement step, after k sweeps of evaluation
The same sweeps as in section 1.2, with, on each grid, the greedy policy of vk (green arrows, several where actions tie). At k = 0 every action ties: the greedy policy is the random one again. From k = 3, every greedy action leads toward the nearest corner: one improvement step gives an optimal policy, long before the values have converged.
Exercise 2.2 One improvement step on a 3 × 3 grid Math needed Compute: arithmetic, a small linear system, applying a formula of the pageend of high school Reason: expectations, inequalities, an argument in words: no new tools, but a chain of reasoningfirst year of university, with a first course in probability Prove: induction, limits, norms, fixed points: a proof, which can be skipped on a first readingabout two years of university math The level says which math is needed, not how long it takes.

A 3×33 \times 3 grid, the terminal state TT in the top-left corner, the other states numbered 11 to 88 row by row:

TT1122
334455
667788

Actions up, down, left, right, deterministic; a move off the grid leaves the agent in place; r=−1r = -1 per step; γ=1\gamma = 1. The policy π0\pi_0 is the equiprobable random one.

  1. Evaluation. Write the Bellman equation of vπ0(s)v_{\pi_0}(s). Use the symmetry of the grid to reduce the number of unknowns, and solve the system.
  2. Action values. Compute qπ0(s,a)=−1+vπ0(sa′)q_{\pi_0}(s, a) = -1 + v_{\pi_0}(s'_a) for every state, or at least for states 11, 44, 55 and 88.
  3. Improvement. Compute the greedy policy π′\pi'. How does it differ from π0\pi_0?
  4. Without computation, give vπ′v_{\pi'}, and check that vπ′(s)≥vπ0(s)v_{\pi'}(s) \ge v_{\pi_0}(s) everywhere.
Hint

With π0(a∣s)=14\pi_0(a \mid s) = \frac14: v(s)=−1+14∑av(sa′)v(s) = -1 + \frac14 \sum_a v(s'_a), where sa′=ss'_a = s when the move hits a wall.

Hint

The grid and the random policy are symmetric about the diagonal through TT, 44 and 88. Set v(1)=v(3)=av(1) = v(3) = a, v(2)=v(6)=bv(2) = v(6) = b, v(4)=cv(4) = c, v(5)=v(7)=dv(5) = v(7) = d, v(8)=ev(8) = e: five unknowns.

Hint

Start from state 88, which only sees 55, 77 and itself: e=−1+14(2d+2e)e = -1 + \frac14 (2d + 2e), so e=d−2e = d - 2. Then work your way up.

Checkpoint what you should get

vπ0(1)=−16v_{\pi_0}(1) = -16, vπ0(4)=−21.5v_{\pi_0}(4) = -21.5, vπ0(8)=−27v_{\pi_0}(8) = -27; the greedy policy walks straight to TT, and vπ′(8)=−4v_{\pi'}(8) = -4.

Solution
  1. With the symmetry, reading the neighbors of each representative state (up, down, left, right):

    • state 11: a=−1+14(a+c+0+b)a = -1 + \frac14 (a + c + 0 + b) (up bounces, down to 44, left to TT, right to 22);
    • state 22: b=−1+14(b+d+a+b)b = -1 + \frac14 (b + d + a + b) (up and right bounce);
    • state 44: c=−1+14(a+d+a+d)c = -1 + \frac14 (a + d + a + d);
    • state 55: d=−1+14(b+e+c+d)d = -1 + \frac14 (b + e + c + d) (right bounces);
    • state 88: e=−1+14(d+e+d+e)e = -1 + \frac14 (d + e + d + e) (down and right bounce).

    The last one gives e=d−2e = d - 2; then state 55 gives b+c=2d+6b + c = 2d + 6, state 11 gives a=2+2d3a = \frac{2 + 2d}{3}, and states 22 and 44 give a=d+9a = d + 9. So d=−25d = -25, a=−16a = -16, b=−22.5b = -22.5, c=−21.5c = -21.5, e=−27e = -27:

    vπ0v_{\pi_0}
    00−16-16−22.5-22.5
    −16-16−21.5-21.5−25-25
    −22.5-22.5−25-25−27-27
  2. qπ0(s,a)=−1+vπ0(sa′)q_{\pi_0}(s, a) = -1 + v_{\pi_0}(s'_a), in the order up, down, left, right:

    • state 11: −17-17, −22.5-22.5, −1\mathbf{-1}, −23.5-23.5;
    • state 22: −23.5-23.5, −26-26, −17\mathbf{-17}, −23.5-23.5;
    • state 33: −1\mathbf{-1}, −23.5-23.5, −17-17, −22.5-22.5;
    • state 44: −17\mathbf{-17}, −26-26, −17\mathbf{-17}, −26-26;
    • state 55: −23.5-23.5, −28-28, −22.5\mathbf{-22.5}, −26-26;
    • state 66: −17\mathbf{-17}, −23.5-23.5, −23.5-23.5, −26-26;
    • state 77: −22.5\mathbf{-22.5}, −26-26, −23.5-23.5, −28-28;
    • state 88: −26\mathbf{-26}, −28-28, −26\mathbf{-26}, −28-28.
  3. The greedy actions are in bold: left at 11, 22 and 55; up at 33, 66 and 77; up or left (a tie) at 44 and 88. Where π0\pi_0 was uniform over four actions everywhere, π′\pi' is deterministic, up to two ties: it walks to TT along a shortest path.

  4. Each step of π′\pi' costs −1-1 and gets one cell closer to TT, so vπ′(s)v_{\pi'}(s) is minus the distance to TT:

    vπ′v_{\pi'}
    00−1-1−2-2
    −1-1−2-2−3-3
    −2-2−3-3−4-4

    Better than vπ0v_{\pi_0} in every non-terminal state, as the theorem says. In fact vπ′=v∗v_{\pi'} = v_*: on this grid, one improvement step is enough.

What to notice the take-away

vπ0(4)=−21.5v_{\pi_0}(4) = -21.5 is better than vπ0(2)=−22.5v_{\pi_0}(2) = -22.5, although both are two steps from TT: the center has four useful moves, while state 22 wastes half of its moves against the walls. Under a random policy, the value measures how hard it is to escape, not the distance. One improvement step is not always enough: on Jack’s car rental, below, it takes four.

2.3 A larger example: Jack’s car rental

Let’s take as an example a problem that is not a grid, with random dynamics, and too many states to read as a table.

Jack manages two car rental locations. Each day, customers come to each location to rent cars, and other customers return cars, in random numbers that follow Poisson distributions with known rates (3 and 4 requests a day, 3 and 2 returns). Jack earns $10 per car rented, if he has one. Overnight, he can move up to 5 cars between the two locations, at $2 per car moved. Each location holds at most 20 cars, and γ=0.9\gamma = 0.9.

The state is the number of cars at each location, 21×21=44121 \times 21 = 441 states; the action is the number of cars moved overnight, from −5-5 to +5+5. The dynamics are random but fully known, so p(s′,r∣s,a)p(s', r \mid s, a) can be computed exactly: this is a planning problem. Starting from the policy that never moves any car, policy iteration converges in four improvements, π4=π∗\pi_4 = \pi_*. With 441 states, tables of numbers are no longer readable: the policy becomes a map, and the value function a surface.

Jack's car rental, solved
The optimal policy π*, one cell per state: its color is the number of cars moved overnight, positive from the first location to the second. Next to it, the optimal value v* over the same 441 states; drag to turn it. More cars are worth more. The policy mostly sends cars to the second location, which rents more and gets fewer back, and moves a few the other way only when the first location is nearly empty.

Policy iteration already handles problems with hundreds of states. And with that many states, the value function looks less like a table than like a smooth surface: approximating it by a continuous function, instead of storing one number per state, is the idea of Part 4, From Discrete to Continuous.

Interactive Space Jack's car rental, policy iteration
The whole run, from π0 to π*: click a state to see its 11 action values, and compare full, exact and truncated evaluation (m = 1 is value iteration).
Open in a new tab ↗

3. Value iteration: optimize directly

Can we go faster than policy iteration? Are there shortcuts to its cumbersome full evaluations? Value iteration is one of them.

3.1 One sweep of the optimality backup

Policy iteration evaluates each policy to convergence before improving it, and section 2.2 showed that this is often wasted work: the greedy policy settles long before the values do.

Value iteration goes to the extreme, and converges directly to the optimal values v∗v_*. It skips the explicit policy, and applies the Bellman optimality equation as an update rule:

vk+1(s)=max⁡a∑s′,rp(s′,r∣s,a) [r+γ vk(s′)]v_{k+1}(s) = \max_a \sum_{s', r} p(s', r \mid s, a)\, \big[ r + \gamma\, v_k(s') \big]

Same sweeps as policy evaluation, with a max over the actions instead of an average under π\pi. This has the consequence to converge to the optimal value policy (because of the properties of the Bellman equations). When the values have converged, one last greedy sweep extracts the policy:

Algorithm Value iteration

Input: a threshold θ>0\theta > 0

  • v(s)←0v(s) \leftarrow 0 for every state ss
  • repeat
    • Δ←0\Delta \leftarrow 0
    • for each state ss
      • vold←v(s)v_{\text{old}} \leftarrow v(s)
      • v(s)←max⁡a∑s′,rp(s′,r∣s,a)[r+γ v(s′)]v(s) \leftarrow \max_a \sum_{s', r} p(s', r \mid s, a) \big[ r + \gamma\, v(s') \big]
      • Δ←max⁡(Δ,∣v(s)−vold∣)\Delta \leftarrow \max\big(\Delta, |v(s) - v_{\text{old}}|\big)
  • until Δ<θ\Delta < \theta
  • return π(s)=argmax⁡a∑s′,rp(s′,r∣s,a)[r+γ v(s′)]\pi(s) = \operatorname{argmax}_a \sum_{s', r} p(s', r \mid s, a) \big[ r + \gamma\, v(s') \big]
Value iteration on the gridworld of section 1.2, in place, one state at a time. The highlighted line is the one being run; on the grid, the state being updated, the neighbors its max reads, and the action that wins. The values settle one ring of cells per sweep; the fourth sweep changes nothing, Δ = 0, and the greedy policy is read off.

A sweep costs the same as a sweep of policy evaluation, about ∣S∣2∣A∣|\mathcal{S}|^2 |\mathcal{A}| operations. Each one is an improvement and a partial evaluation at once. This method is often preferred to Policy Iteration, but its number of sweeps can somletimes be higher.

Exercise 3.1 Value iteration on the 3 × 3 grid Math needed Compute: arithmetic, a small linear system, applying a formula of the pageend of high school Reason: expectations, inequalities, an argument in words: no new tools, but a chain of reasoningfirst year of university, with a first course in probability Prove: induction, limits, norms, fixed points: a proof, which can be skipped on a first readingabout two years of university math The level says which math is needed, not how long it takes.

The grid of Exercise 2.2: terminal TT in the top-left corner, states 11 to 88, r=−1r = -1 per step, γ=1\gamma = 1. This time, solve the optimality equation directly, from v0(s)=0v_0(s) = 0 in every state, with vk+1(s)=max⁡a[−1+vk(sa′)]v_{k+1}(s) = \max_a \big[ -1 + v_k(s'_a) \big] and vk(T)=0v_k(T) = 0.

  1. Compute v1v_1. Are all eight values equal? Why does the shape of the grid not matter yet?
  2. Compute v2v_2. What appears?
  3. Without computing further, what does vkv_k converge to? After how many sweeps?
  4. Deduce an optimal policy. Which states have several optimal actions? How many deterministic optimal policies are there?
Hint

After one sweep, every successor still has v0=0v_0 = 0, including TT: nothing yet tells TT apart from the other cells.

Hint

In v2v_2, only the neighbors of TT can use v1(T)=0v_1(T) = 0; every other state only sees cells worth −1-1.

Checkpoint what you should get

v1=−1v_1 = -1 everywhere; v2=−1v_2 = -1 at states 11 and 33, −2-2 elsewhere; v∗(s)=−dist(s,T)v_*(s) = -\text{dist}(s, T), so v∗(8)=−4v_*(8) = -4, exact after 4 sweeps; ties at states 44, 55, 77, 88, so 1616 deterministic optimal policies.

Solution
  1. Every action gives −1+0-1 + 0, so v1(s)=−1v_1(s) = -1 for all eight states. After one sweep, the only information available is the immediate reward, the same everywhere; TT is not distinguishable yet, since v0(T)=0=v0(s)v_0(T) = 0 = v_0(s).

  2. States 11 and 33 are next to TT: their best move gives −1+v1(T)=−1-1 + v_1(T) = -1. Every other state can only reach cells worth −1-1, hence −1+(−1)=−2-1 + (-1) = -2:

    v2v_2
    00−1-1−2-2
    −1-1−2-2−2-2
    −2-2−2-2−2-2
  3. Each sweep makes one more ring exact: vk(s)=−min⁡(k,dist(s,T))v_k(s) = -\min(k, \text{dist}(s, T)), with dist the number of moves to TT. So vkv_k converges to v∗(s)=−dist(s,T)v_*(s) = -\text{dist}(s, T), exactly, after max⁡sdist(s,T)=4\max_s \text{dist}(s, T) = 4 sweeps:

    v∗v_*
    00−1-1−2-2
    −1-1−2-2−3-3
    −2-2−3-3−4-4
  4. An optimal action is any move that gets one cell closer to TT: left at 11 and 22, up at 33 and 66, and up or left at 44, 55, 77 and 88, the states off the first row and the first column. That makes 14×24=161^4 \times 2^4 = 16 deterministic optimal policies, plus every random mixture of the tied actions.

What to notice the take-away

In Exercise 2.2, the greedy policy of vπ0v_{\pi_0} had ties at states 44 and 88 only; the greedy policy of v∗v_* has ties at 44, 55, 77 and 88. Both policies are optimal: they just break the ties differently. v∗v_* is unique, π∗\pi_* is not, so algorithms must be compared on the values they reach, not on the actions they pick.

3.2 Why does it converge?

On our grids, value iteration stops after a few sweeps. With γ<1\gamma < 1 and random dynamics, as in Jack’s car rental, vkv_k never reaches v∗v_* exactly: it only gets closer at every sweep. That it always converges, and to v∗v_* only, comes from one property of the update, folded below for those who want the math.

Optional Why do these algorithms converge?: the Bellman operator is a contraction Math needed Compute: arithmetic, a small linear system, applying a formula of the pageend of high school Reason: expectations, inequalities, an argument in words: no new tools, but a chain of reasoningfirst year of university, with a first course in probability Prove: induction, limits, norms, fixed points: a proof, which can be skipped on a first readingabout two years of university math The level says which math is needed, not how long it takes.

Write one sweep of value iteration as an operator T∗\mathcal{T}^* that maps a value function to another:

(T∗v)(s)=max⁡a∑s′,rp(s′,r∣s,a) [r+γ v(s′)](\mathcal{T}^* v)(s) = \max_a \sum_{s', r} p(s', r \mid s, a)\, \big[ r + \gamma\, v(s') \big]

For γ<1\gamma < 1, it is a γ\gamma-contraction in the max norm, ∥v∥∞=max⁡s∣v(s)∣\|v\|_\infty = \max_s |v(s)|:

∥T∗v1−T∗v2∥∞≤γ ∥v1−v2∥∞\| \mathcal{T}^* v_1 - \mathcal{T}^* v_2 \|_\infty \le \gamma\, \| v_1 - v_2 \|_\infty

Proof in two steps. For a fixed (s,a)(s, a), the rewards cancel in the difference, and the weights p(s′,r∣s,a)p(s', r \mid s, a) are nonnegative and sum to 11: ∣∑s′,rp(s′,r∣s,a) γ [v1(s′)−v2(s′)]∣≤γ ∥v1−v2∥∞\big| \sum_{s', r} p(s', r \mid s, a)\, \gamma\, [v_1(s') - v_2(s')] \big| \le \gamma\, \|v_1 - v_2\|_\infty. Then, for any two functions of the action, ∣max⁡af(a)−max⁡ag(a)∣≤max⁡a∣f(a)−g(a)∣|\max_a f(a) - \max_a g(a)| \le \max_a |f(a) - g(a)|, which carries the bound through the max.

The Banach fixed-point theorem does the rest: a contraction has a unique fixed point, the solution of v=T∗vv = \mathcal{T}^* v, which is the Bellman optimality equation, so that point is v∗v_*. And iterating from any v0v_0 converges to it exponentially fast, ∥vk−v∗∥∞≤γk∥v0−v∗∥∞\|v_k - v_*\|_\infty \le \gamma^k \|v_0 - v_*\|_\infty. The same argument without the max shows that policy evaluation converges to vπv_\pi.

With γ=1\gamma = 1, as in our gridworlds, the operator is no longer a contraction, and the argument fails. The iterations still converge there because every step costs −1-1: a policy that never reaches the terminal state is worth −∞-\infty and is never the max, and a policy that reaches it exists. The “Trap” preset of the gridworld Space shows what happens when some states cannot reach it.

4. Taking a step back

Policy iteration and value iteration look like two different algorithms. Before wrapping everything up, let’s take a step back on what they share: the same two processes underneath, and the same answer at the end, a unique optimal value function, with possibly different optimal policies.

4.1 Evaluate and improve: generalized policy iteration

Side by side, the two algorithms that find π∗\pi_*:

Both are instances of one idea, generalized policy iteration (GPI): two processes that interact, an evaluation that makes the values consistent with the current policy, and an improvement that makes the policy greedy with respect to the current values. Together they converge to the only point that both accept, v∗v_* and π∗\pi_*. Almost every method of Part 3 is GPI with an approximate evaluation.

Generalized policy iteration
Left: the two processes, evaluation and improvement, each one pulling toward its own goal. Right: the same, as two lines that meet at (v*, π*). Evaluation moves the values toward the line v = vπ, improvement moves the policy onto the line π = greedy(v). Policy iteration reaches the line v = vπ at every cycle, value iteration takes a single sweep toward it, modified policy iteration takes m.

4.2 One optimal value function, many optimal policies

Exercise 3.1 ended on a fact worth a second example: the optimal value function is unique, the optimal policy is not.

One v*, sixteen optimal policies
The 3 × 3 grid of Exercise 3.1 and its optimal values v*, which never change. The arrows run through the 16 deterministic optimal policies, which only differ in the four outlined states, where up and left tie; the green band is the path from 8.

In the gambler’s problem of the extra exercises (E.1 and E.2), the ties are not an accident of symmetry: with a fair coin, every policy is optimal.

4.3 Beyond full sweeps

Every algorithm of this page sweeps the whole state set, again and again: fine for 16 states, hopeless for a Rubik’s cube. Updating only some states, in any order, still works, and is the first step toward Part 3.

Optional Asynchronous dynamic programming: sweeps don't scale

Every algorithm so far sweeps the whole state set at each iteration. A 3×3×33 \times 3 \times 3 Rubik’s cube has about 4.3×10194.3 \times 10^{19} states and a fully known model; at a million state updates per second, a single sweep would take more than a million years.

The first step away from sweeps is the in-place update of section 1.2: one array, and each update immediately visible to the next ones. Asynchronous DP goes further: update the states in any order, some more often than others, using whatever values are available. It still converges, under one condition: no state may be ignored forever.

This freedom lets us spend the computation where it helps: order the updates so that information propagates fast (backward from the goal, for instance), skip states that are irrelevant to good behavior, or run the updates while an agent interacts with the environment, on the states it actually visits. But every update still computes ∑s′,rp(s′,r∣s,a) [r+γv(s′)]\sum_{s', r} p(s', r \mid s, a)\, [r + \gamma v(s')]: the experience decides where to update, the model is still needed to decide what the update is. Part 3 removes that last requirement.

5. Wrap-up

To wrap up, back to the hike of Part 1, solved with the two algorithms of this page.

The hike, solved by dynamic programming
The altitude map of Part 1, γ = 1. Policy iteration starts from πB: one evaluation, one improvement, and the policy is πA, which a second cycle confirms. Value iteration starts from v0 = 0: step through the sweeps and watch the greedy arrows. The start v0 = 0 is optimistic, and stay, at −1 a step, looks cheap: at B, staying beats the −35 descent until the values have counted 34 stays.

Policy iteration needs two cycles, and the first improvement already gives the optimal policy πA\pi_A. Value iteration needs 36 sweeps, yet its greedy policy is optimal on the hiker’s path, at SS and AA, after 5. The long tail is spent on BB, a state the optimal hiker never visits: a case for the asynchronous updates of section 4.3.

All three algorithms of this page, on the gridworld and on harder grids, in one Space:

Interactive Space Solving gridworlds by dynamic programming
Policy evaluation, policy iteration and value iteration, one tab each, on the Sutton and Barto gridworld (Example 4.1) and on other presets: walls, a slippery cliff, a trap with no exit.
Open in a new tab ↗
What to try in the Space above
  • Policy evaluation. Click state 1, then step from k=0k = 0 to k=2k = 2: the backup reads the four neighbors, with the walls sending the agent back to itself. Switch between two arrays and in place, and compare the number of sweeps.
  • Policy iteration. Step from π0\pi_0 to π1\pi_1: one improvement turns the random walk into the shortest-path policy. From π1\pi_1 to π2\pi_2, the values do not change any more, but two states still widen their set of greedy actions: there, all four moves are equally good. Change the number of sweeps mm to get modified policy iteration.
  • Value iteration. Compare the sweep counts in the status line: 4 sweeps on this grid (the values are exact after 3, the 4th detects that nothing changes), against more than a hundred for policy iteration, whose cycles each evaluate a policy to convergence. Then try the Walls preset, and watch the values flow around the walls, one ring per sweep.

In a few lines:

The next move, from exact and exhaustive (this page) to sampled, starts in Part 3.1: Learning from Experience and continues in Part 3.2: Learning to Act. There, the agent no longer knows pp: Monte Carlo methods, temporal-difference learning, SARSA and Q-learning estimate the same values from the transitions they experience. The last step, from tables to approximate functions, is Part 4: From Discrete to Continuous, where these methods start solving real problems.

Extra exercises

Three exercises to go further, from the lightest to the most mathematical:

Exercise E.1 The gambler's problem, reduced Math needed Compute: arithmetic, a small linear system, applying a formula of the pageend of high school Reason: expectations, inequalities, an argument in words: no new tools, but a chain of reasoningfirst year of university, with a first course in probability Prove: induction, limits, norms, fixed points: a proof, which can be skipped on a first readingabout two years of university math The level says which math is needed, not how long it takes.

A gambler bets on a sequence of fair coin flips, ph=0.5p_h = 0.5. Her capital is s∈{1,2,3}s \in \{1, 2, 3\}; she wins when it reaches 44 and loses when it drops to 00. At each flip she stakes an integer amount a∈{1,…,min⁡(s,4−s)}a \in \{1, \dots, \min(s, 4 - s)\}: heads, her capital becomes s+as + a; tails, s−as - a. The reward is +1+1 on reaching 44 and 00 otherwise, and γ=1\gamma = 1, so v∗(s)v_*(s) is the probability of winning from ss when playing optimally.

  1. Write the Bellman optimality equation for v∗(s)v_*(s), s∈{1,2,3}s \in \{1, 2, 3\}, with v∗(0)=v∗(4)=0v_*(0) = v_*(4) = 0 (the +1+1 is counted on the transition that reaches 44).
  2. Check that v(s)=s4v(s) = \frac{s}{4} satisfies the equation for every possible stake in every state.
  3. What does this say about the optimal policy? How many deterministic optimal policies are there?
  4. Without computing: how should v∗v_* and the best stakes change when the coin is unfavorable, ph<0.5p_h < 0.5, or favorable, ph>0.5p_h > 0.5?
Hint

The stakes allowed are A(1)={1}\mathcal{A}(1) = \lbrace 1 \rbrace, A(2)={1,2}\mathcal{A}(2) = \lbrace 1, 2 \rbrace, A(3)={1}\mathcal{A}(3) = \lbrace 1 \rbrace.

Hint

For question 4: with an unfavorable coin, is it better to flip many times or few times?

Checkpoint what you should get

v∗(s)=s4v_*(s) = \frac{s}{4}, every stake attains the max, and there are 1×2×1=21 \times 2 \times 1 = 2 deterministic optimal policies.

Solution
  1. v∗(s)=max⁡a{12[1s+a=4+v∗(s+a)]+12 v∗(s−a)}v_*(s) = \max_{a} \Big\lbrace \tfrac12 \big[ \mathbf{1}_{s + a = 4} + v_*(s + a) \big] + \tfrac12\, v_*(s - a) \Big\rbrace, with the max over 1≤a≤min⁡(s,4−s)1 \le a \le \min(s, 4 - s).

  2. With v(1)=0.25v(1) = 0.25, v(2)=0.5v(2) = 0.5, v(3)=0.75v(3) = 0.75:

    • s=1s = 1, a=1a = 1: 12v(2)+12v(0)=0.25\frac12 v(2) + \frac12 v(0) = 0.25;
    • s=2s = 2, a=1a = 1: 12v(3)+12v(1)=0.375+0.125=0.5\frac12 v(3) + \frac12 v(1) = 0.375 + 0.125 = 0.5;
    • s=2s = 2, a=2a = 2: 12(1+0)+12v(0)=0.5\frac12 (1 + 0) + \frac12 v(0) = 0.5;
    • s=3s = 3, a=1a = 1: 12(1+0)+12v(2)=0.75\frac12 (1 + 0) + \frac12 v(2) = 0.75.

    Every stake gives back v(s)v(s): the max is over equal numbers, v=s4v = \frac{s}{4} is the fixed point, and v∗(s)=s4v_*(s) = \frac{s}{4}.

  3. Every action attains the max, so every policy is optimal: 1×2×1=21 \times 2 \times 1 = 2 deterministic ones, and any random mixture at s=2s = 2. The reason: with a fair coin, the expected capital after a flip equals the capital before, whatever the stake. The probability of reaching 44 before 00 is then s4\frac{s}{4}, however you bet.

  4. With ph<0.5p_h < 0.5, each flip is a losing bet on average, so v∗(s)<s4v_*(s) < \frac{s}{4} and the gambler should flip as few times as possible: stake big (with ph=0.4p_h = 0.4, the optimal stake at s=2s = 2 is 22, and v∗=(0.16,0.4,0.64)v_* = (0.16, 0.4, 0.64)). With ph>0.5p_h > 0.5, each flip is a winning bet, v∗(s)>s4v_*(s) > \frac{s}{4}, and she should stake small and play many flips (with ph=0.6p_h = 0.6, the optimal stake at s=2s = 2 is 11).

What to notice the take-away

With ph≠0.5p_h \neq 0.5, the ties disappear and the optimal stakes depend on the coin. In the full problem of Sutton and Barto (Example 4.3, capital up to 100100, ph=0.4p_h = 0.4), the plotted optimal policy has a strange sawtooth shape, with an all-in bet at 5050 but a stake of 11 at 5151. Part of that shape comes from how ties are broken: many stakes are optimal, and the plot shows one choice among them.

Exercise E.2 The gambler's curious optimal policy Math needed Compute: arithmetic, a small linear system, applying a formula of the pageend of high school Reason: expectations, inequalities, an argument in words: no new tools, but a chain of reasoningfirst year of university, with a first course in probability Prove: induction, limits, norms, fixed points: a proof, which can be skipped on a first readingabout two years of university math The level says which math is needed, not how long it takes.

The full problem of Sutton and Barto (Example 4.3): the gambler of Exercise E.1, with a capital s∈{1,…,99}s \in \{1, \dots, 99\}, the goal at 100100, stakes a∈{1,…,min⁡(s,100−s)}a \in \{1, \dots, \min(s, 100 - s)\}, and an unfavorable coin, ph=0.4p_h = 0.4. The optimal policy plotted in the book has an odd shape: at capital 5050 it bets everything, but at capital 5151 it stakes only 11.

  1. Give an intuitive argument for why staking everything is optimal at 5050.
  2. At 5151, the largest stake allowed is 4949. Why does a stake of 11 lose nothing there?
  3. Value iteration gives v∗(51)≈0.403v_*(51) \approx 0.403, attained by both stakes 11 and 4949. What does this say about the plot in the book?
Hint

With ph<0.5p_h < 0.5, each flip is a losing bet on average: does the gambler want to play many flips, or few?

Hint

At 5151, with a stake of 11: where is the gambler if the flip is lost? If it is won?

Checkpoint what you should get

v∗(50)=0.4v_*(50) = 0.4, attained only by the all-in bet; at 5151, stakes 11 and 4949 tie.

Solution
  1. With ph<0.5p_h < 0.5, every flip has a negative expected value: the gambler wants to reach 100100 in as few flips as possible. At 5050, the largest stake allowed is 5050, and it reaches the goal in a single flip, with probability 0.40.4. Any smaller stake buys at least one more flip, each a losing bet. So v∗(50)=0.4v_*(50) = 0.4, attained by the all-in bet only.
  2. If the flip is lost, the gambler is at 5050, from which the one-flip shot at the goal is still available; if it is won, she is at 5252, better still. The small stake keeps the all-in option at 5050 instead of spending it.
  3. The two stakes are equally good: the optimal policy is not unique at 5151, and the plot shows one tie-break among many, the smallest maximizing stake. Another tie-break draws a different picture, just as optimal. The plot of π∗\pi_* is one answer; v∗v_* is the answer.
What to notice the take-away

Exercise E.1 (fair coin) had every stake tied; with ph=0.4p_h = 0.4, many ties remain. Before reading meaning into the shape of an optimal policy, check whether the algorithm had to break ties to draw it.

Exercise E.3 Why a deterministic optimal policy exists Math needed Compute: arithmetic, a small linear system, applying a formula of the pageend of high school Reason: expectations, inequalities, an argument in words: no new tools, but a chain of reasoningfirst year of university, with a first course in probability Prove: induction, limits, norms, fixed points: a proof, which can be skipped on a first readingabout two years of university math The level says which math is needed, not how long it takes.

Part 1 (section 4.1) stated a theorem: a finite MDP has a deterministic optimal policy, greedy with respect to v∗v_*. This exercise proves it, for γ<1\gamma < 1 and bounded rewards. Besides the operator T∗\mathcal{T}^* of section 3.2, define, for a policy π\pi,

(Tπv)(s)=∑aπ(a∣s)∑s′,rp(s′,r∣s,a) [r+γ v(s′)](\mathcal{T}^\pi v)(s) = \sum_a \pi(a \mid s) \sum_{s', r} p(s', r \mid s, a)\, \big[ r + \gamma\, v(s') \big]

Both are γ\gamma-contractions: the unique fixed point of T∗\mathcal{T}^* is v∗v_*, that of Tπ\mathcal{T}^\pi is vπv_\pi.

  1. Let π∗(s)∈arg⁡max⁡a∑s′,rp(s′,r∣s,a) [r+γ v∗(s′)]\pi_*(s) \in \arg\max_a \sum_{s', r} p(s', r \mid s, a)\, \big[ r + \gamma\, v_*(s') \big], a deterministic policy. Show that Tπ∗v∗=v∗\mathcal{T}^{\pi_*} v_* = v_*, and deduce vπ∗=v∗v_{\pi_*} = v_*.
  2. Show that Tπ\mathcal{T}^\pi is monotone: if v≤v′v \le v' in every state, then Tπv≤Tπv′\mathcal{T}^\pi v \le \mathcal{T}^\pi v'. Show also that Tπv≤T∗v\mathcal{T}^\pi v \le \mathcal{T}^* v for every vv.
  3. Deduce that vπ≤v∗v_\pi \le v_* for every policy π\pi, so that π∗\pi_* is optimal.
  4. Two actions tie for the argmax in some state. Why do both choices give an optimal policy, and why does v∗v_* not depend on the choice? Compare with the ties of Exercise 3.1.
  5. Where is it used that each A(s)\mathcal{A}(s) is finite?
Hint

Question 1: π∗(s)\pi_*(s) is an action that attains the max in (T∗v∗)(s)(\mathcal{T}^* v_*)(s).

Hint

Question 3: start from Tπv∗≤T∗v∗=v∗\mathcal{T}^\pi v_* \le \mathcal{T}^* v_* = v_*, and apply Tπ\mathcal{T}^\pi to both sides, again and again.

Checkpoint what you should get

v∗=Tπ∗v∗v_* = \mathcal{T}^{\pi_*} v_*, so vπ∗=v∗v_{\pi_*} = v_*; and v∗≥Tπv∗≥(Tπ)2v∗≥⋯→vπv_* \ge \mathcal{T}^\pi v_* \ge (\mathcal{T}^\pi)^2 v_* \ge \cdots \to v_\pi.

Solution
  1. By definition of π∗\pi_*, the action π∗(s)\pi_*(s) attains the max: v∗(s)=(T∗v∗)(s)=∑s′,rp(s′,r∣s,π∗(s)) [r+γ v∗(s′)]=(Tπ∗v∗)(s)v_*(s) = (\mathcal{T}^* v_*)(s) = \sum_{s', r} p(s', r \mid s, \pi_*(s))\, \big[ r + \gamma\, v_*(s') \big] = (\mathcal{T}^{\pi_*} v_*)(s). So v∗v_* is a fixed point of Tπ∗\mathcal{T}^{\pi_*}, whose only fixed point is vπ∗v_{\pi_*}: vπ∗=v∗v_{\pi_*} = v_*.
  2. If v≤v′v \le v', then for every (s,a)(s, a), ∑s′,rp(s′,r∣s,a) [r+γ v(s′)]≤∑s′,rp(s′,r∣s,a) [r+γ v′(s′)]\sum_{s', r} p(s', r \mid s, a)\, [r + \gamma\, v(s')] \le \sum_{s', r} p(s', r \mid s, a)\, [r + \gamma\, v'(s')], because p≥0p \ge 0 and γ≥0\gamma \ge 0. Averaging over aa with the nonnegative weights π(a∣s)\pi(a \mid s) keeps the inequality. And an average is at most a max: Tπv≤T∗v\mathcal{T}^\pi v \le \mathcal{T}^* v.
  3. By question 2, Tπv∗≤T∗v∗=v∗\mathcal{T}^\pi v_* \le \mathcal{T}^* v_* = v_*. Applying the monotone Tπ\mathcal{T}^\pi again and again: v∗≥Tπv∗≥(Tπ)2v∗≥⋯v_* \ge \mathcal{T}^\pi v_* \ge (\mathcal{T}^\pi)^2 v_* \ge \cdots, and this sequence converges to vπv_\pi, the fixed point of the contraction Tπ\mathcal{T}^\pi. So vπ≤v∗v_\pi \le v_* for every π\pi, and with question 1, vπ∗=v∗≥vπv_{\pi_*} = v_* \ge v_\pi: π∗\pi_* is optimal, and deterministic by construction.
  4. Either tied action attains the max, so questions 1 to 3 apply to both choices: each gives a deterministic optimal policy. v∗v_* is the fixed point of T∗\mathcal{T}^*, which does not involve any tie-break. This is what Exercise 3.1 showed: several optimal policies, one v∗v_*.
  5. In the definition of π∗\pi_*: over a finite set of actions, the max is always attained by some action. Over an infinite set, the supremum may not be reached, and no greedy action may exist.
What to notice the take-away

The proof never compares policies one by one, there are too many of them: it compares operators. The max of the Bellman optimality equation is attained by a single action in each state, and that is the whole reason a deterministic policy can be optimal.