Chapter 10

On-policy Control with Approximation

Control with a function approximator looks at first like a routine extension of Chapter 9: approximate action values q^(s,a,𝐰)≈qπ(s,a)\hat q(s, a, \mathbf{w}) \approx q_\pi(s, a) instead of state values, act ε\varepsilon-greedily with respect to them, and let generalized policy iteration do the rest.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 For episodic tasks this is exactly what we do, and the result is semi-gradient Sarsa. For continuing tasks the extension forces a surprising retreat. Once states can no longer be told apart, the discounted formulation stops defining a sensible problem: averaged over the states the policy visits, the discounted value of every policy is its average reward divided by 1−γ1 - \gamma, whatever γ\gamma is. We therefore replace discounting by the average-reward setting, with differential returns, values, Bellman equations and TD errors. This chapter develops both halves and proves the facts on which the change of setting rests.

10.1Episodic Semi-gradient Control

Training examples now have the form St,At↦UtS_t, A_t \mapsto U_t, where UtU_t is any approximation of qπ(St,At)q_\pi(S_t, A_t): a Monte Carlo return, a one-step or an nn-step Sarsa target. The semi-gradient update (9.3) carries over word for word,

𝐰t+1≐𝐰t+α[Ut−q^(St,At,𝐰t)]∇q^(St,At,𝐰t), \mathbf{w}_{t+1} \doteq \mathbf{w}_t + \alpha\bigl[U_t - \hat q(S_t, A_t, \mathbf{w}_t)\bigr]\nabla\hat q(S_t, A_t, \mathbf{w}_t), (10.1)

and with the one-step target it becomes episodic semi-gradient Sarsa, the function-approximation form of Algorithm 6.6:

𝐰t+1≐𝐰t+α[Rt+1+γq^(St+1,At+1,𝐰t)−q^(St,At,𝐰t)]×∇q^(St,At,𝐰t). \begin{aligned} \mathbf{w}_{t+1} \doteq \mathbf{w}_t &+ \alpha\bigl[R_{t+1} + \gamma\hat q(S_{t+1}, A_{t+1}, \mathbf{w}_t) - \hat q(S_t, A_t, \mathbf{w}_t)\bigr] \\ &\times \nabla\hat q(S_t, A_t, \mathbf{w}_t). \end{aligned} (10.2)

For a fixed policy this is nothing new. The pairs (St,At)(S_t, A_t) form a Markov chain with transition probabilities p(s′∣s,a) π(a′∣s′)p(s' \mid s, a)\,\pi(a' \mid s'), and with linear q^(s,a,𝐰)=𝐰⊤𝐱(s,a)\hat q(s, a, \mathbf{w}) = \mathbf{w}^\top\mathbf{x}(s, a) the update is linear TD(0) on that chain. Everything in Section 9.4 applies with state–action pairs in place of states: convergence to a TD fixed point, and the error bound Theorem 9.7 in the norm weighted by the on-policy distribution of pairs.

Control couples this evaluation with improvement. If the action set is finite and small, we compute q^(St,a,𝐰t)\hat q(S_t, a, \mathbf{w}_t) for every aa, find the greedy action, and act ε\varepsilon-greedily; the same policy generates the data and is evaluated, so the method is on-policy. Large or continuous action sets make the maximization itself a problem, one of the reasons for the policy-gradient methods of Chapter 13.

Algorithm 10.1 (Episodic Semi-gradient Sarsa). Input: a differentiable q^:𝒮×𝒜×ℝd→ℝ\hat q : \mathcal{S} \times \mathcal{A} \times \mathbb{R}^d \to \mathbb{R}. Parameters: α>0\alpha > 0, small ε>0\varepsilon > 0. Initialize: 𝐰\mathbf{w} arbitrarily (e.g. 𝐰=𝟎\mathbf{w} = \mathbf{0}).

  1. Loop for each episode: choose initial SS and AA (e.g. ε\varepsilon-greedy w.r.t. q^(S,⋅,𝐰)\hat q(S, \cdot, \mathbf{w})). Repeat:
    1. Take AA, observe R,S′R, S'.
    2. If S′S' is terminal: 𝐰←𝐰+α[R−q^(S,A,𝐰)]∇q^(S,A,𝐰)\mathbf{w} \leftarrow \mathbf{w} + \alpha[R - \hat q(S, A, \mathbf{w})]\nabla\hat q(S, A, \mathbf{w}), and go to the next episode.
    3. Choose A′A' ε\varepsilon-greedily w.r.t. q^(S′,⋅,𝐰)\hat q(S', \cdot, \mathbf{w}).
    4. 𝐰←𝐰+α[R+γq^(S′,A′,𝐰)−q^(S,A,𝐰)]∇q^(S,A,𝐰)\mathbf{w} \leftarrow \mathbf{w} + \alpha[R + \gamma\hat q(S', A', \mathbf{w}) - \hat q(S, A, \mathbf{w})]\nabla\hat q(S, A, \mathbf{w}).
    5. S←S′S \leftarrow S', A←A′A \leftarrow A'.

With a changing policy the guarantees weaken. The ε\varepsilon-greedy policy is a discontinuous function of 𝐰\mathbf{w}: an arbitrarily small change of weights can flip the greedy action, which changes the on-policy distribution, which changes the fixed point. Gordon showed that linear Sarsa with ε\varepsilon-greedy improvement need not converge, but stays in a bounded region where it may chatter among policies;Geoffrey J. Gordon. Reinforcement Learning with Function Approximation Converges to a Region. In Advances in Neural Information Processing Systems 13 (NIPS 2000), pages 1040–1046. MIT Press, 2001 Perkins and Precup proved convergence when the improvement operator is a sufficiently smooth (Lipschitz) function of the action values.Theodore J. Perkins and Doina Precup. A Convergent Form of Approximate Policy Iteration. In Advances in Neural Information Processing Systems 15 (NIPS 2002), pages 1595–1602. MIT Press, 2003 The deeper reason, discussed in Section 10.4, is that with approximation the policy improvement theorem is lost.

10.1.1The Mountain Car

An underpowered car sits in a valley and must reach the top of the hill on its right. Gravity is stronger than the engine, so the car must first back up the left slope and then use the momentum gained. Things have to get worse (farther from the goal) before they can get better, a structure on which many control methods fail without help. The state is position xt∈[−1.2,0.5]x_t \in [-1.2, 0.5] and velocity x˙t∈[−0.07,0.07]\dot x_t \in [-0.07, 0.07]; the actions are At∈{−1,0,+1}A_t \in \{-1, 0, +1\} (full reverse, zero, full forward throttle); and the simplified physics is

x˙t+1≐bound⁡[x˙t+0.001At−0.0025cos⁡(3xt)],xt+1≐bound⁡[xt+x˙t+1], \begin{aligned} \dot x_{t+1} &\doteq \operatorname{bound}\bigl[\dot x_t + 0.001A_t - 0.0025\cos(3x_t)\bigr], \\ x_{t+1} &\doteq \operatorname{bound}\bigl[x_t + \dot x_{t+1}\bigr], \end{aligned}

where bound⁡\operatorname{bound} clips to the intervals, the velocity is reset to zero at the left wall, and reaching x=0.5x = 0.5 ends the episode. The reward is −1-1 per step, so −q∗-q_* is the number of steps to go. Episodes start at a random x0∈[−0.6,−0.4)x_0 \in [-0.6, -0.4) with zero velocity.

Sutton and Barto tile-code the two continuous variables with eight 8×88 \times 8 grid tilings, asymmetrically offset, separately for each action, and run semi-gradient Sarsa with q^=𝐰⊤𝐱(s,a)\hat q = \mathbf{w}^\top\mathbf{x}(s, a). The interesting part is exploration. All true values are negative, so initial weights 𝐰=𝟎\mathbf{w} = \mathbf{0} are optimistic: every state–action pair the car has tried soon looks worse than those it has not, and even with ε=0\varepsilon = 0 the car is driven to new parts of state space, spiraling outwards in the (x,x˙)(x, \dot x) plane until it swings high enough to escape. Optimistic initialization, a tabular trick of Chapter 2, survives generalization here because tile coding generalizes only locally. With step sizes α\alpha around 0.1/80.1/8 to 0.5/80.5/8 (that is, 0.10.1 to 0.50.5 divided by the number of tilings, as (9.11) suggests), episodes shrink from about a thousand steps to a couple of hundred or fewer within a few hundred episodes.

10.2n-step Semi-gradient Sarsa

Replacing the one-step target by the nn-step return of (7.1), now bootstrapping from q^\hat q,

Gt:t+n≐Rt+1+γRt+2+⋯+γn−1Rt+n+γnq^(St+n,At+n,𝐰t+n−1), \begin{aligned} G_{t:t+n} \doteq{} &R_{t+1} + \gamma R_{t+2} + \dots + \gamma^{n-1}R_{t+n} \\ &+ \gamma^n\hat q(S_{t+n}, A_{t+n}, \mathbf{w}_{t+n-1}), \end{aligned} (10.3)

for t+n<Tt + n < T, with Gt:t+n≐GtG_{t:t+n} \doteq G_t if t+n≥Tt + n \ge T, gives the update 𝐰t+n≐𝐰t+n−1+α[Gt:t+n−q^(St,At,𝐰t+n−1)]∇q^(St,At,𝐰t+n−1)\mathbf{w}_{t+n} \doteq \mathbf{w}_{t+n-1} + \alpha[G_{t:t+n} - \hat q(S_t, A_t, \mathbf{w}_{t+n-1})]\nabla\hat q(S_t, A_t, \mathbf{w}_{t+n-1}).

Algorithm 10.2 (Episodic Semi-gradient n-step Sarsa). Input: differentiable q^\hat q; a policy π\pi if evaluating. Parameters: α>0\alpha > 0, ε>0\varepsilon > 0, integer n≥1n \ge 1. Initialize: 𝐰\mathbf{w} arbitrarily; stored St,At,RtS_t, A_t, R_t may be indexed mod n+1n + 1.

  1. Loop for each episode: store S0S_0 and A0A_0 (∼π\sim\pi or ε\varepsilon-greedy w.r.t. q^(S0,⋅,𝐰)\hat q(S_0, \cdot, \mathbf{w})); T←∞T \leftarrow \infty.
  2. For t=0,1,2,…t = 0, 1, 2, \dots:
    1. If t<Tt < T: take AtA_t, store Rt+1,St+1R_{t+1}, S_{t+1}. If St+1S_{t+1} is terminal, T←t+1T \leftarrow t + 1; else store At+1A_{t+1} chosen as in step 1.
    2. τ←t−n+1\tau \leftarrow t - n + 1. If τ≥0\tau \ge 0:
      1. G←∑i=τ+1min⁡(τ+n,T)γi−τ−1RiG \leftarrow \sum_{i=\tau+1}^{\min(\tau+n, T)}\gamma^{i-\tau-1}R_i; if τ+n<T\tau + n < T, G←G+γnq^(Sτ+n,Aτ+n,𝐰)G \leftarrow G + \gamma^n\hat q(S_{\tau+n}, A_{\tau+n}, \mathbf{w}).
      2. 𝐰←𝐰+α[G−q^(Sτ,Aτ,𝐰)]∇q^(Sτ,Aτ,𝐰)\mathbf{w} \leftarrow \mathbf{w} + \alpha[G - \hat q(S_\tau, A_\tau, \mathbf{w})]\nabla\hat q(S_\tau, A_\tau, \mathbf{w}).
    3. Stop when τ=T−1\tau = T - 1.

On the mountain car the familiar pattern of Chapter 7 returns: n=8n = 8 learns faster and reaches shorter episodes than n=1n = 1, and over the first fifty episodes an intermediate nn (four in Sutton and Barto’s sweep over nn and α\alpha) does best. Large nn also brings larger variance across runs, because long returns sum many random rewards. A Monte Carlo version (n=∞n = \infty) is possible in principle but nearly useless here: the first episodes are long, and nothing is learned until one of them ends.

10.3Average Reward: A New Problem Setting for Continuing Tasks

Alongside the episodic and the discounted formulations of Chapter 3 there is a third classical one for continuing tasks, standard in dynamic programming: the agent cares about delayed rewards as much as immediate ones, and judges a policy by its average reward

r(π)≐lim⁡h→∞1h∑t=1h𝔼[Rt∣S0,A0:t−1∼π]=∑sμπ(s)∑aπ(a∣s)∑s′,rp(s′,r∣s,a) r. \begin{aligned} r(\pi) &\doteq \lim_{h \to \infty}\frac{1}{h}\sum_{t=1}^h \mathbb{E}\bigl[R_t \mid S_0, A_{0:t-1} \sim \pi\bigr] \\ &= \sum_s \mu_\pi(s)\sum_a \pi(a \mid s)\sum_{s', r}p(s', r \mid s, a)\,r . \end{aligned} (10.4)

Here μπ(s)≐lim⁡t→∞Pr⁡{St=s∣A0:t−1∼π}\mu_\pi(s) \doteq \lim_{t \to \infty}\Pr\{S_t = s \mid A_{0:t-1} \sim \pi\} is the steady-state distribution, assumed to exist for every π\pi and not to depend on S0S_0. This assumption on the MDP is called ergodicity: where the process starts, and any early decision, has only a transient effect. For a finite MDP it holds when every policy induces an irreducible, aperiodic chain, and we assume this throughout. The steady-state distribution is the one that reproduces itself under the policy,

∑sμπ(s)∑aπ(a∣s) p(s′∣s,a)=μπ(s′),i.e.μπ⊤𝐏π=μπ⊤. \sum_s \mu_\pi(s)\sum_a \pi(a \mid s)\,p(s' \mid s, a) = \mu_\pi(s'), \qquad \text{i.e.}\quad \mu_\pi^\top\mathbf{P}_\pi = \mu_\pi^\top . (10.5)

Policies are ordered by r(π)r(\pi), and those attaining the maximum are optimal.Finer distinctions are possible among the policies with maximal average reward: one may also compare the transient rewards collected on the way to the steady state, which is what the differential values below measure. The sharpest criterion is Blackwell’s; see Section 10.6.2. Returns measure rewards relative to the average, through the differential return

Gt≐Rt+1−r(π)+Rt+2−r(π)+Rt+3−r(π)+⋯ , G_t \doteq R_{t+1} - r(\pi) + R_{t+2} - r(\pi) + R_{t+3} - r(\pi) + \cdots, (10.6)

and the differential value functions vπ(s)≐𝔼π[Gt∣St=s]v_\pi(s) \doteq \mathbb{E}_\pi[G_t \mid S_t = s], qπ(s,a)≐𝔼π[Gt∣St=s,At=a]q_\pi(s, a) \doteq \mathbb{E}_\pi[G_t \mid S_t = s, A_t = a], with the same notation as before. The series converges: by ergodicity 𝔼[Rt+1∣S0=s]−r(π)=((𝐏πt−𝟏μπ⊤)𝐫π)(s)\mathbb{E}[R_{t+1} \mid S_0 = s] - r(\pi) = \bigl((\mathbf{P}_\pi^t - \mathbf{1}\mu_\pi^\top)\mathbf{r}_\pi\bigr)(s) tends to zero geometrically. A one-step analysis, exactly as in Chapter 3 but with every γ\gamma removed and every reward replaced by its excess over the average, gives the differential Bellman equations

vπ(s)=∑aπ(a∣s)∑r,s′p(s′,r∣s,a)[r−r(π)+vπ(s′)],qπ(s,a)=∑r,s′p(s′,r∣s,a)[r−r(π)+∑a′π(a′∣s′) qπ(s′,a′)],v∗(s)=max⁡a∑r,s′p(s′,r∣s,a)[r−max⁡πr(π)+v∗(s′)],q∗(s,a)=∑r,s′p(s′,r∣s,a)[r−max⁡πr(π)+max⁡a′q∗(s′,a′)]. \begin{aligned} v_\pi(s) &= \sum_a \pi(a \mid s)\sum_{r, s'}p(s', r \mid s, a)\bigl[r - r(\pi) + v_\pi(s')\bigr], \\ q_\pi(s, a) &= \sum_{r, s'}p(s', r \mid s, a)\Bigl[r - r(\pi) + \sum_{a'}\pi(a' \mid s')\,q_\pi(s', a')\Bigr], \\ v_*(s) &= \max_a\sum_{r, s'}p(s', r \mid s, a)\bigl[r - \max_\pi r(\pi) + v_*(s')\bigr], \\ q_*(s, a) &= \sum_{r, s'}p(s', r \mid s, a)\Bigl[r - \max_\pi r(\pi) + \max_{a'}q_*(s', a')\Bigr]. \end{aligned} (10.7)

They differ from the discounted ones in a way that matters: they do not determine vπv_\pi by themselves.

Proposition 10.3 (Differential Bellman Equations Determine Values up to a Constant). Let 𝐏=𝐏π\mathbf{P} = \mathbf{P}_\pi be irreducible with stationary distribution μ\mu, and 𝐫=𝐫π\mathbf{r} = \mathbf{r}_\pi. The system 𝐯=𝐫−ρ𝟏+𝐏𝐯\mathbf{v} = \mathbf{r} - \rho\mathbf{1} + \mathbf{P}\mathbf{v} in the unknowns (ρ,𝐯)(\rho, \mathbf{v}) has solutions, every solution has ρ=μ⊤𝐫=r(π)\rho = \mu^\top\mathbf{r} = r(\pi), and the solutions in 𝐯\mathbf{v} are exactly 𝐯π+c𝟏\mathbf{v}_\pi + c\mathbf{1}, c∈ℝc \in \mathbb{R}. The differential value function is the unique solution with μ⊤𝐯=0\mu^\top\mathbf{v} = 0.

Proof. Multiply the equation by μ⊤\mu^\top and use μ⊤𝐏=μ⊤\mu^\top\mathbf{P} = \mu^\top: μ⊤𝐯=μ⊤𝐫−ρ+μ⊤𝐯\mu^\top\mathbf{v} = \mu^\top\mathbf{r} - \rho + \mu^\top\mathbf{v}, so ρ=μ⊤𝐫\rho = \mu^\top\mathbf{r}. The difference of two solutions satisfies 𝐮=𝐏𝐮\mathbf{u} = \mathbf{P}\mathbf{u}; at a state where uu is largest, u(s)=∑s′𝐏ss′u(s′)u(s) = \sum_{s'}\mathbf{P}_{ss'}u(s') forces u(s′)=u(s)u(s') = u(s) for every successor, and by irreducibility the maximum spreads to all states, so 𝐮=c𝟏\mathbf{u} = c\mathbf{1}. The differential value function solves the system: splitting off the first reward in (10.6) gives the first line of (10.7). It is centered, because μ⊤(𝐏t−𝟏μ⊤)=𝟎⊤\mu^\top(\mathbf{P}^t - \mathbf{1}\mu^\top) = \mathbf{0}^\top for every tt, so each term of μ⊤𝐯π=∑tμ⊤(𝐏t−𝟏μ⊤)𝐫\mu^\top\mathbf{v}_\pi = \sum_t\mu^\top(\mathbf{P}^t - \mathbf{1}\mu^\top)\mathbf{r} vanishes. □

Only differences of differential values matter for choosing actions, so the additive constant is harmless; but it means that a learning algorithm may converge to 𝐯π\mathbf{v}_\pi plus an offset that depends on its initialization, and for linear approximation that the matrix 𝐗⊤𝐃(𝐈−𝐏)𝐗\mathbf{X}^\top\mathbf{D}(\mathbf{I} - \mathbf{P})\mathbf{X} is singular whenever the constant function is representable.

10.3.1Discounted Values Near γ = 1

How do the discounted values 𝐯γ≐(𝐈−γ𝐏)−1𝐫\mathbf{v}_\gamma \doteq (\mathbf{I} - \gamma\mathbf{P})^{-1}\mathbf{r} of Chapter 3 relate to these new objects? They blow up as γ→1\gamma \to 1, and the way they blow up contains the whole average-reward theory.

Theorem 10.4 (Laurent Expansion of Discounted Values). Let 𝐏\mathbf{P} be irreducible and aperiodic with stationary distribution μ\mu, let 𝐏∗=𝟏μ⊤\mathbf{P}^* = \mathbf{1}\mu^\top, and define the deviation matrix 𝐇≐(𝐈−𝐏+𝐏∗)−1−𝐏∗\mathbf{H} \doteq (\mathbf{I} - \mathbf{P} + \mathbf{P}^*)^{-1} - \mathbf{P}^*. Then 𝐯π=𝐇𝐫\mathbf{v}_\pi = \mathbf{H}\mathbf{r}, and as γ→1−\gamma \to 1^-

𝐯γ=r(π)1−γ𝟏+𝐯π+O(1−γ). \mathbf{v}_\gamma = \frac{r(\pi)}{1 - \gamma}\mathbf{1} + \mathbf{v}_\pi + O(1 - \gamma). (10.8)

Proof. Since 𝐏𝐏∗=𝐏∗𝐏=𝐏∗𝐏∗=𝐏∗\mathbf{P}\mathbf{P}^* = \mathbf{P}^*\mathbf{P} = \mathbf{P}^*\mathbf{P}^* = \mathbf{P}^*, induction gives 𝐏t−𝐏∗=(𝐏−𝐏∗)t\mathbf{P}^t - \mathbf{P}^* = (\mathbf{P} - \mathbf{P}^*)^t for t≥1t \ge 1. For an irreducible aperiodic chain 𝐏t→𝐏∗\mathbf{P}^t \to \mathbf{P}^*, so the spectral radius of 𝐐=𝐏−𝐏∗\mathbf{Q} = \mathbf{P} - \mathbf{P}^* is less than one. Expanding the Neumann series,

(𝐈−γ𝐏)−1=∑t≥0γt𝐏t=𝐏∗1−γ+∑t≥0γt(𝐏t−𝐏∗)=𝐏∗1−γ+(𝐈−γ𝐐)−1−𝐏∗, \begin{aligned} (\mathbf{I} - \gamma\mathbf{P})^{-1} &= \sum_{t \ge 0}\gamma^t\mathbf{P}^t = \frac{\mathbf{P}^*}{1 - \gamma} + \sum_{t \ge 0}\gamma^t(\mathbf{P}^t - \mathbf{P}^*) \\ &= \frac{\mathbf{P}^*}{1 - \gamma} + (\mathbf{I} - \gamma\mathbf{Q})^{-1} - \mathbf{P}^*, \end{aligned}

where the t=0t = 0 term 𝐈−𝐏∗\mathbf{I} - \mathbf{P}^* was rewritten as 𝐐0−𝐏∗\mathbf{Q}^0 - \mathbf{P}^*. The last two terms are analytic in γ\gamma on a neighborhood of γ=1\gamma = 1, where they equal (𝐈−𝐐)−1−𝐏∗=𝐇(\mathbf{I} - \mathbf{Q})^{-1} - \mathbf{P}^* = \mathbf{H}; so they are 𝐇+O(1−γ)\mathbf{H} + O(1 - \gamma). Multiply by 𝐫\mathbf{r} and use 𝐏∗𝐫=(μ⊤𝐫)𝟏=r(π)𝟏\mathbf{P}^*\mathbf{r} = (\mu^\top\mathbf{r})\mathbf{1} = r(\pi)\mathbf{1}. Finally 𝐇𝐫=∑t≥0(𝐏t−𝐏∗)𝐫\mathbf{H}\mathbf{r} = \sum_{t \ge 0}(\mathbf{P}^t - \mathbf{P}^*)\mathbf{r}, which is 𝐯π\mathbf{v}_\pi term by term. □

The leading term is the average reward, the same for all states; the constant term is the differential value; only higher-order terms distinguish discount rates. Equation (10.8) also gives the alternative definition vπ(s)=lim⁡γ→1(vγ(s)−r(π)/(1−γ))v_\pi(s) = \lim_{\gamma \to 1}\bigl(v_\gamma(s) - r(\pi)/(1 - \gamma)\bigr), which remains meaningful for periodic chains where the series (10.6) oscillates (Exercise 10.11).

10.3.2Differential Semi-gradient Sarsa

To learn, we need an estimate R¯t\bar R_t of r(π)r(\pi) and the differential TD errors

δt≐Rt+1−R¯t+v^(St+1,𝐰t)−v^(St,𝐰t),δt≐Rt+1−R¯t+q^(St+1,At+1,𝐰t)−q^(St,At,𝐰t), \begin{aligned} \delta_t &\doteq R_{t+1} - \bar R_t + \hat v(S_{t+1}, \mathbf{w}_t) - \hat v(S_t, \mathbf{w}_t), \\ \delta_t &\doteq R_{t+1} - \bar R_t + \hat q(S_{t+1}, A_{t+1}, \mathbf{w}_t) - \hat q(S_t, A_t, \mathbf{w}_t), \end{aligned} (10.9)

for state and action values. With these in place of (6.2), the algorithms and much of the theory carry over. In particular, at the true average reward and the true differential values the expected differential TD error is zero in every state, for every offset cc in Proposition 10.3. The update of the weights is 𝐰t+1=𝐰t+αδt∇q^(St,At,𝐰t)\mathbf{w}_{t+1} = \mathbf{w}_t + \alpha\delta_t\nabla\hat q(S_t, A_t, \mathbf{w}_t). The average-reward estimate is moved by the TD error too, R¯←R¯+βδt\bar R \leftarrow \bar R + \beta\delta_t, rather than by Rt+1−R¯R_{t+1} - \bar R; both converge, but δt\delta_t has far lower variance once the values are roughly right, because it has the predictable part of the reward fluctuation removed (Exercise 10.10).

Algorithm 10.5 (Differential Semi-gradient Sarsa). Input: differentiable q^:𝒮×𝒜×ℝd→ℝ\hat q : \mathcal{S} \times \mathcal{A} \times \mathbb{R}^d \to \mathbb{R}. Parameters: α,β>0\alpha, \beta > 0, ε>0\varepsilon > 0. Initialize: 𝐰\mathbf{w} and R¯∈ℝ\bar R \in \mathbb{R} arbitrarily (e.g. 𝟎\mathbf{0} and 00); state SS and action AA.

  1. Loop for each step:
    1. Take AA, observe R,S′R, S'; choose A′A' ε\varepsilon-greedily w.r.t. q^(S′,⋅,𝐰)\hat q(S', \cdot, \mathbf{w}).
    2. δ←R−R¯+q^(S′,A′,𝐰)−q^(S,A,𝐰)\delta \leftarrow R - \bar R + \hat q(S', A', \mathbf{w}) - \hat q(S, A, \mathbf{w}).
    3. R¯←R¯+βδ\bar R \leftarrow \bar R + \beta\delta.
    4. 𝐰←𝐰+αδ∇q^(S,A,𝐰)\mathbf{w} \leftarrow \mathbf{w} + \alpha\delta\nabla\hat q(S, A, \mathbf{w}).
    5. S←S′S \leftarrow S', A←A′A \leftarrow A'.

This is the on-policy analogue of Schwartz’s R-learning.Anton Schwartz. A Reinforcement Learning Method for Maximizing Undiscounted Rewards. In Proceedings of the Tenth International Conference on Machine Learning (ICML 1993), pages 298–305. Morgan Kaufmann, 1993 Sutton and Barto’s test case is an access-control queuing task: customers of four priorities, paying 1,2,41, 2, 4 or 88, arrive at a queue in front of ten servers; at each step the customer at the head is accepted (if a server is free) or rejected, and each busy server frees up with probability 0.060.06. Tabular differential Sarsa (α=β=0.01\alpha = \beta = 0.01, ε=0.1\varepsilon = 0.1, two million steps) learns to accept the highest priority whenever possible and the lower priorities only when enough servers are free, with R¯\bar R near 2.312.31. The problem is too large to be worth reproducing exactly here; the mechanism is visible on a two-state chain.

10.3.3In Wolfram Language

Take two states and two actions, stay and go (to the other state). The policy goes with probability aa from state 1 and bb from state 2, and every step spent in state ss pays rsr_s, whatever the action. First r(π)r(\pi) as the triple sum of (10.4):

μ\mu is the normalized left null vector of 𝐏π−𝐈\mathbf{P}_\pi - \mathbf{I}; the sum runs over states, actions and next states, with deterministic transitions.
const P2 = await wl`{{1 - a, a}, {b, 1 - b}}`
const piGo = await wl`{a, b}`
const mu2 = await wl`With[{n = First[NullSpace[Transpose[${P2}] - IdentityMatrix[2]]]}, Simplify[n/Total[n]]]`
const rho = await show`Simplify[Sum[${mu2}[[s]] Sum[If[act == 2, ${piGo}[[s]], 1 - ${piGo}[[s]]] Sum[Boole[s2 == If[act == 2, 3 - s, s]] Subscript[r, s], {s2, 2}], {act, 2}], {s, 2}]]`
r(π)=∑sμ(s)∑aπ(a∣s)∑s′,rp(s′,r∣s,a) r=br1+ar2a+b r(\pi) = \sum_s \mu(s)\sum_a \pi(a \mid s)\sum_{s', r} p(s', r \mid s, a)\, r = \frac{b r_1+a r_2}{a+b}

Next the differential Bellman equations, solved for ρ\rho and v2v_2 with v1v_1 left free: ρ\rho is forced to be r(π)r(\pi), and the values are fixed only up to the free constant, as Proposition 10.3 says.

Two equations, three unknowns (ρ,v1,v2)(\rho, v_1, v_2): the solution leaves v1v_1 free.
await show`Solve[Thread[{Subscript[v, 1], Subscript[v, 2]} == {Subscript[r, 1], Subscript[r, 2]} - \[Rho] + ${P2} . {Subscript[v, 1], Subscript[v, 2]}], {\[Rho], Subscript[v, 2]}]`
{v=r−ρ𝟏+𝐏v}  ⟺  {{ρ→−−br1−ar2a+b,v2→−r1−r2−av1−bv1a+b}} \bigl\{v = r - \rho\mathbf{1} + \mathbf{P}v\bigr\} \;\Longleftrightarrow\; \left\{\left\{\rho \to -\frac{-b r_1-a r_2}{a+b},v_2\to -\frac{r_1-r_2-a v_1-b v_1}{a+b}\right\}\right\}

The deviation matrix gives the centered solution, and we check that it satisfies the equations with any offset cc and that μ⊤𝐯π=0\mu^\top\mathbf{v}_\pi = 0:

𝐇=(𝐈−𝐏+𝐏∗)−1−𝐏∗\mathbf{H} = (\mathbf{I} - \mathbf{P} + \mathbf{P}^*)^{-1} - \mathbf{P}^* with 𝐏∗=𝟏μ⊤\mathbf{P}^* = \mathbf{1}\mu^\top.
const Pstar = await wl`{${mu2}, ${mu2}}`
const H = await show`Simplify[Inverse[IdentityMatrix[2] - ${P2} + ${Pstar}] - ${Pstar}]`
𝐇=(a(a+b)2−a(a+b)2−b(a+b)2b(a+b)2) \mathbf{H} = \left( \begin{array}{cc} \frac{a}{(a+b)^2} & -\frac{a}{(a+b)^2} \\ -\frac{b}{(a+b)^2} & \frac{b}{(a+b)^2} \\ \end{array} \right) Output: 𝐯π=𝐇𝐫\mathbf{v}_\pi = \mathbf{H}\mathbf{r}; the expected differential TD error 𝐫−r(π)𝟏+𝐏(𝐯π+c𝟏)−(𝐯π+c𝟏)\mathbf{r} - r(\pi)\mathbf{1} + \mathbf{P}(\mathbf{v}_\pi + c\mathbf{1}) - (\mathbf{v}_\pi + c\mathbf{1}); and μ⊤𝐯π\mu^\top\mathbf{v}_\pi.
const rvec = await wl`{Subscript[r, 1], Subscript[r, 2]}`
const vdiff = await wl`Simplify[${H} . ${rvec}]`
await show`Simplify[{${vdiff}, ${rvec} - ${rho} + ${P2} . (${vdiff} + c) - (${vdiff} + c), ${mu2} . ${vdiff}}]`
(𝐯π, 𝔼[δ∣S=s]s=1,2, μ⊤𝐯π)={{a(r1−r2)(a+b)2,b(−r1+r2)(a+b)2},{0,0},0} \bigl(\mathbf{v}_\pi,\ \mathbb{E}[\delta \mid S = s]_{s = 1, 2},\ \mu^\top\mathbf{v}_\pi\bigr) = \left\{\left\{\frac{a \left(r_1-r_2\right)}{(a+b)^2},\frac{b \left(-r_1+r_2\right)}{(a+b)^2}\right\},\{0,0\},0\right\}

Now the Laurent expansion (10.8). The kernel expands the discounted value of state 1 around γ=1\gamma = 1; the pole is r(π)/(1−γ)r(\pi)/(1 - \gamma) and the constant term is vπ(1)v_\pi(1) from the previous output.

Series of vγ(1)=[(𝐈−γ𝐏)−1𝐫]1v_\gamma(1) = [(\mathbf{I} - \gamma\mathbf{P})^{-1}\mathbf{r}]_1 at γ=1\gamma = 1, up to the constant term; the kernel writes powers of γ−1\gamma - 1.
const vgam = await wl`Simplify[Inverse[IdentityMatrix[2] - \[Gamma] ${P2}] . ${rvec}]`
await show`Series[${vgam}[[1]], {\[Gamma], 1, 0}]`
vγ(1)=−br1−ar2(a+b)(γ−1)+a(r1−r2)(a+b)2+O((γ−1)1) v_\gamma(1) = \frac{-b r_1-a r_2}{(a+b) (\gamma -1)}+\frac{a \left(r_1-r_2\right)}{(a+b)^2}+O\left((\gamma -1)^1\right) Both components at once: 𝐯γ\mathbf{v}_\gamma minus the first two Laurent terms vanishes at γ=1\gamma = 1.
await show`Simplify[Normal[Series[${vgam}, {\[Gamma], 1, 0}]] - (${rho}/(1 - \[Gamma]) + ${vdiff})]`
[𝐯γ−r(π)1−γ𝟏−𝐯π]γ=1={0,0} \Bigl[\mathbf{v}_\gamma - \frac{r(\pi)}{1 - \gamma}\mathbf{1} - \mathbf{v}_\pi\Bigr]_{\gamma = 1} = \{0,0\}

10.4Deprecating the Discounted Setting

Discounting served us well with tables, where each state’s return can be identified and averaged separately. Now imagine a continuing task whose states are seen only through features that barely tell them apart; in the extreme, all feature vectors are equal. All we have is a stream of rewards. To judge a policy we can average the rewards over a long time, which is the average-reward criterion. Or we can compute the discounted return from each time step and average those. In a continuing problem no time step is special, so averaging over time is the only option, and it gives nothing new: every reward RtR_t appears in the return from time t−1t-1 with weight 11, in the return from t−2t-2 with weight γ\gamma, from t−k−1t-k-1 with weight γk\gamma^k, for a total weight of 1/(1−γ)1/(1 - \gamma). Hence the average discounted return is r(π)/(1−γ)r(\pi)/(1 - \gamma).

Proposition 10.6 (The Futility of Discounting in Continuing Problems). For every policy π\pi with stationary distribution μπ\mu_\pi and every γ∈[0,1)\gamma \in [0, 1),

Jγ(π)≐∑sμπ(s) vπγ(s)=r(π)1−γ. J_\gamma(\pi) \doteq \sum_s \mu_\pi(s)\,v^\gamma_\pi(s) = \frac{r(\pi)}{1 - \gamma}. (10.10)

Hence JγJ_\gamma orders policies exactly as r(π)r(\pi) does, for every γ\gamma, including γ=0\gamma = 0.

Proof. By the discounted Bellman equation and then stationarity (10.5),

Jγ(π)=∑sμπ(s)∑aπ(a∣s)∑s′,rp(s′,r∣s,a)[r+γvπγ(s′)]=r(π)+γ∑s′vπγ(s′)∑sμπ(s)∑aπ(a∣s)p(s′∣s,a)=r(π)+γJγ(π). \begin{aligned} J_\gamma(\pi) &= \sum_s \mu_\pi(s)\sum_a\pi(a \mid s)\sum_{s', r}p(s', r \mid s, a)\bigl[r + \gamma v^\gamma_\pi(s')\bigr] \\ &= r(\pi) + \gamma\sum_{s'}v^\gamma_\pi(s')\sum_s\mu_\pi(s)\sum_a\pi(a \mid s)p(s' \mid s, a) \\ &= r(\pi) + \gamma J_\gamma(\pi). \end{aligned}

Solve for Jγ(π)J_\gamma(\pi). In matrix form: μ⊤(𝐈−γ𝐏)=(1−γ)μ⊤\mu^\top(\mathbf{I} - \gamma\mathbf{P}) = (1 - \gamma)\mu^\top, so μ⊤(𝐈−γ𝐏)−1=μ⊤/(1−γ)\mu^\top(\mathbf{I} - \gamma\mathbf{P})^{-1} = \mu^\top/(1 - \gamma), the column-sum identity of Theorem 9.4 once more. □

The proposition concerns true discounted values. The same identity holds for the approximate values that semi-gradient TD actually finds, as soon as the features can represent constants.

Corollary 10.7 (Average of the TD Fixed Point). Let v^=𝐗𝐰\hat v = \mathbf{X}\mathbf{w} be linear with features whose span contains the constant function, and let 𝐰TD\mathbf{w}_{\mathrm{TD}} be the TD fixed point (9.5) for policy π\pi under its on-policy distribution. Then ∑sμπ(s) v^(s,𝐰TD)=r(π)/(1−γ)\sum_s \mu_\pi(s)\,\hat v(s, \mathbf{w}_{\mathrm{TD}}) = r(\pi)/(1 - \gamma).

Proof. The fixed point satisfies 𝐗⊤𝐃(𝐫+γ𝐏𝐗𝐰−𝐗𝐰)=𝟎\mathbf{X}^\top\mathbf{D}(\mathbf{r} + \gamma\mathbf{P}\mathbf{X}\mathbf{w} - \mathbf{X}\mathbf{w}) = \mathbf{0}, so 𝐲⊤𝐗⊤𝐃(⋯ )=0\mathbf{y}^\top\mathbf{X}^\top\mathbf{D}(\cdots) = 0 for every 𝐲\mathbf{y}; choose 𝐲\mathbf{y} with 𝐗𝐲=𝟏\mathbf{X}\mathbf{y} = \mathbf{1}. Since 𝟏⊤𝐃=μ⊤\mathbf{1}^\top\mathbf{D} = \mu^\top and μ⊤𝐏=μ⊤\mu^\top\mathbf{P} = \mu^\top, this reads μ⊤𝐫+γμ⊤𝐗𝐰−μ⊤𝐗𝐰=0\mu^\top\mathbf{r} + \gamma\mu^\top\mathbf{X}\mathbf{w} - \mu^\top\mathbf{X}\mathbf{w} = 0. □

In the extreme case of a single constant feature, v^≡r(π)/(1−γ)\hat v \equiv r(\pi)/(1 - \gamma): the approximate discounted value is the average reward, rescaled. On the two-state chain:

First μ⊤𝐯γ\mu^\top\mathbf{v}_\gamma for the true discounted values; then the TD fixed point with the single feature x(s)=1x(s) = 1, i.e. 𝐀=1−γ\mathbf{A} = 1 - \gamma and 𝐛=μ⊤𝐫\mathbf{b} = \mu^\top\mathbf{r}.
await show`Simplify[{${mu2} . ${vgam}, Inverse[Transpose[{{1}, {1}}] . DiagonalMatrix[${mu2}] . (IdentityMatrix[2] - \[Gamma] ${P2}) . {{1}, {1}}] . (Transpose[{{1}, {1}}] . DiagonalMatrix[${mu2}] . ${rvec})}]`
(μ⊤𝐯γ, 𝐰TD for x(s)≡1)={−br1+ar2(a+b)(−1+γ),{−br1+ar2(a+b)(−1+γ)}} \bigl(\mu^\top\mathbf{v}_\gamma,\ \mathbf{w}_{\mathrm{TD}}\ \text{for}\ x(s) \equiv 1\bigr) = \left\{-\frac{b r_1+a r_2}{(a+b) (-1+\gamma )},\left\{-\frac{b r_1+a r_2}{(a+b) (-1+\gamma )}\right\}\right\}
Figure 10.1: The two-state chain with a=1/10a = 1/10, b=3/10b = 3/10, r1=0r_1 = 0, r2=1r_2 = 1, so that μ=(3/4,1/4)\mu = (3/4, 1/4) and r(π)=1/4r(\pi) = 1/4. Solid: (1−γ)vγ(s)(1 - \gamma)v_\gamma(s) for each state, which tends to r(π)r(\pi) as γ→1\gamma \to 1 with a gap of order (1−γ)vπ(s)(1 - \gamma)v_\pi(s) (Theorem 10.4). Black: their μ\mu-weighted average, equal to r(π)r(\pi) for every γ\gamma (Proposition 10.6).Two curves of the rescaled discounted value (1 - gamma) times v_gamma for the two states of a chain, starting at 0 and 1 at gamma equals 0 and both converging to 1/4 as gamma approaches 1, and a black horizontal line at 1/4 for their stationary average

If we optimized the discounted value averaged over the on-policy distribution, we would therefore be optimizing the average reward, and γ\gamma would play no role at all. Discounting may still be useful inside solution methods, to keep bootstrapped values bounded or to reduce variance; γ\gamma then changes from a parameter of the problem into a parameter of the algorithm. But a discounted control method with approximation is then no longer guaranteed to optimize anything in particular.Beyond Sutton and Barto (2018). Naik, Shariff, Yasui, Yao and Sutton (NeurIPS 2019 workshop on optimization foundations of RL) sharpened this into the claim that discounted reinforcement learning with function approximation in continuing tasks is not an optimization problem: with approximation there need not be any policy that maximizes the discounted values of all states at once, and the usual fixes (a start-state or on-policy-weighted objective) either do not apply to continuing tasks or collapse to the average reward, as above. They recommend the average-reward objective.Abhishek Naik, Roshan Shariff, Niko Yasui, Hengshuai Yao, and Richard S. Sutton. Discounted Reinforcement Learning Is Not an Optimization Problem, 2019. Optimization Foundations for Reinforcement Learning Workshop at NeurIPS 2019

The root of the difficulty is that with approximation we have lost the policy improvement theorem (Theorem 4.6). In the tabular case, making a policy greedy in one state could only help. With shared weights, improving the value of one state changes others, and there is no guarantee that ε\varepsilon-greedification improves the policy in any useful sense; it may chatter instead. This gap is shared by the episodic and the average-reward settings, and for methods that learn action values it is still largely open. The policy-gradient theorem of Chapter 13 supplies a substitute for parameterized policies.

10.5Differential Semi-gradient n-step Sarsa

The differential version of the nn-step return (10.3) subtracts the estimated average from each reward and does not discount:

Gt:t+n≐Rt+1−R¯t+1+Rt+2−R¯t+2+⋯+Rt+n−R¯t+n+q^(St+n,At+n,𝐰t+n−1), \begin{aligned} G_{t:t+n} \doteq{} &R_{t+1} - \bar R_{t+1} + R_{t+2} - \bar R_{t+2} + \dots + R_{t+n} - \bar R_{t+n} \\ &+ \hat q(S_{t+n}, A_{t+n}, \mathbf{w}_{t+n-1}), \end{aligned} (10.11)

and the nn-step differential TD error is δt≐Gt:t+n−q^(St,At,𝐰)\delta_t \doteq G_{t:t+n} - \hat q(S_t, A_t, \mathbf{w}), used both for 𝐰\mathbf{w} and for R¯\bar R.

Algorithm 10.8 (Differential Semi-gradient n-step Sarsa). Input: differentiable q^\hat q; a policy π\pi if evaluating. Parameters: α,β>0\alpha, \beta > 0, integer n≥1n \ge 1. Initialize: 𝐰\mathbf{w}, R¯\bar R arbitrarily; store S0S_0, A0A_0. Stored St,At,RtS_t, A_t, R_t may be indexed mod n+1n + 1.

  1. For t=0,1,2,…t = 0, 1, 2, \dots:
    1. Take AtA_t; store Rt+1R_{t+1} and St+1S_{t+1}; store At+1∼π(⋅∣St+1)A_{t+1} \sim \pi(\cdot \mid S_{t+1}) or ε\varepsilon-greedy w.r.t. q^(St+1,⋅,𝐰)\hat q(S_{t+1}, \cdot, \mathbf{w}).
    2. τ←t−n+1\tau \leftarrow t - n + 1. If τ≥0\tau \ge 0:
      1. δ←∑i=τ+1τ+n(Ri−R¯)+q^(Sτ+n,Aτ+n,𝐰)−q^(Sτ,Aτ,𝐰)\delta \leftarrow \sum_{i=\tau+1}^{\tau+n}(R_i - \bar R) + \hat q(S_{\tau+n}, A_{\tau+n}, \mathbf{w}) - \hat q(S_\tau, A_\tau, \mathbf{w}).
      2. R¯←R¯+βδ\bar R \leftarrow \bar R + \beta\delta; 𝐰←𝐰+αδ∇q^(Sτ,Aτ,𝐰)\mathbf{w} \leftarrow \mathbf{w} + \alpha\delta\nabla\hat q(S_\tau, A_\tau, \mathbf{w}).

The step size β\beta must be small for R¯\bar R to be a good long-run average, which makes R¯\bar R slow to forget its initial value; Exercise 10.12 removes that bias.

10.6Beyond the Book: Average-Reward Learning Revisited

10.6.1Differential Q-learning

The tabular theory of average-reward off-policy control long rested on RVI Q-learning (Abounadi, Bertsekas and Borkar, 2001),Jinane Abounadi, Dimitri P. Bertsekas, and Vivek S. Borkar. Learning Algorithms for Markov Decision Processes with Average Cost. SIAM Journal on Control and Optimization, 40(3):681–698, 2001 which subtracts a reference function f(Q)f(Q), for instance the value of a fixed reference state–action pair, in place of an average-reward estimate. Its performance depends heavily on that choice, and the best reference is a frequently visited pair, which is hard to know in advance. Wan, Naik and Sutton removed the reference function.Yi Wan, Abhishek Naik, and Richard S. Sutton. Learning and Planning in Average-Reward Markov Decision Processes. In Proceedings of the 38th International Conference on Machine Learning (ICML 2021), volume 139 of Proceedings of Machine Learning Research, 2021Beyond Sutton and Barto (2018). Wan, Naik and Sutton, Learning and planning in average-reward Markov decision processes, ICML 2021 (arXiv June 2020). Besides Differential Q-learning they give an off-policy prediction algorithm, Differential TD-learning, planning versions of both, and centered variants that converge to the differential values themselves rather than to values plus an offset. Their Differential Q-learning updates

δt=Rt+1−R¯t+max⁡aQt(St+1,a)−Qt(St,At),Qt+1(St,At)=Qt(St,At)+αtδt,R¯t+1=R¯t+η αtδt, \begin{aligned} \delta_t &= R_{t+1} - \bar R_t + \max_a Q_t(S_{t+1}, a) - Q_t(S_t, A_t), \\ Q_{t+1}(S_t, A_t) &= Q_t(S_t, A_t) + \alpha_t\delta_t, \\ \bar R_{t+1} &= \bar R_t + \eta\,\alpha_t\delta_t , \end{aligned}

with η>0\eta > 0: the average-reward estimate moves by the TD error, as in Algorithm 10.5. The proof of convergence is a neat reduction. Every increment of R¯\bar R is η\eta times an increment of Σt=∑s,aQt(s,a)\Sigma_t = \sum_{s,a}Q_t(s, a), so R¯t=ηΣt−c\bar R_t = \eta\Sigma_t - c with a constant cc fixed by the initial values; Differential Q-learning is therefore RVI Q-learning with the reference function f(Q)=η∑s,aQ(s,a)f(Q) = \eta\sum_{s,a}Q(s, a) on an MDP whose rewards are shifted by cc. Wan et al. extend the RVI theory to cover this ff and prove that in communicating MDPs, under the usual step-size conditions, R¯t\bar R_t converges almost surely to the optimal average reward r∗r_* and QtQ_t to a solution of the differential Bellman optimality equation (10.7).

10.6.2Blackwell Optimality

Ordering policies by r(π)r(\pi) ignores everything transient. The Laurent expansion (10.8) suggests a lexicographic refinement: among policies with maximal average reward (the gain), prefer larger differential values (the bias), then larger higher-order coefficients. Blackwell showed that a finite MDP has a stationary deterministic policy that is optimal for the discounted criterion simultaneously for all γ\gamma in some interval (γ0,1)(\gamma_0, 1).David Blackwell. Discrete Dynamic Programming. The Annals of Mathematical Statistics, 33(2):719–726, Institute of Mathematical Statistics, 1962. doi: 10.1214/aoms/1177704593Beyond Sutton and Barto (2018). Blackwell optimality is classical (Blackwell, Annals of Mathematical Statistics, 1962) and treated at length by Puterman (1994), but Sutton and Barto only allude to “subtle distinctions” among optimality criteria. It has recently been used to connect discounted and average-reward learning, for instance through the Blackwell discount factor above which discounted optimality implies Blackwell optimality (Grand-Clément and Petrik, 2023).Martin L. Puterman. Markov Decision Processes: Discrete Stochastic Dynamic Programming. John Wiley & Sons, New York, 1994. ISBN 978-0-471-61977-2. doi: 10.1002/9780470316887; and Julien Grand-Clément and Marek Petrik. Reducing Blackwell and Average Optimality to Discounted MDPs via the Blackwell Discount Factor. In Advances in Neural Information Processing Systems 36 (NeurIPS 2023), 2023 Such a Blackwell-optimal policy is gain-optimal and, among gain-optimal policies, bias-optimal: dividing the difference of two Laurent expansions by the leading nonzero power of 1−γ1 - \gamma shows why. This is the precise sense in which “discounting with γ\gamma close enough to one” solves the average-reward problem in the tabular case, and the reason it cannot be relied on with approximation: γ0\gamma_0 depends on the whole MDP and can be arbitrarily close to one, and Proposition 10.6 shows that the on-policy average cannot see anything beyond the gain.

10.6.3Deep Q-Networks

The most visible use of semi-gradient control with a nonlinear approximator is the Deep Q-Network of Mnih et al., which learned to play 49 Atari games from pixels.Volodymyr Mnih, Koray Kavukcuoglu, David Silver, Andrei A. Rusu, Joel Veness, Marc G. Bellemare, Alex Graves, Martin Riedmiller, Andreas K. Fidjeland, Georg Ostrovski, Stig Petersen, Charles Beattie, Amir Sadik, Ioannis Antonoglou, Helen King, Dharshan Kumaran, Daan Wierstra, Shane Legg, and Demis Hassabis. Human-Level Control through Deep Reinforcement Learning. Nature, 518(7540):529–533, 2015. doi: 10.1038/nature14236 It is semi-gradient Q-learning, hence off-policy, and its replay memory and target network are described in Section 15.5.

Q-learning is the natural next step from the Sarsa methods of this chapter, and the one that breaks them. Chapter 11 shows that learning off-policy, with bootstrapping and function approximation together, can make semi-gradient methods diverge, and looks for objectives and algorithms that are stable again.

10.7Exercises

Exercise 10.9. Three states A,B,CA, B, C form a ring traversed deterministically, A→B→C→AA \to B \to C \to A, with reward +1+1 on arrival in AA and 00 otherwise. Find r(π)r(\pi) and the differential values. The chain is periodic; is the differential value still well defined?

Solution

Each state is occupied a third of the time and one transition in three pays 11, so r(π)=1/3r(\pi) = 1/3. The differential Bellman equations are vA=−13+vBv_A = -\frac{1}{3} + v_B, vB=−13+vCv_B = -\frac{1}{3} + v_C, vC=23+vAv_C = \frac{2}{3} + v_A; with the centering vA+vB+vC=0v_A + v_B + v_C = 0 they give (vA,vB,vC)=(−13,0,13)(v_A, v_B, v_C) = (-\frac{1}{3}, 0, \frac{1}{3}). The series (10.6) oscillates for a periodic chain, but the definition through (10.8), lim⁡γ→1(vγ−r(π)/(1−γ))\lim_{\gamma \to 1}(v_\gamma - r(\pi)/(1 - \gamma)), exists and agrees:

The centered solution of the differential Bellman equations, and the limit of vγ−13/(1−γ)v_\gamma - \frac{1}{3}/(1 - \gamma) as γ→1−\gamma \to 1^-.
const ring = await wl`{{0, 1, 0}, {0, 0, 1}, {1, 0, 0}}`
await show`With[{v = {Subscript[v, A], Subscript[v, B], Subscript[v, C]}, r = {0, 0, 1}},
  {Solve[Append[Thread[v == r - 1/3 + ${ring} . v], Total[v] == 0], v],
   Limit[Simplify[Inverse[IdentityMatrix[3] - \[Gamma] ${ring}] . r - 1/3/(1 - \[Gamma])], \[Gamma] -> 1, Direction -> "FromBelow"]}]`
(centered solution, lim⁡γ→1−(𝐯γ−1/31−γ𝟏))={{{vA→−13,vB→0,vC→13}},{−13,0,13}} \begin{aligned} &\Bigl(\text{centered solution},\ \lim_{\gamma \to 1^-}\bigl(\mathbf{v}_\gamma - \tfrac{1/3}{1 - \gamma}\mathbf{1}\bigr)\Bigr) \\ &\quad = \left\{\left\{\left\{v_A\to -\frac{1}{3},v_B\to 0,v_C\to \frac{1}{3}\right\}\right\},\left\{-\frac{1}{3},0,\frac{1}{3}\right\}\right\} \end{aligned}

Exercise 10.10. In the ring of Exercise 10.9, suppose R¯\bar R already equals 1/31/3 and the values are the true differential values. Write the sequences of errors Rt+1−R¯R_{t+1} - \bar R and δt\delta_t starting from AA. Which would give the more stable estimate of the average reward if R¯\bar R were allowed to change?

SolutionSix steps from AA: first Rt+1−R¯R_{t+1} - \bar R, then the differential TD error with the true values.
await show`With[{v = {-1/3, 0, 1/3}, r = {0, 0, 1}, states = {1, 2, 3, 1, 2, 3, 1}},
  {Table[r[[states[[t]]]] - 1/3, {t, 6}], Table[r[[states[[t]]]] - 1/3 + v[[states[[t + 1]]]] - v[[states[[t]]]], {t, 6}]}]`
(Rt+1−R¯δt)t=0,…,5=(−13−1323−13−1323000000) \begin{pmatrix} R_{t+1} - \bar R \\ \delta_t \end{pmatrix}_{t = 0, \dots, 5} = \left( \begin{array}{cccccc} -\frac{1}{3} & -\frac{1}{3} & \frac{2}{3} & -\frac{1}{3} & -\frac{1}{3} & \frac{2}{3} \\ 0 & 0 & 0 & 0 & 0 & 0 \\ \end{array} \right)

The raw error cycles through −13,−13,23-\frac{1}{3}, -\frac{1}{3}, \frac{2}{3}: an update R¯←R¯+β(R−R¯)\bar R \leftarrow \bar R + \beta(R - \bar R) keeps oscillating around 1/31/3 with amplitude proportional to β\beta. The TD error is identically zero, so R¯\bar R stays put. In general δt\delta_t removes from Rt+1−R¯R_{t+1} - \bar R the part that the current state predicts, v^(St+1)−v^(St)\hat v(S_{t+1}) - \hat v(S_t), which is exactly the regular, non-random part of the fluctuation.

Exercise 10.11. An MDP produces the reward sequence +1,0,+1,0,…+1, 0, +1, 0, \dots from state AA and 0,+1,0,+1,…0, +1, 0, +1, \dots from state BB, under every policy. Show that r(π)=1/2r(\pi) = 1/2 even though the limit lim⁡t𝔼[Rt]\lim_t\mathbb{E}[R_t] does not exist, and compute the values of AA and BB under the definition vπ(s)≐lim⁡γ→1lim⁡h→∞∑t=0hγt(𝔼π[Rt+1∣S0=s]−r(π))v_\pi(s) \doteq \lim_{\gamma \to 1}\lim_{h \to \infty}\sum_{t=0}^h\gamma^t\bigl(\mathbb{E}_\pi[R_{t+1} \mid S_0 = s] - r(\pi)\bigr).

Solution

The Cesàro averages 1h∑t≤h𝔼[Rt]\frac{1}{h}\sum_{t \le h}\mathbb{E}[R_t] tend to 1/21/2, which is the first form of (10.4). From AA the centered rewards are +12,−12,…+\frac{1}{2}, -\frac{1}{2}, \dots, i.e. 12(−1)t\frac{1}{2}(-1)^t, and from BB their negatives:

The inner sums are 12(1+γ)\frac{1}{2(1 + \gamma)} and its negative; their limits as γ→1−\gamma \to 1^-.
await show`Limit[{Sum[\[Gamma]^t (-1)^t/2, {t, 0, Infinity}], Sum[-\[Gamma]^t (-1)^t/2, {t, 0, Infinity}]}, \[Gamma] -> 1, Direction -> "FromBelow"]`
(v(A),v(B))={14,−14} \bigl(v(A), v(B)\bigr) = \left\{\frac{1}{4},-\frac{1}{4}\right\}

So v(A)=14v(A) = \frac{1}{4} and v(B)=−14v(B) = -\frac{1}{4}. They differ by 12\frac{1}{2}, the advantage of collecting the first reward one step earlier, and average to zero, as differential values should.

Exercise 10.12. The estimate R¯\bar R in Algorithm 10.8 needs a small β\beta to average over the long run, but is then biased by its initial value for a long time; a sample average would adapt quickly at first but too slowly later, and cannot track a changing policy. Adapt the unbiased constant-step-size trick of Chapter 2 (step size β/o¯t\beta/\bar o_t with o¯t=o¯t−1+β(1−o¯t−1)\bar o_t = \bar o_{t-1} + \beta(1 - \bar o_{t-1}), o¯0=0\bar o_0 = 0) to the update of R¯\bar R.

Solution

Keep a scalar trace o¯\bar o, initialized to 00. Each time R¯\bar R is updated, first set o¯←o¯+β(1−o¯)\bar o \leftarrow \bar o + \beta(1 - \bar o), then R¯←R¯+(β/o¯) δ\bar R \leftarrow \bar R + (\beta/\bar o)\,\delta. At the first update o¯=β\bar o = \beta, the step is 11 and the initial R¯\bar R is forgotten completely; afterwards o¯→1\bar o \to 1 geometrically and the step tends to β\beta, so the estimate becomes an exponential recency-weighted average with no trace of its initialization, and it still tracks a slowly changing policy.

Exercise 10.13. Give pseudocode for a differential version of semi-gradient Q-learning, and explain why it is off-policy.

Solution

Replace the Sarsa target in Algorithm 10.5 by the greedy one: after taking AA (chosen ε\varepsilon-greedily) and observing R,S′R, S', set δ←R−R¯+max⁡aq^(S′,a,𝐰)−q^(S,A,𝐰)\delta \leftarrow R - \bar R + \max_{a}\hat q(S', a, \mathbf{w}) - \hat q(S, A, \mathbf{w}), then R¯←R¯+βδ\bar R \leftarrow \bar R + \beta\delta, 𝐰←𝐰+αδ∇q^(S,A,𝐰)\mathbf{w} \leftarrow \mathbf{w} + \alpha\delta\nabla\hat q(S, A, \mathbf{w}), and choose the next action ε\varepsilon-greedily in S′S'. The target evaluates the greedy policy while the data come from the ε\varepsilon-greedy one, so the method is off-policy: in the tabular case it is Differential Q-learning of Section 10.6.1 with β=ηα\beta = \eta\alpha, and with function approximation it inherits the difficulties of Chapter 11.