Chapter 4

Dynamic Programming

Dynamic programming turns the Bellman equations of Chapter 3 into assignment statements. Given a perfect model p(s′,r∣s,a)p(s', r \mid s, a), it computes vπv_\pi by repeatedly replacing the left side of (3.9) with its right side, and computes v∗v_* the same way from (3.17).Richard S. Sutton and Andrew G. Barto. Reinforcement Learning: An Introduction. Adaptive Computation and Machine Learning. MIT Press, Cambridge, MA, second edition, 2018. ISBN 978-0-262-03924-6. URL http://incompleteideas.net/book/the-book-2nd.html; and Richard Bellman. Dynamic Programming. Princeton University Press, Princeton, NJ, 1957. A Rand Corporation Research Study; xxv+342 pp. The assumption of a perfect model and the cost of sweeping the whole state set limit its direct use, but almost every method in the rest of the book is an attempt to get the effect of dynamic programming (DP) with less computation and without the model. This chapter proves that the assignments work — they are contractions — proves the policy improvement theorem, and assembles policy iteration and value iteration from the two pieces. The pattern that emerges, generalized policy iteration, is the organizing idea of the whole book.

4.1Bellman Operators Are Contractions

Value functions on a finite state set are vectors in ℝ∣𝒮∣\mathbb{R}^{\lvert\mathcal{S}\rvert}, and we measure them with the max norm ∥v∥∞=max⁡s∣v(s)∣\lVert v \rVert_\infty = \max_s \lvert v(s) \rvert. Recall the operators of (3.13): Tπv=𝐫π+γ𝐏πvT_\pi v = \mathbf{r}_\pi + \gamma\mathbf{P}_\pi v, the right side of the Bellman equation, and T∗T_*, the right side of the optimality equation.

Lemma 4.1 (Contraction and Monotonicity). For every policy π\pi and all v,uv, u: (i) v≤uv \le u implies Tπv≤TπuT_\pi v \le T_\pi u and T∗v≤T∗uT_* v \le T_* u; (ii)

∥Tπv−Tπu∥∞≤γ∥v−u∥∞,∥T∗v−T∗u∥∞≤γ∥v−u∥∞. \lVert T_\pi v - T_\pi u \rVert_\infty \le \gamma \lVert v - u \rVert_\infty, \qquad \lVert T_* v - T_* u \rVert_\infty \le \gamma \lVert v - u \rVert_\infty. (4.1)

Proof. (i) 𝐏π\mathbf{P}_\pi has nonnegative entries, and a maximum of monotone functions is monotone. (ii) Tπv−Tπu=γ𝐏π(v−u)T_\pi v - T_\pi u = \gamma\mathbf{P}_\pi(v - u), and each row of 𝐏π\mathbf{P}_\pi is a probability vector, so each component is an average of components of γ(v−u)\gamma(v - u). For T∗T_*, fix ss and write f(a)f(a) and g(a)g(a) for the bracketed one-step lookaheads under vv and uu. Maxima over the same finite set satisfy ∣max⁡af(a)−max⁡ag(a)∣≤max⁡a∣f(a)−g(a)∣\lvert \max_a f(a) - \max_a g(a) \rvert \le \max_a \lvert f(a) - g(a) \rvert: if, say, max⁡f≥max⁡g\max f \ge \max g and a⋆a^\star attains max⁡f\max f, then 0≤max⁡f−max⁡g≤f(a⋆)−g(a⋆)0 \le \max f - \max g \le f(a^\star) - g(a^\star). Finally f(a)−g(a)=γ∑s′p(s′∣s,a)(v(s′)−u(s′))f(a) - g(a) = \gamma\sum_{s'} p(s' \mid s, a)\bigl(v(s') - u(s')\bigr) is bounded by γ∥v−u∥∞\gamma\lVert v - u \rVert_\infty. □

ℝ∣𝒮∣\mathbb{R}^{\lvert\mathcal{S}\rvert} with the max norm is complete, so the Banach fixed-point theorem (Theorem A.3) applies to both operators at once.

Theorem 4.2 (Fixed Points and Convergence of the Bellman Operators). Let γ<1\gamma < 1 and let TT be TπT_\pi or T∗T_*. Then TT has a unique fixed point vTv_T (vπv_\pi or v∗v_*, respectively), and for every v0v_0 the iterates vk+1=Tvkv_{k+1} = T v_k satisfy

∥vk−vT∥∞≤γk∥v0−vT∥∞,∥vk−vT∥∞≤γ1−γ∥vk−vk−1∥∞. \begin{aligned} \lVert v_k - v_T \rVert_\infty &\le \gamma^k \lVert v_0 - v_T \rVert_\infty, \\ \lVert v_k - v_T \rVert_\infty &\le \frac{\gamma}{1 - \gamma}\lVert v_k - v_{k-1} \rVert_\infty. \end{aligned} (4.2)

Proof. Existence, uniqueness and the first bound are the Banach theorem applied to (4.1). For the second, ∥vk−vT∥=∥Tvk−1−TvT∥≤γ∥vk−1−vT∥≤γ(∥vk−1−vk∥+∥vk−vT∥)\lVert v_k - v_T \rVert = \lVert T v_{k-1} - T v_T \rVert \le \gamma \lVert v_{k-1} - v_T \rVert \le \gamma\bigl(\lVert v_{k-1} - v_k \rVert + \lVert v_k - v_T \rVert\bigr); solve for ∥vk−vT∥\lVert v_k - v_T \rVert. □

The theorem closes the gap left in Theorem 3.5: the optimality equation has a solution, so v∗v_* is that solution, and any policy greedy with respect to it — in particular a deterministic one — is optimal. The first bound is an a priori rate; the second is an a posteriori stopping rule that uses only quantities the algorithm can see. For undiscounted episodic tasks the same conclusions hold when all policies are proper, with the max norm replaced by a weighted max norm in which the operators are again contractions.Dimitri P. Bertsekas and John N. Tsitsiklis. Neuro-Dynamic Programming. Athena Scientific, Belmont, MA, 1996. ISBN 1-886529-10-8

4.1.1In Wolfram Language

On the two-state chain of Section 3.3.2 we can watch the contraction act. Each application of TπT_\pi multiplies the error v−vπv - v_\pi by the matrix γ𝐏π\gamma\mathbf{P}_\pi.

Tπ3v−vπ−(γ𝐏π)3(v−vπ)T_\pi^3 v - v_\pi - (\gamma\mathbf{P}_\pi)^3(v - v_\pi) for a symbolic starting vector v=(v1,v2)v = (v_1, v_2): identically zero, so the error after kk sweeps is exactly (γ𝐏π)k(v0−vπ)(\gamma\mathbf{P}_\pi)^k(v_0 - v_\pi).
const chainP = await wl`{{p, 1 - p}, {1 - q, q}}`
const chainR = await wl`{Subscript[r, 1], Subscript[r, 2]}`
const chainV = await wl`Simplify[Inverse[IdentityMatrix[2] - \[Gamma] ${chainP}] . ${chainR}]`
const chainT = await wl`Function[{v}, ${chainR} + \[Gamma] ${chainP} . v]`
const errorIdentity = await show`Simplify[Nest[${chainT}, {Subscript[v, 1], Subscript[v, 2]}, 3] - ${chainV} - MatrixPower[\[Gamma] ${chainP}, 3] . ({Subscript[v, 1], Subscript[v, 2]} - ${chainV})]`
Tπ3v−vπ−(γ𝐏π)3(v−vπ)={0,0} T_\pi^3 v - v_\pi - (\gamma\mathbf{P}_\pi)^3 (v - v_\pi) = \{0,0\} The eigenvalues of γ𝐏π\gamma\mathbf{P}_\pi: the stochastic matrix contributes 11 and p+q−1∈(−1,1)p + q - 1 \in (-1, 1), so the slowest error mode decays exactly like γk\gamma^k — the a priori bound is tight.
const chainEig = await show`Eigenvalues[\[Gamma] ${chainP}]`
eig⁡(γ𝐏π)={γ,(−1+p+q)γ} \operatorname{eig}(\gamma\mathbf{P}_\pi) = \{\gamma ,(-1+p+q) \gamma \}

The contraction of T∗T_* rests on a single inequality about maxima. The kernel can prove it for two actions by quantifier elimination over the reals.

∣max⁡(a,b)−max⁡(c,d)∣≤max⁡(∣a−c∣,∣b−d∣)\lvert \max(a, b) - \max(c, d) \rvert \le \max(\lvert a - c \rvert, \lvert b - d \rvert) for all real a,b,c,da, b, c, d: the step in Lemma 4.1 that makes T∗T_* a contraction.
const maxIneq = await show`Resolve[ForAll[{a, b, c, d}, Element[{a, b, c, d}, Reals], Abs[Max[a, b] - Max[c, d]] <= Max[Abs[a - c], Abs[b - d]]]]`
∀a,b,c,d∈ℝ ⁣:∣max⁡(a,b)−max⁡(c,d)∣≤max⁡(∣a−c∣,∣b−d∣)isTrue \begin{aligned} &\forall a, b, c, d \in \mathbb{R}\colon \\ &\quad \lvert \max(a, b) - \max(c, d) \rvert \le \max(\lvert a - c \rvert, \lvert b - d \rvert) \quad\text{is}\quad \text{True} \end{aligned}

4.2Policy Evaluation

Iterative policy evaluation computes vπv_\pi — the prediction problem — by iterating TπT_\pi from an arbitrary v0v_0 (with 00 at the terminal state):

vk+1(s)≐∑aπ(a∣s)∑s′,rp(s′,r∣s,a)[r+γvk(s′)]. v_{k+1}(s) \doteq \sum_a \pi(a \mid s) \sum_{s', r} p(s', r \mid s, a)\bigl[r + \gamma v_k(s')\bigr]. (4.3)

Each assignment is an expected update: it uses the probabilities of all successor states, not a sample of one. A literal implementation of (4.3) keeps two arrays, old and new, and is called synchronous or Jacobi-style. The usual implementation overwrites values in place, so that later states in a sweep already see the new values of earlier ones (Gauss–Seidel style).

Algorithm 4.3 (Iterative Policy Evaluation (in place)). Input: a policy π\pi, the model pp; a threshold θ>0\theta > 0. Initialize: V(s)V(s) arbitrarily for s∈𝒮s \in \mathcal{S}, and V(terminal)=0V(\text{terminal}) = 0.

  1. Repeat:
    1. Δ←0\Delta \leftarrow 0.
    2. For each s∈𝒮s \in \mathcal{S}: v←V(s)v \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)\bigl[r + \gamma V(s')\bigr]; Δ←max⁡(Δ,∣v−V(s)∣)\Delta \leftarrow \max(\Delta, \lvert v - V(s) \rvert).
  2. until Δ<θ\Delta < \theta. Return V≈vπV \approx v_\pi.

The in-place sweep is also a γ\gamma-contraction toward vπv_\pi: when state ss is updated, the values it reads are either new ones, whose errors are already at most γ\gamma times the old maximal error, or old ones, whose errors are at most that maximal error; either way the new error at ss is at most γ\gamma times it. In practice the in-place version converges faster, by an amount that depends on the order of the sweep. With γ<1\gamma < 1, stopping at Δ<θ\Delta < \theta guarantees ∥V−vπ∥∞≤γθ/(1−γ)\lVert V - v_\pi \rVert_\infty \le \gamma\theta/(1 - \gamma) for the synchronous version, by (4.2).

Example 4.4 (The Small Gridworld). A 4×44 \times 4 grid has terminal states in two opposite corners (formally one state) and fourteen nonterminal cells numbered 1,…,141, \dots, 14 in reading order. The four moves are deterministic, moves off the grid leave the agent in place, every transition gives reward −1-1, and γ=1\gamma = 1. Under the equiprobable random policy, −vπ(s)-v_\pi(s) is the expected number of steps to termination.

The random policy is proper, so Proposition 3.4 applies with γ=1\gamma = 1, and the kernel can skip the iteration altogether.

vπv_\pi for the random policy on the 4×44 \times 4 grid, by solving (𝐈−𝐏π)𝐯=𝐫π(\mathbf{I} - \mathbf{P}_\pi)\mathbf{v} = \mathbf{r}_\pi exactly: the values are integers, the limits that the book’s iterative sweeps approach.
const g4moves = await wl`{{-1, 0}, {1, 0}, {0, 1}, {0, -1}}`
const g4next = await wl`Function[{k, a}, With[{c = {Quotient[k - 1, 4] + 1, Mod[k - 1, 4] + 1} + a}, If[1 <= c[[1]] <= 4 && 1 <= c[[2]] <= 4, 4 (c[[1]] - 1) + c[[2]], k]]]`
const g4eval = await wl`Function[{pol}, Module[{P, r},
  P = Table[If[k == 1 || k == 16, ConstantArray[0, 16], Sum[pol[[k, i]] UnitVector[16, ${g4next}[k, ${g4moves}[[i]]]], {i, 4}]], {k, 16}];
  P[[All, {1, 16}]] = 0;
  r = Table[If[k == 1 || k == 16, 0, -1], {k, 16}];
  LinearSolve[IdentityMatrix[16] - P, r]]]`
const g4random = await wl`${g4eval}[ConstantArray[1/4, {16, 4}]]`
const g4show = await show`Partition[${g4random}, 4]`
vπ=(0−14−20−22−14−18−20−20−20−20−18−14−22−20−140) v_\pi = \left( \begin{array}{cccc} 0 & -14 & -20 & -22 \\ -14 & -18 & -20 & -20 \\ -20 & -20 & -18 & -14 \\ -22 & -20 & -14 & 0 \\ \end{array} \right)

4.3Policy Improvement

Why compute vπv_\pi at all? To find a better policy. Knowing vπv_\pi, we can evaluate any one-step deviation: 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)[r + \gamma v_\pi(s')] is the return for taking aa once and then following π\pi. If qπ(s,a)>vπ(s)q_\pi(s, a) > v_\pi(s), deviating once pays; the policy improvement theorem says that deviating every time pays too, and the gain is exactly accounted for by an identity.

Lemma 4.5 (Performance Difference). For any two policies π,π′\pi, \pi' and γ<1\gamma < 1,

𝐯π′−𝐯π=(𝐈−γ𝐏π′)−1(Tπ′𝐯π−𝐯π),(Tπ′vπ)(s)=∑aπ′(a∣s) qπ(s,a). \begin{aligned} \mathbf{v}_{\pi'} - \mathbf{v}_\pi &= (\mathbf{I} - \gamma\mathbf{P}_{\pi'})^{-1}\bigl(T_{\pi'}\mathbf{v}_\pi - \mathbf{v}_\pi\bigr), \\ (T_{\pi'} v_\pi)(s) &= \sum_a \pi'(a \mid s)\, q_\pi(s, a). \end{aligned} (4.4)

Equivalently, vπ′(s)−vπ(s)=∑k=0∞γk 𝔼π′[qπ(Sk,Ak)−vπ(Sk)∣S0=s]v_{\pi'}(s) - v_\pi(s) = \sum_{k=0}^\infty \gamma^k\, \mathbb{E}_{\pi'}\bigl[q_\pi(S_k, A_k) - v_\pi(S_k) \bigm| S_0 = s\bigr].

Proof. Subtract Tπ′𝐯π=𝐫π′+γ𝐏π′𝐯πT_{\pi'}\mathbf{v}_\pi = \mathbf{r}_{\pi'} + \gamma\mathbf{P}_{\pi'}\mathbf{v}_\pi from 𝐯π′=𝐫π′+γ𝐏π′𝐯π′\mathbf{v}_{\pi'} = \mathbf{r}_{\pi'} + \gamma\mathbf{P}_{\pi'}\mathbf{v}_{\pi'} to get 𝐯π′−Tπ′𝐯π=γ𝐏π′(𝐯π′−𝐯π)\mathbf{v}_{\pi'} - T_{\pi'}\mathbf{v}_\pi = \gamma\mathbf{P}_{\pi'}(\mathbf{v}_{\pi'} - \mathbf{v}_\pi), that is, (𝐈−γ𝐏π′)(𝐯π′−𝐯π)=Tπ′𝐯π−𝐯π(\mathbf{I} - \gamma\mathbf{P}_{\pi'})(\mathbf{v}_{\pi'} - \mathbf{v}_\pi) = T_{\pi'}\mathbf{v}_\pi - \mathbf{v}_\pi. Invert. The series form is the Neumann expansion (𝐈−γ𝐏π′)−1=∑kγk𝐏π′k(\mathbf{I} - \gamma\mathbf{P}_{\pi'})^{-1} = \sum_k \gamma^k\mathbf{P}_{\pi'}^k, since (𝐏π′k𝐱)(s)(\mathbf{P}_{\pi'}^k \mathbf{x})(s) is the expectation of x(Sk)x(S_k) under π′\pi'. □

The quantity qπ(s,a)−vπ(s)q_\pi(s, a) - v_\pi(s) is the advantage of aa in ss; the lemma says that the gain of switching to π′\pi' is the advantage of π′\pi'’s actions, measured with π\pi’s values, accumulated along π′\pi'’s own trajectories.In this form the identity is usually credited to Kakade and Langford (2002). It underlies conservative policy iteration and, through the trust-region and proximal methods built on it, much of modern policy optimization (Chapter 13).Sham Kakade and John Langford. Approximately Optimal Approximate Reinforcement Learning. In Claude Sammut and Achim G. Hoffmann, editors, Proceedings of the Nineteenth International Conference on Machine Learning (ICML 2002), pages 267–274. Morgan Kaufmann, 2002

Theorem 4.6 (Policy Improvement). Let π\pi and π′\pi' be policies, deterministic or stochastic, such that for all s∈𝒮s \in \mathcal{S}

∑aπ′(a∣s) qπ(s,a)≥vπ(s). \sum_a \pi'(a \mid s)\, q_\pi(s, a) \ge v_\pi(s). (4.5)

Then vπ′(s)≥vπ(s)v_{\pi'}(s) \ge v_\pi(s) for all ss. If the inequality (4.5) is strict at a state ss, then vπ′(s)>vπ(s)v_{\pi'}(s) > v_\pi(s).

Proof. Let 𝐝=Tπ′𝐯π−𝐯π\mathbf{d} = T_{\pi'}\mathbf{v}_\pi - \mathbf{v}_\pi, which is ≥0\ge 0 by hypothesis. In (4.4), (𝐈−γ𝐏π′)−1=𝐈+∑k≥1γk𝐏π′k(\mathbf{I} - \gamma\mathbf{P}_{\pi'})^{-1} = \mathbf{I} + \sum_{k \ge 1}\gamma^k\mathbf{P}_{\pi'}^k is the identity plus a matrix with nonnegative entries, so 𝐯π′−𝐯π≥𝐝≥𝟎\mathbf{v}_{\pi'} - \mathbf{v}_\pi \ge \mathbf{d} \ge \mathbf{0} componentwise. At a state where d(s)>0d(s) > 0 this gives vπ′(s)−vπ(s)≥d(s)>0v_{\pi'}(s) - v_\pi(s) \ge d(s) > 0. □

The book proves the theorem by “unrolling”: start from vπ(s)≤qπ(s,π′(s))v_\pi(s) \le q_\pi(s, \pi'(s)), expand qπq_\pi one step, apply the hypothesis at the next state, and repeat, accumulating π′\pi'’s rewards until only vπ′(s)v_{\pi'}(s) is left. The matrix proof is the same argument with the bookkeeping done by the Neumann series. For γ=1\gamma = 1 both need π′\pi' to be proper.

The natural way to satisfy (4.5) everywhere is to be greedy:

π′(s)≐argmax⁡aqπ(s,a)=argmax⁡a∑s′,rp(s′,r∣s,a)[r+γvπ(s′)]. \pi'(s) \doteq \operatorname{argmax}_a q_\pi(s, a) = \operatorname{argmax}_a \sum_{s', r} p(s', r \mid s, a)\bigl[r + \gamma v_\pi(s')\bigr]. (4.6)

This is policy improvement. If the greedy policy is no better than π\pi, then T∗𝐯π=Tπ′𝐯π=𝐯πT_*\mathbf{v}_\pi = T_{\pi'}\mathbf{v}_\pi = \mathbf{v}_\pi — otherwise the theorem would give a strict improvement somewhere — so vπv_\pi solves the optimality equation and π\pi is already optimal by Theorem 3.5. Greedy improvement either strictly improves a policy or certifies that it is optimal. With ties, a stochastic greedy policy may split its probability among the maximizing actions in any way; the theorem covers all such splits.

4.3.1In Wolfram Language

The performance difference identity holds for any pair of policies, which the kernel confirms on a fully symbolic two-state MDP: two arbitrary transition matrices and reward vectors, one for each policy.

𝐯π′−𝐯π−(𝐈−γ𝐏π′)−1(Tπ′𝐯π−𝐯π)\mathbf{v}_{\pi'} - \mathbf{v}_\pi - (\mathbf{I} - \gamma\mathbf{P}_{\pi'})^{-1}(T_{\pi'}\mathbf{v}_\pi - \mathbf{v}_\pi) with six free parameters: zero, which is (4.4).
const pdP1 = await wl`{{Subscript[p, 1], 1 - Subscript[p, 1]}, {1 - Subscript[q, 1], Subscript[q, 1]}}`
const pdP2 = await wl`{{Subscript[p, 2], 1 - Subscript[p, 2]}, {1 - Subscript[q, 2], Subscript[q, 2]}}`
const pdR1 = await wl`{Subscript[r, 1], Subscript[r, 2]}`
const pdR2 = await wl`{Subscript[r, 3], Subscript[r, 4]}`
const pdV = await wl`Function[{P, r}, Inverse[IdentityMatrix[2] - \[Gamma] P] . r]`
const pdCheck = await show`Simplify[${pdV}[${pdP2}, ${pdR2}] - ${pdV}[${pdP1}, ${pdR1}] - Inverse[IdentityMatrix[2] - \[Gamma] ${pdP2}] . (${pdR2} + \[Gamma] ${pdP2} . ${pdV}[${pdP1}, ${pdR1}] - ${pdV}[${pdP1}, ${pdR1}])]`
𝐯π′−𝐯π−(𝐈−γ𝐏π′)−1(Tπ′𝐯π−𝐯π)={0,0} \mathbf{v}_{\pi'} - \mathbf{v}_\pi - (\mathbf{I} - \gamma\mathbf{P}_{\pi'})^{-1}(T_{\pi'}\mathbf{v}_\pi - \mathbf{v}_\pi) = \{0,0\}

On the small gridworld, one greedy step from the random policy already lands on an optimal policy: the greedy policy’s values are minus the shortest distances to a terminal corner.

Greedy (first maximizer) with respect to the random policy’s vπv_\pi, then evaluated exactly: vπ′=−(steps to the nearest corner)v_{\pi'} = -(\text{steps to the nearest corner}), against vπv_\pi as low as −22-22.
const g4greedy = await wl`Table[If[k == 1 || k == 16, {1, 0, 0, 0}, With[{q = Table[-1 + ${g4random}[[${g4next}[k, a]]], {a, ${g4moves}}]}, UnitVector[4, First[Ordering[q, -1]]]]], {k, 16}]`
const g4improved = await show`Partition[${g4eval}[${g4greedy}], 4]`
vπ′=(0−1−2−3−1−2−3−2−2−3−2−1−3−2−10) v_{\pi'} = \left( \begin{array}{cccc} 0 & -1 & -2 & -3 \\ -1 & -2 & -3 & -2 \\ -2 & -3 & -2 & -1 \\ -3 & -2 & -1 & 0 \\ \end{array} \right)

4.4Policy Iteration

Alternating the two steps produces a monotone sequence of policies,

π0→  E  vπ0→  I  π1→  E  vπ1→  I  π2→  E  ⋯→  I  π∗→  E  v∗, \pi_0 \xrightarrow{\;\text{E}\;} v_{\pi_0} \xrightarrow{\;\text{I}\;} \pi_1 \xrightarrow{\;\text{E}\;} v_{\pi_1} \xrightarrow{\;\text{I}\;} \pi_2 \xrightarrow{\;\text{E}\;} \cdots \xrightarrow{\;\text{I}\;} \pi_* \xrightarrow{\;\text{E}\;} v_*,

where E is evaluation and I is greedy improvement. This is policy iteration, due to Howard.Ronald A. Howard. Dynamic Programming and Markov Processes. MIT Press, Cambridge, MA, 1960 One detail matters for termination: when several actions tie for the maximum, the current action should be kept, or the algorithm may cycle forever among equally good policies.

Algorithm 4.7 (Policy Iteration). Input: the model pp; a threshold θ>0\theta > 0 for evaluation. Initialize: V(s)∈ℝV(s) \in \mathbb{R} and a deterministic π(s)∈𝒜(s)\pi(s) \in \mathcal{A}(s) arbitrarily, for all s∈𝒮s \in \mathcal{S}; V(terminal)=0V(\text{terminal}) = 0.

  1. Policy evaluation. Run Algorithm 4.3 for π\pi, starting from the current VV (or solve (3.11) exactly).
  2. Policy improvement. stable←true\textit{stable} \leftarrow \text{true}. For each s∈𝒮s \in \mathcal{S}:
    1. Q(s,a)←∑s′,rp(s′,r∣s,a)[r+γV(s′)]Q(s, a) \leftarrow \sum_{s', r} p(s', r \mid s, a)\bigl[r + \gamma V(s')\bigr] for all a∈𝒜(s)a \in \mathcal{A}(s).
    2. If Q(s,π(s))<max⁡aQ(s,a)Q(s, \pi(s)) < \max_a Q(s, a): set π(s)←argmax⁡aQ(s,a)\pi(s) \leftarrow \operatorname{argmax}_a Q(s, a) and stable←false\textit{stable} \leftarrow \text{false}.
  3. If stable\textit{stable}, return V≈v∗V \approx v_* and π≈π∗\pi \approx \pi_*; otherwise go to step 1.

Theorem 4.8 (Finite Termination of Policy Iteration). With exact evaluation and γ<1\gamma < 1, policy iteration stops after at most ∏s∣𝒜(s)∣\prod_{s}\lvert\mathcal{A}(s)\rvert improvement steps, and the policy it returns is optimal.

Proof. If step 2 changes π(s)\pi(s), the new action has qπ(s,π′(s))>qπ(s,π(s))=vπ(s)q_\pi(s, \pi'(s)) > q_\pi(s, \pi(s)) = v_\pi(s), and unchanged states satisfy (4.5) with equality. By Theorem 4.6, vπ′≥vπv_{\pi'} \ge v_\pi with strict inequality at the changed states. The value vectors therefore strictly increase in the partial order, so no policy can occur twice, and there are only ∏s∣𝒜(s)∣\prod_s \lvert\mathcal{A}(s)\rvert deterministic policies. When the loop stops, π\pi is greedy with respect to its own value function, so vπ=T∗vπv_\pi = T_* v_\pi and π\pi is optimal by Theorem 3.5. □

This is a second, purely combinatorial proof that a deterministic optimal policy exists. Policy iteration is also never slower than value iteration, iteration for iteration.

Proposition 4.9 (Policy Iteration Dominates Value Iteration). If πk+1\pi_{k+1} is greedy with respect to vπkv_{\pi_k}, then v∗≥vπk+1≥T∗vπkv_* \ge v_{\pi_{k+1}} \ge T_* v_{\pi_k}. Consequently ∥v∗−vπk∥∞≤γk∥v∗−vπ0∥∞\lVert v_* - v_{\pi_k} \rVert_\infty \le \gamma^k \lVert v_* - v_{\pi_0} \rVert_\infty.

Proof. Since Tπk+1vπk=T∗vπk≥Tπkvπk=vπkT_{\pi_{k+1}} v_{\pi_k} = T_* v_{\pi_k} \ge T_{\pi_k} v_{\pi_k} = v_{\pi_k}, monotonicity makes the sequence Tπk+1jvπkT_{\pi_{k+1}}^j v_{\pi_k}, j≥1j \ge 1, nondecreasing, and its limit is vπk+1v_{\pi_{k+1}}; hence vπk+1≥Tπk+1vπk=T∗vπkv_{\pi_{k+1}} \ge T_{\pi_{k+1}} v_{\pi_k} = T_* v_{\pi_k}. Then 0≤v∗−vπk+1≤T∗v∗−T∗vπk0 \le v_* - v_{\pi_{k+1}} \le T_* v_* - T_* v_{\pi_k}, whose max norm is at most γ∥v∗−vπk∥∞\gamma \lVert v_* - v_{\pi_k} \rVert_\infty by Lemma 4.1. Iterate. □

In practice the bound of Theorem 4.8 is wildly pessimistic: policy iteration usually stops after a handful of iterations, because each improvement is global — every state switches to its best action at once.Puterman and Brumelle (1979) showed that policy iteration is Newton’s method applied to v−T∗v=0v - T_* v = 0: the improvement step picks the linearization 𝐈−γ𝐏π′\mathbf{I} - \gamma\mathbf{P}_{\pi'} of this piecewise-linear map at vπv_\pi, and the evaluation step solves the linearized equation, since vπ′=vπ−(𝐈−γ𝐏π′)−1(vπ−T∗vπ)v_{\pi'} = v_\pi - (\mathbf{I} - \gamma\mathbf{P}_{\pi'})^{-1}(v_\pi - T_* v_\pi) by (4.4). This explains its very fast convergence near the solution.Martin L. Puterman and Shelby L. Brumelle. On the Convergence of Policy Iteration in Stationary Dynamic Programming. Mathematics of Operations Research, 4(1):60–69, 1979. doi: 10.1287/moor.4.1.60 Sutton and Barto’s Jack’s car rental problem illustrates this: two locations with up to 20 cars each (441441 states), Poisson requests and returns, and overnight moves of up to five cars at two dollars each; starting from the policy that never moves cars, policy iteration reaches an optimal policy after four improvements. Evaluation is warm-started from the previous value function, which changes little between iterations.

4.4.1In Wolfram Language

A complete run of policy iteration on the recycling robot, with the parameters of Section 3.4.1 (α=4/5\alpha = 4/5, β=3/5\beta = 3/5, rsearch=2r_{\text{search}} = 2, rwait=1r_{\text{wait}} = 1, γ=9/10\gamma = 9/10), exact evaluation, and the tie rule of Algorithm 4.7. One improvement step is a pure function from policies to policies; FixedPointList iterates it until the policy stops changing.

Each pair is a policy (actions in 𝗁𝗂𝗀𝗁\mathsf{high}, 𝗅𝗈𝗐\mathsf{low}; 1=𝗌𝖾𝖺𝗋𝖼𝗁1 = \mathsf{search}, 2=𝗐𝖺𝗂𝗍2 = \mathsf{wait}, 3=𝗋𝖾𝖼𝗁𝖺𝗋𝗀𝖾3 = \mathsf{recharge}) above its exact value (v(𝗁𝗂𝗀𝗁),v(𝗅𝗈𝗐))(v(\mathsf{high}), v(\mathsf{low})). From “always wait”, two improvements reach (𝗌𝖾𝖺𝗋𝖼𝗁,𝗋𝖾𝖼𝗁𝖺𝗋𝗀𝖾)(\mathsf{search}, \mathsf{recharge}); the repeated last entry is the stability test.
const rbP = await wl`{{{4/5, 1/5}, {1, 0}}, {{2/5, 3/5}, {0, 1}, {1, 0}}}`
const rbR = await wl`{{2, 1}, {0, 1, 0}}`
const rbQ = await wl`Function[{v}, Table[${rbR}[[s, a]] + 9/10 ${rbP}[[s, a]] . v, {s, 2}, {a, Length[${rbR}[[s]]]}]]`
const rbEval = await wl`Function[{pol}, LinearSolve[IdentityMatrix[2] - 9/10 Table[${rbP}[[s, pol[[s]]]], {s, 2}], Table[${rbR}[[s, pol[[s]]]], {s, 2}]]]`
const rbImprove = await wl`Function[{pol}, With[{q = ${rbQ}[${rbEval}[pol]]}, Table[If[q[[s, pol[[s]]]] == Max[q[[s]]], pol[[s]], First[Ordering[q[[s]], -1]]], {s, 2}]]]`
const rbPI = await show`Map[{#, ${rbEval}[#]} &, FixedPointList[${rbImprove}, {2, 2}]]`
(πk, 𝐯πk)k=0,1,2,3={(221010),(1295710),(1310005990059),(1310005990059)} \bigl(\pi_k,\ \mathbf{v}_{\pi_k}\bigr)_{k = 0, 1, 2, 3} = \left\{\left( \begin{array}{cc} 2 & 2 \\ 10 & 10 \\ \end{array} \right),\left( \begin{array}{cc} 1 & 2 \\ \frac{95}{7} & 10 \\ \end{array} \right),\left( \begin{array}{cc} 1 & 3 \\ \frac{1000}{59} & \frac{900}{59} \\ \end{array} \right),\left( \begin{array}{cc} 1 & 3 \\ \frac{1000}{59} & \frac{900}{59} \\ \end{array} \right)\right\}

4.5Value Iteration

Policy iteration evaluates each policy to convergence before improving it, which is wasteful far from the solution: in the small gridworld the greedy policy stops changing after three sweeps of evaluation, long before the values converge. The extreme alternative stops evaluation after one sweep and folds the improvement into it:

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

that is, vk+1=T∗vkv_{k+1} = T_* v_k. This is value iteration: the optimality equation turned into an assignment, just as policy evaluation is the Bellman equation turned into one. Its convergence to v∗v_* at rate γk\gamma^k is Theorem 4.2.

Algorithm 4.10 (Value Iteration). Input: the model pp; a threshold θ>0\theta > 0. Initialize: V(s)V(s) arbitrarily for s∈𝒮s \in \mathcal{S}, and V(terminal)=0V(\text{terminal}) = 0.

  1. Repeat:
    1. Δ←0\Delta \leftarrow 0.
    2. For each s∈𝒮s \in \mathcal{S}: v←V(s)v \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)\bigl[r + \gamma V(s')\bigr]; Δ←max⁡(Δ,∣v−V(s)∣)\Delta \leftarrow \max(\Delta, \lvert v - V(s) \rvert).
  2. until Δ<θ\Delta < \theta.
  3. Return the deterministic policy π(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)\bigl[r + \gamma V(s')\bigr].

Value iteration returns a policy, and what we care about is that policy’s value, not the accuracy of VV. The two are linked by a classical bound.

Proposition 4.11 (Loss of a Greedy Policy). Let γ<1\gamma < 1, let ∥V−v∗∥∞≤ε\lVert V - v_* \rVert_\infty \le \varepsilon, and let π\pi be greedy with respect to VV. Then

0≤v∗(s)−vπ(s)≤2γε1−γfor all s. 0 \le v_*(s) - v_\pi(s) \le \frac{2\gamma\varepsilon}{1 - \gamma} \quad \text{for all } s. (4.8)

Moreover, if ε\varepsilon is smaller than half the smallest gap min⁡s[v∗(s)−max⁡a not optimalq∗(s,a)]\min_s \bigl[v_*(s) - \max_{a \text{ not optimal}} q_*(s, a)\bigr] divided by γ\gamma, then π\pi is optimal.

Proof. Greediness means TπV=T∗VT_\pi V = T_* V. Hence v∗−vπ=(T∗v∗−T∗V)+(TπV−Tπvπ)v_* - v_\pi = (T_* v_* - T_* V) + (T_\pi V - T_\pi v_\pi), and by Lemma 4.1 the max norm of the right side is at most γε+γ∥V−vπ∥≤γε+γ(ε+∥v∗−vπ∥)\gamma\varepsilon + \gamma\lVert V - v_\pi \rVert \le \gamma\varepsilon + \gamma(\varepsilon + \lVert v_* - v_\pi \rVert). Solve for ∥v∗−vπ∥\lVert v_* - v_\pi \rVert. For the second claim, the one-step lookaheads computed from VV differ from q∗q_* by at most γε\gamma\varepsilon, less than half the gap, so no non-optimal action can attain the maximum. □

Together with (4.2): if a sweep changes no value by more than θ\theta, the greedy policy loses at most 2γ2θ/(1−γ)22\gamma^2\theta/(1 - \gamma)^2.Satinder P. Singh and Richard C. Yee. An Upper Bound on the Loss from Approximate Optimal-Value Functions. Machine Learning, 16(3):227–233, 1994. doi: 10.1007/BF00993308; and Ronald J. Williams and Leemon C. Baird III. Tight Performance Bounds on Greedy Policies Based on Imperfect Value Functions. Technical Report NU-CCS-93-14, Northeastern University, College of Computer Science, Boston, MA, 1993 The second statement explains a phenomenon visible in every run: the greedy policy becomes optimal after finitely many sweeps, while the values converge only in the limit.

Between the two extremes lie the truncated or modified policy iteration algorithms, which improve the policy and then apply mm sweeps of evaluation, vk+1=Tπk+1mvkv_{k+1} = T_{\pi_{k+1}}^m v_k with πk+1\pi_{k+1} greedy for vkv_k: m=1m = 1 is value iteration and m=∞m = \infty is policy iteration. All of them converge for discounted finite MDPs.Martin L. Puterman and Moon Chirl Shin. Modified Policy Iteration Algorithms for Discounted Markov Decision Problems. Management Science, 24(11):1127–1137, 1978. doi: 10.1287/mnsc.24.11.1127

4.5.1In Wolfram Language

On the recycling robot, value iteration from v0=0v_0 = 0 in exact arithmetic lets us measure the a priori bound directly.

Rows (k, ∥vk−v∗∥∞/γk∥v0−v∗∥∞, greedy action in 𝗁𝗂𝗀𝗁, in 𝗅𝗈𝗐)(k,\ \lVert v_k - v_* \rVert_\infty / \gamma^k\lVert v_0 - v_* \rVert_\infty,\ \text{greedy action in } \mathsf{high},\ \text{in } \mathsf{low}) for k=0,…,8k = 0, \dots, 8. The ratio stays below 11 and settles near 0.970.97: the error shrinks essentially at rate γ\gamma. The greedy policy is optimal, (1,3)(1, 3), from k=2k = 2 on.
const rbStar = await wl`${rbEval}[{1, 3}]`
const rbVI = await wl`NestList[Function[v, Map[Max, ${rbQ}[v]]], {0, 0}, 8]`
const rbRates = await show`Table[Join[{k, N[Max[Abs[${rbVI}[[k + 1]] - ${rbStar}]]/((9/10)^k Max[Abs[${rbStar}]]), 4]}, Map[First[Ordering[#, -1]] &, ${rbQ}[${rbVI}[[k + 1]]]]], {k, 0, 8}]`
(k, ∥vk−v∗∥∞γk∥v0−v∗∥∞, greedy⁡(vk))k=0,…,8=(01.0001210.98001220.97271330.97131340.97131350.97121360.97121370.97121380.971213) \Bigl(k,\ \tfrac{\lVert v_k - v_* \rVert_\infty}{\gamma^k \lVert v_0 - v_* \rVert_\infty},\ \operatorname{greedy}(v_k)\Bigr)_{k = 0, \dots, 8} = \left( \begin{array}{cccc} 0 & 1.000 & 1 & 2 \\ 1 & 0.9800 & 1 & 2 \\ 2 & 0.9727 & 1 & 3 \\ 3 & 0.9713 & 1 & 3 \\ 4 & 0.9713 & 1 & 3 \\ 5 & 0.9712 & 1 & 3 \\ 6 & 0.9712 & 1 & 3 \\ 7 & 0.9712 & 1 & 3 \\ 8 & 0.9712 & 1 & 3 \\ \end{array} \right)

The same iteration solves the optimality equation of the 5×55 \times 5 gridworld of Section 3.3.2, in machine arithmetic this time; 300300 sweeps reduce the error below 10−1210^{-12}.

v∗v_* for the 5×55 \times 5 gridworld at γ=0.9\gamma = 0.9 by value iteration from 00: the book’s Figure 3.5 values, with v∗(A)=10/(1−0.95)=24.419v_*(A) = 10/(1 - 0.9^5) = 24.419 as in Exercise 3.9.
const g5step = await wl`Function[{s, a}, Which[
  s == {1, 2}, {{5, 2}, 10},
  s == {1, 4}, {{3, 4}, 5},
  1 <= (s + a)[[1]] <= 5 && 1 <= (s + a)[[2]] <= 5, {s + a, 0},
  True, {s, -1}]]`
const g5out = await wl`Table[${g5step}[s, a], {s, Tuples[{Range[5], Range[5]}]}, {a, {{-1, 0}, {1, 0}, {0, 1}, {0, -1}}}]`
const g5star = await show`NumberForm[Partition[Nest[Function[v, Map[Max, Map[Function[o, o[[2]] + 0.9 v[[5 (o[[1, 1]] - 1) + o[[1, 2]]]]], ${g5out}, {2}]]], ConstantArray[0., 25], 300], 5], {3, 1}]`
v∗≈(22.024.422.019.417.519.822.019.817.816.017.819.817.816.014.416.017.816.014.413.014.416.014.413.011.7) v_* \approx \left( \begin{array}{ccccc} 22.0 & 24.4 & 22.0 & 19.4 & 17.5 \\ 19.8 & 22.0 & 19.8 & 17.8 & 16.0 \\ 17.8 & 19.8 & 17.8 & 16.0 & 14.4 \\ 16.0 & 17.8 & 16.0 & 14.4 & 13.0 \\ 14.4 & 16.0 & 14.4 & 13.0 & 11.7 \\ \end{array} \right)

4.5.2The Gambler’s Problem

A gambler with capital s∈{1,…,99}s \in \{1, \dots, 99\} stakes an integer a∈{1,…,min⁡(s,100−s)}a \in \{1, \dots, \min(s, 100 - s)\} on a coin that comes up heads with probability php_h; heads adds aa to the capital, tails removes it. The episode ends at 00 or at the goal 100100, and the only reward is +1+1 on reaching the goal. With γ=1\gamma = 1, v∗(s)v_*(s) is the maximal probability of reaching the goal from ss, and the optimality equation reads

v∗(s)=max⁡1≤a≤min⁡(s,100−s)[ph v∗(s+a)+(1−ph) v∗(s−a)], v_*(s) = \max_{1 \le a \le \min(s, 100 - s)} \bigl[p_h\, v_*(s + a) + (1 - p_h)\, v_*(s - a)\bigr], (4.9)

with v∗(0)=0v_*(0) = 0 and v∗(100)=1v_*(100) = 1.

Every policy is proper — a run of at most 9999 heads ends any episode — so the conclusions of Chapter 3 hold with γ=1\gamma = 1. For a subfair coin, ph<1/2p_h < 1/2, Dubins and Savage proved that bold play, staking min⁡(s,100−s)\min(s, 100 - s), is optimal: an unfavorable game should be played as few times as possible.Lester E. Dubins and Leonard J. Savage. How to Gamble If You Must: Inequalities for Stochastic Processes. McGraw-Hill Series in Probability and Statistics. McGraw-Hill, New York, 1965 Bold play’s own value solves a linear system, v(s)=phv(2s)v(s) = p_h v(2s) for s≤50s \le 50 and v(s)=ph+(1−ph)v(2s−100)v(s) = p_h + (1 - p_h) v(2s - 100) for s≥50s \ge 50, so we can obtain v∗v_* in exact rational arithmetic and then test every action against it.

Remark 4.12. The book also allows the stake a=0a = 0. That action leaves the state unchanged with reward 00, so it attains the maximum in (4.9) at every state — yet a policy that uses it can stall forever. This is the trap of the remark after Theorem 3.5: with γ=1\gamma = 1, greedy with respect to v∗v_* is safe only among proper policies. We exclude a=0a = 0.

On a smaller instance — goal 88 and ph=2/5p_h = 2/5 — the exact computation fits in two small tables and shows the ties among optimal actions.

Bold play’s value for goal 88 in rationals, and for each capital s=1,…,7s = 1, \dots, 7 the column (s, v∗(s), max⁡aq(s,a)−v∗(s))\bigl(s,\ v_*(s),\ \max_a q(s, a) - v_*(s)\bigr). The residual row is 00, so bold play solves (4.9).
const boldValue = await wl`Function[{n, ph}, Module[{v, eqs},
  eqs = Table[v[s] == If[s <= n - s, ph v[2 s], ph + (1 - ph) v[2 s - n]], {s, 1, n - 1}] /. {v[0] -> 0, v[n] -> 1};
  Join[{0}, Table[v[s], {s, 1, n - 1}] /. First[Solve[eqs, Table[v[s], {s, 1, n - 1}]]], {1}]]]`
const gamblerV = await wl`${boldValue}[8, 2/5]`
const gamblerQ = await wl`Table[Table[2/5 ${gamblerV}[[s + a + 1]] + 3/5 ${gamblerV}[[s - a + 1]], {a, 1, Min[s, 8 - s]}], {s, 1, 7}]`
const gamblerTable = await show`Transpose[Table[{s, ${gamblerV}[[s + 1]], Max[${gamblerQ}[[s]]] - ${gamblerV}[[s + 1]]}, {s, 1, 7}]]`
sv∗(s)max⁡aq(s,a)−v∗(s)  =  (123456781254253212525621251625981250000000) \begin{matrix} s \\ v_*(s) \\ \max_a q(s, a) - v_*(s) \end{matrix} \;=\; \left( \begin{array}{ccccccc} 1 & 2 & 3 & 4 & 5 & 6 & 7 \\ \frac{8}{125} & \frac{4}{25} & \frac{32}{125} & \frac{2}{5} & \frac{62}{125} & \frac{16}{25} & \frac{98}{125} \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ \end{array} \right) All the stakes that attain the maximum, for s=1,…,7s = 1, \dots, 7: at s=3s = 3 and s=5s = 5 the timid stake 11 ties with the bold stake 33.
const gamblerStakes = await show`Table[Flatten[Position[${gamblerQ}[[s]], Max[${gamblerQ}[[s]]]]], {s, 1, 7}]`
(argmax⁡aq(s,a))s=1,…,7={{1},{2},{1,3},{4},{1,3},{2},{1}} \bigl(\operatorname{argmax}_a q(s, a)\bigr)_{s = 1, \dots, 7} = \{\{1\},\{2\},\{1,3\},\{4\},\{1,3\},\{2\},\{1\}\}
Left: value against capital from 1 to 99; three gray staircase curves for the first three sweeps of value iteration lie below a red curve for the exact optimal value, which rises irregularly from 0 to 1 with jumps of slope at 25, 50 and 75. Right: dots for every optimal stake at every capital, forming a large tent peaking at stake 50 at capital 50, with smaller nested tents peaking at capitals 25 and 75 and smaller still at 12.5 multiples.
Figure 4.1: The gambler’s problem with goal 100100 and ph=0.4p_h = 0.4. Left: synchronous value-iteration sweeps 11, 22, 33 from v0=0v_0 = 0 (gray, lightest first) and the exact v∗v_* (red), the value of bold play obtained in rational arithmetic. Right: every optimal stake at every capital, found by exact comparison of all actions with v∗v_*. Bold play is the outer tent; the self-similar inner tents are the ties, so the family of optimal policies is large, and which member value iteration reports depends on how its round-off breaks the ties.

The figure answers the book’s question about the shape of the family of optimal policies. v∗v_* is self-similar: by the equations above, its graph on [0,50][0, 50] is a copy of the whole graph scaled by php_h, which is why the optimal stakes repeat at capitals 2525, 7575, 12.512.5, and so on. Numerically computed policies look jagged because floating-point value iteration breaks these exact ties arbitrarily.

4.6Asynchronous Dynamic Programming

A sweep over 102010^{20} backgammon positions would take millennia. Asynchronous DP drops the sweep: it updates the values of states in any order, in place, using whatever values the other states currently have, possibly updating some states many times before others once.Dimitri P. Bertsekas. Distributed Dynamic Programming. IEEE Transactions on Automatic Control, 27(3):610–616, 1982. doi: 10.1109/TAC.1982.1102980; and Dimitri P. Bertsekas and John N. Tsitsiklis. Parallel and Distributed Computation: Numerical Methods. Prentice-Hall, Englewood Cliffs, NJ, 1989

Proposition 4.13 (Convergence of Asynchronous Value Iteration). Let γ<1\gamma < 1 and apply the update (4.7) in place to one state sks_k at a time. If every state appears infinitely often in the sequence s0,s1,…s_0, s_1, \dots, then V→v∗V \to v_*.

Proof. Let e=∥V−v∗∥∞e = \lVert V - v_* \rVert_\infty before some update. Updating ss replaces V(s)V(s) by (T∗V)(s)(T_* V)(s), whose distance to v∗(s)=(T∗v∗)(s)v_*(s) = (T_* v_*)(s) is at most γe\gamma e; other components do not change. So ee never increases, and once every state has been updated at least once since a moment when the error was ee, all components have error at most γe\gamma e. Divide time into consecutive intervals in each of which every state is updated; there are infinitely many, and after mm of them the error is at most γme0\gamma^m e_0. □

Mixing evaluation updates with optimality updates in the same way gives asynchronous versions of truncated policy iteration. Avoiding sweeps does not reduce the total work needed for a guarantee, but it removes the commitment to one long sweep, and it lets us choose where to compute. In particular, DP can run while an agent acts, updating the states the agent actually visits and feeding the latest greedy policy back into its behavior. This focusing on relevant states is developed in Chapter 8 (prioritized sweeping, real-time DP, trajectory sampling).

4.7Generalized Policy Iteration

Policy iteration consists of two interacting processes. Evaluation makes the value function consistent with the current policy, pushing VV toward vπv_\pi. Improvement makes the policy greedy with respect to the current value function, pushing π\pi toward greedy⁡(V)\operatorname{greedy}(V). Policy iteration runs each to completion before starting the other; value iteration runs a single sweep of evaluation between improvements; asynchronous DP interleaves them state by state. In all cases the result is the same. Generalized policy iteration (GPI) is the name for this whole family: any scheme in which an approximate policy and an approximate value function are kept and each is repeatedly moved toward the target defined by the other, whatever the granularity.

Why must GPI end at optimality? Because its only resting points are optimal.

Proposition 4.14 (The Fixed Points of GPI). For a value function VV and a policy π\pi, the following are equivalent: (a) neither process changes anything, that is, V=vπV = v_\pi and π\pi is greedy with respect to VV; (b) V=v∗V = v_* and π\pi is optimal.

Proof. (a) ⇒ (b): V=TπVV = T_\pi V because V=vπV = v_\pi, and TπV=T∗VT_\pi V = T_* V because π\pi is greedy; so V=T∗VV = T_* V, hence V=v∗V = v_* and π\pi is optimal by Theorem 3.5. (b) ⇒ (a): if π\pi is optimal then Tπv∗=Tπvπ=vπ=v∗=T∗v∗T_\pi v_* = T_\pi v_\pi = v_\pi = v_* = T_* v_*, so π\pi puts weight only on maximizing actions, that is, it is greedy with respect to v∗v_*. □

The two processes both compete and cooperate. They compete because each spoils the other’s target: making π\pi greedy changes vπv_\pi, so VV is no longer accurate; making VV accurate usually changes which actions look greedy. They cooperate because, by the improvement theorem, each greedy step can only raise the true value, so the pair drifts toward the single point where both targets coincide. Figure 4.2 draws this in the value plane of the recycling robot, where both extremes of GPI can be seen at once.

Figure 4.2: Generalized policy iteration in the value plane (v(𝗁𝗂𝗀𝗁),v(𝗅𝗈𝗐))(v(\mathsf{high}), v(\mathsf{low})) of the recycling robot (α=0.8\alpha = 0.8, β=0.6\beta = 0.6, rsearch=2r_{\text{search}} = 2, rwait=1r_{\text{wait}} = 1, γ=0.9\gamma = 0.9). Black points: vπv_\pi for the six deterministic policies. Red: policy iteration from “wait, wait”, which jumps between policy values and stops after two improvements. Blue: value iteration from 00, whose iterates are not the values of any policy and approach v∗v_* geometrically at rate γ\gamma. The thin gray line is the diagonal.Plane with axes v(high) and v(low). Six black points mark the values of the six deterministic policies, labeled by their actions; search-recharge is at the upper right near (16.9, 15.3). Red arrows go from wait-wait at (10, 10) to search-wait at (13.6, 10) and then to search-recharge. A blue polyline of small dots starts at the origin and climbs in shrinking steps to the same upper-right point.

GPI is the lens for the rest of the book. Every method keeps a policy and a value function and alternates, at some granularity, between evaluating and improving; they differ in how the evaluation is done and how greedy the improvement is.

methodevaluationimprovement
policy and value iteration (this chapter)expected updates with the modelexact greedy
Monte Carlo control (Chapter 5)averages of sampled returnsgreedy or ε\varepsilon-greedy, per episode
Sarsa (Chapter 6)one sampled, bootstrapped update per stepε\varepsilon-greedy with respect to the current QQ
Q-learning (Chapter 6)sampled update toward max⁡a′Q\max_{a'} Q — sampled value iterationimplicit in the max
Dyna (Chapter 8)real and simulated updatesε\varepsilon-greedy
actor–critic (Chapter 13)TD learning of v^\hat v (critic)a small gradient step on the policy (actor)

DP has one more property that the table hides. Its updates estimate the value of a state from estimates of the values of successor states; this is bootstrapping. Monte Carlo methods do not bootstrap and do not need a model; TD methods bootstrap without a model. Whether a method uses a model (expected updates) or samples, and whether it bootstraps, are separate choices, and the next chapters explore the combinations. With function approximation, finally, the evaluation step can no longer represent vπv_\pi exactly, and the guarantees of this section are lost; Section 11.3 explains when GPI can then diverge.

4.8Efficiency and Linear Programming

DP is often dismissed as impractical, but compared with the alternatives it is efficient. One sweep of value iteration costs O(∣𝒮∣2∣𝒜∣)O(\lvert\mathcal{S}\rvert^2\lvert\mathcal{A}\rvert) operations, one exact policy evaluation O(∣𝒮∣3)O(\lvert\mathcal{S}\rvert^3), and the number of iterations needed is polynomial in ∣𝒮∣\lvert\mathcal{S}\rvert, ∣𝒜∣\lvert\mathcal{A}\rvert, 1/(1−γ)1/(1 - \gamma) and the number of bits of the input,Michael L. Littman, Thomas L. Dean, and Leslie Pack Kaelbling. On the Complexity of Solving Markov Decision Problems. In Proceedings of the Eleventh Conference on Uncertainty in Artificial Intelligence (UAI 1995), pages 394–402, 1995 whereas there are ∣𝒜∣∣𝒮∣\lvert\mathcal{A}\rvert^{\lvert\mathcal{S}\rvert} deterministic policies to search through. What makes large problems hard is the curse of dimensionality — Bellman’s phrase for the exponential growth of ∣𝒮∣\lvert\mathcal{S}\rvert with the number of state variables — which is a property of the problems, not a weakness of DP. Today DP handles MDPs with millions of states; beyond that, asynchronous and sample-based methods take over.

4.8.1The Linear Programming Formulation

MDPs can also be solved by linear programming, an observation as old as policy iteration: Manne formulated the average-cost problem as a linear program in 1960, and d’Epenoux the discounted one.Alan S. Manne. Linear Programming and Sequential Decisions. Management Science, 6(3):259–267, 1960. doi: 10.1287/mnsc.6.3.259; and F. d’Epenoux. A Probabilistic Production and Inventory Problem. Management Science, 10(1):98–108, 1963. doi: 10.1287/mnsc.10.1.98 Fix weights μ(s)>0\mu(s) > 0 and consider

min⁡v∑sμ(s) v(s)subject tov(s)≥r(s,a)+γ∑s′p(s′∣s,a) v(s′)for all s,a. \begin{aligned} &\min_{v} \sum_s \mu(s)\, v(s) \quad \text{subject to} \\ &\quad v(s) \ge r(s, a) + \gamma\sum_{s'} p(s' \mid s, a)\, v(s') \quad \text{for all } s, a. \end{aligned} (4.10)

Proposition 4.15 (The LP Solution is v∗v_*). For γ<1\gamma < 1, the unique solution of (4.10) is v∗v_*.

Proof. The constraints say v≥T∗vv \ge T_* v. If π∗\pi_* is a deterministic optimal policy, then v≥T∗v≥Tπ∗vv \ge T_* v \ge T_{\pi_*} v, and by monotonicity v≥Tπ∗kv→v∗v \ge T_{\pi_*}^k v \to v_*. So every feasible vv is ≥v∗\ge v_* componentwise, while v∗v_* itself is feasible. Any other feasible vv exceeds v∗v_* in some component, and since μ>0\mu > 0 its objective is strictly larger. □

The dual LP has one variable x(s,a)≥0x(s, a) \ge 0 per state–action pair, maximizes ∑s,ax(s,a) r(s,a)\sum_{s,a} x(s, a)\, r(s, a) subject to ∑ax(s′,a)−γ∑s,ap(s′∣s,a) x(s,a)=μ(s′)\sum_a x(s', a) - \gamma\sum_{s, a} p(s' \mid s, a)\, x(s, a) = \mu(s'), and its feasible points are exactly the discounted occupancy measures x(s,a)=∑tγtPr⁡{St=s,At=a}x(s, a) = \sum_t \gamma^t \Pr\{S_t = s, A_t = a\} of policies started from μ\mu; an optimal dual solution is supported on optimal actions, and π(a∣s)∝x(s,a)\pi(a \mid s) \propto x(s, a) recovers an optimal policy. LP solvers have better worst-case guarantees than DP on some classes of problems but become impractical much sooner as ∣𝒮∣\lvert\mathcal{S}\rvert grows. The formulation matters more as a theoretical bridge: the dual variables are the state–action visitation distributions that reappear in off-policy learning and in policy-gradient theory.

The LP (4.10) for the recycling robot with μ=(1,1)\mu = (1, 1): the primal minimizer is v∗=(1000/59,900/59)v_* = (1000/59, 900/59), and the dual is positive only on (𝗁𝗂𝗀𝗁,𝗌𝖾𝖺𝗋𝖼𝗁)(\mathsf{high}, \mathsf{search}) and (𝗅𝗈𝗐,𝗋𝖾𝖼𝗁𝖺𝗋𝗀𝖾)(\mathsf{low}, \mathsf{recharge}), the optimal pairs; its total mass is 2/(1−γ)=202/(1 - \gamma) = 20.
const rbLP = await show`LinearOptimization[x + y, Flatten[Table[{x, y}[[s]] >= ${rbR}[[s, a]] + 9/10 ${rbP}[[s, a]] . {x, y}, {s, 2}, {a, Length[${rbR}[[s]]]}]], {x, y}, {"PrimalMinimizer", "DualMaximizer"}]`
(primal, dual)={{100059,90059},{{95059,0,0,0,23059},{}}} (\text{primal},\ \text{dual}) = \left\{\left\{\frac{1000}{59},\frac{900}{59}\right\},\left\{\left\{\frac{950}{59},0,0,0,\frac{230}{59}\right\},\{\}\right\}\right\}

4.9Beyond the Book: The Complexity of Policy Iteration

How many iterations does policy iteration need? Theorem 4.8 gives the exponential bound ∏s∣𝒜(s)∣\prod_s \lvert\mathcal{A}(s)\rvert, practice suggests a handful, and for decades the truth was open.Beyond Sutton and Barto (2018). The strongly polynomial bounds for policy iteration (Ye 2011; Hansen, Miltersen and Zwick 2013; Scherrer 2016) and anchored value iteration (Lee and Ryu 2023) are not discussed in the book, whose Section 4.7 says only that DP is polynomial in the numbers of states and actions. Write n=∣𝒮∣n = \lvert\mathcal{S}\rvert and m=∑s∣𝒜(s)∣m = \sum_s \lvert\mathcal{A}(s)\rvert for the total number of actions.

Polynomial, but not strongly. Value iteration finds an ε\varepsilon-optimal policy in fewer than (B+log⁡(1/ε)+log⁡(1/(1−γ))+1)/(1−γ)(B + \log(1/\varepsilon) + \log(1/(1-\gamma)) + 1)/(1 - \gamma) iterations, where BB is the number of bits of the input,Michael L. Littman, Thomas L. Dean, and Leslie Pack Kaelbling. On the Complexity of Solving Markov Decision Problems. In Proceedings of the Eleventh Conference on Uncertainty in Artificial Intelligence (UAI 1995), pages 394–402, 1995 and Proposition 4.9 transfers the same rate to policy iteration. Such bounds depend on the size of the numbers, not only on nn and mm; an algorithm whose number of arithmetic operations is bounded by a polynomial in nn and mm alone is called strongly polynomial.

Policy iteration is strongly polynomial for a fixed discount. Ye proved that Howard’s policy iteration, and the simplex method with Dantzig’s pivoting rule on the LP of (4.10), terminate after O(nm1−γlog⁡n1−γ)O\bigl(\tfrac{nm}{1-\gamma}\log\tfrac{n}{1-\gamma}\bigr) iterations.Yinyu Ye. The Simplex and Policy-Iteration Methods Are Strongly Polynomial for the Markov Decision Problem with a Fixed Discount Rate. Mathematics of Operations Research, 36(4):593–603, 2011. doi: 10.1287/moor.1110.0516 Hansen, Miltersen and Zwick improved the bound for policy iteration to O(m1−γlog⁡n1−γ)O\bigl(\tfrac{m}{1-\gamma}\log\tfrac{n}{1-\gamma}\bigr), and showed that the same strategy iteration solves two-player turn-based discounted stochastic games in strongly polynomial time for fixed γ\gamma.Thomas Dueholm Hansen, Peter Bro Miltersen, and Uri Zwick. Strategy Iteration Is Strongly Polynomial for 2-Player Turn-Based Stochastic Games with a Constant Discount Factor. Journal of the ACM, 60(1):1:1–1:16, 2013. doi: 10.1145/2432622.2432623 Scherrer removed the nn from the logarithm: policy iteration stops within (m−n)⌈11−γlog⁡11−γ⌉(m - n)\bigl\lceil \tfrac{1}{1-\gamma}\log\tfrac{1}{1-\gamma}\bigr\rceil iterations.Bruno Scherrer. Improved and Generalized Upper Bounds on the Complexity of Policy Iteration. Mathematics of Operations Research, 41(3):758–774, 2016. doi: 10.1287/moor.2015.0753 The common idea is an elimination argument. By Proposition 4.9 the error v∗−vπkv_* - v_{\pi_k} shrinks by γ\gamma per iteration; by the performance difference lemma, every action that a policy still uses carries part of that error as negative advantage q∗(s,a)−v∗(s)q_*(s, a) - v_*(s). After about 11−γlog⁡11−γ\tfrac{1}{1-\gamma}\log\tfrac{1}{1-\gamma} iterations the error is too small for the worst action in use to remain in use, and it is never chosen again; there are at most m−nm - n non-optimal actions to eliminate. The factor 1/(1−γ)1/(1 - \gamma) cannot simply be dropped: for undiscounted total-reward and average-reward MDPs, Fearnley built instances on which Howard’s policy iteration takes exponentially many iterations.John Fearnley. Exponential Lower Bounds for Policy Iteration. In Samson Abramsky, Cyril Gavoille, Claude Kirchner, Friedhelm Meyer auf der Heide, and Paul G. Spirakis, editors, Automata, Languages and Programming (ICALP 2010), Part II, volume 6199 of Lecture Notes in Computer Science, pages 551–562. Springer, 2010. doi: 10.1007/978-3-642-14162-1_46

Faster value iteration. Value iteration’s Bellman error ∥T∗vk−vk∥∞\lVert T_* v_k - v_k \rVert_\infty decays like γk\gamma^k, which is slow when γ\gamma is close to 11. Lee and Ryu’s anchored value iteration mixes every iterate with the starting point,

vk=βkv0+(1−βk) Tvk−1,βk=(∑i=0kγ−2i)−1, v_k = \beta_k v_0 + (1 - \beta_k)\, T v_{k-1}, \qquad \beta_k = \Bigl(\sum_{i=0}^{k} \gamma^{-2i}\Bigr)^{-1}, (4.11)

with T=TπT = T_\pi or T∗T_*, and achieves a Bellman error of order (2k+1+(1−γ))∥v0−vfix∥∞\bigl(\tfrac{2}{k+1} + (1 - \gamma)\bigr)\lVert v_0 - v_{\text{fix}} \rVert_\infty — an O(1/k)O(1/k) rate that does not degrade as γ→1\gamma \to 1 and holds even for γ=1\gamma = 1 when v0≤Tv0v_0 \le T v_0.Jongmin Lee and Ernest K. Ryu. Accelerating Value Iteration with Anchoring. In A. Oh, T. Naumann, A. Globerson, K. Saenko, M. Hardt, and S. Levine, editors, Advances in Neural Information Processing Systems, volume 36, pages 53924–53963. Curran Associates, Inc., 2023. doi: 10.52202/075280-2346 They also prove a lower bound showing that no method whose iterates lie in the span of the past Bellman residuals can do better than their rate by more than a factor of 44.

Linear programming with features. The LP of Section 4.8.1 has one variable per state. De Farias and Van Roy restricted it to v=𝚽𝐰v = \boldsymbol{\Phi}\mathbf{w} for a feature matrix 𝚽\boldsymbol{\Phi} with few columns — the approximate linear program, minimize 𝐜⊤𝚽𝐰\mathbf{c}^\top\boldsymbol{\Phi}\mathbf{w} subject to 𝚽𝐰≥T∗𝚽𝐰\boldsymbol{\Phi}\mathbf{w} \ge T_*\boldsymbol{\Phi}\mathbf{w} — and proved that if the constant function is representable and 𝐜\mathbf{c} is a probability distribution, the solution satisfies ∥v∗−𝚽𝐰~∥1,𝐜≤21−γmin⁡𝐰∥v∗−𝚽𝐰∥∞\lVert v_* - \boldsymbol{\Phi}\tilde{\mathbf{w}} \rVert_{1, \mathbf{c}} \le \tfrac{2}{1-\gamma}\min_{\mathbf{w}}\lVert v_* - \boldsymbol{\Phi}\mathbf{w} \rVert_\infty, with sharper bounds through Lyapunov-weighted norms.Daniela Pucci de Farias and Benjamin Van Roy. The Linear Programming Approach to Approximate Dynamic Programming. Operations Research, 51(6):850–865, 2003. doi: 10.1287/opre.51.6.850.24925 The weights 𝐜\mathbf{c} play the role of the state distribution μ\mu of Chapter 9: the approximation is good where the weights are large. The number of constraints is still mm, so in practice they are sampled.

Every method of this chapter, exact or approximate, still takes the model pp as given and sweeps it with expected updates. Chapter 5 drops the model: it estimates the same value functions from sampled episodes of experience, and keeps generalized policy iteration as the way to turn them into better policies.

4.10Exercises

Exercise 4.16. In the small gridworld under the random policy, what are qπ(11,𝖽𝗈𝗐𝗇)q_\pi(11, \mathsf{down}) and qπ(7,𝖽𝗈𝗐𝗇)q_\pi(7, \mathsf{down})?

Solution

By the second relation of (3.8), qπ(s,a)=−1+vπ(s′)q_\pi(s, a) = -1 + v_\pi(s'). Moving down from cell 1111 reaches the terminal state, so qπ(11,𝖽𝗈𝗐𝗇)=−1q_\pi(11, \mathsf{down}) = -1; moving down from 77 reaches 1111, so qπ(7,𝖽𝗈𝗐𝗇)=−1+vπ(11)=−15q_\pi(7, \mathsf{down}) = -1 + v_\pi(11) = -15. In the kernel’s numbering the cell kk of the book is index k+1k + 1.

−1+vπ(next state)-1 + v_\pi(\text{next state}) for the two pairs, read off the exact vπv_\pi.
const exQ = await show`{-1 + ${g4random}[[${g4next}[12, {1, 0}]]], -1 + ${g4random}[[${g4next}[8, {1, 0}]]]}`
(qπ(11,𝖽𝗈𝗐𝗇), qπ(7,𝖽𝗈𝗐𝗇))={−1,−15} \bigl(q_\pi(11, \mathsf{down}),\ q_\pi(7, \mathsf{down})\bigr) = \{-1,-15\}

Exercise 4.17. Add a state 1515 below cell 1313 of the small gridworld, whose moves 𝗅𝖾𝖿𝗍,𝗎𝗉,𝗋𝗂𝗀𝗁𝗍,𝖽𝗈𝗐𝗇\mathsf{left}, \mathsf{up}, \mathsf{right}, \mathsf{down} lead to 12,13,14,1512, 13, 14, 15. (a) If the old transitions are unchanged, what is vπ(15)v_\pi(15) for the random policy? (b) What if, in addition, 𝖽𝗈𝗐𝗇\mathsf{down} from 1313 now leads to 1515?

Solution

(a) No old state can reach 1515, so the old values stand and v(15)=−1+14(v(12)+v(13)+v(14)+v(15))=−1+14(−22−20−14+v(15))v(15) = -1 + \tfrac{1}{4}\bigl(v(12) + v(13) + v(14) + v(15)\bigr) = -1 + \tfrac{1}{4}(-22 - 20 - 14 + v(15)), giving v(15)=−20v(15) = -20. (b) Now 1515 feeds back into 1313, so in principle all values change; but the old vπv_\pi together with v(15)=−20v(15) = -20 still satisfies every equation, because from 1313 the new successor 1515 has the same value −20-20 as the old successor 1313 itself. By uniqueness (Proposition 3.4) nothing changes.

Case (b) solved from scratch: the 17-index system with the new state at index 1717 and 𝖽𝗈𝗐𝗇\mathsf{down} from cell 1313 (index 1414) redirected to it; we print v(13)v(13) and v(15)v(15).
const exState15 = await show`Module[{next, P, r, v},
  next = Function[{k, a}, Which[
    k == 17, {13, 14, 15, 17}[[Position[{{0, -1}, {-1, 0}, {0, 1}, {1, 0}}, a][[1, 1]]]],
    k == 14 && a == {1, 0}, 17,
    True, ${g4next}[k, a]]];
  P = Table[If[k == 1 || k == 16, ConstantArray[0, 17], Sum[UnitVector[17, next[k, a]]/4, {a, ${g4moves}}]], {k, 17}];
  P[[All, {1, 16}]] = 0;
  r = Table[If[k == 1 || k == 16, 0, -1], {k, 17}];
  v = LinearSolve[IdentityMatrix[17] - P, r];
  {v[[14]], v[[17]]}]`
(vπ(13), vπ(15))={−20,−20} \bigl(v_\pi(13),\ v_\pi(15)\bigr) = \{-20,-20\}

Exercise 4.18. For goal 100100 and ph=0.4p_h = 0.4, a policy found by value iteration stakes everything at capital 5050 but only 11 at capital 5151. Why is this a good policy?

Solution

The game is subfair, so each additional bet costs probability in expectation; the best one can do from 5050 is a single all-or-nothing bet, which wins with probability ph=v∗(50)p_h = v_*(50). From 5151, the bold stake 4949 wins outright with probability php_h and otherwise leaves 22. The stake 11 moves to 5252 or back to 5050; from 5050 the gambler then bets bold. Exact arithmetic shows that the two choices, and with them the whole family of tied stakes in Figure 4.1, give exactly the same probability of success, v∗(51)≈0.4031v_*(51) \approx 0.4031, larger than php_h.

v∗(51)v_*(51) exactly, and the differences q∗(51,1)−v∗(51)q_*(51, 1) - v_*(51) and q∗(51,49)−v∗(51)q_*(51, 49) - v_*(51), for goal 100100 and ph=2/5p_h = 2/5.
const exGambler = await show`With[{v = ${boldValue}[100, 2/5], ph = 2/5}, {v[[52]], ph v[[53]] + (1 - ph) v[[51]] - v[[52]], ph v[[101]] + (1 - ph) v[[3]] - v[[52]]}]`
(v∗(51), q∗(51,1)−v∗(51), q∗(51,49)−v∗(51))={9610609569234342384184279361225,0,0} \begin{aligned} &\bigl(v_*(51),\ q_*(51, 1) - v_*(51),\ q_*(51, 49) - v_*(51)\bigr) \\ &= \left\{\frac{961060956923434}{2384184279361225},0,0\right\} \end{aligned}

Exercise 4.19. Write policy evaluation, policy iteration, and value iteration for action values qk(s,a)q_k(s, a), and explain what is gained by working with QQ instead of VV.

Solution

Evaluation iterates (3.10): qk+1(s,a)=∑s′,rp(s′,r∣s,a)[r+γ∑a′π(a′∣s′) qk(s′,a′)]q_{k+1}(s, a) = \sum_{s', r} p(s', r \mid s, a)\bigl[r + \gamma\sum_{a'}\pi(a' \mid s')\, q_k(s', a')\bigr]. Value iteration iterates (3.18): qk+1(s,a)=∑s′,rp(s′,r∣s,a)[r+γmax⁡a′qk(s′,a′)]q_{k+1}(s, a) = \sum_{s', r} p(s', r \mid s, a)\bigl[r + \gamma\max_{a'} q_k(s', a')\bigr]; both are γ\gamma-contractions in the max norm over state–action pairs, by the proof of Lemma 4.1. Policy iteration alternates evaluation of qπq_\pi with π′(s)=argmax⁡aqπ(s,a)\pi'(s) = \operatorname{argmax}_a q_\pi(s, a), keeping the current action on ties. The gain is that the improvement step needs no model: the one-step lookahead has already been done and stored in QQ. When the expectations over pp in the evaluation step are in turn replaced by samples, these become Sarsa and Q-learning (Chapter 6).

Exercise 4.20. Suppose only ε\varepsilon-soft policies are allowed, with π(a∣s)≥ε/∣𝒜(s)∣\pi(a \mid s) \ge \varepsilon/\lvert\mathcal{A}(s)\rvert for all s,as, a. How must steps 3, 2 and 1 of policy iteration change, and what does the algorithm converge to?

Solution

Step 3 (improvement): instead of the greedy action, choose the ε\varepsilon-greedy policy, with probability 1−ε+ε/∣𝒜(s)∣1 - \varepsilon + \varepsilon/\lvert\mathcal{A}(s)\rvert on a maximizing action and ε/∣𝒜(s)∣\varepsilon/\lvert\mathcal{A}(s)\rvert on each other action; the stopping test compares these policies. Step 2 (evaluation): evaluate the stochastic policy, averaging over π(a∣s)\pi(a \mid s) as in (4.3). Step 1 (initialization): start from an ε\varepsilon-soft policy, such as the uniform one. The ε\varepsilon-greedy step satisfies (4.5) relative to any ε\varepsilon-soft π\pi, because it shifts all the freely assignable probability 1−ε1 - \varepsilon to a best action, so the theorem still applies; the algorithm converges to the best policy among ε\varepsilon-soft ones, which is generally not optimal in the unrestricted sense. This is the policy improvement step of on-policy Monte Carlo control in Chapter 5.