Chapter 3

Finite Markov Decision Processes

A bandit has one situation and many actions; the problems Sutton and Barto study have many situations, and the action taken in one of them decides which ones come next. The Markov decision process (MDP) is the mathematical form of that problem: rewards are still evaluative, as in Chapter 2, but they are now delayed, because an action changes the state and therefore every reward that follows.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 This chapter sets up the objects that every later chapter manipulates — dynamics, returns, policies, value functions — and derives the two families of equations that tie them together: the Bellman equations for a fixed policy, which are linear, and the Bellman optimality equations, which are not. Everything in the rest of the book is a way of solving one of them, exactly or approximately, with or without a model.

3.1The Agent–Environment Interface

An agent interacts with an environment at discrete times t=0,1,2,…t = 0, 1, 2, \dots. At time tt it observes a state St∈𝒮S_t \in \mathcal{S}, chooses an action At∈𝒜(St)A_t \in \mathcal{A}(S_t), and one step later receives a reward Rt+1∈ℛ⊂ℝR_{t+1} \in \mathcal{R} \subset \mathbb{R} together with the next state St+1S_{t+1}. The interaction produces a trajectory

S0,A0,R1,S1,A1,R2,S2,A2,R3,… S_0, A_0, R_1, S_1, A_1, R_2, S_2, A_2, R_3, \dots (3.1)

Reward and next state carry the same index t+1t+1 because they are produced jointly by the environment’s response to AtA_t.Both conventions, RtR_t and Rt+1R_{t+1} for the reward due to AtA_t, are common in the literature. Sutton and Barto’s choice makes the joint distribution of (St+1,Rt+1)(S_{t+1}, R_{t+1}) the primitive object, which is what sample-based methods observe.

Definition 3.1 (Finite Markov Decision Process). A finite MDP consists of finite sets of states 𝒮\mathcal{S} (with 𝒮+=𝒮∪{terminal}\mathcal{S}^+ = \mathcal{S} \cup \{\text{terminal}\} in episodic tasks), actions 𝒜(s)\mathcal{A}(s) for each s∈𝒮s \in \mathcal{S}, and rewards ℛ⊂ℝ\mathcal{R} \subset \mathbb{R}, together with a dynamics function p:𝒮+×ℛ×𝒮×𝒜→[0,1]p : \mathcal{S}^+ \times \mathcal{R} \times \mathcal{S} \times \mathcal{A} \to [0, 1],

p(s′,r∣s,a)≐Pr⁡{St=s′,Rt=r∣St−1=s,At−1=a},∑s′∈𝒮+∑r∈ℛp(s′,r∣s,a)=1for all s,a, \begin{gathered} p(s', r \mid s, a) \doteq \Pr\{S_t = s', R_t = r \mid S_{t-1} = s, A_{t-1} = a\}, \\ \sum_{s' \in \mathcal{S}^+} \sum_{r \in \mathcal{R}} p(s', r \mid s, a) = 1 \quad \text{for all } s, a, \end{gathered} (3.2)

such that the conditional law of (St,Rt)(S_t, R_t) given the whole past S0,A0,R1,…,St−1,At−1S_0, A_0, R_1, \dots, S_{t-1}, A_{t-1} depends on it only through (St−1,At−1)(S_{t-1}, A_{t-1}), and is given by pp.

The last clause is the Markov property. It is best read as a requirement on the state, not on the world: the state must summarize everything about the past interaction that matters for the future. A robot whose state is its position but not its velocity lives in a non-Markov process; adding the velocity restores the property. Throughout Part I we assume that the state given to the agent is Markov; Chapter 16 returns to what can be done when it is not.

The four-argument pp determines every other quantity one might want, and three of them recur:

p(s′∣s,a)=∑rp(s′,r∣s,a),r(s,a)=∑rr∑s′p(s′,r∣s,a),r(s,a,s′)=∑rr p(s′,r∣s,a)p(s′∣s,a). \begin{aligned} p(s' \mid s, a) &= \sum_{r} p(s', r \mid s, a), \\ r(s, a) &= \sum_{r} r \sum_{s'} p(s', r \mid s, a), \\ r(s, a, s') &= \frac{\sum_{r} r\, p(s', r \mid s, a)}{p(s' \mid s, a)}. \end{aligned} (3.3)

These are the state-transition probabilities, the expected reward of a state–action pair, and the expected reward of a transition. Classical MDP texts start from p(s′∣s,a)p(s' \mid s, a) and r(s,a)r(s, a); the four-argument form is preferred in reinforcement learning because learning methods see sampled rewards, not their expectations.The theory of MDPs is older than reinforcement learning by three decades: Bellman’s Dynamic Programming (1957) and Howard’s policy iteration (1960) are the sources, and Puterman (1994) is the standard modern reference for the finite case.Richard Bellman. Dynamic Programming. Princeton University Press, Princeton, NJ, 1957. A Rand Corporation Research Study; xxv+342 pp.; Ronald A. Howard. Dynamic Programming and Markov Processes. MIT Press, Cambridge, MA, 1960; and 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

Where to draw the boundary between agent and environment is a modeling choice with one firm rule: anything the agent cannot change arbitrarily belongs to the environment. A robot’s motors, its sensors, and even the computation of its reward are outside the agent. The boundary marks the limit of the agent’s control, not of its knowledge — an agent may know its environment completely, as we know the rules of chess, and still face a hard problem.

Example 3.2 (The Recycling Robot). A can-collecting robot has two battery levels, 𝒮={𝗁𝗂𝗀𝗁,𝗅𝗈𝗐}\mathcal{S} = \{\mathsf{high}, \mathsf{low}\}, with 𝒜(𝗁𝗂𝗀𝗁)={𝗌𝖾𝖺𝗋𝖼𝗁,𝗐𝖺𝗂𝗍}\mathcal{A}(\mathsf{high}) = \{\mathsf{search}, \mathsf{wait}\} and 𝒜(𝗅𝗈𝗐)={𝗌𝖾𝖺𝗋𝖼𝗁,𝗐𝖺𝗂𝗍,𝗋𝖾𝖼𝗁𝖺𝗋𝗀𝖾}\mathcal{A}(\mathsf{low}) = \{\mathsf{search}, \mathsf{wait}, \mathsf{recharge}\}. Searching yields rsearchr_{\text{search}} cans, waiting rwait<rsearchr_{\text{wait}} < r_{\text{search}}; recharging yields nothing. A search from 𝗁𝗂𝗀𝗁\mathsf{high} keeps the battery high with probability α\alpha; a search from 𝗅𝗈𝗐\mathsf{low} keeps it low with probability β\beta, and otherwise the battery dies, the robot is rescued (reward −3-3), and it restarts with a full battery. Treating the number of cans as deterministic for simplicity, the dynamics p(s′,r∣s,a)p(s', r \mid s, a) have these nonzero entries:

ssaa(s′,r)(s', r) with probability
𝗁𝗂𝗀𝗁\mathsf{high}𝗌𝖾𝖺𝗋𝖼𝗁\mathsf{search}(𝗁𝗂𝗀𝗁,rsearch)(\mathsf{high}, r_{\text{search}}): α\alpha; (𝗅𝗈𝗐,rsearch)(\mathsf{low}, r_{\text{search}}): 1−α1 - \alpha
𝗁𝗂𝗀𝗁\mathsf{high}𝗐𝖺𝗂𝗍\mathsf{wait}(𝗁𝗂𝗀𝗁,rwait)(\mathsf{high}, r_{\text{wait}}): 11
𝗅𝗈𝗐\mathsf{low}𝗌𝖾𝖺𝗋𝖼𝗁\mathsf{search}(𝗁𝗂𝗀𝗁,−3)(\mathsf{high}, -3): 1−β1 - \beta; (𝗅𝗈𝗐,rsearch)(\mathsf{low}, r_{\text{search}}): β\beta
𝗅𝗈𝗐\mathsf{low}𝗐𝖺𝗂𝗍\mathsf{wait}(𝗅𝗈𝗐,rwait)(\mathsf{low}, r_{\text{wait}}): 11
𝗅𝗈𝗐\mathsf{low}𝗋𝖾𝖼𝗁𝖺𝗋𝗀𝖾\mathsf{recharge}(𝗁𝗂𝗀𝗁,0)(\mathsf{high}, 0): 11

3.1.1In Wolfram Language

We encode the robot once, as a list of outcomes {s′,r,p}\{s', r, p\} for each of the five state–action pairs (states numbered 1=𝗁𝗂𝗀𝗁1 = \mathsf{high}, 2=𝗅𝗈𝗐2 = \mathsf{low}), and let the kernel derive (3.3) from it.

For each pair (s,a)(s, a) in the order of the table: total probability, the transition row (p(𝗁𝗂𝗀𝗁∣s,a),p(𝗅𝗈𝗐∣s,a))\bigl(p(\mathsf{high} \mid s, a), p(\mathsf{low} \mid s, a)\bigr), and the expected reward r(s,a)r(s, a). Every row sums to 11, as (3.2) demands.
const robotDyn = await wl`{
  {{1, Subscript[r, search], \[Alpha]}, {2, Subscript[r, search], 1 - \[Alpha]}},
  {{1, Subscript[r, wait], 1}},
  {{1, -3, 1 - \[Beta]}, {2, Subscript[r, search], \[Beta]}},
  {{2, Subscript[r, wait], 1}},
  {{1, 0, 1}}}`
const robotDerived = await show`Map[Function[o, {Total[o[[All, 3]]],
    Total[Select[o, Function[x, x[[1]] == 1]][[All, 3]]],
    Total[Select[o, Function[x, x[[1]] == 2]][[All, 3]]],
    Simplify[o[[All, 2]] . o[[All, 3]]]}], ${robotDyn}]`
(∑p, p(𝗁𝗂𝗀𝗁∣s,a), p(𝗅𝗈𝗐∣s,a), r(s,a))(s,a)=(1α1−αrsearch110rwait11−ββ−3+3β+βrsearch101rwait1100) \begin{aligned} &\Bigl(\textstyle\sum p,\ p(\mathsf{high} \mid s, a),\ p(\mathsf{low} \mid s, a),\ r(s, a)\Bigr)_{(s, a)} \\ &= \left( \begin{array}{cccc} 1 & \alpha & 1-\alpha & r_{\text{search}} \\ 1 & 1 & 0 & r_{\text{wait}} \\ 1 & 1-\beta & \beta & -3+3 \beta +\beta r_{\text{search}} \\ 1 & 0 & 1 & r_{\text{wait}} \\ 1 & 1 & 0 & 0 \\ \end{array} \right) \end{aligned}

3.2Goals, Rewards, and Returns

The goal of the agent is encoded entirely in the scalar reward. Sutton and Barto, crediting Michael Littman, state this as the reward hypothesis: “all of what we mean by goals and purposes can be well thought of as the maximization of the expected value of the cumulative sum of a received scalar signal (called reward).”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 A corollary is a discipline in designing rewards: they should say what we want achieved, not how. A chess program rewarded for capturing pieces will learn to capture pieces, even at the price of the game; prior knowledge about how to win belongs in the initial policy or value function, not in the reward. Whether the hypothesis is actually true turns out to be a mathematical question, answered only recently (Section 3.6).

What exactly is maximized is the expected return GtG_t, a function of the rewards after tt. In episodic tasks the interaction breaks into episodes that end at a random time TT in a terminal state, and the return can be the plain sum Rt+1+⋯+RTR_{t+1} + \dots + R_T. In continuing tasks there is no end, and a plain sum may diverge; the remedy is discounting with a rate 0≤γ≤10 \le \gamma \le 1:

Gt≐Rt+1+γRt+2+γ2Rt+3+⋯=∑k=0∞γkRt+k+1. G_t \doteq R_{t+1} + \gamma R_{t+2} + \gamma^2 R_{t+3} + \dots = \sum_{k=0}^\infty \gamma^k R_{t+k+1}. (3.4)

With γ<1\gamma < 1 and rewards bounded by Rmax⁡R_{\max}, ∣Gt∣≤Rmax⁡/(1−γ)\lvert G_t \rvert \le R_{\max}/(1 - \gamma). A reward kk steps ahead is worth γk\gamma^{k} times its face value; γ=0\gamma = 0 makes the agent myopic, and γ→1\gamma \to 1 makes it far-sighted. The single most useful fact about returns is that they are recursive:

Gt=Rt+1+γGt+1. G_t = R_{t+1} + \gamma G_{t+1}. (3.5)

Every bootstrapping method in the book — dynamic programming, TD learning, Q-learning — is an expectation, or a sample, of this identity.

3.2.1One Notation for Both Kinds of Task

The two cases merge if termination is treated as entry into an absorbing state that loops on itself with reward 00. The sequence +1,+1,+1+1, +1, +1 then becomes +1,+1,+1,0,0,…+1, +1, +1, 0, 0, \dots, and its sum, discounted or not, is unchanged. So we may always write

Gt≐∑k=t+1Tγk−t−1Rk, G_t \doteq \sum_{k = t+1}^{T} \gamma^{k - t - 1} R_k, (3.6)

allowing T=∞T = \infty or γ=1\gamma = 1, but not both; (3.5) then holds for all t<Tt < T with the convention GT=0G_T = 0. With the absorbing state the terminal value is automatically 00, which is why every algorithm in the book initializes V(terminal)=0V(\text{terminal}) = 0 and never updates it.The MDP literature speaks of finite-horizon, indefinite-horizon (terminating, but at no fixed time), and infinite-horizon problems. Episodic and continuing tasks are close to the last two; the book prefers its terms because they describe the interaction rather than the objective. Chapter 10 adds a third objective, the average reward, for continuing tasks without discounting.

3.3Policies and Value Functions

A policy π\pi maps each state to a distribution over actions: π(a∣s)\pi(a \mid s) is the probability that At=aA_t = a when St=sS_t = s. Together with pp it determines the distribution of the whole trajectory, and we write 𝔼π\mathbb{E}_\pi for expectations under it. The state-value function and action-value function of π\pi are

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

for s∈𝒮s \in \mathcal{S}, a∈𝒜(s)a \in \mathcal{A}(s); the terminal state has value 00. By the Markov property and the stationarity of π\pi and pp, neither depends on tt. Conditioning on the first action, and then on the first transition, gives the two relations that connect them:

vπ(s)=∑aπ(a∣s) qπ(s,a),qπ(s,a)=∑s′,rp(s′,r∣s,a)[r+γvπ(s′)]. \begin{aligned} v_\pi(s) &= \sum_a \pi(a \mid s)\, q_\pi(s, a), \\ q_\pi(s, a) &= \sum_{s', r} p(s', r \mid s, a)\bigl[r + \gamma v_\pi(s')\bigr]. \end{aligned} (3.8)

The first is an average over the agent’s choice, the second an average over the environment’s response. Substituting each into the other gives the central equations of the chapter.

Proposition 3.3 (Bellman Equations for a Policy). For every policy π\pi, every s∈𝒮s \in \mathcal{S}, and every a∈𝒜(s)a \in \mathcal{A}(s),

vπ(s)=∑aπ(a∣s)∑s′,rp(s′,r∣s,a)[r+γvπ(s′)], v_\pi(s) = \sum_a \pi(a \mid s) \sum_{s', r} p(s', r \mid s, a)\bigl[r + \gamma v_\pi(s')\bigr], (3.9)qπ(s,a)=∑s′,rp(s′,r∣s,a)[r+γ∑a′π(a′∣s′) qπ(s′,a′)]. q_\pi(s, a) = \sum_{s', r} p(s', r \mid s, a)\Bigl[r + \gamma \sum_{a'} \pi(a' \mid s')\, q_\pi(s', a')\Bigr]. (3.10)

Proof. By (3.5) and linearity, vπ(s)=𝔼π[Rt+1∣St=s]+γ 𝔼π[Gt+1∣St=s]v_\pi(s) = \mathbb{E}_\pi[R_{t+1} \mid S_t = s] + \gamma\, \mathbb{E}_\pi[G_{t+1} \mid S_t = s]. Condition both terms on (At,St+1,Rt+1)=(a,s′,r)(A_t, S_{t+1}, R_{t+1}) = (a, s', r), which has probability π(a∣s) p(s′,r∣s,a)\pi(a \mid s)\,p(s', r \mid s, a) given St=sS_t = s. The first term becomes ∑a,s′,rπ(a∣s) p(s′,r∣s,a) r\sum_{a, s', r} \pi(a \mid s)\,p(s', r \mid s, a)\, r. For the second we need

𝔼π[Gt+1∣St=s,At=a,Rt+1=r,St+1=s′]=𝔼π[Gt+1∣St+1=s′]=vπ(s′). \mathbb{E}_\pi[G_{t+1} \mid S_t = s, A_t = a, R_{t+1} = r, S_{t+1} = s'] = \mathbb{E}_\pi[G_{t+1} \mid S_{t+1} = s'] = v_\pi(s').

This is where the Markov property enters: Gt+1G_{t+1} is a function of At+1,Rt+2,St+2,…A_{t+1}, R_{t+2}, S_{t+2}, \dots, whose joint law given the past is generated by π\pi and pp from St+1S_{t+1} alone, so the extra conditioning on (s,a,r)(s, a, r) is irrelevant; and since π\pi and pp do not depend on time, the result is the same function vπv_\pi evaluated at s′s'. Adding the two terms gives (3.9). The same argument started from qπ(s,a)q_\pi(s, a), with At=aA_t = a fixed rather than averaged, gives 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')], and substituting the first relation of (3.8) for vπ(s′)v_\pi(s') gives (3.10). □

Read (3.9) as an expectation over the triple (a,s′,r)(a, s', r) with probability π(a∣s)p(s′,r∣s,a)\pi(a \mid s)p(s', r \mid s, a): the value of a state is the expected immediate reward plus the discounted value of where you land. A backup diagram draws this as a two-level tree: an open circle for the state ss at the root, filled dots for the actions it may take (branching by π\pi), and open circles for the successor states (branching by pp, each edge carrying a reward). Value information flows up the tree, from leaves to root; that transfer is a backup, and every algorithm of Part I is characterized by the shape of the tree it backs up and by whether it averages over all branches or samples one (Figure 3.1).

Four tree diagrams. The v-pi tree has an open state circle at the top, three filled action dots below it, and two open successor-state circles under each action. The q-pi tree starts at a filled state-action dot, branches to two successor states, and each of these to three actions. The v-star and q-star trees have the same shapes with an arc drawn across the branches at the agent's choice points, labeled max.
Figure 3.1: Backup diagrams. Open circles are states, filled dots are state–action pairs; branching below a state is the agent’s choice (weighted by π\pi, or maximized where an arc is drawn), branching below a state–action pair is the environment’s response pp, which also emits the reward rr. From left to right: the Bellman equations (3.9) and (3.10), and the optimality equations (3.17) and (3.18).

3.3.1The Bellman Equation as a Linear System

For a fixed π\pi, write 𝐫π(s)=∑aπ(a∣s) r(s,a)\mathbf{r}_\pi(s) = \sum_a \pi(a \mid s)\, r(s, a) for the expected one-step reward and 𝐏π(s,s′)=∑aπ(a∣s) p(s′∣s,a)\mathbf{P}_\pi(s, s') = \sum_a \pi(a \mid s)\, p(s' \mid s, a) for the state-to-state transition matrix of the Markov chain that π\pi induces (restricted to nonterminal states in episodic tasks). Then (3.9) is the linear system

𝐯π=𝐫π+γ𝐏π𝐯π. \mathbf{v}_\pi = \mathbf{r}_\pi + \gamma \mathbf{P}_\pi \mathbf{v}_\pi. (3.11)

Proposition 3.4 (Unique Solution of the Bellman Equation). If γ<1\gamma < 1, the matrix 𝐈−γ𝐏π\mathbf{I} - \gamma\mathbf{P}_\pi is invertible and

𝐯π=(𝐈−γ𝐏π)−1𝐫π=∑k=0∞γk𝐏πk 𝐫π. \mathbf{v}_\pi = (\mathbf{I} - \gamma\mathbf{P}_\pi)^{-1}\mathbf{r}_\pi = \sum_{k=0}^\infty \gamma^k \mathbf{P}_\pi^k\, \mathbf{r}_\pi. (3.12)

The same holds for γ=1\gamma = 1 in an episodic task if π\pi is proper, that is, reaches the terminal state with probability one from every state.

Proof. Rows of 𝐏π\mathbf{P}_\pi are nonnegative and sum to at most 11, so ∥𝐏π𝐱∥∞≤∥𝐱∥∞\lVert \mathbf{P}_\pi \mathbf{x} \rVert_\infty \le \lVert \mathbf{x} \rVert_\infty. If (𝐈−γ𝐏π)𝐱=𝟎(\mathbf{I} - \gamma \mathbf{P}_\pi)\mathbf{x} = \mathbf{0}, then ∥𝐱∥∞=γ∥𝐏π𝐱∥∞≤γ∥𝐱∥∞\lVert \mathbf{x} \rVert_\infty = \gamma \lVert \mathbf{P}_\pi \mathbf{x} \rVert_\infty \le \gamma \lVert \mathbf{x} \rVert_\infty, forcing 𝐱=𝟎\mathbf{x} = \mathbf{0}; and ∥γk𝐏πk∥∞≤γk\lVert \gamma^k \mathbf{P}_\pi^k \rVert_\infty \le \gamma^k makes the Neumann series converge to the inverse. In the proper case, 𝐏πk(s,⋅)\mathbf{P}_\pi^k(s, \cdot) sums to the probability of not having terminated after kk steps; properness of a finite chain makes this decay geometrically, so again ∑k𝐏πk\sum_k \mathbf{P}_\pi^k converges and inverts 𝐈−𝐏π\mathbf{I} - \mathbf{P}_\pi. □

The series is just the definition of vπv_\pi read again: (𝐏πk𝐫π)(s)=𝔼π[Rt+k+1∣St=s](\mathbf{P}_\pi^k \mathbf{r}_\pi)(s) = \mathbb{E}_\pi[R_{t+k+1} \mid S_t = s], so (3.12) is ∑kγk𝔼π[Rt+k+1∣St=s]\sum_k \gamma^k \mathbb{E}_\pi[R_{t+k+1} \mid S_t = s]. Solving ∣𝒮∣\lvert\mathcal{S}\rvert linear equations costs O(∣𝒮∣3)O(\lvert\mathcal{S}\rvert^3); Chapter 4 replaces the direct solution by iteration, and the rest of the book replaces the known 𝐏π\mathbf{P}_\pi and 𝐫π\mathbf{r}_\pi by samples.

It is convenient to name the right-hand sides of the Bellman equations as operators on value functions v:𝒮→ℝv : \mathcal{S} \to \mathbb{R}:

(Tπv)(s)≐∑aπ(a∣s)∑s′,rp(s′,r∣s,a)[r+γv(s′)],(T∗v)(s)≐max⁡a∑s′,rp(s′,r∣s,a)[r+γv(s′)]. \begin{aligned} (T_\pi v)(s) &\doteq \sum_a \pi(a \mid s) \sum_{s', r} p(s', r \mid s, a)\bigl[r + \gamma v(s')\bigr], \\ (T_* v)(s) &\doteq \max_a \sum_{s', r} p(s', r \mid s, a)\bigl[r + \gamma v(s')\bigr]. \end{aligned} (3.13)

In vector form Tπ𝐯=𝐫π+γ𝐏π𝐯T_\pi \mathbf{v} = \mathbf{r}_\pi + \gamma\mathbf{P}_\pi\mathbf{v}, and vπv_\pi is the unique fixed point of TπT_\pi. Two properties are immediate: both operators are monotone (v≤uv \le u pointwise implies Tv≤TuTv \le Tu, since 𝐏π≥0\mathbf{P}_\pi \ge 0), and iterating TπT_\pi from any vv gives

Tπk𝐯=∑j=0k−1γj𝐏πj𝐫π+γk𝐏πk𝐯  ⟶  𝐯π(k→∞), T_\pi^k \mathbf{v} = \sum_{j=0}^{k-1} \gamma^j \mathbf{P}_\pi^j \mathbf{r}_\pi + \gamma^k \mathbf{P}_\pi^k \mathbf{v} \;\longrightarrow\; \mathbf{v}_\pi \quad (k \to \infty), (3.14)

the expected kk-step return with vv as a terminal payoff. The second operator, T∗T_*, is the subject of Section 3.4.

3.3.2In Wolfram Language

We first evaluate a policy of the recycling robot in closed form. Actions are numbered as in the table, so the policy “search when high, recharge when low” is {1,3}\{1, 3\}.

A generic evaluator: assemble 𝐏π\mathbf{P}_\pi and 𝐫π\mathbf{r}_\pi for a deterministic policy and return (𝐈−γ𝐏π)−1𝐫π(\mathbf{I} - \gamma\mathbf{P}_\pi)^{-1}\mathbf{r}_\pi, here for π=(𝗌𝖾𝖺𝗋𝖼𝗁,𝗋𝖾𝖼𝗁𝖺𝗋𝗀𝖾)\pi = (\mathsf{search}, \mathsf{recharge}).
const robotP = await wl`{{{\[Alpha], 1 - \[Alpha]}, {1, 0}}, {{1 - \[Beta], \[Beta]}, {0, 1}, {1, 0}}}`
const robotR = await wl`{{Subscript[r, search], Subscript[r, wait]}, {\[Beta] Subscript[r, search] - 3 (1 - \[Beta]), Subscript[r, wait], 0}}`
const robotEval = await wl`Function[{pol}, Simplify[Inverse[IdentityMatrix[2] - \[Gamma] Table[${robotP}[[s, pol[[s]]]], {s, 2}]] . Table[${robotR}[[s, pol[[s]]]], {s, 2}]]]`
const vSearchRecharge = await show`${robotEval}[{1, 3}]`
𝐯(𝗌𝖾𝖺𝗋𝖼𝗁,𝗋𝖾𝖼𝗁𝖺𝗋𝗀𝖾)={rsearch(−1+γ)(−1+(−1+α)γ),γrsearch(−1+γ)(−1+(−1+α)γ)} \mathbf{v}_{(\mathsf{search}, \mathsf{recharge})} = \left\{\frac{r_{\text{search}}}{(-1+\gamma ) (-1+(-1+\alpha ) \gamma )},\frac{\gamma r_{\text{search}}}{(-1+\gamma ) (-1+(-1+\alpha ) \gamma )}\right\}

Recharging when low gives vπ(𝗅𝗈𝗐)=γ vπ(𝗁𝗂𝗀𝗁)v_\pi(\mathsf{low}) = \gamma\, v_\pi(\mathsf{high}) exactly — one step of delay, no reward — and vπ(𝗁𝗂𝗀𝗁)v_\pi(\mathsf{high}) is the per-step reward rsearchr_{\text{search}} inflated by 1/(1−γ)1/(1 - \gamma) and deflated by the factor 1+γ(1−α)1 + \gamma(1 - \alpha) that accounts for the steps spent recharging. From vπv_\pi we get qπq_\pi by the second relation of (3.8), and the differences qπ(s,a)−vπ(s)q_\pi(s, a) - v_\pi(s) for the actions π\pi does not take tell us whether a single deviation would pay.

qπ(s,a)−vπ(s)q_\pi(s, a) - v_\pi(s) for (𝗁𝗂𝗀𝗁,𝗐𝖺𝗂𝗍)(\mathsf{high}, \mathsf{wait}), (𝗅𝗈𝗐,𝗌𝖾𝖺𝗋𝖼𝗁)(\mathsf{low}, \mathsf{search}), (𝗅𝗈𝗐,𝗐𝖺𝗂𝗍)(\mathsf{low}, \mathsf{wait}), with rsearchr_{\text{search}} written as reff(1+γ(1−α))r_{\text{eff}}\bigl(1 + \gamma(1 - \alpha)\bigr). If all three are ≤0\le 0, no one-step deviation helps; we shall see that this makes π\pi optimal.
const robotQ = await wl`Function[{v}, Table[${robotR}[[s, a]] + \[Gamma] ${robotP}[[s, a]] . v, {s, 2}, {a, Length[${robotR}[[s]]]}]]`
const robotAdv = await show`With[{q = ${robotQ}[${vSearchRecharge}], v = ${vSearchRecharge}}, FullSimplify[{q[[1, 2]] - v[[1]], q[[2, 1]] - v[[2]], q[[2, 2]] - v[[2]]} /. Subscript[r, search] -> Subscript[r, eff] (1 + \[Gamma] (1 - \[Alpha]))]]`
(qπ(h,𝗐)−vπ(h), qπ(l,𝗌)−vπ(l), qπ(l,𝗐)−vπ(l))={−reff+rwait,3(−1+β)+(β−αβγ)reff,−γreff+rwait} \begin{aligned} &\bigl(q_\pi(h, \mathsf{w}) - v_\pi(h),\ q_\pi(l, \mathsf{s}) - v_\pi(l),\ q_\pi(l, \mathsf{w}) - v_\pi(l)\bigr) \\ &= \left\{-r_{\text{eff}}+r_{\text{wait}},3 (-1+\beta )+(\beta -\alpha \beta \gamma ) r_{\text{eff}},-\gamma r_{\text{eff}}+r_{\text{wait}}\right\} \end{aligned}

Each condition is readable: waiting when high is worse if rwaitr_{\text{wait}} is below the effective per-step reward reff=rsearch/(1+γ(1−α))r_{\text{eff}} = r_{\text{search}}/(1 + \gamma(1-\alpha)) of the search–recharge cycle, waiting when low is worse if rwait<γ reffr_{\text{wait}} < \gamma\, r_{\text{eff}}, and searching when low is worse if the expected rescue penalty 3(1−β)3(1 - \beta) exceeds β(1−αγ) reff\beta(1 - \alpha\gamma)\, r_{\text{eff}}. The general two-state chain shows the structure of (3.12) with nothing specific to robots.

𝐯π=(𝐈−γ𝐏)−1𝐫\mathbf{v}_\pi = (\mathbf{I} - \gamma\mathbf{P})^{-1}\mathbf{r} for a two-state chain that stays in state 11 with probability pp and in state 22 with probability qq.
const chainP = await wl`{{p, 1 - p}, {1 - q, q}}`
const chainR = await wl`{Subscript[r, 1], Subscript[r, 2]}`
const chainV = await show`Simplify[Inverse[IdentityMatrix[2] - \[Gamma] ${chainP}] . ${chainR}]`
𝐯π={(1−qγ)r1−(−1+p)γr2(−1+γ)(−1+(−1+p+q)γ),(γ−qγ)r1+(1−pγ)r2(−1+γ)(−1+(−1+p+q)γ)} \mathbf{v}_\pi = \left\{\frac{(1-q \gamma ) r_1-(-1+p) \gamma r_2}{(-1+\gamma ) (-1+(-1+p+q) \gamma )},\frac{(\gamma -q \gamma ) r_1+(1-p \gamma ) r_2}{(-1+\gamma ) (-1+(-1+p+q) \gamma )}\right\}

The common factor 1/(1−γ)1/(1 - \gamma) says that the values blow up as γ→1\gamma \to 1, at a rate set by the long-run average reward. Multiplying by 1−γ1 - \gamma isolates that rate.

lim⁡γ→1(1−γ)𝐯π\lim_{\gamma \to 1}(1 - \gamma)\mathbf{v}_\pi: both components tend to the same number, the reward averaged over the chain’s stationary distribution (1−q2−p−q,1−p2−p−q)\bigl(\tfrac{1-q}{2-p-q}, \tfrac{1-p}{2-p-q}\bigr).
const chainAvg = await show`Assuming[0 < p < 1 && 0 < q < 1, FullSimplify[Limit[(1 - \[Gamma]) ${chainV}, \[Gamma] -> 1]]]`
lim⁡γ→1(1−γ) 𝐯π={(−1+q)r1+(−1+p)r2−2+p+q,(−1+q)r1+(−1+p)r2−2+p+q} \lim_{\gamma \to 1}(1 - \gamma)\,\mathbf{v}_\pi = \left\{\frac{(-1+q) r_1+(-1+p) r_2}{-2+p+q},\frac{(-1+q) r_1+(-1+p) r_2}{-2+p+q}\right\}

This is the first appearance of the average reward r(π)r(\pi) of Section 10.3: as γ→1\gamma \to 1, vπ(s)=r(π)/(1−γ)+hπ(s)+o(1)v_\pi(s) = r(\pi)/(1 - \gamma) + h_\pi(s) + o(1), a constant that is the same in every state plus a state-dependent correction.This expansion is the starting point of Blackwell’s (1962) analysis of the discounted problem for γ\gamma near 11. He proved that a finite MDP has a stationary deterministic policy that is optimal simultaneously for all discount factors in some interval (γ0,1)(\gamma_0, 1), now called a Blackwell-optimal policy; such a policy is in particular optimal for the average-reward criterion.David Blackwell. Discrete Dynamic Programming. The Annals of Mathematical Statistics, 33(2):719–726, Institute of Mathematical Statistics, 1962. doi: 10.1214/aoms/1177704593

Finally, the gridworld of the book’s Example 3.5: a 5×55 \times 5 grid, four deterministic moves, reward −1-1 for bumping into the edge, and two special cells AA (row 1, column 2) and BB (row 1, column 4) that teleport the agent to A′A' and B′B' with rewards +10+10 and +5+5. For the equiprobable random policy and γ=9/10\gamma = 9/10, (3.11) is a 25×2525 \times 25 system with rational coefficients, which the kernel solves exactly.

Build 𝐏π\mathbf{P}_\pi and 𝐫π\mathbf{r}_\pi for the uniform random policy, solve exactly in rationals, and print to one decimal: these are the book’s values for Figure 3.2, now verified.
const gridStep = 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 gridMoves = await wl`{{-1, 0}, {1, 0}, {0, 1}, {0, -1}}`
const gridStates = await wl`Tuples[{Range[5], Range[5]}]`
const gridPr = await wl`Table[Total[Table[UnitVector[25, First[FirstPosition[${gridStates}, ${gridStep}[s, a][[1]]]]]/4, {a, ${gridMoves}}]], {s, ${gridStates}}]`
const gridRr = await wl`Table[Total[Table[${gridStep}[s, a][[2]]/4, {a, ${gridMoves}}]], {s, ${gridStates}}]`
const gridV = await wl`LinearSolve[IdentityMatrix[25] - 9/10 ${gridPr}, ${gridRr}]`
const gridShow = await show`NumberForm[N[Partition[${gridV}, 5]], {3, 1}]`
vπ=(3.38.84.45.31.51.53.02.31.90.50.10.70.70.4−0.4−1.0−0.4−0.4−0.6−1.2−1.9−1.3−1.2−1.4−2.0) v_\pi = \left( \begin{array}{ccccc} 3.3 & 8.8 & 4.4 & 5.3 & 1.5 \\ 1.5 & 3.0 & 2.3 & 1.9 & 0.5 \\ 0.1 & 0.7 & 0.7 & 0.4 & -0.4 \\ -1.0 & -0.4 & -0.4 & -0.6 & -1.2 \\ -1.9 & -1.3 & -1.2 & -1.4 & -2.0 \\ \end{array} \right)

The exact value of AA is the ratio of a 27-digit integer to a 26-digit one, about 8.7898.789: less than the immediate +10+10, because A′A' sits on the bottom edge where the random walk keeps bumping into the wall.

3.4Optimal Policies and Optimal Value Functions

Value functions order policies: π≥π′\pi \ge \pi' if vπ(s)≥vπ′(s)v_\pi(s) \ge v_{\pi'}(s) for every state. This is only a partial order — one policy may be better in some states and worse in others — so it is not obvious that a single policy can be best everywhere. Define the optimal value functions

v∗(s)≐max⁡πvπ(s),q∗(s,a)≐max⁡πqπ(s,a), v_*(s) \doteq \max_\pi v_\pi(s), \qquad q_*(s, a) \doteq \max_\pi q_\pi(s, a), (3.15)

pointwise, and call π∗\pi_* an optimal policy if vπ∗=v∗v_{\pi_*} = v_* in every state. Acting optimally after a first action aa gives

q∗(s,a)=∑s′,rp(s′,r∣s,a)[r+γv∗(s′)],v∗(s)=max⁡aq∗(s,a). q_*(s, a) = \sum_{s', r} p(s', r \mid s, a)\bigl[r + \gamma v_*(s')\bigr], \qquad v_*(s) = \max_{a} q_*(s, a). (3.16)

Combining the two in both orders gives the Bellman optimality equations:

v∗(s)=max⁡a∈𝒜(s)∑s′,rp(s′,r∣s,a)[r+γv∗(s′)], v_*(s) = \max_{a \in \mathcal{A}(s)} \sum_{s', r} p(s', r \mid s, a)\bigl[r + \gamma v_*(s')\bigr], (3.17)q∗(s,a)=∑s′,rp(s′,r∣s,a)[r+γmax⁡a′q∗(s′,a′)]. q_*(s, a) = \sum_{s', r} p(s', r \mid s, a)\Bigl[r + \gamma \max_{a'} q_*(s', a')\Bigr]. (3.18)

The first says v∗=T∗v∗v_* = T_* v_*. Unlike (3.9), it contains no policy at all, and because of the max⁡\max it is a nonlinear system of ∣𝒮∣\lvert\mathcal{S}\rvert equations. The heuristic derivation just given assumes that an optimal policy exists, which is what we must prove. The theorem below does the half that needs only this chapter’s tools; the other half, existence of a solution, is constructive and is the business of Chapter 4.

Theorem 3.5 (Verification Theorem for the Bellman Optimality Equation). Let γ<1\gamma < 1, and let vv be any function satisfying v=T∗vv = T_* v, that is, (3.17) with vv in place of v∗v_*. Then:

  1. v≥vπv \ge v_\pi for every policy π\pi;
  2. every policy π\pi that is greedy with respect to vv — that puts positive probability only on actions attaining the maximum in (T∗v)(s)(T_* v)(s) — satisfies vπ=vv_\pi = v.

Hence v=v∗v = v_*, the optimality equation has at most one solution, and any greedy policy, in particular a deterministic one, is optimal in every state simultaneously.

Proof. (1) For any π\pi, the average ∑aπ(a∣s)(⋅)\sum_a \pi(a \mid s)(\cdot) never exceeds the maximum, so v=T∗v≥Tπvv = T_* v \ge T_\pi v. Applying the monotone operator TπT_\pi repeatedly, v≥Tπv≥Tπ2v≥⋯≥Tπkvv \ge T_\pi v \ge T_\pi^2 v \ge \dots \ge T_\pi^k v, and by (3.14) the right side tends to vπv_\pi. (2) If π\pi is greedy with respect to vv, the average equals the maximum: Tπv=T∗v=vT_\pi v = T_* v = v. Then vv is a fixed point of TπT_\pi, whose only fixed point is vπv_\pi (Proposition 3.4). Together, v=vπ≥vπ′v = v_\pi \ge v_{\pi'} for every π′\pi', so v=max⁡π′vπ′=v∗v = \max_{\pi'} v_{\pi'} = v_* and π\pi is optimal. If vv and uu both solve the equation, both equal v∗v_*. □

Remark 3.6. For γ=1\gamma = 1 the argument needs every policy to be proper. Without that assumption greedy policies can fail: if some action leaves the state and reward unchanged (a “do nothing” move with reward 00), it attains the maximum in any undiscounted optimality equation, yet the policy that always takes it never terminates and collects nothing. The gambler’s problem of Chapter 4 has exactly this trap.

The theorem also explains why we restrict attention to stationary policies π(a∣s)\pi(a \mid s): part (1) holds just as well for policies that depend on the entire history, by the same induction on the horizon, so nothing is gained by remembering the past when the state is Markov.For finite discounted MDPs the optimal value over all history-dependent randomized policies equals v∗v_* and is attained by a stationary deterministic policy; see Puterman (1994), Chapter 6. The practical message is the one to remember: once v∗v_* is known, acting optimally is a one-step lookahead. The greedy action argmax⁡a∑s′,rp(s′,r∣s,a)[r+γv∗(s′)]\operatorname{argmax}_a \sum_{s', r} p(s', r \mid s, a)[r + \gamma v_*(s')] looks only at immediate consequences, yet is optimal in the long run, because v∗v_* has already folded all of the future into a number available locally. With q∗q_* even the lookahead disappears: argmax⁡aq∗(s,a)\operatorname{argmax}_a q_*(s, a) needs no model of the environment at all. This is why model-free control methods such as Q-learning (Chapter 6) estimate action values rather than state values: q∗q_* caches the result of every one-step search.

3.4.1In Wolfram Language

The recycling robot has 2×3=62 \times 3 = 6 deterministic policies, so we can check the theorem by brute force. We fix α=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 and evaluate every policy exactly.

Rows: the policy as (action in 𝗁𝗂𝗀𝗁\mathsf{high}, action in 𝗅𝗈𝗐\mathsf{low}) with 1=𝗌𝖾𝖺𝗋𝖼𝗁1 = \mathsf{search}, 2=𝗐𝖺𝗂𝗍2 = \mathsf{wait}, 3=𝗋𝖾𝖼𝗁𝖺𝗋𝗀𝖾3 = \mathsf{recharge}, then vπ(𝗁𝗂𝗀𝗁)v_\pi(\mathsf{high}) and vπ(𝗅𝗈𝗐)v_\pi(\mathsf{low}) in exact arithmetic. One row, (1,3)(1, 3), dominates all others in both states.
const robotNums = await wl`{\[Alpha] -> 4/5, \[Beta] -> 3/5, Subscript[r, search] -> 2, Subscript[r, wait] -> 1, \[Gamma] -> 9/10}`
const robotTable = await show`Table[Join[pol, ${robotEval}[pol] /. ${robotNums}], {pol, Tuples[{{1, 2}, {1, 2, 3}}]}]`
(π(𝗁𝗂𝗀𝗁), π(𝗅𝗈𝗐), vπ(𝗁𝗂𝗀𝗁), vπ(𝗅𝗈𝗐))=(1111584541295710131000599005921101802322101023109) \bigl(\pi(\mathsf{high}),\ \pi(\mathsf{low}),\ v_\pi(\mathsf{high}),\ v_\pi(\mathsf{low})\bigr) = \left( \begin{array}{cccc} 1 & 1 & \frac{115}{8} & \frac{45}{4} \\ 1 & 2 & \frac{95}{7} & 10 \\ 1 & 3 & \frac{1000}{59} & \frac{900}{59} \\ 2 & 1 & 10 & \frac{180}{23} \\ 2 & 2 & 10 & 10 \\ 2 & 3 & 10 & 9 \\ \end{array} \right)

That a single policy wins in every state at once is the content of the theorem: here v∗=(1000/59,900/59)≈(16.95,15.25)v_* = (1000/59, 900/59) \approx (16.95, 15.25). Plugging it into the optimality equation confirms that it is a fixed point of T∗T_* and that the greedy policy is the winner of the table.

The residual T∗v−vT_* v - v at v=v(1,3)v = v_{(1,3)}, and the greedy action in each state: {0,0}\{0, 0\} and {1,3}\{1, 3\}.
const robotBOE = await show`With[{q = ${robotQ}[${vSearchRecharge}] /. ${robotNums}, v = ${vSearchRecharge} /. ${robotNums}}, {Map[Max, q] - v, Map[First[Ordering[#, -1]] &, q]}]`
T∗v−vgreedy⁡(v)  =  (0013) \begin{matrix} T_* v - v \\ \operatorname{greedy}(v) \end{matrix} \;=\; \left( \begin{array}{cc} 0 & 0 \\ 1 & 3 \\ \end{array} \right)

3.5Optimality and Approximation

Solving (3.17) exactly requires three things that are rarely available together: an accurate model pp, a Markov state, and enough computation. Backgammon has a known model and a Markov state, but some 102010^{20} states, so its optimality equations cannot even be written down. Two constraints shape everything that follows. Computation per time step is limited, so exact solutions must be replaced by iterative approximations that can be interrupted. Memory is limited, so the tabular representation with one entry per state, used throughout Part I, must eventually give way to parameterized functions v^(s,𝐰)\hat v(s, \mathbf{w}) in Part II.

Reinforcement learning has one structural advantage in this situation: because it learns from the agent’s own experience, it spends its effort where the agent actually goes. Tesauro’s backgammon player plays superbly although its evaluations of positions that never arise between strong players may be poor. An approximation that is accurate on the states visited under good play and crude elsewhere is often all that is needed; this focusing by the on-policy distribution reappears as the weighting μ(s)\mu(s) of the prediction objective in Chapter 9.

3.6Beyond the Book: What Rewards Can Express

The reward hypothesis of Section 3.2 was, in the book, a working assumption. Since 2021 it has become a theorem with precise hypotheses, and with known counterexamples when they fail.Beyond Sutton and Barto (2018). Abel et al. (NeurIPS 2021) characterized which tasks a Markov reward can express; Bowling, Martin, Abel and Dabney (ICML 2023) gave axioms that are necessary and sufficient for goals to be representable as expected cumulative reward. Neither result is in the book.

What a Markov reward cannot say. Abel and coauthors formalize a “task” in three ways: a set of acceptable policies, a partial order on policies, or a partial order on trajectories. For each, they show that some environment–task pairs admit no Markov reward function r(s,a,s′)r(s, a, s') under which exactly the desired policies or orderings come out optimal.David Abel, Will Dabney, Anna Harutyunyan, Mark K. Ho, Michael L. Littman, Doina Precup, and Satinder Singh. On the Expressivity of Markov Reward. In M. Ranzato, A. Beygelzimer, Y. Dauphin, P. S. Liang, and J. Wortman Vaughan, editors, Advances in Neural Information Processing Systems, volume 34, pages 7799–7812. Curran Associates, Inc., 2021 Their simplest example is “always move in the same direction” in a grid. The intuition comes from Theorem 3.5: a Markov reward judges actions state by state, so the policies it makes optimal are closed under combining optimal actions from different states, whereas combining “always up” in some cells with “always left” in others yields an unacceptable policy. The positive side is constructive: for a finite environment, a reward that realizes a given task, or a certificate that none exists, can be found in polynomial time by linear programming (when rewards may take arbitrarily many values; with a fixed finite set of reward values the problem is NP-hard).

When the hypothesis is exactly right. Bowling and coauthors start from preferences ⪰\succeq over lotteries of finite histories.Michael Bowling, John D. Martin, David Abel, and Will Dabney. Settling the Reward Hypothesis. In Proceedings of the 40th International Conference on Machine Learning, volume 202 of Proceedings of Machine Learning Research, pages 3003–3020. PMLR, 2023. URL https://proceedings.mlr.press/v202/bowling23a.html If ⪰\succeq satisfies the four von Neumann–Morgenstern axioms (completeness, transitivity, independence, continuity), it has a utility uu. Their new fifth axiom, temporal γ\gamma-indifference, ties the utility of a history to that of its continuation, and the main theorem states that the five axioms hold if and only if there are a reward r(t)r(t) and a discount γ(t)∈[0,1]\gamma(t) \in [0, 1] for each transition tt with

u(empty history)=0,u(t⋅h)=r(t)+γ(t) u(h), u(\text{empty history}) = 0, \qquad u(t \cdot h) = r(t) + \gamma(t)\, u(h), (3.19)

which is the return recursion (3.5) with a transition-dependent discount; rr is unique up to a positive scale. So goals are expressible as expected cumulative reward precisely when the agent’s preferences are rational in the vNM sense and obey this temporal axiom; the episodic, discounted and total-reward settings of this chapter are special choices of γ(t)\gamma(t). A fixed γ<1\gamma < 1 for all transitions is a further restriction, not a consequence.

At the other extreme, Silver, Singh, Precup and Sutton conjecture that reward maximization is not just sufficient to express goals but enough to produce intelligence: that perception, language, social skills and generalization can all be understood as serving the maximization of reward by an agent acting in a rich environment.David Silver, Satinder Singh, Doina Precup, and Richard S. Sutton. Reward is Enough. Artificial Intelligence, 299:103535, 2021. doi: 10.1016/j.artint.2021.103535 That is a research hypothesis, not a theorem; the two results above mark where its formal counterpart is known to hold.

With the problem and its Bellman equations in place, Chapter 4 solves them when the dynamics pp are known, turning each equation into an iterative update and proving that the iteration converges; the existence of a solution of the optimality equation, left open by Theorem 3.5, comes out of that analysis.

3.7Exercises

Exercise 3.7. (a) In a continuing task with discount γ<1\gamma < 1, show that adding a constant cc to every reward adds the same constant vcv_c to every state value under every policy, and find vcv_c. (b) In an episodic task such as running a maze, does adding cc to every reward leave the task unchanged? Give an example.

Solution

(a) The new return is ∑kγk(Rt+k+1+c)=Gt+∑kγkc\sum_k \gamma^k (R_{t+k+1} + c) = G_t + \sum_k \gamma^k c, so every value increases by vc=c/(1−γ)v_c = c/(1 - \gamma), independently of the state and the policy; relative values, hence the ordering of policies, are unchanged.

The constant added to every value by a constant reward shift cc.
const shiftSum = await show`Sum[\[Gamma]^k c, {k, 0, Infinity}, Assumptions -> 0 < \[Gamma] < 1]`
vc=∑k=0∞γkc=c1−γ v_c = \sum_{k=0}^\infty \gamma^k c = \frac{c}{1-\gamma }

(b) In an episodic task the shift is c (1−γT−t)/(1−γ)c\,(1 - \gamma^{T-t})/(1 - \gamma) (or c (T−t)c\,(T - t) for γ=1\gamma = 1), which depends on the remaining episode length. With reward −1-1 per step in a maze the agent hurries to the exit; adding c=2c = 2 makes every step worth +1+1 and the agent now prefers to wander forever. The task has changed, because termination interacts with the shift.

Exercise 3.8. A continuing MDP has a single decision state. Action 𝗅𝖾𝖿𝗍\mathsf{left} gives reward +1+1 and leads to a state that returns with reward 00; action 𝗋𝗂𝗀𝗁𝗍\mathsf{right} gives 00 and leads to a state that returns with reward +2+2. For which γ\gamma is each of the two deterministic policies optimal?

Solution

From the decision state the reward sequences are 1,0,1,0,…1, 0, 1, 0, \dots and 0,2,0,2,…0, 2, 0, 2, \dots, so v𝗅𝖾𝖿𝗍=1/(1−γ2)v_{\mathsf{left}} = 1/(1 - \gamma^2) and v𝗋𝗂𝗀𝗁𝗍=2γ/(1−γ2)v_{\mathsf{right}} = 2\gamma/(1 - \gamma^2). In the two other states there is no choice. Hence 𝗋𝗂𝗀𝗁𝗍\mathsf{right} is optimal if and only if 2γ≥12\gamma \ge 1:

Solve v𝗋𝗂𝗀𝗁𝗍>v𝗅𝖾𝖿𝗍v_{\mathsf{right}} > v_{\mathsf{left}} for γ∈[0,1)\gamma \in [0, 1).
const lrReduce = await show`Reduce[2 \[Gamma]/(1 - \[Gamma]^2) > 1/(1 - \[Gamma]^2) && 0 <= \[Gamma] < 1, \[Gamma]]`
v𝗋𝗂𝗀𝗁𝗍>v𝗅𝖾𝖿𝗍  ⟺  12<γ<1 v_{\mathsf{right}} > v_{\mathsf{left}} \iff \frac{1}{2}<\gamma <1

So 𝗅𝖾𝖿𝗍\mathsf{left} is optimal for γ=0\gamma = 0, both are optimal at γ=1/2\gamma = 1/2, and 𝗋𝗂𝗀𝗁𝗍\mathsf{right} is optimal for γ=0.9\gamma = 0.9. The optimal policy depends on γ\gamma, which is part of the problem specification.

Exercise 3.9. In the gridworld of Section 3.3.2, an optimal policy from AA jumps to A′A' and walks straight back up to AA in four steps. Express v∗(A)v_*(A) symbolically and evaluate it at γ=0.9\gamma = 0.9.

Solution

The reward sequence from AA is 10,0,0,0,0,10,0,…10, 0, 0, 0, 0, 10, 0, \dots with period 55, so v∗(A)=10∑kγ5k=10/(1−γ5)v_*(A) = 10\sum_{k} \gamma^{5k} = 10/(1 - \gamma^5).

The exact value at γ=9/10\gamma = 9/10 and its decimal expansion; the book reports 24.424.4.
const gridA = await show`With[{v = 10/(1 - (9/10)^5)}, {v, N[v, 6]}]`
v∗(A)=101−(9/10)5={100000040951,24.4194} v_*(A) = \frac{10}{1 - (9/10)^5} = \left\{\frac{1000000}{40951},24.4194\right\}

That no better policy exists from AA follows from the theorem: this value, together with the values of the other cells under the same policy, satisfies (3.17), which Chapter 4 checks by value iteration.

Exercise 3.10. Write the Bellman optimality equation (3.18) for the recycling robot as five equations in the five unknowns q∗(s,a)q_*(s, a), and verify it for the numerical instance of Section 3.4.1.

Solution

Abbreviating h,lh, l and 𝗌,𝗐,𝗋𝖾\mathsf{s}, \mathsf{w}, \mathsf{re} and writing m(x)=max⁡aq∗(x,a)m(x) = \max_{a} q_*(x, a),

q∗(h,𝗌)=rsearch+γ[α m(h)+(1−α) m(l)],q∗(h,𝗐)=rwait+γ m(h),q∗(l,𝗌)=βrsearch−3(1−β)+γ[(1−β) m(h)+β m(l)],q∗(l,𝗐)=rwait+γ m(l),q∗(l,𝗋𝖾)=γ m(h). \begin{aligned} q_*(h, \mathsf{s}) &= r_{\text{search}} + \gamma[\alpha\, m(h) + (1 - \alpha)\, m(l)], \\ q_*(h, \mathsf{w}) &= r_{\text{wait}} + \gamma\, m(h), \\ q_*(l, \mathsf{s}) &= \beta r_{\text{search}} - 3(1 - \beta) + \gamma[(1 - \beta)\, m(h) + \beta\, m(l)], \\ q_*(l, \mathsf{w}) &= r_{\text{wait}} + \gamma\, m(l), \\ q_*(l, \mathsf{re}) &= \gamma\, m(h). \end{aligned}

By (3.16), q∗q_* is the one-step lookahead of v∗v_*; the check is that its row maxima reproduce v∗v_* and that it is a fixed point of the right-hand side.

q∗q_* from v∗=(1000/59,900/59)v_* = (1000/59, 900/59), and the residual of the q∗q_* optimality equation (all zeros).
const robotQstar = await show`With[{q = ${robotQ}[${vSearchRecharge}] /. ${robotNums}}, {q, (${robotQ}[Map[Max, q]] /. ${robotNums}) - q}]`
(q∗, residual)={{{100059,95959},{84659,86959,90059}},{{0,0},{0,0,0}}} \bigl(q_*,\ \text{residual}\bigr) = \left\{\left\{\left\{\frac{1000}{59},\frac{959}{59}\right\},\left\{\frac{846}{59},\frac{869}{59},\frac{900}{59}\right\}\right\},\{\{0,0\},\{0,0,0\}\}\right\}