Chapter 3 · Book pp. 47–71

Finite Markov Decision Processes

The formal problem the rest of the book solves. MDPs add the associative aspect (different actions in different situations) and delayed consequences: actions influence the next state and, through it, future rewards.

Dynamics p(s′,r|s,a) Markov property Return & discount γ Episodic vs continuing vπ, qπ, v*, q* Bellman equations
Exam focus for this chapter

3.1 The agent–environment interface

Agent Environment action Aₜ Sₜ₊₁ Rₜ₊₁ state Sₜ reward Rₜ
Figure 3.1: at each step $t=0,1,2,\dots$ the agent sees $S_t\in\mathcal S$, picks $A_t\in\mathcal A(s)$, and one step later receives $R_{t+1}\in\mathcal R\subset\mathbb R$ and finds itself in $S_{t+1}$.
$$S_0, A_0, R_1, S_1, A_1, R_2, S_2, A_2, R_3, \dots$$
Trajectory of an MDP(3.1)

Note the indexing: $R_{t+1}$ (not $R_t$) is the reward due to $A_t$ — it emphasises that $R_{t+1}$ and $S_{t+1}$ are jointly determined.

In a finite MDP, $\mathcal S,\mathcal A,\mathcal R$ are finite. The dynamics are completely described by:

$$p(s',r\mid s,a) \doteq \Pr\{S_t=s', R_t=r \mid S_{t-1}=s, A_{t-1}=a\}$$
Four-argument dynamics function(3.2)
$$\sum_{s'\in\mathcal S}\sum_{r\in\mathcal R} p(s',r\mid s,a) = 1, \quad \text{for all } s\in\mathcal S,\ a\in\mathcal A(s)$$
Dynamics is a distribution for each (s, a)(3.3)

Everything else follows from $p$:

$$p(s'\mid s,a) = \sum_{r} p(s',r\mid s,a) \qquad r(s,a) = \sum_r r\sum_{s'}p(s',r\mid s,a) \qquad r(s,a,s') = \sum_r r\,\frac{p(s',r\mid s,a)}{p(s'\mid s,a)}$$
Transition probabilities and expected rewards(3.4)–(3.6)
Definition — Markov property

The probability of each possible $S_t, R_t$ depends only on the immediately preceding state and action $S_{t-1},A_{t-1}$, not on earlier history. This is a restriction on the state, not the decision process: the state must include all information about the past interaction that makes a difference for the future.

Where is the agent–environment boundary?

The whole framework reduces goal-directed learning to three signals: actions (the choices), states (the basis for choices), rewards (the goal). Time steps need not be fixed real-time intervals; states and actions can be low-level (voltages, sensor readings) or high-level and even mental. Rewards are always single numbers; states and actions are often structured vectors (bioreactor, pick-and-place robot).

Example 3.3 — the recycling robot

States $\{\text{high},\text{low}\}$ (battery). $\mathcal A(\text{high})=\{\text{search},\text{wait}\}$, $\mathcal A(\text{low})=\{\text{search},\text{wait},\text{recharge}\}$. Searching from high stays high w.p. $\alpha$; from low stays low w.p. $\beta$, else the battery dies, the robot is rescued (reward $-3$) and returned to high. $r_\text{search}>r_\text{wait}$.

$s$$a$$s'$$p(s'\mid s,a)$$r(s,a,s')$
highsearchhigh$\alpha$$r_\text{search}$
highsearchlow$1-\alpha$$r_\text{search}$
lowsearchhigh$1-\beta$$-3$
lowsearchlow$\beta$$r_\text{search}$
highwaithigh1$r_\text{wait}$
lowwaitlow1$r_\text{wait}$
lowrechargehigh10

Zero-probability rows (high-wait-low, low-wait-high, low-recharge-low) are omitted. In a transition graph, state nodes are large open circles, action nodes are small solid circles, and arrows out of an action node carry $p(s'\mid s,a)$ and $r(s,a,s')$ and sum to 1. Exercise 3.4 (the $p(s',r\mid s,a)$ table) has exactly these rows, with $r$ as its own column, since each $(s,a,s')$ here has a single reward.

3.2 Goals and rewards

The reward hypothesis

"That 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)."

3.3 Returns and episodes

$$G_t \doteq R_{t+1} + R_{t+2} + R_{t+3} + \dots + R_T$$
Return — episodic (undiscounted)(3.7)

Episodic tasks break into episodes (plays of a game, trips through a maze), each ending in a terminal state followed by a reset; $T$ is a random variable. $\mathcal S$ = non-terminal states, $\mathcal S^+$ = all states incl. terminal. Continuing tasks go on without limit ($T=\infty$) — the sum (3.7) could be infinite, so we discount:

$$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}, \qquad 0\le\gamma\le1$$
Discounted return(3.8)
$$G_t = R_{t+1} + \gamma G_{t+1}, \qquad G_T \doteq 0$$
Recursive relation between successive returns(3.9)
$$\text{If } R=+1 \text{ forever:}\quad G_t = \sum_{k=0}^{\infty}\gamma^k = \frac{1}{1-\gamma}$$
Return of a constant reward stream(3.10)
The discount rate γ — a problem parameter, not just a learning parameter

Example 3.4 — pole balancing, two formulations

FormulationRewardReturn
Episodic, undiscounted+1 every step without failureNumber of steps until failure (∞ if balanced forever)
Continuing, discounted−1 on failure, 0 otherwise$-\gamma^{K-1}$ for the next failure $K$ steps away (plus later failures)

Either way the return is maximised by balancing as long as possible.

Returns calculator — work backwards with $G_t = R_{t+1} + \gamma G_{t+1}$

Defaults are Exercise 3.8 (γ = 0.5, T = 5).

3.4 Unified notation for episodic and continuing tasks

Treat episode termination as entering a special absorbing state that transitions only to itself with reward 0. Then the episodic return equals the infinite sum, with or without discounting:

$$G_t \doteq \sum_{k=t+1}^{T}\gamma^{\,k-t-1}R_k \qquad \text{(allowing } T=\infty \text{ or } \gamma=1\text{, but not both)}$$
Unified return(3.11)

3.5 Policies and value functions

A policy $\pi(a\mid s)$ is a mapping from states to probabilities of selecting each action. RL methods specify how the policy changes with experience.

$$v_\pi(s) \doteq \E_\pi[G_t\mid S_t=s] = \E_\pi\Big[\sum_{k=0}^\infty\gamma^kR_{t+k+1}\,\Big|\,S_t=s\Big]$$
State-value function for policy π(3.12)
$$q_\pi(s,a) \doteq \E_\pi[G_t\mid S_t=s, A_t=a] = \E_\pi\Big[\sum_{k=0}^\infty\gamma^kR_{t+k+1}\,\Big|\,S_t=s, A_t=a\Big]$$
Action-value function for policy π(3.13)

The value of a terminal state is always 0. Values can be estimated by averaging returns observed after each state (Monte Carlo, Ch 5) or, for many states, by a parameterised function (Part II).

Conversions between v and q (Exercises 3.12, 3.13, 3.18, 3.19)

$$v_\pi(s) = \sum_a \pi(a\mid s)\,q_\pi(s,a) = \E_\pi[q_\pi(S_t,A_t)\mid S_t=s]$$
v from qEx 3.12 / 3.18
$$q_\pi(s,a) = \sum_{s',r}p(s',r\mid s,a)\big[r + \gamma v_\pi(s')\big] = \E[R_{t+1}+\gamma v_\pi(S_{t+1})\mid S_t=s,A_t=a]$$
q from v (needs the model p)Ex 3.13 / 3.19

The Bellman equation for $v_\pi$

$$v_\pi(s) = \sum_a\pi(a\mid s)\sum_{s',r}p(s',r\mid s,a)\Big[r+\gamma v_\pi(s')\Big], \quad\text{for all } s\in\mathcal S$$
Bellman equation for vπ(3.14)

Derivation: $v_\pi(s)=\E_\pi[G_t\mid S_t=s]=\E_\pi[R_{t+1}+\gamma G_{t+1}\mid S_t=s]$, then expand the expectation over $a$ (by π), $s',r$ (by $p$). Read it as: value of a state = expected immediate reward + discounted expected value of the next state, averaged over all possibilities weighted by $\pi(a\mid s)p(s',r\mid s,a)$. $v_\pi$ is the unique solution of this system of $|\mathcal S|$ linear equations.

$$q_\pi(s,a) = \sum_{s',r}p(s',r\mid s,a)\Big[r+\gamma\sum_{a'}\pi(a'\mid s')\,q_\pi(s',a')\Big]$$
Bellman equation for qπEx 3.17
Backup diagrams. Open circle = state, solid dot = state–action pair, arc = max. Updates transfer value back from successors to the root. Unlike transition graphs, nodes needn't be distinct states (a state can be its own successor).

Example 3.5 — gridworld

5×5 grid, actions N/S/E/W move deterministically; moving off-grid leaves you in place with reward −1; other moves give 0, except: from A every action gives +10 and teleports to A′; from B every action gives +5 and teleports to B′. With the equiprobable random policy and γ = 0.9:

Try it: solve the gridworld (Figures 3.2 and 3.5)
Exercise 3.14 — check the Bellman equation at the centre state

Centre value 0.7, neighbours 2.3, 0.4, −0.4, 0.7, all rewards 0, each action prob. ¼: $\;0.9\times\tfrac14(2.3+0.4-0.4+0.7)=0.9\times0.75=0.675\approx0.7$ ✓.

Adding a constant to all rewards (Exercises 3.15, 3.16)

$$\text{Continuing: } G_t \to G_t + \sum_{k=0}^{\infty}\gamma^k c = G_t + \underbrace{\frac{c}{1-\gamma}}_{v_c}$$
Reward shift in a continuing taskEx 3.15

Continuing: every state's value shifts by the same $v_c=c/(1-\gamma)$, so relative values and the optimal policy are unchanged — only the intervals between rewards matter. Episodic: the shift is $c\sum_{k=0}^{T-t-1}\gamma^k$, which depends on how long the episode lasts, so it can change the task. Example: maze with −1 per step; add $c=+2$ → +1 per step, and the agent now prefers never escaping.

Example 3.6 — golf

Reward −1 per stroke until the ball is in the hole; value = −(strokes to go). $v_\text{putt}$: −1 on the green, contours −2, −3, … outward; sand traps $-\infty$ with the putter; 6 strokes from the tee by putting only.

3.6 Optimal policies and optimal value functions

Policies are partially ordered: $\pi\ge\pi'$ iff $v_\pi(s)\ge v_{\pi'}(s)$ for all $s$. There is always at least one policy better than or equal to all others — an optimal policy $\pi_*$ (possibly several, all sharing the same value functions).

$$v_*(s) \doteq \max_\pi v_\pi(s) \qquad\qquad q_*(s,a)\doteq\max_\pi q_\pi(s,a)$$
Optimal state- and action-value functions(3.15), (3.16)
$$q_*(s,a) = \E\big[R_{t+1}+\gamma v_*(S_{t+1})\mid S_t=s,A_t=a\big]$$
q* in terms of v*(3.17)

The Bellman optimality equation: the value of a state under an optimal policy equals the expected return for the best action from that state — no reference to any specific policy.

$$v_*(s) = \max_{a\in\mathcal A(s)} q_{\pi_*}(s,a) = \max_a\E\big[R_{t+1}+\gamma v_*(S_{t+1})\mid S_t=s,A_t=a\big] = \max_a\sum_{s',r}p(s',r\mid s,a)\big[r+\gamma v_*(s')\big]$$
Bellman optimality equation for v*(3.18), (3.19)
$$q_*(s,a) = \E\Big[R_{t+1}+\gamma\max_{a'}q_*(S_{t+1},a')\,\Big|\,S_t=s,A_t=a\Big] = \sum_{s',r}p(s',r\mid s,a)\Big[r+\gamma\max_{a'}q_*(s',a')\Big]$$
Bellman optimality equation for q*(3.20)
GoalFormulaNeeds model?
$v_*$ from $q_*$ (Ex 3.25)$v_*(s)=\max_a q_*(s,a)$No
$q_*$ from $v_*$ (Ex 3.26)$q_*(s,a)=\sum_{s',r}p(s',r\mid s,a)[r+\gamma v_*(s')]$Yes
$\pi_*$ from $q_*$ (Ex 3.27)$\pi_*(s)=\argmax_a q_*(s,a)$No
$\pi_*$ from $v_*$ (Ex 3.28)$\pi_*(s)=\argmax_a\sum_{s',r}p(s',r\mid s,a)[r+\gamma v_*(s')]$Yes

Exercise 3.29 — Bellman equations with $p(s'\mid s,a)$ and $r(s,a)$

$$\begin{aligned} v_\pi(s)&=\sum_a\pi(a\mid s)\Big[r(s,a)+\gamma\sum_{s'}p(s'\mid s,a)v_\pi(s')\Big] & v_*(s)&=\max_a\Big[r(s,a)+\gamma\sum_{s'}p(s'\mid s,a)v_*(s')\Big]\\ q_\pi(s,a)&=r(s,a)+\gamma\sum_{s'}p(s'\mid s,a)\sum_{a'}\pi(a'\mid s')q_\pi(s',a') & q_*(s,a)&=r(s,a)+\gamma\sum_{s'}p(s'\mid s,a)\max_{a'}q_*(s',a') \end{aligned}$$
All four Bellman equations with three-argument p and r(s,a)Ex 3.29

Example 3.8 — solving the gridworld

$v_*(A)=24.4$. From A the optimal policy collects +10, jumps to A′, and walks 4 steps back up to A: a 5-step cycle.

$$v_*(A) = 10 + \gamma^5 v_*(A) \;\Rightarrow\; v_*(A)=\frac{10}{1-\gamma^5}=\frac{10}{1-0.9^5}=\frac{10}{0.40951}\approx \mathbf{24.419}$$
Optimal value of AEx 3.24

Example 3.9 — recycling robot Bellman optimality equations

$$\begin{aligned} v_*(h)&=\max\begin{cases} r_s+\gamma[\alpha v_*(h)+(1-\alpha)v_*(l)]\\ r_w+\gamma v_*(h)\end{cases}\\[4pt] v_*(l)&=\max\begin{cases} \beta r_s-3(1-\beta)+\gamma[(1-\beta)v_*(h)+\beta v_*(l)]\\ r_w+\gamma v_*(l)\\ \gamma v_*(h)\end{cases} \end{aligned}$$
Recycling robot — two nonlinear equations, one unique solution pairEx 3.9
Why we rarely solve the Bellman optimality equation directly

It amounts to exhaustive search and relies on three assumptions rarely true together: (1) dynamics accurately known; (2) enough computation; (3) Markov states. Backgammon satisfies 1 and 3 but has ~1020 states, so 2 fails. Heuristic search, DP and RL methods can all be viewed as approximately solving it — RL uses experienced transitions in place of expected ones.

3.7 Optimality and approximation

Chapter summary

ConceptSummary
Agent / environmentEverything inside the agent is known and controllable; the environment is incompletely controllable and may or may not be known.
Finite MDPFinite $\mathcal S,\mathcal A,\mathcal R$ with dynamics $p(s',r\mid s,a)$.
ReturnUndiscounted for episodic tasks; discounted for continuing tasks; unified by an absorbing terminal state.
Value functions$v_\pi,q_\pi$: expected return under π. $v_*,q_*$: the largest achievable — unique; optimal policies may be many.
Bellman equationsConsistency conditions; linear for $v_\pi$, nonlinear (max) for $v_*$.
Complete vs incomplete knowledgeComplete = the agent has the full model $p$ (DP, Ch 4). Incomplete = it doesn't (MC, TD, Ch 5–7).

Self-test

Ex 3.5 — Modify (3.3) for episodic tasks.

$\sum_{s'\in\mathcal S^+}\sum_{r\in\mathcal R}p(s',r\mid s,a)=1$ for all $s\in\mathcal S$, $a\in\mathcal A(s)$ — the next state can be the terminal state, so sum over $\mathcal S^+$.

Ex 3.6 — Pole-balancing treated as episodic with discounting, reward −1 on failure, 0 otherwise. What is the return? How does it differ from the continuing formulation?

$G_t=-\gamma^{K-1}$ where $K$ is the number of steps until failure (the episode ends there). In the continuing version, $G_t=-\sum_i\gamma^{K_i-1}$ over all future failures — later failures also count.

Ex 3.7 — Maze robot: +1 for escaping, 0 otherwise, undiscounted episodic. It shows no improvement. Why?

Every policy that eventually escapes gets return 1, so there's no incentive to escape quickly (and with random exploration it nearly always escapes eventually). You haven't communicated "fast". Use −1 per step or discount (γ < 1).

Ex 3.8 — γ = 0.5, R = 1, 2, 6, 3, 2, T = 5. Compute G₀…G₅.

Backwards: $G_5=0$; $G_4=2$; $G_3=3+0.5\cdot2=4$; $G_2=6+0.5\cdot4=8$; $G_1=2+0.5\cdot8=6$; $G_0=1+0.5\cdot6=\mathbf 4$.

Ex 3.9 — γ = 0.9, R₁ = 2 then an infinite sequence of 7s. G₁ and G₀?

$G_1 = 7/(1-0.9)=70$; $G_0=2+0.9\cdot70=65$.

Ex 3.10 — Prove $\sum_{k=0}^\infty\gamma^k=1/(1-\gamma)$.

Let $S=\sum_{k\ge0}\gamma^k$ (converges for $|\gamma|<1$). Then $S=1+\gamma S$, so $S(1-\gamma)=1$.

Ex 3.11 — Expected next reward under a stochastic policy, in terms of π and p.

$\E[R_{t+1}\mid S_t=s]=\sum_a\pi(a\mid s)\sum_{s',r}r\,p(s',r\mid s,a)$.

Ex 3.22 — Top state: left gives +1 then 0 (and returns); right gives 0 then +2. Which policy is optimal for γ = 0, 0.9, 0.5?

$v_\text{left}=1+\gamma^2+\gamma^4+\dots=1/(1-\gamma^2)$; $v_\text{right}=2\gamma/(1-\gamma^2)$. Left is better iff $1>2\gamma$. γ = 0: left (1 vs 0). γ = 0.9: right (9.47 vs 5.26). γ = 0.5: both equal (1.33).

Why can the optimal action be chosen from $q_*$ without a model, but not from $v_*$?

$q_*(s,a)$ already includes the expected next reward and next-state value. From $v_*$ you must do a one-step lookahead $\sum_{s',r}p(s',r\mid s,a)[r+\gamma v_*(s')]$, which needs $p$.

Is the optimal value function unique? Is the optimal policy unique?

$v_*$ and $q_*$ are unique for a given MDP; there can be multiple optimal policies (any policy giving non-zero probability only to maximising actions — e.g. ties in the gridworld).

Ex 3.2 — Is the MDP framework adequate for all goal-directed learning? An exception?

Not always usefully: tasks where the state is not observable (non-Markov, partial observability), multiple goals that can't be reduced to a single scalar, or when the reward itself can be altered by the agent. Very long horizons or continuous time also strain it.

Ex 3.3 — Driving: where should the agent–environment line go?

It's a modelling choice determined by what you can control arbitrarily and what decision problem you want to learn. Too far in (muscle twitches) makes learning the high-level task very hard; too far out (tyre torques) assumes control you don't directly have. A sensible choice is where actions are reliably executable and meaningful for the goal (e.g. accelerator/brake/steering, or route choices for a navigation agent).

Chapter quiz — check your understanding

Scored quiz: pick an answer (or type a number) to get instant feedback and an explanation. Questions marked Exam-style give you values to interpret (sweeps, logs, returns, learning curves), the way exam problems do. Your best score is saved in this browser and shown on the home page. "Shuffle & restart" re-orders the questions.