Simple Algorithm
Artificial Intelligence · Deep Layer · 26 min

Dynamic Programming in RL

Policy evaluation, improvement and iteration.

Value iteration and policy iteration are the two ends of a spectrum. Between them lies most of practical reinforcement learning: evaluate a little, improve a little, repeat, in whatever order and with whatever errors your samples and function approximators impose. This lesson makes the spectrum precise: modified and asynchronous policy iteration, why any fair interleaving still converges, and what approximation errors cost, including why the famous 2γε/(1−γ)22\gamma\varepsilon/(1-\gamma)^2 bound is tight and how to beat it.

What you’ll learn

  • State generalized policy iteration, and prove that modified policy iteration from a pessimistic start improves monotonically and is never behind value iteration.
  • Prove that asynchronous value iteration converges whenever every state is backed up infinitely often, and explain Gauss–Seidel ordering, prioritized sweeping and real-time dynamic programming.
  • Derive the error bounds of approximate value iteration and approximate policy iteration, and explain their tightness and the improvement from non-stationary policies.
  • Show how least-squares fitted value iteration can diverge, which approximators are safe, and how concentrability coefficients replace sup-norm assumptions.

Generalized and modified policy iteration

Sutton and Barto call the common pattern generalized policy iteration (GPI): an evaluation process pulls VV toward VπV^\pi, an improvement process makes π\pi greedy for VV, and the two interleave at any granularity. Value iteration improves after a single backup, policy iteration after exact evaluation, and actor–critic methods and Q-learning interleave the two sample by sample. Modified policy iteration (MPI; Puterman & Shin, 1978) turns the granularity into a parameter mm: evaluate the current greedy policy with mm backups.

text
function MODIFIED-POLICY-ITERATION(MDP, m, V0):  # require T V0 ≥ V0, e.g. V0 = min r / (1 − γ)
    V ← V0
    repeat
        π ← a greedy policy for V                 # improvement: T^π V = T V
        repeat m times:  V ← T^π V                 # partial evaluation
    until the largest change in V is small
    return π, V
# m = 1 is value iteration; m → ∞ is policy iteration

An iteration costs one greedy step, O(∣S∣2∣A∣)\order{|\mathcal S|^2|\mathcal A|}, plus mm backups of a fixed policy, O(∣S∣2)\order{|\mathcal S|^2} each and less with sparse transitions. A moderate mm often beats both extremes.

DerivationProof by a monotone sandwich
  1. Vk≤TπkVk≤(Tπk)2Vk≤⋯≤(Tπk)mVk=Vk+1V_k\le T^{\pi_k}V_k\le\big(T^{\pi_k}\big)^2V_k\le\cdots\le\big(T^{\pi_k}\big)^mV_k=V_{k+1}

    Invariant: TVk≥VkTV_k\ge V_k. Suppose it holds at kk. Greediness gives TπkVk=TVk≥VkT^{\pi_k}V_k=TV_k\ge V_k, and the monotone map TπkT^{\pi_k} propagates the inequality along its iterates, which therefore increase:

  2. Vk+1 ≥ TπkVk = TVkV_{k+1}\ \ge\ T^{\pi_k}V_k\ =\ TV_k

    Then TVk+1≥TπkVk+1=(Tπk)m+1Vk≥Vk+1TV_{k+1}\ge T^{\pi_k}V_{k+1}=(T^{\pi_k})^{m+1}V_k\ge V_{k+1}, so the invariant passes to k+1k+1. The same chain shows that one MPI iteration does at least as much as one value-iteration sweep:

  3. Uk ≤ Vk ≤ lim⁡j→∞(Tπk)jVk=Vπk ≤ V∗U_k\ \le\ V_k\ \le\ \lim_{j\to\infty}\big(T^{\pi_k}\big)^jV_k=V^{\pi_k}\ \le\ V^*

    Lower bound: by induction and the monotonicity of TT, Vk+1≥TVk≥TUk=Uk+1V_{k+1}\ge TV_k\ge TU_k=U_{k+1}. Upper bounds: the iterates of TπkT^{\pi_k} from VkV_k increase to their fixed point VπkV^{\pi_k}, and the iterates of TT from VkV_k increase to V∗V^*:

  4. ∥V∗−Vk∥∞≤∥V∗−Uk∥∞≤γk∥V∗−V0∥∞\norm{V^*-V_k}_\infty\le\norm{V^*-U_k}_\infty\le\gamma^k\norm{V^*-V_0}_\infty

    The rate follows from 0≤V∗−Vk≤V∗−Uk0\le V^*-V_k\le V^*-U_k and the contraction of TT. From an arbitrary start MPI still converges, but not monotonically, and with approximate evaluation, policy iteration with very partial ("optimistic") evaluation can oscillate (Bertsekas & Tsitsiklis, 1996).

Asynchronous dynamic programming

Nothing forces a sweep to update every state from the same old values. Gauss–Seidel value iteration updates states in place, so later updates in a sweep already use new values. Asynchronous DP updates one state at a time in any order, possibly on parallel processors with stale information (Bertsekas, 1982). Such schedules converge under a minimal fairness condition.

DerivationProof
  1. ∣(TVt)(s)−V∗(s)∣=∣(TVt)(s)−(TV∗)(s)∣≤γ∥Vt−V∗∥∞=γ et\abs{(TV_t)(s)-V^*(s)}=\abs{(TV_t)(s)-(TV^*)(s)}\le\gamma\norm{V_t-V^*}_\infty=\gamma\,e_t

    Let et=∥Vt−V∗∥∞e_t=\norm{V_t-V^*}_\infty. An update at ss reads the current vector, and V∗(s)=(TV∗)(s)V^*(s)=(TV^*)(s), so the new error at ss is at most γet\gamma e_t, while all other states are untouched:

  2. etj+1≤γ etj⟹etj≤γj e0e_{t_{j+1}}\le\gamma\,e_{t_j}\qquad\Longrightarrow\qquad e_{t_j}\le\gamma^j\,e_0

    So ete_t never increases. Within an interval that updates every state, each state's error after its last update is at most γ\gamma times the maximal error at that moment, hence at most γetj\gamma e_{t_j}, and states are untouched otherwise. At the end of the interval every state has been updated:

  3. If every state is updated infinitely often, infinitely many such intervals exist, so et→0e_t\to0. Only the sup-norm contraction was used, so the argument covers TπT^\pi, soft backups and Q-value backups alike, and with bounded delays it extends to stale information.

Predict before you look

A deterministic chain of 20 states leads to a goal that pays 1 on entry, and all values start at 0. How many synchronous (Jacobi) sweeps of value iteration pass before the state farthest from the goal has a nonzero value?

Show the answer

20

After kk synchronous sweeps, exactly the states within kk steps of the goal have nonzero values, so the farthest state needs 20. A Gauss–Seidel sweep that visits the states from the goal backward carries the reward along the whole chain in one sweep. Ordering along the flow of value is what makes backward and prioritized sweeps fast, although in the worst case a Gauss–Seidel sweep still contracts by only γ\gamma.

Two refinements choose which states to update. Prioritized sweeping (Moore & Atkeson, 1993) keeps a priority queue keyed by each state's pending Bellman error and, after updating a state, raises the priority of its predecessors, so computation follows the changes. Real-time dynamic programming (RTDP; Barto, Bradtke & Singh, 1995) updates only the states visited by greedy trajectories from the start. For stochastic shortest-path problems with an optimistic (admissible) initial value function, RTDP converges to optimal values on the states reachable under optimal policies, possibly without ever touching the rest of the state space. Its deterministic special case is Korf's LRTA* (1990), which ties dynamic programming to heuristic search (Weighted, Anytime & Real-Time Search).

Check your understanding

Asynchronous value iteration updates one state per step. Which schedules are guaranteed to converge to V∗V^*?

  1. Any schedule, even one that eventually stops updating some state
  2. Only the fixed cyclic order s1,s2,…,sn,s1,…s_1,s_2,\dots,s_n,s_1,\dots
  3. Any schedule that updates every state infinitely often
  4. Only schedules that update states in decreasing order of their current values
Show the answer

C. Any schedule that updates every state infinitely often

Each update shrinks that state's error to at most γ\gamma times the current maximal error, and the maximal error never grows, so every interval that touches all states shrinks it by a factor γ\gamma. Fairness is all that is needed; a state that is never updated again keeps its error forever.

Approximate dynamic programming: what errors cost

In large problems every backup is approximate: a regression fits Vk+1V_{k+1} to sampled targets for TVkTV_k, or a learned critic evaluates the current policy. The question is how per-iteration errors propagate to the quality of the policies produced.

DerivationProof of (a), and the key steps of (b)
  1. ∥Vk+1−V∗∥∞≤∥Vk+1−TVk∥∞+∥TVk−TV∗∥∞≤ε+γ∥Vk−V∗∥∞ ⟹ lim sup⁡k∥Vk−V∗∥∞≤ε1−γ\norm{V_{k+1}-V^*}_\infty\le\norm{V_{k+1}-TV_k}_\infty+\norm{TV_k-TV^*}_\infty\le\varepsilon+\gamma\norm{V_k-V^*}_\infty\ \Longrightarrow\ \limsup_k\norm{V_k-V^*}_\infty\le\frac{\varepsilon}{1-\gamma}

    (a) Contraction plus the per-iteration error, then unroll the recursion:

  2. Tk+1Vk≥Vk−2γε1⟹Vk+1−Vk=(I−γPk+1)−1(Tk+1Vk−Vk)≥−2γε1−γ1T_{k+1}V^k\ge V^k-2\gamma\varepsilon\mathbf 1\quad\Longrightarrow\quad V^{k+1}-V^k=\big(I-\gamma P_{k+1}\big)^{-1}\big(T_{k+1}V^k-V^k\big)\ge-\frac{2\gamma\varepsilon}{1-\gamma}\mathbf 1

    The greedy-loss bound ∥V∗−Vπ∥∞≤2γ1−γ∥V−V∗∥∞\norm{V^*-V^\pi}_\infty\le\frac{2\gamma}{1-\gamma}\norm{V-V^*}_\infty of MDPs: Value & Policy Iteration multiplies in the second horizon factor. (b) Write Vk=VπkV^k=V^{\pi_k}, Tk=TπkT_k=T^{\pi_k}, Pk=PπkP_k=P^{\pi_k} and Vk=Vk+ekV_k=V^k+e_k with ∥ek∥∞≤ε\norm{e_k}_\infty\le\varepsilon. Greediness for VkV_k, Tk+1Vk≥TkVkT_{k+1}V_k\ge T_kV_k, yields near-monotone improvement:

  3. V∗−Vk+1=Tπ∗V∗−Tπ∗Vk⏟= γPπ∗(V∗−Vk)+Tπ∗Vk−Tk+1Vk⏟≤ 2γε1+Tk+1Vk−Tk+1Vk+1⏟= γPk+1(Vk−Vk+1) ≤ 2γ2ε1−γ1V^*-V^{k+1}=\underbrace{T^{\pi^*}V^*-T^{\pi^*}V^k}_{=\,\gamma P^{\pi^*}(V^*-V^k)}+\underbrace{T^{\pi^*}V^k-T_{k+1}V^k}_{\le\,2\gamma\varepsilon\mathbf 1}+\underbrace{T_{k+1}V^k-T_{k+1}V^{k+1}}_{=\,\gamma P_{k+1}(V^k-V^{k+1})\,\le\,\frac{2\gamma^2\varepsilon}{1-\gamma}\mathbf 1}

    Greediness also gives Tk+1Vk≥Tπ∗VkT_{k+1}V_k\ge T^{\pi^*}V_k, hence Tπ∗Vk−Tk+1Vk≤2γε1T^{\pi^*}V^k-T_{k+1}V^k\le2\gamma\varepsilon\mathbf 1. Split the loss through these two facts:

  4. ∥V∗−Vk+1∥∞≤γ∥V∗−Vk∥∞+2γε1−γ ⟹ lim sup⁡k∥V∗−Vπk∥∞≤2γε(1−γ)2\norm{V^*-V^{k+1}}_\infty\le\gamma\norm{V^*-V^k}_\infty+\frac{2\gamma\varepsilon}{1-\gamma}\ \Longrightarrow\ \limsup_k\norm{V^*-V^{\pi_k}}_\infty\le\frac{2\gamma\varepsilon}{(1-\gamma)^2}

    Since V∗−Vk≥0V^*-V^k\ge0, taking sup norms gives a contraction with a constant input, whose limit is the bound:

One factor 1/(1−γ)1/(1-\gamma) accumulates errors over iterations, the other is the greedy step's sensitivity. Scherrer and Lesner (2012) argued that the guarantee is tight for stationary policies, and removed a factor by giving up stationarity: executing the last ℓ\ell greedy policies in a periodic loop improves the guarantee toward 2γε/(1−γ)2\gamma\varepsilon/(1-\gamma) as ℓ\ell grows. With γ=0.99\gamma=0.99 and ε=0.01\varepsilon=0.01 that is the difference between a worst-case loss of about 200 and about 2.

When fitted value iteration diverges

Sup-norm error bounds are an idealization. Function approximators fitted by least squares control an average error, and composing a least-squares projection with the Bellman operator can fail outright.

DerivationFitted value iteration can diverge (Tsitsiklis & Van Roy, 1996)
  1. wk+1=arg min⁡w[(w−γ⋅2wk)2+(2w−(1−ϵ) γ⋅2wk)2]w_{k+1}=\argmin_w\Big[\big(w-\gamma\cdot2w_k\big)^2+\big(2w-(1-\epsilon)\,\gamma\cdot2w_k\big)^2\Big]

    Two states with one feature each, ϕ(1)=1\phi(1)=1 and ϕ(2)=2\phi(2)=2, so the approximate values are ww and 2w2w. State 1 moves to state 2; state 2 stays put with probability 1−ϵ1-\epsilon and terminates otherwise; every reward is 0, so V∗=0V^*=0, which w=0w=0 represents exactly. Each iteration fits ww by least squares, with equal weights, to the backed-up targets (Sutton & Barto, §11.2):

  2. wk+1=6−4ϵ5 γ wkw_{k+1}=\frac{6-4\epsilon}{5}\,\gamma\,w_k

    Setting the derivative to zero, (w−2γwk)+2(2w−2(1−ϵ)γwk)=0(w-2\gamma w_k)+2\big(2w-2(1-\epsilon)\gamma w_k\big)=0, gives a linear recursion:

  3. ∣wk∣=(6−4ϵ5γ)k∣w0∣ ⟶ ∞\abs{w_k}=\Big(\frac{6-4\epsilon}{5}\gamma\Big)^k\abs{w_0}\ \longrightarrow\ \infty

    For γ>5/(6−4ϵ)\gamma>5/(6-4\epsilon), for example γ=0.9\gamma=0.9 with ϵ<0.1\epsilon<0.1, the weight grows geometrically from any w0≠0w_0\ne0, although every exact backup is a contraction and the target function is representable. The projection is a non-expansion in the uniform L2L_2 norm, the backup is a contraction in the sup norm, and their composition contracts in neither.

Two repairs are known. Averagers (Gordon, 1995), approximators whose output is a fixed convex combination of target values (state aggregation, kk-nearest-neighbor averaging, interpolation on a grid), are sup-norm non-expansions, so fitted value iteration with them remains a contraction.

More generally, LpL_p error propagation replaces the sup norm by weighted norms. If every regression has error at most ε\varepsilon in Lp(μ)L_p(\mu) under a sampling distribution μ\mu, the final policy's loss measured in Lp(ρ)L_p(\rho) is bounded by a term of the form 2γ(1−γ)2C1/pε\frac{2\gamma}{(1-\gamma)^2}C^{1/p}\varepsilon, plus one that vanishes with the number of iterations (Munos, 2007). The concentrability coefficient CC measures how much more probability the state distributions reachable by policies can put on a state than μ\mu does, so it is finite only if the data cover everything those policies can reach. Finite-sample versions (Munos & Szepesvári, 2008) turn regression error into sample complexity, and fitted Q-iteration with trees or neural networks (Ernst, Geurts & Wehenkel, 2005; Riedmiller, 2005) is the batch template that DQN's target network runs online (Deep Q-Networks).

Key takeaways

  • Generalized policy iteration interleaves evaluation and improvement at any granularity. Modified policy iteration with mm backups interpolates between value iteration (m=1m=1) and policy iteration (m→∞m\to\infty), and from a pessimistic start satisfies Uk≤Vk≤Vπk≤V∗U_k\le V_k\le V^{\pi_k}\le V^*.
  • Asynchronous dynamic programming converges whenever every state is updated infinitely often; good orderings (backward Gauss–Seidel, prioritized sweeping, RTDP from an admissible start) can save most of the work.
  • A per-iteration error ε\varepsilon costs up to 2γε/(1−γ)22\gamma\varepsilon/(1-\gamma)^2 in policy value for both approximate value and policy iteration. The bound is tight for stationary policies, and periodic non-stationary policies remove one horizon factor.
  • Least-squares fitted value iteration can diverge even when the target is representable; averagers are safe, and LpL_p bounds with concentrability coefficients make the role of data coverage explicit.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

Modified policy iteration evaluates each greedy policy with mm backups. Which algorithms are the cases m=1m=1 and m→∞m\to\infty?

  1. Policy iteration and value iteration
  2. Value iteration and policy iteration
  3. Q-learning and Monte Carlo evaluation
  4. Gauss–Seidel and Jacobi value iteration
Show the answer

B. Value iteration and policy iteration

With m=1m=1, one backup of the policy greedy for VV is TπV=TVT^\pi V=TV: value iteration. With m→∞m\to\infty, (Tπ)mV→Vπ(T^\pi)^mV\to V^\pi, exact evaluation: policy iteration. Everything in between is generalized policy iteration with partial evaluation.

Question 2

Approximate value iteration at γ=0.9\gamma=0.9 makes a sup-norm error of at most 0.010.01 in every backup. What limiting loss does the error-propagation theorem guarantee for the greedy policies?

  1. 0.0180.018
  2. 0.180.18
  3. 1.81.8
  4. 1818
Show the answer

C. 1.81.8

2γε(1−γ)2=2×0.9×0.010.01=1.8\frac{2\gamma\varepsilon}{(1-\gamma)^2}=\frac{2\times0.9\times0.01}{0.01}=1.8. The values themselves are within ε/(1−γ)=0.1\varepsilon/(1-\gamma)=0.1, and the greedy step multiplies by another 2γ/(1−γ)=182\gamma/(1-\gamma)=18. Both factors are worst cases.

Question 3

In the two-state counterexample, least-squares fitted value iteration diverges although the true values are representable and each exact backup is a contraction. What goes wrong?

  1. The rewards are too large
  2. The least-squares projection is a non-expansion only in an L2L_2 norm, the backup contracts only in the sup norm, and their composition can expand
  3. The feature matrix is rank-deficient
  4. The discount factor exceeds 1
Show the answer

B. The least-squares projection is a non-expansion only in an L2L_2 norm, the backup contracts only in the sup norm, and their composition can expand

Contraction is a property relative to a norm, and two maps that are well behaved in different norms need not compose well. Here the composition multiplies ww by 6−4ϵ5γ>1\frac{6-4\epsilon}{5}\gamma>1. Averagers, which are sup-norm non-expansions, or on-policy weighting, under which the backup contracts in the matching L2L_2 norm, avoid the problem.

Question 4

LpL_p error bounds for fitted value iteration involve a concentrability coefficient CC. When is CC small?

  1. When the sampling distribution covers, with comparable probability, every state that policies can drive the system to
  2. When the function class is small
  3. When the discount factor is small
  4. When the rewards are sparse
Show the answer

A. When the sampling distribution covers, with comparable probability, every state that policies can drive the system to

CC compares the state distributions that policies induce with the data distribution μ\mu; if some reachable state is rarely or never sampled, regression error there is unconstrained, and CC is large or infinite. It is the formal reason offline data must cover the behavior one hopes to evaluate or improve.

Further reading

  • D. P. Bertsekas & J. N. Tsitsiklis, Neuro-Dynamic Programming (Athena Scientific, 1996).
  • M. L. Puterman & M. C. Shin, "Modified policy iteration algorithms for discounted Markov decision problems," Management Science (1978).
  • A. G. Barto, S. J. Bradtke & S. P. Singh, "Learning to act using real-time dynamic programming," Artificial Intelligence 72, 81–138 (1995).
  • R. Munos, "Performance bounds in LpL_p-norm for approximate value iteration," SIAM Journal on Control and Optimization (2007).
  • B. Scherrer & B. Lesner, "On the use of non-stationary policies for stationary infinite-horizon Markov decision processes," NeurIPS (2012).