← back to EEE 485
Week 14131 min full read
7 concepts27 worked examples32 exercises5 exam-level7 figures
What are you here for?

14 Reinforcement learning: returns, Markov decision processes, value iteration and Q-learning

Start with this

One question before you read anything. Getting it wrong is the point: it shows you what this section is for.

§14.1 — take the +1 now or walk for the +10?

A delivery robot stands in a corridor. One step left is a charging pad that pays $+1$ and ends the run. Going right, it pays $1$ for each of the next two moves and then collects $+10$ at the drop point, which also ends the run. are discounted by $\gamma=0.9$ per step.

Find(a) Which plan has the larger discounted total, and what is it worth?
Given
  • left: $+1$, then the run ends

  • right: $-1$, $-1$, then $+10$, then the run ends

  • $\gamma=0.9$: the reward $k$ steps after the first one is multiplied by $0.9^k$

Hint 1/4

Write each plan as a list of rewards and give each reward its weight.

Hint 2/4

$G_0=R_1+\gamma R_2+\gamma^2R_3$; the first reward has weight $1$.

Hint 3/4

Left: $G_0=1$. Right: $R_1=-1$, $R_2=-1$, $R_3=10$ with $\gamma=0.9$, so $G_0=-1-0.9+0.81\cdot10$.

Hint 4/4

Right is worth $6.2$ against $1$: go right.

Show solution

Both plans are fixed sequences of rewards, so each is one weighted sum.

Weighted sums

$$G_0^{\text{left}}=1$$

A single reward at weight 1.

$$G_0^{\text{right}}=-1-0.9+0.81\cdot10=6.2$$

The rewards arrive $0$, $1$ and $2$ steps after the first, so they get $\gamma^0$, $\gamma^1$ and $\gamma^2$.

Answer $$\boxed{\text{right: }6.2>1}$$
Check

Backwards: the last cell is worth $10$, the one before $-1+9=8$, the start $-1+7.2=6.2$.

Comparing only the first rewards picks the pad; the discounted total picks the drop. The section starts from exactly this gap.

A delivery robot stands in a corridor. One step to its left is a charging pad that pays $+1$ and ends the run; three steps to its right is the drop point that pays $+10$, and each of the two moves before it costs $1$. Which way should it go, and what single number decides it?

By the end you can show that the right plan is worth $6.2$ against $1$ when each later reward is discounted by $0.9$ per step, find the $0.5$ at which the two plans tie, and reach the same decision by and by .

In 60 seconds

An picks actions to maximize the discounted return $G_t=\sum_k\gamma^kR_{t+k+1}$; in a Markov decision process the best values satisfy the Bellman optimality equations, value iteration solves them when $p(s'|s,a)$ is known, and Q-learning learns $q_*$ from observed transitions when it is not.

Return
$$G_t=\sum_{k=0}^{\infty}\gamma^kR_{t+k+1}=R_{t+1}+\gamma\,G_{t+1}$$

adding up a reward stream; $r$ forever is worth $r/(1-\gamma)$

Bellman optimality
$$v_*(s)=\max_a\Big[r(s,a)+\gamma\sum_{s'}p(s'|s,a)\,v_*(s')\Big]$$

checking optimal values; then $\pi^*(s)=\arg\max_aq_*(s,a)$

Value iteration
$$v_{k+1}(s)=\max_a\Big[r(s,a)+\gamma\sum_{s'}p(s'|s,a)\,v_k(s')\Big]$$

$p$ and $r$ known: sweep from $v_0=0$ until the largest change is below $\epsilon$

Q-learning
$$Q(S_t,A_t)\leftarrow(1-\alpha)\,Q(S_t,A_t)+\alpha\big[R_{t+1}+\gamma\max_{a'}Q(S_{t+1},a')\big]$$

$p$ unknown: one update per observed transition

Three most common mistakes
  1. Discounting the first reward. $R_{t+1}$ has weight $1$ and $R_{t+k+1}$ has weight $\gamma^k$; writing $\gamma R_{t+1}+\gamma^2R_{t+2}+\dots$ shrinks every term by an extra factor $\gamma$.

  2. Putting the max in the wrong place. $v_*(s)=\max_a\sum_{s'}p(s'|s,a)[\dots]$ chooses the action before the landing state is known; $\sum_{s'}p\max_a[\dots]$ pretends the agent sees $s'$ first.

  3. Forgetting that ε-greedy's random draw can land on the greedy action: with $\epsilon=0.1$ and $4$ actions the greedy action has probability $0.9+0.1/4=0.925$, not $0.9$.

Two course documents give different weights:

  • Chapter 1 slides, undergraduate line: midterm 25, final 25, four quizzes 20, two-phase project 30.
  • STARS syllabus page printed on 21 September 2026: midterm 30, final 30, problem sets and quizzes 20, project 20. It is the later document; confirm which split applies.
  • The STARS weekly list has as its thirteenth item; the lecture slides number it chapter 14.
How much time do you have?
10 minutes

The return, one value iteration sweep and one Q-learning update: the three hand computations the rest of the section is built on.

The 60-second card · The return: one number for a stream of rewards · Value iteration · Q-learning: learning q* from experience · Formula card
45 minutes

Every block once with its first worked example, then value iteration from a full solution down to a bare problem.

The 60-second card · The return: one number for a stream of rewards · Markov decision processes · Policies and their values · The optimal policy and the Bellman optimality equations · Value iteration · Q-learning: learning q* from experience · Choosing actions while learning · Scaffolding comes off · Formula card
full read

Adds the look-alike pairs, a full exam-style question and mixed practice where you choose the method yourself.

The opening pages · Recall first · The return: one number for a stream of rewards · Markov decision processes · Policies and their values · The optimal policy and the Bellman optimality equations · Value iteration · Q-learning: learning q* from experience · Choosing actions while learning · Method boxes · Look-alike pairs · Scaffolding comes off · Full exam-style question · Practice set · Check yourself
By the end of this section
  1. Compute discounted returns of finite and infinite reward streams, directly and with the recursion $G_t=R_{t+1}+\gamma G_{t+1}$.

  2. Model a decision problem as a Markov decision process, with states, action sets, $p(s'|s,a)$ and $r(s,a,s')$, and test whether a chosen state is Markov.

  3. Evaluate a given policy: compute $v_\pi$ and $q_\pi$ in small examples and link them through $v_\pi(s)=\sum_a\pi(a|s)\,q_\pi(s,a)$.

  4. Apply the Bellman optimality equations: write them for a small MDP, test claimed optimal values against them and read off $\pi^*(s)=\arg\max_aq_*(s,a)$.

  5. Run value iteration by hand in its v and q forms, apply the stopping rule and extract the greedy policy.

  6. Carry out Q-learning updates from observed transitions and state when the table converges to $q_*$.

  7. Compute action probabilities under greedy, ε-greedy and , and explain why a purely greedy learner can get stuck.

Syllabus coverage

Reinforcement learning — covered

  • Agent and environment, rewards, the discount rate and the return
  • policies and the
  • Markov decision processes
  • state and
  • optimal policies and the Bellman optimality equations
  • value iteration in v and q form
  • Q-learning
  • greedy, ε-greedy and Boltzmann exploration
  • the idea

The lecturer's chapter title and the thirteenth item of the STARS weekly list, which the slides number chapter 14. The weekly line names no chapter of the textbook, so no section numbers are cited.

Markov decision process — covered

  • Finite states and state-dependent action sets
  • transition probabilities
  • expected rewards of transitions
  • the Markov property
  • how to repair a state that is not Markov

One of the slides' four headings. The slides' own example is replaced here by an original machine-maintenance example.

Value iteration — covered

  • The v form and the q form
  • the stopping rule
  • reading off the greedy policy
  • deterministic against stochastic robots

The slides' grid robot with slippery moves appears here as the ledge example.

Q learning — covered

  • The table
  • the one-sample target
  • the
  • the convergence condition
  • greedy
  • ε-greedy and Boltzmann action selection

The action-selection rules have their own block right after it.

Deep Q network — covered

Why a table cannot hold the values of screen images, and the idea of replacing it by a network trained with gradient steps toward the same target.

The slides show it as a video; one worked example gives the idea. Training details are not covered.

Bellman equation for a fixed policy — off syllabus

The one-step equation that turns the definitions of v and q for a given policy into linear equations.

Further reading, not on the slides, which give one-step equations only for the optimal values. Used to evaluate given policies in small examples.

How fast value iteration converges — off syllabus

Each sweep shrinks the largest error by at least the factor γ.

Further reading: the slides say 'repeat until convergence'. It appears in the box and one figure caption, in no exercise.

Learning-rate condition for Q-learning — off syllabus

Why the standard convergence theorem also asks each pair's learning rate to shrink.

Further reading: the slide states the visiting condition only. Used in one worked example and one interleaved question.

Temperature in Boltzmann exploration — off syllabus

Dividing the entries by a temperature before the exponential.

Further reading, mentioned only: the slide's rule has no temperature and no exercise uses one.

Recall first
Expectation and total expectation

$\mathbb E[X]=\sum_xx\,p(x)$, expectation is linear, and $\mathbb E[X]=\sum_y\mathbb E[X\mid Y=y]\,P(Y=y)$.

Value functions are expectations of returns, split by the next state.

Geometric series

$\sum_{k=0}^{\infty}\gamma^k=\frac{1}{1-\gamma}$ for $0\le\gamma<1$, and $\sum_{k=0}^{n-1}\gamma^k=\frac{1-\gamma^n}{1-\gamma}$.

Returns of constant and repeating reward streams.

Law of large numbers

For i.i.d. $X_i$ with finite mean, $\bar X_n=\frac1n\sum_{i\le n}X_i\to\mathbb E[X]$. For independent $X_i$ with variance $\sigma^2$, $\operatorname{Var}(\sum_iw_iX_i)=\sigma^2\sum_iw_i^2$, so $\operatorname{Var}(\bar X_n)=\sigma^2/n$.

Q-learning with $\alpha=1/n$ keeps a running sample mean.

Waiting for a first success

If every trial succeeds independently with probability $p$, the expected number of trials up to and including the first success is $1/p$.

How long ε-greedy takes, on average, to try a given action.

Softmax

$e^{v_k}/\sum_je^{v_j}$ turns $K$ scores into $K$ probabilities that add to $1$.

Boltzmann exploration is a softmax of the table entries.

Stochastic gradient descent

$w(n+1)=w(n)-\eta\,\nabla l$, with $l$ the loss of one example and $\eta$ the learning rate.

The Q-learning update is one such step, and a deep Q network is trained with it.

Markov blanket

In a directed graph: a node's parents, its children and its children's other parents. A node is independent of its non-descendants given its parents.

One interleaved question reads the Markov property off a graph.

Try it yourself first (2 questions)
1§14.5 — a sure 8 or a risky move

A robot can take a risky move that reaches the goal ($+10$) with probability $0.8$ and falls into a pit ($-10$) with probability $0.2$, or a detour that surely earns $8$. It wants the larger expected reward.

Find(a) Which choice has the larger expected reward?
Given
  • risky move: $+10$ with probability $0.8$, $-10$ with probability $0.2$

  • detour: $8$ for sure

Hint 1/4

Replace each choice by one number, its average reward.

Hint 2/4

$\mathbb E[X]=\sum_xx\,p(x)$.

Hint 3/4

Risky: $0.8\cdot10+0.2\cdot(-10)$. Detour: $8$ with probability $1$.

Hint 4/4

$6<8$: the detour.

Show solution

An expectation weighs every outcome by its probability, the bad one included.

Expectations

$$\mathbb E[\text{risky}]=0.8\cdot10+0.2\cdot(-10)=6$$

Both outcomes, each with its probability.

$$\mathbb E[\text{detour}]=8$$

A sure outcome is its own expectation.

Answer $$\boxed{\text{detour: }8>6}$$
Check

Out of $100$ risky robots, $80$ earn $10$ and $20$ lose $10$: $800-200=600$ in total, $6$ each.

Value iteration compares actions the same way: each action is scored by an average over where it can land.

2§14.1 — a reward that never stops

A robot earns $+1$ at every step, forever. Rewards are discounted by $\gamma=0.9$ per step, the first one at full value.

Find(a) What is $G_t=\sum_{k\ge0}\gamma^kR_{t+k+1}$?
Given
  • $R_{t+1}=R_{t+2}=R_{t+3}=\dots=1$

  • $\gamma=0.9$

Hint 1/4

Recognize the sum of the weights as a series you know.

Hint 2/4

Geometric series: $\sum_{k=0}^{\infty}\gamma^k=\frac1{1-\gamma}$ for $0\le\gamma<1$.

Hint 3/4

Every reward is $1$, so $G_t=\sum_k0.9^k=\frac{1}{1-0.9}$.

Hint 4/4

$G_t=10$.

Show solution

The return of a constant stream is a geometric series, so the closed form replaces an infinite sum.

Geometric series

$$G_t=\sum_{k=0}^{\infty}0.9^k=\frac{1}{1-0.9}=10$$

The ratio $0.9$ is below $1$, so the series converges.

Answer $$\boxed{G_t=10}$$
Check

Recursion check: a constant return must satisfy $G=1+0.9\,G$, which gives $G=10$.

A constant reward $r$ forever is worth $r/(1-\gamma)$.

Notation
symbolreads asmeanswatch out
$S_t,\ A_t,\ R_{t+1}$

S t, A t, R t plus one

state and action at step $t$, and the reward they produce

The reward of step $t$ carries the index $t+1$.

$\gamma$

gamma

discount rate, $0\le\gamma\le1$

Not the step size of the GLM section.

$G_t$

G t

return from step $t$: $\sum_k\gamma^kR_{t+k+1}$

Starts with $R_{t+1}$ at full weight.

$\mathcal H_t$

script H t

history: everything up to and including $S_t$ and $A_t$

A general policy may use all of it; a uses only the current state.

$\mathcal S,\ \mathcal A(s)$

script S, script A of s

the finite state set; the actions allowed in $s$

Action sets can differ from state to state.

$p(s'|s,a)$

p of s prime given s and a

probability of landing in $s'$

Adds to $1$ over $s'$, not over $s$.

$r(s,a,s'),\ r(s,a)$

r of s, a, s prime; r of s, a

expected reward of a transition; of an action

$r(s, \allowbreak a)=\sum_{s'}p(s'|s, \allowbreak a)\,r(s, \allowbreak a, \allowbreak s')$.

$\pi(a|s),\ \pi(s)$

pi of a given s; pi of s

a stationary Markov policy; the action of a deterministic one

Here $\pi$ is a policy, not $3.14$.

$v_\pi(s),\ q_\pi(s,a)$

v pi, q pi

expected return from $s$ under $\pi$; the same with the first action fixed to $a$

$q_\pi$ fixes only the first action.

$v_*,\ q_*,\ \pi^*$

v star, q star, pi star

optimal values and an optimal policy

$\pi^*$ can always be taken deterministic.

$v_k,\ q_k,\ k^*$

v k, q k, k star

value iteration tables after $k$ sweeps; the sweep at which it stops

From $v_0=0$, $v_k$ looks only $k$ steps ahead.

$Q(s,a),\ \hat Q$

Q, Q hat

Q-learning's table entry; the one-sample target

Capital $Q$ is an estimate, lowercase $q_*$ the true function.

$\alpha$

alpha

learning rate of Q-learning

The role that $\eta$ played for neural networks.

$\epsilon,\ C_t$

epsilon, C t

probability and the coin of ε-greedy; also the stopping tolerance of value iteration

The slides use the same letter for both; the context decides.

$a^\circ$

a greedy

the greedy action $\arg\max_aQ(s,a)$

Shorthand of this page, not of the slides.

Conventions used here
Reward index.

The action at step $t$ earns $R_{t+1}$, and $G_t=\sum_{k\ge0}\gamma^kR_{t+k+1}$. The first slide writes the total from the start as $G_1=R_1+\gamma R_2+\dots$; with the later general definition this sum is $G_0$.

The two slides index the same sum differently; answers here use the general definition.

Discount rate.

$\gamma<1$ unless every ends; the existence theorem and value iteration are stated for $\gamma<1$.

With no end and no discount a return can be infinite.

.

A terminal state has value $0$ and no actions, so $\max_{a'}Q(s',a')=0$ there; its reward is paid on the move into it.

The pad and the drop of the corridor work this way.

Ties between actions.

When two actions score the same, take the one listed first unless a problem says otherwise.

Hand traces of greedy choices need a fixed rule.

Synchronous sweeps.

Value iteration computes all of $v_{k+1}$ from $v_k$. Updating in place also converges, but gives different numbers at each sweep.

The slides write $v_{k+1}$ in terms of $v_k$.

Stopping rule.

$\lVert v_k-v_{k-1}\rVert$ is the largest absolute change over the states.

The slides leave the norm open; the maximum is the usual choice.

Letters reused from earlier sections.

$\gamma$ is the discount rate, not the GLM step size; $\alpha$ is the learning rate that $\eta$ was for neural networks; $\epsilon$ is both the stopping tolerance and the exploration probability, as on the slides.

The same letter means different things in different chapters.

Rounding.

Intermediate values keep full precision; results are rounded at the end, to three or four significant digits.

Rounded values drift over many sweeps or updates.

14.1The return: one number for a stream of rewards

Adds a reward stream into one number, weighting the reward $k$ steps after the first by $\gamma^k$.

Every earlier section learned from examples that came with the right answer attached; here the only feedback is a reward, and the reward for a good move may arrive several steps later.

Solvable with what we have
  • Predict a price from features with least squares.

  • Classify an email from labelled examples with logistic regression.

  • Split unlabelled sensor readings into clusters with EM.

  • Estimate an average reward from repeated trials with a sample mean.

Not solvable yet
  • Decide whether a robot should take $+1$ now or walk three steps for $+10$.

  • Say which of forty moves in a game earned the final win.

  • Plan repairs whose cost comes today and whose benefit comes over the next weeks.

  • Learn from data that the learner's own actions select.

The obvious rule: in every cell, take the move with the larger immediate reward. At the robot's start, left pays $+1$ and right pays $-1$, so the rule goes left and collects $1$ in total.

Why it fails

The rule looks one step ahead. Going right costs $1$ twice and then pays $10$; shrinking each later reward by a factor $0.9$ per step, the total is $-1-0.9+8.1=6.2$, six times what the rule collects.

DefinitionDefinition 14.1: Return and discount rate
Conditions
  • $0\le\gamma\le1$ as on the slides; with $\gamma=1$ the sum stays finite only if every episode ends

  • rewards are bounded, $\lvert R_t\rvert\le R_{\max}$, so for $\gamma<1$ the return obeys $\lvert G_t\rvert\le R_{\max}/(1-\gamma)$

  • after a terminal state every reward is $0$

$$\boxed{\begin{aligned}G_t&=\textcolor{#d1690a}{R_{t+1}}+\gamma\,\textcolor{#d1690a}{R_{t+2}}+\gamma^2\,\textcolor{#d1690a}{R_{t+3}}+\dots\\&=\sum_{k=0}^{\infty}\gamma^k\,\textcolor{#d1690a}{R_{t+k+1}}\\&=\textcolor{#d1690a}{R_{t+1}}+\gamma\,G_{t+1}\end{aligned}}$$

The return at time $t$ is the next reward at full value, plus the one after it scaled by $\gamma$, plus the one after that scaled by $\gamma^2$, and so on. Everything after the first reward is itself a return, one step later, scaled by $\gamma$.

Where the recursion comes from

Split off the first term. $G_t=R_{t+1}+\sum_{k\ge1}\gamma^kR_{t+k+1}$.

Shift the index. With $j=k-1$ the tail is $\gamma\sum_{j\ge0}\gamma^jR_{(t+1)+j+1}=\gamma\,G_{t+1}$.

Bounded. If $\lvert R\rvert\le R_{\max}$ and $\gamma<1$, then $\lvert G_t\rvert\le R_{\max}\sum_k\gamma^k=R_{\max}/(1-\gamma)$.

Looks like this, but is not

The return $G_t$ looks as if it starts with $R_t$, the reward carrying the same index.

On the slides the action taken at time $t$ earns $R_{t+1}$, so $G_t$ starts there. $R_t$ was earned by the previous action and belongs to $G_{t-1}$.

$\gamma$$\sum_{k\ge0}\gamma^k=1/(1-\gamma)$$\gamma^{10}$, the weight ten steps after the first reward

$0.5$

$2$

$0.001$

$0.9$

$10$

$0.349$

$0.99$

$100$

$0.904$

A small $\gamma$ makes the agent short-sighted: at $0.5$ a reward ten steps later keeps a thousandth of its value, at $0.99$ still nine tenths.

Left for +1 now or right for +10 later, at γ = 0.9 and at the break-even γ

In the opening corridor, going left pays $+1$ and ends the run. Going right pays $-1$, then $-1$, then $+10$ at the drop point, which also ends the run. Compare the two plans at $\gamma=0.9$, then find the $\gamma$ at which they tie.

Find$G_0$ of each plan at $\gamma=0.9$, and the $\gamma$ at which the two are equal.
Given
  • left: $R_1=+1$, then the run ends

  • right: $R_1=-1$, $R_2=-1$, $R_3=+10$, then the run ends

  • $\gamma=0.9$ first, then $\gamma$ unknown

Solution

Both plans are fixed reward sequences, so the definition of the return applies directly; nothing is random yet.

Return of each plan at γ = 0.9

$$G_0^{\text{left}}=1$$

One reward at weight $\gamma^0=1$; after the pad every reward is $0$.

$$G_0^{\text{right}}=-1+0.9\cdot(-1)+0.9^2\cdot10$$

$R_{k+1}$ gets weight $0.9^k$, so the third reward, $R_3$, gets $0.81$.

$$=-1-0.9+8.1=6.2$$

The $+10$ loses only a fifth of its value over two extra steps.

Break-even discount rate

$$-1-\gamma+10\gamma^2=1\iff10\gamma^2-\gamma-2=0$$

Set the right plan's return, as a function of $\gamma$, equal to the left plan's $1$.

$$\gamma=\frac{1\pm\sqrt{1+80}}{20}=\frac{1\pm9}{20}$$

Quadratic formula; the discriminant $81$ is a perfect square.

$$\gamma=0.5$$

The other root, $-0.4$, is not a discount rate.

Answer $$\boxed{G_0^{\text{right}}=6.2>G_0^{\text{left}}=1\ \text{at}\ \gamma=0.9;\qquad\text{tie at}\ \gamma=0.5}$$
Check

At $\gamma=0.5$: $-1-0.5+0.25\cdot10=1$, exactly the left plan. At $\gamma=0.3$: $-1-0.3+0.9=-0.4<1$, so below the break-even the pad wins.

This settles the opening: at $\gamma=0.9$ the robot goes right, and below $\gamma=0.5$ it should take the $+1$. The discount rate is part of the problem, not a detail.

+2 forever against +5 for three steps, at γ = 0.9

Plan A pays $+2$ at every step forever. Plan B pays $+5$ at each of the next three steps and nothing afterwards. Which has the larger return at $\gamma=0.9$?

Find$G_t$ of both plans.
Given
  • A: $R_{t+k+1}=2$ for every $k\ge0$

  • B: $R_{t+1}=R_{t+2}=R_{t+3}=5$, later rewards $0$

  • $\gamma=0.9$

Solution

Both are geometric sums, so $\sum_{k\ge0}\gamma^k=\frac1{1-\gamma}$ and $\sum_{k=0}^{n-1}\gamma^k=\frac{1-\gamma^n}{1-\gamma}$ replace adding term by term.

Plan A

$$G_t^{A}=2\sum_{k=0}^{\infty}0.9^k=\frac{2}{1-0.9}=20$$

An infinite geometric series with ratio $0.9<1$ converges.

Plan B

$$G_t^{B}=5\,(1+0.9+0.81)=5\cdot2.71=13.55$$

Three terms are short enough to add directly.

Answer $$\boxed{G_t^{A}=20>G_t^{B}=13.55}$$
Check

Closed form for B: $5\cdot\frac{1-0.9^3}{0.1}=5\cdot2.71=13.55$. A's partial sums are $20(1-0.9^n)$: $13.03$ after $10$ steps, $13.72$ after $11$, so A overtakes B only at step $11$.

A constant reward $r$ forever is worth $r/(1-\gamma)$, which at $\gamma=0.9$ is ten times $r$.

Returns of every step of one episode, computed backwards

An episode produces the rewards $R_1=0$, $R_2=0$, $R_3=-1$, $R_4=5$ and then ends. With $\gamma=0.8$, find $G_0$, $G_1$, $G_2$ and $G_3$.

Find$G_3$, $G_2$, $G_1$ and $G_0$.
Given
  • $R_1=0,\ \allowbreak R_2=0,\ \allowbreak R_3=-1,\ \allowbreak R_4=5$; the episode ends after $R_4$

  • $\gamma=0.8$

Solution

Computing each return from scratch repeats work; the recursion $G_t=R_{t+1}+\gamma G_{t+1}$ gives all four with one multiplication each, starting from the end.

Start at the end

$$G_3=R_4=5$$

After the episode ends every reward is $0$, so $G_4=0$.

Step backwards

$$G_2=R_3+0.8\,G_3=-1+4=3$$

The recursion at $t=2$.

$$G_1=R_2+0.8\,G_2=0+2.4=2.4$$

Same step; a zero reward only passes the discounted tail on.

$$G_0=R_1+0.8\,G_1=0+1.92=1.92$$

One more step reaches the start.

Answer $$\boxed{G_3=5,\quad G_2=3,\quad G_1=2.4,\quad G_0=1.92}$$
Check

Directly from the definition: $G_0=0+0.8\cdot0+0.64\cdot(-1)+0.512\cdot5=-0.64+2.56=1.92$.

Backwards through an episode each return costs one multiply and one add; Q-learning passes a final reward back to earlier steps in the same order.

Checkpoint
§14.1 — which power of γ goes with which reward

A robot's only nonzero reward after time $t$ is $+8$, received as $R_{t+3}$. The discount rate is $\gamma=0.5$.

Find(a) What is $G_t$?
Given
  • $R_{t+3}=8$; every other reward after time $t$ is $0$

  • $\gamma=0.5$

Hint 1/4

Find which power of $\gamma$ multiplies $R_{t+3}$ in $G_t$.

Hint 2/4

$G_t=\sum_{k\ge0}\gamma^kR_{t+k+1}$: the reward $R_{t+k+1}$ has weight $\gamma^k$.

Hint 3/4

$R_{t+3}=8$ is the term with $k=2$, and $\gamma=0.5$, so $G_t=0.5^2\cdot8$.

Hint 4/4

$G_t=0.25\cdot8=2$.

Show solution

Only one term of the sum survives, so the whole question is which exponent it carries.

Match the index

$$t+k+1=t+3\ \Rightarrow\ k=2$$

Solve for the position of $R_{t+3}$ in the sum.

$$G_t=0.5^2\cdot8=2$$

Every other term is $0$.

Answer $$\boxed{G_t=2}$$
Check

With the recursion: $G_{t+2}=8$, $G_{t+1}=0+0.5\cdot8=4$, $G_t=0+0.5\cdot4=2$.

The first reward after the decision is undiscounted; count the exponent from there.

⚠ Discounting the first reward

It feels natural that every future reward is discounted, the next one included.

wrong$$G_t=\gamma R_{t+1}+\gamma^2R_{t+2}+\gamma^3R_{t+3}+\dots$$
right$$G_t=R_{t+1}+\gamma R_{t+2}+\gamma^2R_{t+3}+\dots$$
⚠ Treating γ = 1 as harmless in a task that never ends

The slides allow $\gamma=1$, and for tasks that always end it is fine.

wrong$$\gamma=1:\ \ 1+1+1+\dots\ \text{is a finite value}$$
right$$\sum_{k\ge0}\gamma^k=\frac{1}{1-\gamma}\ \text{needs}\ \gamma<1$$

14.2Markov decision processes: a model of how the world answers

Models the environment by states, allowed actions, $p(s'|s,a)$ and $r(s,a,s')$; the future depends only on the present.

To compare plans whose outcomes are random, we need a model of what each action can lead to and how likely each outcome is.

DefinitionDefinition 14.2: Markov decision process
Conditions
  • a finite set of states $\mathcal S$ and, for each state $s$, a finite set of allowed actions $\mathcal A(s)$

  • the Markov property holds for the chosen state (first two lines of the box)

  • each row of the transition table adds up to one: $\sum_{s'}p(s'|s,a)=1$ for every pair $(s,a)$

$$\boxed{\begin{aligned}&\Pr(R_{t+1}=r,\,S_{t+1}=s'\mid\mathcal H_t)\\&\qquad=\Pr(R_{t+1}=r,\,S_{t+1}=s'\mid S_t,A_t)\\&p(s'|s,a)=\Pr(S_{t+1}=s'\mid S_t=s,\,A_t=a)\\&\textcolor{#d1690a}{r(s,a,s')}=\mathbb E[R_{t+1}\mid S_t=s,\,A_t=a,\,S_{t+1}=s']\\&\textcolor{#d1690a}{r(s,a)}=\textstyle\sum_{s'}p(s'|s,a)\,\textcolor{#d1690a}{r(s,a,s')}\end{aligned}}$$

Once you know the present state and action, the rest of the history adds nothing about what comes next. So two tables describe the environment: $p(s'|s,a)$, where each action can lead and how likely each landing state is, and $r(s,a,s')$, the average reward that transition pays. Averaging over the landing state gives the expected reward $r(s,a)$ of the action.

Looks like this, but is not

A cart on a track whose state is its position alone looks Markov: the next position seems to depend only on the position and the push.

If the cart carries momentum, two visits to the same position move differently depending on how the cart got there. Position plus velocity is Markov; position alone is not. The property belongs to the state you choose.

The machine-maintenance problem written as an MDP

A machine is good or worn at the start of each day. Write the problem as an MDP.

  • A good machine wears out during the day with probability $0.5$. Running it yields $7$ units if it ends the day good and $5$ if it wore out.
  • A worn machine can run for $2$ units and stays worn.
  • Or it can be repaired for a cost of $1$ unit and is good the next day.
Find$\mathcal S$, $\mathcal A(s)$, the transition probabilities, $r(s,a,s')$ and $r(s,a)$.
Given
  • a good machine wears out during a day with probability $0.5$

  • rewards: $7$ or $5$ for running a good machine, $2$ for running a worn one, $-1$ for a repair

Solution

Name the states first, since everything else is indexed by them; then list each state's actions, then where each action leads.

States and actions

$$\mathcal S=\{\text{good},\text{worn}\}$$

The machine's condition is all that matters for tomorrow.

$$\mathcal A(\text{good})=\{\text{run}\},\quad\mathcal A(\text{worn})=\{\text{run},\text{repair}\}$$

Repair is offered only when there is something to repair, so the action sets differ by state.

Transitions

$$p(\text{good}|\text{good},\text{run})=p(\text{worn}|\text{good},\text{run})=0.5$$

The daily wear-out probability and its complement.

$$p(\text{worn}|\text{worn},\text{run})=1,\quad p(\text{good}|\text{worn},\text{repair})=1$$

Both are certain; each row still adds up to $1$.

Rewards

$$r(\text{good},\text{run},\text{good})=7,\quad r(\text{good},\text{run},\text{worn})=5$$

The reward depends on the state reached, which is why the slides write $r(s,a,s')$.

$$r(\text{good},\text{run})=0.5\cdot7+0.5\cdot5=6$$

Average over the landing state with the transition probabilities.

$$r(\text{worn},\text{run})=2,\qquad r(\text{worn},\text{repair})=-1$$

A cost enters as a negative reward.

Answer $$\boxed{\begin{aligned}&\text{good}\xrightarrow{\text{run}}\text{good}\ (0.5,\,7)\ \text{or worn}\ (0.5,\,5)\\&\text{worn}\xrightarrow{\text{run}}\text{worn}\ (1,\,2),\qquad\text{worn}\xrightarrow{\text{repair}}\text{good}\ (1,\,-1)\end{aligned}}$$
Check

Each of the three rows adds to $1$: $0.5+0.5$, $1$ and $1$. The expected reward $6$ lies between $5$ and $7$, as an average must.

Write an MDP as a list of rows, $(s,a)\to$ landing states with $p$ and $r$; every later computation reads from that list.

Fixing a state that is not Markov: an elevator

An elevator serves floors $1$, $2$ and $3$. It moves one floor per step and keeps its direction until it reaches floor $1$ or $3$, where it turns around. A student takes the floor number as the state. Check the Markov property at floor $2$ and repair the state.

Find$\Pr(S_{t+1}=3\mid\cdot)$ for two histories that end at floor $2$, and a state that is Markov.
Given
  • floors $1,2,3$; one floor per step

  • the direction reverses only at floors $1$ and $3$

Solution

One counterexample, two histories with the same present but different futures, is enough to show that the property fails.

Two histories ending at floor 2

$$\Pr(S_{t+1}=3\mid S_{t-1}=1,\ S_t=2)=1$$

Coming from floor $1$ the elevator is going up, so it continues to $3$.

$$\Pr(S_{t+1}=3\mid S_{t-1}=3,\ S_t=2)=0$$

Coming from floor $3$ it is going down, so it continues to $1$.

Repair the state

$$S_t=(\text{floor},\text{direction}):\ (2,\uparrow)\to(3,\downarrow),\ \ (2,\downarrow)\to(1,\uparrow)$$

With the direction in the state the present fixes the next state; at an end floor the direction flips.

Answer $$\boxed{\text{floor alone: not Markov;}\qquad(\text{floor},\text{direction}):\ \text{Markov}}$$
Check

The four states $(1,\uparrow)$, $(2,\uparrow)$, $(3,\downarrow)$, $(2,\downarrow)$ form one cycle with a single successor each, so every is $0$ or $1$ and no history can change it.

When something from the past changes the future, put it into the state.

Checkpoint
§14.2 — two days of the machine

A machine is good this morning. Running a good machine wears it out during the day with probability $0.5$; a worn machine that is run stays worn. The machine is run today and tomorrow.

Find(a) What is the probability that it is worn the morning after tomorrow?
Given
  • $p(\text{worn}|\text{good},\text{run})=0.5$, $p(\text{good}|\text{good},\text{run})=0.5$

  • $p(\text{worn}|\text{worn},\text{run})=1$

Hint 1/4

Split the two days by the machine's state in between.

Hint 2/4

Markov property and total probability: $\Pr(S_{t+2}=w)=\sum_{s}\Pr(S_{t+1}=s)\,p(w|s,\text{run})$.

Hint 3/4

Here $\Pr(S_{t+1}=\text{worn})=0.5$ with $p(\text{worn}|\text{worn},\text{run})=1$, and $\Pr(S_{t+1}=\text{good})=0.5$ with $p(\text{worn}|\text{good},\text{run})=0.5$.

Hint 4/4

$0.5\cdot1+0.5\cdot0.5=0.75$.

Show solution

The Markov property lets the second day depend only on the state after the first day, so condition on that state.

Condition on tomorrow's state

$$\Pr(S_{t+2}=\text{w})=0.5\cdot p(\text{w}|\text{w},\text{run})+0.5\cdot p(\text{w}|\text{g},\text{run})$$

Total probability over the two possible middle states.

$$=0.5\cdot1+0.5\cdot0.5=0.75$$

A worn machine stays worn; a good one gets a second chance to wear out.

Answer $$\boxed{0.75}$$
Check

The complement: it stays good only if it survives both days, $0.5\cdot0.5=0.25$, and $1-0.25=0.75$.

For several steps, condition on the states in between; the Markov property makes each factor a one-step probability.

⚠ Adding the transition table over the wrong index

Both indices are states, and the bar in $p(s'|s,a)$ is easy to read backwards.

wrong$$\sum_{s}p(s'|s,a)=1$$
right$$\sum_{s'}p(s'|s,a)=1\ \text{for every pair }(s,a)$$
⚠ Using the likeliest landing state's reward as the action's reward

The likeliest outcome feels like what the action pays.

wrong$$r(s,a)=r(s,a,s'_{\text{likeliest}})$$
right$$r(s,a)=\sum_{s'}p(s'|s,a)\,r(s,a,s')$$

14.3Policies and their values

Scores a policy by its expected return: $v_\pi(s)$ from a state, $q_\pi(s,a)$ from a state with the first action fixed.

A policy says what to do in each state; its value says how much return that earns on average, which lets us compare two policies state by state.

DefinitionDefinition 14.3: Stationary Markov policy and value functions
Conditions
  • general policy: $A_t\sim\pi(\cdot\mid\mathcal H_{t-1},R_t,S_t)$, using everything that happened so far

  • stationary Markov policy: $A_t\sim\pi(\cdot\mid S_t)$, the same rule at every step; deterministic if it puts probability $1$ on one action, written $\pi(s)$

  • $\mathbb E_\pi$: expectation when actions follow $\pi$ and transitions follow $p$

$$\boxed{\begin{aligned}\textcolor{#1f6feb}{v_\pi(s)}&=\mathbb E_\pi[G_t\mid S_t=s]\\\textcolor{#1f6feb}{q_\pi(s,a)}&=\mathbb E_\pi[G_t\mid S_t=s,\,A_t=a]\\\textcolor{#1f6feb}{v_\pi(s)}&=\textstyle\sum_{a\in\mathcal A(s)}\textcolor{#8250df}{\pi(a|s)}\,\textcolor{#1f6feb}{q_\pi(s,a)}\end{aligned}}$$

$v_\pi(s)$ is the average return from $s$ when every action is chosen by $\pi$. $q_\pi(s,a)$ is the same average when the first action is forced to be $a$ and $\pi$ takes over from the next step. Averaging $q_\pi(s,\cdot)$ with $\pi$'s own action probabilities gives back $v_\pi(s)$.

The one-step equation for a fixed policy (further reading)

Not on the slides. It turns the definitions into equations you can solve. Start from $G_t=R_{t+1}+\gamma G_{t+1}$.

Condition on the landing state. By the Markov property the expected $G_{t+1}$ given $S_{t+1}=s'$ is $v_\pi(s')$, whatever happened before.

Result. $q_\pi(s,a)=\sum_{s'}p(s'|s,a)\,[r(s,a,s')+\gamma v_\pi(s')]$. With the third line of the box this is one linear equation per state.

Looks like this, but is not

$v_\pi(s)$ looks like the return you will collect if you start in $s$ and follow $\pi$.

It is an average over random transitions and random action choices. For the machine, one day-by-day run from good can earn far more or far less than $v_\pi(\text{good})$; only the mean over many runs equals it.

Values of 'always right' and 'always left' in the corridor

In the corridor, find $v_\pi$ in S0, S1 and S2 for the policy that always moves right and for the one that always moves left.

Find$v_{\text{right}}$ and $v_{\text{left}}$ in S0, S1 and S2.
Given
  • cells: pad, S0, S1, S2, drop; pad and drop are terminal

  • entering the pad: $+1$; entering the drop: $+10$; any other move: $-1$

  • $\gamma=0.9$

Solution

Moves are deterministic, so each policy produces one fixed reward sequence from each cell; start next to the terminal ends, where the sequences are shortest.

Always right

$$v_{\text{right}}(\text{S2})=10$$

One move into the drop.

$$v_{\text{right}}(\text{S1})=-1+0.9\cdot10=8$$

One move to S2, then S2's return scaled by the discount.

$$v_{\text{right}}(\text{S0})=-1+0.9\cdot8=6.2$$

The same step once more; it matches the $6.2$ of the opening.

Always left

$$v_{\text{left}}(\text{S0})=1$$

One move onto the pad.

$$v_{\text{left}}(\text{S1})=-1+0.9\cdot1=-0.1$$

One costly move, then S0's value.

$$v_{\text{left}}(\text{S2})=-1+0.9\cdot(-0.1)=-1.09$$

Two costly moves before the pad.

Answer $$\boxed{v_{\text{right}}=(6.2,\ 8,\ 10),\qquad v_{\text{left}}=(1,\ -0.1,\ -1.09)}$$
Check

Directly for left from S2: rewards $-1,-1,+1$ give $-1-0.9+0.81=-1.09$. Right is better in all three cells.

In a deterministic world a policy's value is one reward sequence summed with discounts; work backwards from the ends.

Value of 'repair when worn' for the machine, from two linear equations

For the machine with $\gamma=0.8$, the policy $\pi_R$ runs a good machine and repairs a worn one. Find $v_{\pi_R}(\text{good})$ and $v_{\pi_R}(\text{worn})$.

Find$v(\text{good})$ and $v(\text{worn})$ under $\pi_R$.
Given
  • good, run: to good ($0.5$, reward $7$) or to worn ($0.5$, reward $5$)

  • worn, repair: to good ($1$, reward $-1$)

  • $\gamma=0.8$

Solution

The reward sequence is now random, so there is no single sequence to sum. Two linear equations are more work than adding a few numbers, and here they are the only way: the one-step equation (further reading, in the box) gives one per state.

One equation per state

$$v(\text{g})=6+0.8\,[0.5\,v(\text{g})+0.5\,v(\text{w})]$$

Expected reward $0.5\cdot7+0.5\cdot5=6$, then $\gamma$ times the average value of the landing state.

$$v(\text{w})=-1+0.8\,v(\text{g})$$

A repair always lands in good.

Solve the system

$$v(\text{g})=6+0.4\,v(\text{g})+0.4\,(-1+0.8\,v(\text{g}))$$

Worn's equation already gives $v(\text{w})$ in terms of $v(\text{g})$, so substituting it leaves one unknown.

$$0.28\,v(\text{g})=5.6\ \Rightarrow\ v(\text{g})=20,\quad v(\text{w})=15$$

The equation is linear in $v(\text{g})$ with a nonzero coefficient, so it has one solution; worn then follows from $v(\text{w})=-1+0.8\,v(\text{g})$.

Answer $$\boxed{v_{\pi_R}(\text{good})=20,\qquad v_{\pi_R}(\text{worn})=15}$$
Check

Plug back: $6+0.8\,(0.5\cdot20+0.5\cdot15)=6+14=20$ and $-1+0.8\cdot20=15$. Both equations hold.

For a fixed policy the value equations are linear: one unknown per state and no max.

From action values to a state value under a random policy

In a state $s$ with three actions, $q_\pi(s,\cdot)=(4,\ 1,\ -2)$ and the policy picks the actions with probabilities $(0.8,\ 0.1,\ 0.1)$. Find $v_\pi(s)$.

Find$v_\pi(s)$.
Given
  • $q_\pi(s,a_1)=4,\ q_\pi(s,a_2)=1,\ q_\pi(s,a_3)=-2$

  • $\pi(a_1|s)=0.8,\ \pi(a_2|s)=0.1,\ \pi(a_3|s)=0.1$

Solution

The third line of the box averages the action values with the policy's own probabilities; nothing else is needed.

Average over the policy

$$v_\pi(s)=0.8\cdot4+0.1\cdot1+0.1\cdot(-2)$$

Weight each action value by the chance that the policy picks that action.

$$=3.2+0.1-0.2=3.1$$

The two rarely chosen actions nearly cancel: $+0.1$ and $-0.2$.

Answer $$\boxed{v_\pi(s)=3.1}$$
Check

The value must lie between the smallest and largest action values, $-2$ and $4$, and close to $4$ because $a_1$ carries most of the weight; $3.1$ does.

A state's value under a policy is an average of its action values, not their maximum.

Checkpoint
§14.3 — the corridor at a smaller discount

In the corridor, the policy moves right in every cell. From S0 it pays $-1$, $-1$ and then $+10$ as it enters the drop, which ends the run. Now $\gamma=0.5$.

Find(a) What is $v_{\text{right}}(\text{S0})$?
Given
  • rewards from S0 under always right: $-1,\ -1,\ +10$, then the run ends

  • $\gamma=0.5$

Hint 1/4

Write the reward sequence from S0 and give each reward its weight.

Hint 2/4

$v(\text{S0})=R_1+\gamma R_2+\gamma^2R_3$ for this deterministic sequence.

Hint 3/4

Here $R_1=-1$, $R_2=-1$, $R_3=10$ and $\gamma=0.5$: $-1-0.5+0.25\cdot10$.

Hint 4/4

$v_{\text{right}}(\text{S0})=1$.

Show solution

A in a deterministic corridor gives one reward sequence, so the value is that sequence's return.

Weights 1, 0.5, 0.25

$$v=-1+0.5\cdot(-1)+0.25\cdot10=1$$

The third reward is two steps after the first, so it gets $0.5^2$.

Answer $$\boxed{v_{\text{right}}(\text{S0})=1}$$
Check

Backwards: $v(\text{S2})=10$, $v(\text{S1})=-1+5=4$, $v(\text{S0})=-1+2=1$.

This is the break-even $\gamma$ of the opening: right and left are both worth $1$.

⚠ Taking the maximum instead of the policy's average

The optimal value uses a max, and the two formulas sit side by side on the slides.

wrong$$v_\pi(s)=\max_a q_\pi(s,a)$$
right$$v_\pi(s)=\sum_a\pi(a|s)\,q_\pi(s,a)$$
⚠ Repeating the forced action in q

The name $q_\pi(s,a)$ mentions $a$, so it is tempting to keep playing $a$.

wrong$$q_\pi(s,a)=\text{return of always playing }a$$
right$$q_\pi(s,a):\ a\ \text{once, then}\ \pi\ \text{from the next step}$$

14.4The optimal policy and the Bellman optimality equations

Characterizes the best achievable values: $v_*(s)=\max_aq_*(s,a)$, and taking the action with the largest $q_*$ in every state is optimal.

Comparing policies state by state raises two questions: is there one policy that is best in every state at once, and how do we recognize it?

TheoremTheorem 14.1: Optimal policies and the Bellman optimality equations
Conditions
  • infinite-horizon discounted MDP: finite $\mathcal S$ and $\mathcal A$, bounded rewards, $\gamma<1$

  • $\pi^*$ is optimal if $v_{\pi^*}(s)\ge v_\pi(s)$ for every state $s$ and every policy $\pi$; then $v_*=v_{\pi^*}$

  • existence (the slides cite Puterman, 1994): some deterministic stationary Markov policy is optimal

$$\boxed{\begin{aligned}\textcolor{#1f6feb}{v_*(s)}&=\max_{a\in\mathcal A(s)}\textcolor{#1f6feb}{q_*(s,a)}\\\textcolor{#1f6feb}{q_*(s,a)}&=r(s,a)+\gamma\sum_{s'}p(s'|s,a)\,\textcolor{#1f6feb}{v_*(s')}\\\textcolor{#8250df}{\pi^*(s)}&=\arg\max_{a}\,\textcolor{#1f6feb}{q_*(s,a)}\end{aligned}}$$

The best value of a state is the value of its best first action. That action's value is its expected reward plus $\gamma$ times the average best value of where it lands. Substituting one line into the other gives the two equations on the slides, and the optimal policy takes the action with the largest $q_*$.

Why the equations hold (sketch)

Any first action, then optimal play. Taking $a$ in $s$ and acting optimally afterwards earns, by $G_t=R_{t+1}+\gamma G_{t+1}$ and the Markov property, $r(s,a)+\gamma\sum_{s'}p(s'|s,a)\,v_*(s')$ on average.

No policy beats the best first action. A policy that randomizes in $s$ earns a weighted average of these numbers, which is at most their maximum.

So $v_*(s)$ is that maximum, and picking an action that attains it in every state is a deterministic stationary policy.

Looks like this, but is not

Picking the action with the largest immediate reward looks like the optimal policy in disguise, since $v_*$ also takes a max over actions.

The max in $v_*$ is over reward plus $\gamma$ times the value of the landing state. In the corridor left pays more now, $+1$ against $-1$, but $q_*(\text{S0},\text{right})=6.2>q_*(\text{S0},\text{left})=1$.

Checking claimed optimal values in the corridor

A classmate claims that in the corridor with $\gamma=0.9$ the optimal values are $6.2$ in S0, $8$ in S1 and $10$ in S2. Check the optimality equation in each cell and give $\pi^*$.

FindWhether each equation holds, and $\pi^*$.
Given
  • cells in a row: pad, S0, S1, S2, drop; S0, S1 and S2 each have a left and a right move

  • moves cost $1$; entering the pad pays $+1$ and entering the drop $+10$; both end the run and have value $0$

  • $\gamma=0.9$

  • claimed $v_*=(6.2,\ 8,\ 10)$ in S0, S1, S2

Solution

Values that satisfy the optimality equations in every state are the optimal values (for $\gamma<1$ the solution is unique), so three checks settle the claim without searching over policies.

S0

$$q(\text{S0},\text{L})=1,\qquad q(\text{S0},\text{R})=-1+0.9\cdot8=6.2$$

Left lands on the terminal pad; right lands in S1 with claimed value $8$.

$$\max(1,\ 6.2)=6.2\ \checkmark$$

Matches the claim, and right attains it.

S1

$$q(\text{S1},\text{L})=-1+0.9\cdot6.2=4.58,\qquad q(\text{S1},\text{R})=8$$

Each move costs $1$; the landing values come from the claim.

$$\max(4.58,\ 8)=8\ \checkmark$$

The claim holds in S1, and right attains it.

S2

$$q(\text{S2},\text{L})=-1+0.9\cdot8=6.2,\qquad q(\text{S2},\text{R})=10$$

Right enters the drop and the run ends.

$$\max(6.2,\ 10)=10\ \checkmark$$

The claim holds in S2, and right attains it.

Answer $$\boxed{v_*=(6.2,\ 8,\ 10);\qquad\pi^*=\text{right in S0, S1 and S2}}$$
Check

Independently, these are the values of always right found by summing its reward sequences, so that policy attains the claimed optimum.

To test claimed optimal values, score every action with them and check that the best score reproduces the claim.

Optimal action values of the machine and the decision when worn

For the machine with $\gamma=0.8$ it is known that $v_*(\text{good})=20$ and $v_*(\text{worn})=15$. Find $q_*$ for all three state-action pairs and $\pi^*$.

Find$q_*(\text{good},\text{run})$, $q_*(\text{worn},\text{run})$, $q_*(\text{worn},\text{repair})$ and $\pi^*$.
Given
  • good, run: to good ($0.5$, reward $7$) or worn ($0.5$, reward $5$)

  • worn, run: to worn ($1$, reward $2$); worn, repair: to good ($1$, reward $-1$)

  • $\gamma=0.8$, $v_*(\text{good})=20$, $v_*(\text{worn})=15$

Solution

With $v_*$ known, each $q_*$ is one line of the box, so there is no system to solve.

Good

$$q_*(\text{g},\text{run})=0.5\,(7+0.8\cdot20)+0.5\,(5+0.8\cdot15)$$

Average over the two landing states, each with its own reward.

$$=0.5\cdot23+0.5\cdot17=20$$

$7+16=23$ and $5+12=17$, each with weight $0.5$.

Worn

$$q_*(\text{w},\text{run})=2+0.8\cdot15=14$$

It stays worn.

$$q_*(\text{w},\text{repair})=-1+0.8\cdot20=15$$

Pay $1$, land in good.

Policy

$$\pi^*(\text{w})=\arg\max(14,\ 15)=\text{repair},\quad\pi^*(\text{g})=\text{run}$$

Good has only one action.

Answer $$\boxed{\begin{aligned}&q_*(\text{g},\text{run})=20,\quad q_*(\text{w},\text{run})=14,\quad q_*(\text{w},\text{repair})=15\\&\pi^*(\text{w})=\text{repair}\end{aligned}}$$
Check

$\max_aq_*(s,a)$ gives back $20$ and $15$, the given optimal values, as the first line of the box requires. These are also the values of $\pi_R$ found by solving its linear equations.

Knowing $v_*$ turns the policy question into one look-ahead per action; knowing $q_*$ makes it a plain argmax.

A randomized first move cannot beat the best action

At the worn machine, a policy repairs with probability $\theta$ and runs otherwise, then acts optimally. Using $q_*(\text{worn},\text{repair})=15$ and $q_*(\text{worn},\text{run})=14$, show that its value at worn is at most $15$.

FindThe value of the mixed first move as a function of $\theta$.
Given
  • $q_*(\text{worn},\text{repair})=15$, $q_*(\text{worn},\text{run})=14$

  • repair with probability $\theta\in[0,1]$

Solution

The value of a random first action is the probability-weighted average of the two action values, so compare an average with a maximum.

Average against maximum

$$\theta\cdot15+(1-\theta)\cdot14=14+\theta$$

Weight each action value by its probability.

$$14+\theta\le15,\ \text{with equality only at}\ \theta=1$$

$\theta$ is a probability, so $\theta\le1$; the bound is reached only with all weight on repair.

Answer $$\boxed{14+\theta\le15=v_*(\text{worn})}$$
Check

At $\theta=0.5$: $0.5\cdot15+0.5\cdot14=14.5$, halfway between and below $15$.

This is why a deterministic optimal policy exists: randomizing only mixes in actions that are no better.

Checkpoint
§14.4 — reading v* and π* off q*

In a state $s$ with three actions, the optimal action values are $q_*(s,a_1)=3$, $q_*(s,a_2)=7$ and $q_*(s,a_3)=5$.

Find(a) What are $v_*(s)$ and $\pi^*(s)$?
Given$q_*(s,\cdot)=(3,\ 7,\ 5)$
Hint 1/4

Recall how the box builds $v_*$ and $\pi^*$ from $q_*$.

Hint 2/4

$v_*(s)=\max_aq_*(s,a)$ and $\pi^*(s)=\arg\max_aq_*(s,a)$.

Hint 3/4

The entries are $3$, $7$ and $5$ for $a_1$, $a_2$, $a_3$.

Hint 4/4

$v_*(s)=7$ and $\pi^*(s)=a_2$.

Show solution

Once $q_*$ is known no look-ahead is needed; the box gives both answers directly.

Max and argmax

$$v_*(s)=\max(3,\ 7,\ 5)=7$$

The best action's value.

$$\pi^*(s)=a_2$$

The action that attains it; no randomizing, since the maximum is unique.

Answer $$\boxed{v_*(s)=7,\qquad\pi^*(s)=a_2}$$
Check

Any other choice earns a weighted average of $3$, $7$, $5$, which is below $7$ unless all the weight is on $a_2$.

An average of action values belongs to a given policy; the optimal value is their maximum.

⚠ Putting the max after the average

Both $\max$ and $\sum$ appear in the equation, and their order looks cosmetic.

wrong$$v_*(s)=\sum_{s'}p(s'|s,a)\max_a\big[r+\gamma v_*(s')\big]$$
right$$v_*(s)=\max_a\sum_{s'}p(s'|s,a)\big[r(s,a,s')+\gamma v_*(s')\big]$$
⚠ Heading for the most valuable neighbour

Moving toward the state with the largest value feels like following the values.

wrong$$\pi^*(s)=\arg\max_{s'}v_*(s')$$
right$$\pi^*(s)=\arg\max_a\big[r(s,a)+\gamma\textstyle\sum_{s'}p(s'|s,a)\,v_*(s')\big]$$

14.5Value iteration: solving the optimality equations by repeated look-ahead

Computes $v_*$ when $p$ and $r$ are known: sweep $v_{k+1}(s)=\max_a[r(s,a)+\gamma\sum_{s'}p(s'|s,a)v_k(s')]$ until the values settle, then act greedily.

The optimality equations hide a max inside, so they are not a linear system we can solve in one go; value iteration uses them as an update rule instead.

MethodAlgorithm 14.1: Value iteration, in v form and in q form
Conditions
  • $p(s'|s,a)$ and $r(s,a,s')$ are known and $\gamma<1$

  • synchronous sweep: every $v_{k+1}(s)$ is computed from $v_k$ only

  • stop at the first $k^*$ with $\max_s\lvert v_{k^*}(s)-v_{k^*-1}(s)\rvert\le\epsilon$; terminal states keep value $0$

  • the slides write the look-ahead as $\sum_{s'}p(s'|s,a)[r(s,a,s')+\gamma v_k(s')]$, which is the same number as below

$$\boxed{\begin{aligned}&v_0(s)=0\\&\textcolor{#1f6feb}{v_{k+1}(s)}=\max_{a}\Big[r(s,a)+\gamma\sum_{s'}p(s'|s,a)\,\textcolor{#1f6feb}{v_k(s')}\Big]\\&\textcolor{#8250df}{\pi(s)}=\arg\max_{a}\Big[r(s,a)+\gamma\sum_{s'}p(s'|s,a)\,\textcolor{#1f6feb}{v_{k^*}(s')}\Big]\\&\textcolor{#1f6feb}{q_{k+1}(s,a)}=r(s,a)+\gamma\sum_{s'}p(s'|s,a)\max_{a'}\textcolor{#1f6feb}{q_k(s',a')}\end{aligned}}$$

Start from $v_0=0$. In each sweep, give every state the value of its best action, scoring an action by its expected reward plus $\gamma$ times the average old value of where it lands. Stop when no value moves by more than $\epsilon$, and act greedily with the last values. The q form keeps one number per pair and does the same.

Why it converges (further reading)

A sweep shrinks distances. For any two value tables $v$ and $w$, one sweep applied to each gives tables that differ by at most $\gamma\max_s\lvert v(s)-w(s)\rvert$, because a max of averages moves no more than the values it averages.

Apply it to $v_k$ and $v_*$. A sweep leaves $v_*$ unchanged, so $\max_s\lvert v_{k+1}(s)-v_*(s)\rvert\le\gamma\max_s\lvert v_k(s)-v_*(s)\rvert$.

Geometric decay. After $k$ sweeps the error is at most $\gamma^k$ times the first one; with $\gamma=0.8$ each sweep removes at least a fifth of what is left.

Looks like this, but is not

After $k$ sweeps, $v_k$ looks like the value of the greedy policy of that sweep.

From $v_0=0$, $v_k(s)$ is the best expected discounted reward over the next $k$ steps only. For the machine $v_1(\text{worn})=2$, while its greedy policy, run forever, is worth $2/(1-0.8)=10$ at worn.

Two sweeps of value iteration on the machine

Run value iteration on the machine with $\gamma=0.8$ from $v_0=0$ for two sweeps. After each sweep, give the greedy action in the worn state.

Find$v_1$, $v_2$ and the greedy action in worn after each sweep.
Given
  • good, run: to good ($0.5$, reward $7$) or worn ($0.5$, reward $5$), so $r(\text{good},\text{run})=6$

  • worn, run: to worn ($1$, reward $2$); worn, repair: to good ($1$, reward $-1$)

  • $\gamma=0.8$, $v_0(\text{good})=v_0(\text{worn})=0$

Solution

The v form needs one look-ahead per action and one max per state; with two states that is three look-aheads per sweep.

Sweep 1

$$v_1(\text{g})=6+0.8\cdot0=6$$

Only one action, and with $v_0=0$ only the expected reward counts.

$$v_1(\text{w})=\max(2+0,\ -1+0)=2$$

One step of look-ahead: running pays more today, so run is greedy.

Sweep 2

$$v_2(\text{g})=6+0.8\,(0.5\cdot6+0.5\cdot2)=9.2$$

The old values $v_1$, weighted by where running a good machine lands.

$$\text{run: }2+0.8\cdot2=3.6,\qquad\text{repair: }-1+0.8\cdot6=3.8$$

Repair now sees the good machine's value $6$ one step later.

$$v_2(\text{w})=\max(3.6,\ 3.8)=3.8$$

Repair is greedy from this sweep on.

Answer $$\boxed{v_1=(6,\ 2),\ \text{worn}\to\text{run};\qquad v_2=(9.2,\ 3.8),\ \text{worn}\to\text{repair}}$$
Check

With the slides' form, rewards inside the sum: $v_2(\text{g})=0.5\,(7+0.8\cdot6)+0.5\,(5+0.8\cdot2)=5.9+3.3=9.2$, the same.

Two sweeps, three look-aheads each.

Keep the old table fixed during a sweep and fill a new one; the greedy action can change from one sweep to the next.

The same two sweeps in q form

Run two sweeps of value iteration in q form on the machine with $\gamma=0.8$, keeping the table $q_k(s,a)$ and starting from $q_0=0$.

Find$q_1$ and $q_2$ for the three pairs, and $\max_aq_2(s,a)$.
Given
  • the machine: $r(\text{g},\text{run})=6$ with landing states good and worn ($0.5$ each); $r(\text{w},\text{run})=2$, stays worn; $r(\text{w},\text{repair})=-1$, lands in good

  • $\gamma=0.8$, $q_0=0$ for all three pairs

Solution

The q form replaces $v_k(s')$ by $\max_{a'}q_k(s',a')$: the max is taken at the landing state, before averaging.

Sweep 1

$$q_1(\text{g},\text{run})=6,\quad q_1(\text{w},\text{run})=2,\quad q_1(\text{w},\text{rep})=-1$$

With $q_0=0$ each entry is its expected reward.

Sweep 2

$$q_2(\text{g},\text{run})=6+0.8\,(0.5\cdot6+0.5\cdot2)=9.2$$

$\max_{a'}q_1(\text{g},a')=6$ and $\max_{a'}q_1(\text{w},a')=2$.

$$q_2(\text{w},\text{run})=2+0.8\cdot2=3.6$$

It stays worn, whose best entry is 2.

$$q_2(\text{w},\text{rep})=-1+0.8\cdot6=3.8$$

It lands in good, whose best entry is 6.

Back to v

$$\max_aq_2(\text{g},a)=9.2,\qquad\max_aq_2(\text{w},a)=3.8$$

The slides' step: $v_{k^*}(s)=\max_aq_{k^*}(s,a)$.

Answer $$\boxed{q_2=(9.2;\ 3.6,\ 3.8),\qquad\max_aq_2=(9.2,\ 3.8)=v_2}$$
Check

The q form must reproduce the v form sweep by sweep, and it does: the previous example gave $v_2=(9.2,\ 3.8)$.

The q form stores one number per pair instead of per state, but then the policy is a plain argmax with no look-ahead.

A slippery robot takes the long way

From its start cell a robot can walk along a ledge straight into the goal, or take a detour: one step to a safe cell for $-1$, then into the goal. The goal pays $+10$ and ends the run. On the ledge the robot slips with probability $q$ into a pit that pays $-10$ and ends the run.

FindThe optimal first move for $q=0$ and $q=0.2$, and the $q$ at which the two moves tie.
Given
  • ledge: goal with probability $1-q$ ($+10$), pit with probability $q$ ($-10$); both end the run

  • detour: safe cell ($-1$), then goal ($+10$)

  • $\gamma=0.9$

Solution

Both landing cells of the ledge are terminal and the safe cell has one move, so one optimality look-ahead per action at the start settles everything.

Detour

$$v_*(\text{safe})=10,\qquad q_*(\text{start},\text{detour})=-1+0.9\cdot10=8$$

The safe cell's only move enters the goal.

Ledge

$$q_*(\text{start},\text{ledge})=(1-q)\cdot10+q\cdot(-10)=10-20q$$

An average over two terminal landing cells, so no $\gamma$ term.

$$q=0:\ 10>8;\qquad q=0.2:\ 6<8$$

The deterministic robot takes the ledge; the slippery one takes the detour.

Tie

$$10-20q=8\iff q=0.1$$

Above one slip in ten, the detour wins.

Answer $$\boxed{q=0:\ \text{ledge};\qquad q=0.2:\ \text{detour};\qquad\text{tie at }q=0.1}$$
Check

Picture $100$ slippery robots on the ledge: $80$ collect $+10$ and $20$ lose $10$, a total of $600$, or $6$ each. $100$ robots on the detour collect $8$ each, $800$ in all.

Noise in the moves pushes the optimal path away from hazards; this is the difference between the deterministic and the stochastic grid robot on the slides.

Checkpoint
§14.5 — what the first sweep computes

Value iteration starts from $v_0(s)=0$ in every state of an MDP with expected rewards $r(s,a)$.

Find(a) True or false: after one sweep, $v_1(s)=\max_ar(s,a)$, the best expected immediate reward.
Given
  • $v_0(s)=0$ for all $s$

  • $v_{k+1}(s)=\max_a[r(s,a)+\gamma\sum_{s'}p(s'|s,a)v_k(s')]$

Hint 1/4

Ask what is left of the update when every old value is zero.

Hint 2/4

$v_1(s)=\max_a[r(s,a)+\gamma\sum_{s'}p(s'|s,a)\,v_0(s')]$.

Hint 3/4

Here $v_0(s')=0$ for every $s'$, so the $\gamma$ term is $\gamma\cdot0=0$.

Hint 4/4

$v_1(s)=\max_ar(s,a)$: true.

Show solution

Substitute the starting values into the update and see which terms survive.

Substitute

$$v_1(s)=\max_a\big[r(s,a)+\gamma\cdot0\big]=\max_ar(s,a)$$

Every landing value is zero.

Answer $$\boxed{\text{True}}$$
Check

For the machine: $v_1(\text{good})=6$ and $v_1(\text{worn})=\max(2,-1)=2$, the expected immediate rewards.

From $v_0=0$, $v_k$ looks exactly $k$ steps ahead.

⚠ Using a value from the current sweep

Filling the table from top to bottom, the fresh number is right there.

wrong$$v_2(\text{w})=\max(2+0.8\cdot2,\ -1+0.8\cdot\underbrace{9.2}_{v_2(\text{g})})=6.36$$
right$$v_2(\text{w})=\max(2+0.8\cdot2,\ -1+0.8\cdot\underbrace{6}_{v_1(\text{g})})=3.8$$
⚠ Extracting the policy from the immediate reward

At the end the values feel finished, so the look-ahead seems no longer needed.

wrong$$\pi(s)=\arg\max_ar(s,a)$$
right$$\pi(s)=\arg\max_a\big[r(s,a)+\gamma\textstyle\sum_{s'}p(s'|s,a)\,v_{k^*}(s')\big]$$
sweep 3: S0 turns right, converged, γ = 0.9pad+1endS06.2S18S210drop+10end−1−1each move costs 1 (orange); entering pad or drop pays and ends the run

Step through value iteration on the corridor, $\gamma=0.9$. Each frame shows $\textcolor{#1f6feb}{v_k}$ in every cell and the $\textcolor{#8250df}{\text{greedy move}}$: the $+10$ travels one cell per sweep, and S0 switches from the pad to the drop at sweep $3$.

At the edges
sweep 1 S0 goes left

With one step of look-ahead the pad's $+1$ beats the $-1$ of moving right, exactly the one-step rule of the opening.

sweep 3 converged

Sweep $4$ changes no value, so the stopping rule fires for any $\epsilon>0$.

14.6Q-learning: learning q* from experience

Learns $q_*$ without $p$ or $r$: after each observed step, move $Q(S_t,A_t)$ a fraction $\alpha$ toward $R_{t+1}+\gamma\max_{a'}Q(S_{t+1},a')$.

Value iteration needs $p(s'|s,a)$ and $r(s,a,s')$; a robot in a new building or a recommender facing new users knows neither, but it can act and watch what happens.

MethodAlgorithm 14.2: Q-learning
Conditions
  • a table $Q(s,a)$ for every pair, for example all $0$ at the start

  • learning rate $0<\alpha\le1$; the target reads the table before the update, and $\max_{a'}Q(s',a')=0$ at a terminal $s'$

  • as on the slides: if every pair is tried infinitely often, $Q(s,a)\to q_*(s,a)$ with probability $1$

  • further reading: the standard theorem also needs each pair's learning rate to shrink, for example $\alpha=1/n$ at its $n$th update

$$\boxed{\begin{aligned}\textcolor{#d1690a}{\hat Q(S_t,A_t)}&=\textcolor{#d1690a}{R_{t+1}}+\gamma\max_{a'}\textcolor{#1f6feb}{Q(S_{t+1},a')}\\\textcolor{#1f6feb}{Q(S_t,A_t)}&\leftarrow(1-\alpha)\,\textcolor{#1f6feb}{Q(S_t,A_t)}+\alpha\,\textcolor{#d1690a}{\hat Q(S_t,A_t)}\end{aligned}}$$

After each step, form a one-sample guess of $q_*(S_t,A_t)$: the reward just received plus $\gamma$ times the best current entry at the state just reached. Keep a fraction $1-\alpha$ of the old entry and add $\alpha$ of the guess. Only the entry of the pair just tried changes.

Looks like this, but is not

Q-learning seems to need the action the agent takes next, $A_{t+1}$, to form its target.

The target uses $\max_{a'}Q(S_{t+1},a')$, the best entry at the next state, whatever the agent does there. Exploratory moves therefore do not drag the table toward the value of exploring; it still aims at $q_*$.

Three episodes of Q-learning in the corridor

In the corridor, start with $Q=0$ for every pair, $\alpha=0.5$ and $\gamma=0.9$. Three episodes each move right from S0 to the drop. Update the table after every move.

Find$Q(\text{S0},\text{R})$, $Q(\text{S1},\text{R})$ and $Q(\text{S2},\text{R})$ after each episode.
Given
  • cells in a row: pad, S0, S1, S2, drop; the pad and the drop end the run

  • moves: S0 to S1 and S1 to S2 pay $-1$; S2 to the drop pays $+10$ and ends the run

  • every cell also has a left action: S0 to the pad pays $+1$ and ends the run, S1 to S0 and S2 to S1 pay $-1$; left is never taken here, so its entries stay $0$ but still count in the max

  • $Q(s,a)=0$ for all pairs at the start

  • $\alpha=0.5$, $\gamma=0.9$

Solution

Apply the update in the order the moves happen, reading the table as it stands at that moment; the untried left entries stay 0 and still take part in the max.

Episode 1

$$Q(\text{S0},\text{R})=0.5\cdot0+0.5\,(-1+0.9\cdot0)=-0.5$$

Both entries of S1 are still 0.

$$Q(\text{S1},\text{R})=-0.5,\qquad Q(\text{S2},\text{R})=0.5\,(10+0)=5$$

The drop is terminal, so its max is 0.

Episode 2

$$Q(\text{S0},\text{R})=0.5\,(-0.5)+0.5\,(-1+0.9\cdot0)=-0.75$$

At S1 the untried $Q(\text{S1},\text{L})=0$ beats $-0.5$, so the max is 0.

$$Q(\text{S1},\text{R})=0.5\,(-0.5)+0.5\,(-1+0.9\cdot5)=1.5$$

The $+10$ reaches S1 through $Q(\text{S2},\text{R})=5$.

$$Q(\text{S2},\text{R})=0.5\cdot5+0.5\cdot10=7.5$$

Halfway from 5 to 10.

Episode 3

$$Q(\text{S0},\text{R})=0.5\,(-0.75)+0.5\,(-1+0.9\cdot1.5)=-0.2$$

Now S1's best entry is $1.5$.

$$Q(\text{S1},\text{R})=0.5\cdot1.5+0.5\,(-1+0.9\cdot7.5)=3.625$$

S1 is updated before S2 in this run, so its target uses $Q(\text{S2},\text{R})=7.5$ from episode 2, not this episode's $8.75$.

$$Q(\text{S2},\text{R})=0.5\cdot7.5+0.5\cdot10=8.75$$

The drop's target is always $10$, so each update halves the gap to it.

Answer $$\boxed{\begin{aligned}&\text{after 3 episodes: }Q(\cdot,\text{R})=(-0.2,\ 3.625,\ 8.75)\\&\text{heading for }q_*(\cdot,\text{R})=(6.2,\ 8,\ 10)\end{aligned}}$$
Check

The drop's entry follows $Q\leftarrow\frac12(Q+10)$, so its gap to $10$ halves each episode: $10,\ 5,\ 2.5,\ 1.25$, and $10-1.25=8.75$.

Nine updates; only the three right entries ever changed.

The final reward travels backwards one cell per episode; early cells stay wrong until the cells after them have learned.

Q-learning on the machine from four observed days

A plant does not know the machine's probabilities. It starts with $Q=0$, uses $\alpha=0.5$ and $\gamma=0.8$, and records four days as $(S_t,A_t,R_{t+1},S_{t+1})$: (good, run, $7$, good), (good, run, $5$, worn), (worn, repair, $-1$, good), (good, run, $7$, good).

Find$Q(\text{good},\text{run})$ and $Q(\text{worn},\text{repair})$ after the four days.
Given
  • the four transitions in the order observed

  • $Q=0$ for all three pairs at the start

  • $\alpha=0.5$, $\gamma=0.8$

Solution

Each day is one update of that day's pair; no probability is needed because every landing state is observed.

Day 1

$$\hat Q=7+0.8\cdot0=7,\qquad Q(\text{g},\text{run})=0.5\cdot0+0.5\cdot7=3.5$$

Before the update the best entry at good is 0.

Day 2

$$\hat Q=5+0.8\cdot\max(0,\ 0)=5,\qquad Q(\text{g},\text{run})=0.5\cdot3.5+0.5\cdot5=4.25$$

It lands in worn, where both entries are still 0.

Day 3

$$\hat Q=-1+0.8\cdot4.25=2.4,\qquad Q(\text{w},\text{rep})=0.5\cdot0+0.5\cdot2.4=1.2$$

The repair lands in good, whose only entry is now $4.25$.

Day 4

$$\hat Q=7+0.8\cdot4.25=10.4$$

The target reads the entry as it was before this day's update.

$$Q(\text{g},\text{run})=0.5\cdot4.25+0.5\cdot10.4=7.325$$

Halfway from the old entry to the target.

Answer $$\boxed{Q(\text{g},\text{run})=7.325,\qquad Q(\text{w},\text{rep})=1.2,\qquad Q(\text{w},\text{run})=0}$$
Check

Each new entry must lie between the old entry and its target: $3.5\in[0,7]$, $4.25\in[3.5,5]$, $1.2\in[0,2.4]$, $7.325\in[4.25,10.4]$. All four do.

The order of the days matters: swap days 3 and 4 and $Q(\text{w},\text{rep})$ comes out $2.43$ instead of $1.2$.

A shrinking learning rate turns Q-learning into a running average

One pair always ends the episode, so its target is just the reward. Its first four rewards are $4$, $0$, $8$, $4$. Track $Q$ from $0$ with $\alpha=1/n$ at the $n$th update, and with a constant $\alpha=0.5$.

Find$Q$ after each update under both learning rates, and what each one estimates.
Given
  • targets $\hat Q=4,\ 0,\ 8,\ 4$ (the next state is terminal)

  • $Q=0$ at the start

Solution

With a terminal next state there is no max term, so the update is a pure averaging rule and the two learning rates can be compared cleanly.

Learning rate 1/n

$$Q_1=4,\quad Q_2=\tfrac12\cdot4+\tfrac12\cdot0=2$$

With $\alpha=1$ the first update copies the target.

$$Q_3=\tfrac23\cdot2+\tfrac13\cdot8=4,\quad Q_4=\tfrac34\cdot4+\tfrac14\cdot4=4$$

Each step keeps the running mean: $(4+0+8+4)/4=4$.

Constant learning rate 0.5

$$Q_1=2,\quad Q_2=1,\quad Q_3=4.5,\quad Q_4=4.25$$

Each update halves the distance to the latest target, so recent rewards weigh more.

What each one estimates

$$\alpha=\tfrac1n:\ Q_n=\bar X_n\to\mathbb E[\hat Q]$$

By the law of large numbers the running mean converges.

$$\alpha=0.5:\ \text{the latest reward keeps weight }0.5$$

The estimate keeps jumping with every new reward.

Answer $$\boxed{\alpha=\tfrac1n:\ 4,\ 2,\ 4,\ 4;\qquad\alpha=0.5:\ 2,\ 1,\ 4.5,\ 4.25}$$
Check

Unrolled, the constant-rate estimate is $0.5\cdot4+0.25\cdot8+0.125\cdot0+0.0625\cdot4=4.25$.

A fixed learning rate tracks the recent past instead of settling, which is why the standard convergence theorem asks it to shrink.

Why the Atari player cannot keep a table

A game screen is shrunk to $10\times10$ pixels, each black or white, and the player has $4$ actions. How many entries would a Q table need, and what does a deep Q network do instead?

FindThe number of table entries, and the idea that replaces the table.
Given
  • $100$ pixels with $2$ values each

  • $4$ actions

Solution

Count the states first; the size alone shows why the Atari demo on the slides uses a network.

Count the entries

$$\lvert\mathcal S\rvert=2^{100}\approx1.27\times10^{30},\qquad4\cdot2^{100}\approx5.07\times10^{30}$$

Every pixel doubles the number of possible screens.

Replace the table

$$Q(s,a)\approx Q(s,a;w)$$

A neural network with weights w: similar screens get similar values, so learning on one screen carries over to screens never seen.

$$w\leftarrow w+\alpha\,\big(\hat Q-Q(S_t,A_t;w)\big)\,\nabla_wQ(S_t,A_t;w)$$

The same target as before; a gradient step on the weights moves the output toward it.

Answer $$\boxed{\approx5\times10^{30}\ \text{entries; a network}\ Q(s,a;w)\ \text{replaces the table}}$$
Check

Even at one entry per nanosecond, visiting each once takes $5.07\times10^{21}$ seconds, about $1.6\times10^{14}$ years.

A deep Q network keeps the Q-learning target and swaps the table lookup for a network trained by gradient steps, as in the neural networks section.

Checkpoint
§14.6 — one update by hand

A Q-learning agent observes a transition from $(s,a)$ with reward $2$ into a state $s'$.

Find(a) What is $Q(s,a)$ after the update?
Given
  • $Q(s,a)=4$ before the update

  • $R_{t+1}=2$, $\max_{a'}Q(s',a')=10$

  • $\alpha=0.1$, $\gamma=0.9$

Hint 1/4

Form the target first, then blend it into the old entry.

Hint 2/4

$\hat Q=R_{t+1}+\gamma\max_{a'}Q(s',a')$ and $Q\leftarrow(1-\alpha)Q+\alpha\hat Q$.

Hint 3/4

Here $R_{t+1}=2$, $\gamma=0.9$, the max is $10$, the old entry is $4$ and $\alpha=0.1$.

Hint 4/4

$\hat Q=11$ and the new entry is $0.9\cdot4+0.1\cdot11=4.7$.

Show solution

Two lines of the box, in order: target, then blend.

Target

$$\hat Q=2+0.9\cdot10=11$$

Reward plus the discounted best entry at the next state.

Blend

$$Q\leftarrow0.9\cdot4+0.1\cdot11=4.7$$

Keep $1-\alpha$ of the old entry, add $\alpha$ of the target.

Answer $$\boxed{Q(s,a)=4.7}$$
Check

Gap form: $4+0.1\cdot(11-4)=4.7$, and $4.7$ lies between the old entry $4$ and the target $11$.

With a small $\alpha$ one surprising target moves the entry only a little.

⚠ Swapping the two weights

Both weights add to one, so the formula looks symmetric.

wrong$$Q\leftarrow\alpha\,Q+(1-\alpha)\,\hat Q$$
right$$Q\leftarrow(1-\alpha)\,Q+\alpha\,\hat Q$$
⚠ Using the next action actually taken instead of the max

The agent's next move is the most visible thing at $S_{t+1}$.

wrong$$\hat Q=R_{t+1}+\gamma\,Q(S_{t+1},A_{t+1})$$
right$$\hat Q=R_{t+1}+\gamma\max_{a'}Q(S_{t+1},a')$$
Q table after episode 40 (α = 0.5, γ = 0.9)stateQ(s, left)Q(s, right)q*(s, right)S006.26.2S1088S201010every episode moves right three times; left entries are never tried

Step through Q-learning on the corridor with $\alpha=0.5$, $\gamma=0.9$, every episode moving right from S0 to the drop. The $\textcolor{#1f6feb}{Q(s,\text{right})}$ entries start at $0$; the $+10$ reaches one more cell per episode and the column settles on $q_*=(6.2,\ 8,\ 10)$.

At the edges
episode 3 S0 still negative

$Q(\text{S0},\text{right})=-0.2$ is below the untried $Q(\text{S0},\text{left})=0$, so a purely greedy agent would now walk to the pad.

episode 40 settled

The corridor's moves and rewards are deterministic, so even a constant $\alpha$ lets the entries settle; by episode $40$ they agree with $q_*$ to three decimals.

14.7Choosing actions while learning: greedy, ε-greedy and Boltzmann

Turns the current entries $Q(S_t,\cdot)$ into an action: exploit the best entry, or explore so that every pair keeps being tried.

Q-learning converges only if every pair keeps being tried, yet the agent also wants reward now; the rule that picks the next action has to balance the two.

DefinitionDefinition 14.4: Greedy, ε-greedy and Boltzmann action selection
Conditions
  • $\lvert\mathcal A\rvert$ actions in the current state; $a^\circ=\arg\max_aQ(S_t,a)$ is the greedy action

  • ε-greedy: a coin $C_t$ shows heads with probability $\epsilon$; heads draws uniformly from all actions, $a^\circ$ included

  • Boltzmann as on the slides, with no temperature; dividing $Q$ by a temperature $\tau$ is a common extension (further reading)

$$\boxed{\begin{aligned}&\text{greedy: }\textcolor{#8250df}{A_t}=\arg\max_aQ(S_t,a)\\&\epsilon\text{-greedy: }\Pr(A_t=a\mid S_t)=\begin{cases}1-\epsilon+\epsilon/\lvert\mathcal A\rvert,&a=a^\circ\\\epsilon/\lvert\mathcal A\rvert,&a\ne a^\circ\end{cases}\\&\text{Boltzmann: }\Pr(A_t=a\mid S_t)=\frac{e^{Q(S_t,a)}}{\sum_{a'}e^{Q(S_t,a')}}\end{aligned}}$$

Greedy always takes the current best entry and never checks the others. ε-greedy does the same most of the time, but with probability $\epsilon$ it draws an action at random, so every action gets at least $\epsilon/\lvert\mathcal A\rvert$. Boltzmann gives each action a share that grows exponentially with its entry: close competitors are tried often, clearly worse actions rarely.

Looks like this, but is not

With $\epsilon=0.1$ and $4$ actions, ε-greedy looks as if it takes the greedy action with probability $0.9$.

The random draw can land on the greedy action too, so its probability is $0.9+0.1/4=0.925$, and each of the other three gets $0.025$.

Action probabilities under ε-greedy and Boltzmann for the same entries

In state $s$ the table holds $Q(s,a_1)=2$, $Q(s,a_2)=1$ and $Q(s,a_3)=0$. Find the action probabilities under ε-greedy with $\epsilon=0.3$ and under Boltzmann exploration.

Find$\Pr(A_t=a_i\mid S_t=s)$ for $i=1,2,3$ under both rules.
Given
  • $Q(s,\cdot)=(2,\ 1,\ 0)$

  • $\epsilon=0.3$, three actions

  • $e\approx2.718$, $e^2\approx7.389$

Solution

Both rules are explicit formulas of the entries; the only trap is the greedy action's share under ε-greedy.

ε-greedy

$$\Pr(a_1)=0.7+\tfrac{0.3}{3}=0.8,\qquad\Pr(a_2)=\Pr(a_3)=\tfrac{0.3}{3}=0.1$$

Tails, probability $0.7$, picks $a_1$; heads spreads $0.3$ evenly over all three, $a_1$ included.

Boltzmann

$$\textstyle\sum_{a'}e^{Q(s,a')}=7.389+2.718+1=11.107$$

The normalizing sum, as in a softmax.

$$\Pr(a_1)=\tfrac{7.389}{11.107}=0.665,\qquad\Pr(a_2)=\tfrac{2.718}{11.107}=0.245$$

Dividing by the common sum makes the shares add to $1$ while keeping the order of the $Q$ values.

$$\Pr(a_3)=\tfrac{1}{11.107}=0.090$$

The worst action keeps a small share, since $e^0=1>0$.

Answer $$\boxed{\epsilon\text{-greedy: }(0.8,\ 0.1,\ 0.1);\qquad\text{Boltzmann: }(0.665,\ 0.245,\ 0.090)}$$
Check

Both rows add to $1$. Boltzmann's ratio $\Pr(a_1)/\Pr(a_2)$ must be $e^{2-1}=2.718$, and $0.665/0.245=2.71$.

ε-greedy treats all non-greedy actions alike; Boltzmann ranks them by their entries.

Pure greedy gets stuck at the charging pad

In the corridor, $Q=0$ at the start, $\alpha=0.5$ and $\gamma=0.9$, and ties are broken toward left. The agent acts greedily in S0. Follow the first two episodes and say what happens next.

FindThe action chosen in S0 and the entries $Q(\text{S0},\cdot)$ over the episodes.
Given
  • S0: left onto the pad ($+1$, the run ends); right to S1 ($-1$)

  • $Q=0$ for every pair, $\alpha=0.5$, $\gamma=0.9$

  • ties go to left

Solution

Trace the rule literally: the greedy choice depends only on the two entries at S0, and only the chosen entry is ever updated.

Episode 1

$$Q(\text{S0},\text{L})=Q(\text{S0},\text{R})=0\ \Rightarrow\ \text{left}$$

The tie rule sends it left.

$$Q(\text{S0},\text{L})=0.5\cdot0+0.5\cdot1=0.5$$

The pad is terminal, so the target is $+1$.

Episode 2 and after

$$0.5>0=Q(\text{S0},\text{R})\ \Rightarrow\ \text{left},\qquad Q(\text{S0},\text{L})=0.75$$

Left now wins without a tie.

$$Q(\text{S0},\text{L})\to1,\qquad Q(\text{S0},\text{R})=0\ \text{forever}$$

Right is never tried, so its entry never moves; left's entry creeps toward 1 and stays ahead.

Answer $$\boxed{\text{greedy: left forever, worth }1;\qquad\text{optimal: right, worth }6.2}$$
Check

The slides' guarantee needs every pair tried infinitely often; greedy tries (S0, right) zero times, so the guarantee does not apply, and the result is indeed wrong.

An untried action whose entry looks worse can stay untried forever; some exploration is required, not optional.

How often ε-greedy explores, and how the scale of Q changes Boltzmann

(a) In S0 with two actions, left is greedy and $\epsilon=0.2$. On average, how many visits until ε-greedy first tries right? (b) Under Boltzmann exploration, what probability does the second action get when the entries are $(2,\ 1)$, and when they are $(20,\ 10)$?

Find(a) The expected number of visits. (b) The second action's probability in both cases.
Given
  • (a) $\epsilon=0.2$, two actions, left greedy

  • (b) entries $(2,\ 1)$ and $(20,\ 10)$

Solution

(a) Each visit is an independent trial with the same chance, so the wait is geometric. (b) Boltzmann depends on the difference of the entries, so compare the two gaps.

(a) Chance per visit

$$\Pr(\text{right})=\epsilon/2=0.1$$

Only heads can choose right, and then with probability $1/2$.

$$\mathbb E[\text{visits}]=1/0.1=10$$

Mean of a geometric waiting time with success probability $0.1$.

(b) Two scales

$$\Pr(a_2)=\frac{e^{1}}{e^{2}+e^{1}}=\frac{1}{1+e}=0.269$$

Divide top and bottom by $e^1$.

$$\Pr(a_2)=\frac{1}{1+e^{10}}=4.54\times10^{-5}$$

The same entries times ten: the gap grows from $1$ to $10$.

Answer $$\boxed{(a)\ 10\ \text{visits};\qquad(b)\ 0.269\ \text{against}\ 4.54\times10^{-5}}$$
Check

(a) In $1000$ visits ε-greedy tries right about $1000\cdot0.1=100$ times. (b) The odds are $e^{\text{gap}}$: $e^1=2.72$ and $e^{10}=22026$, matching $0.731/0.269=2.72$ and $1/(4.54\times10^{-5})\approx22\,000$.

Rewards measured in larger units make Boltzmann exploration nearly greedy; that is why a temperature is often added (further reading).

Checkpoint
§14.7 — the share of a non-greedy action

An agent uses ε-greedy with $\epsilon=0.2$ in a state with four actions, and $a_1$ is the greedy action.

Find(a) What is $\Pr(A_t=a_3\mid S_t)$?
Given
  • $\epsilon=0.2$

  • $\lvert\mathcal A\rvert=4$; greedy action $a_1$

Hint 1/4

Decide in which branch of the coin $a_3$ can be chosen at all.

Hint 2/4

Heads, with probability $\epsilon$, draws uniformly from all $\lvert\mathcal A\rvert$ actions; tails takes $a_1$.

Hint 3/4

Here $\epsilon=0.2$ and $\lvert\mathcal A\rvert=4$, so $\Pr(a_3)=0.2\cdot\frac14$.

Hint 4/4

$\Pr(A_t=a_3\mid S_t)=0.05$.

Show solution

A non-greedy action can only come from the uniform draw, so multiply the two probabilities.

Heads, then that action

$$\Pr(a_3)=\epsilon\cdot\tfrac1{\lvert\mathcal A\rvert}=0.2\cdot0.25=0.05$$

The coin and the uniform draw are independent.

Answer $$\boxed{0.05}$$
Check

The four probabilities are $0.85,\ \allowbreak 0.05,\ \allowbreak 0.05,\ \allowbreak 0.05$, which add to $1$.

Non-greedy actions each get $\epsilon/\lvert\mathcal A\rvert$; the greedy one gets the rest.

⚠ Leaving the greedy action out of the random draw

Exploring sounds like trying something else, but the coin's draw on the slides is uniform over the whole action set.

wrong$$\Pr(a\ne a^\circ)=\frac{\epsilon}{\lvert\mathcal A\rvert-1}$$
right$$\Pr(a\ne a^\circ)=\frac{\epsilon}{\lvert\mathcal A\rvert}$$
⚠ Reading Boltzmann as a ratio of entries

Normalizing looks like dividing by a sum of entries, but it divides a sum of exponentials.

wrong$$\frac{\Pr(a_1)}{\Pr(a_2)}=\frac{Q(s,a_1)}{Q(s,a_2)}$$
right$$\frac{\Pr(a_1)}{\Pr(a_2)}=e^{\,Q(s,a_1)-Q(s,a_2)}$$
Value iteration by hand

The problem gives $p(s'|s,a)$ and the rewards and asks for values after some sweeps, for the optimal values or for the optimal policy.

  1. Tabulate the model

    For each pair $(s,a)$ list the landing states with $p$ and $r$, and $r(s,a)$.

  2. Freeze the old table

    Copy $v_k$; nothing computed during this sweep is used before the sweep ends.

  3. Score every action

    $r(s,a)+\gamma\sum_{s'}p(s'|s,a)\,v_k(s')$; terminal states have value $0$.

  4. Max and argmax

    $v_{k+1}(s)$ is the largest score; record the action that attains it.

  5. Stopping check

    Stop when $\max_s\lvert v_{k+1}(s)-v_k(s)\rvert\le\epsilon$; the policy is the last argmax.

Where it goes wrong
  • Reading a value computed earlier in the same sweep.

  • Multiplying the immediate reward by $\gamma$.

  • Giving a terminal state a value other than $0$.

One Q-learning update by hand

The problem gives an observed transition $(S_t,A_t,R_{t+1},S_{t+1})$, a table, $\alpha$ and $\gamma$.

  1. Best entry at the next state

    $\max_{a'}Q(S_{t+1},a')$ from the table as it is now; $0$ if $S_{t+1}$ is terminal.

  2. Target

    $\hat Q=R_{t+1}+\gamma\max_{a'}Q(S_{t+1},a')$.

  3. Blend

    $Q(S_t,A_t)\leftarrow(1-\alpha)Q(S_t,A_t)+\alpha\hat Q$; no other entry changes.

  4. Sanity check

    The new entry lies between the old entry and the target.

Where it goes wrong
  • Using $Q(S_{t+1},A_{t+1})$ instead of the max.

  • Swapping $\alpha$ and $1-\alpha$.

  • Updating an entry of the next state instead of the current one.

A: value iteration updates q(good, run) with the model

For the machine ($\gamma=0.8$), value iteration in q form holds $q_1=(6;\ 2,\ -1)$ for (good, run), (worn, run), (worn, repair). Compute $q_2(\text{good},\text{run})$.

Find$q_2(\text{good},\text{run})$.
Given
  • good, run: good ($0.5$, $7$) or worn ($0.5$, $5$)

  • $q_1=(6;\ 2,\ -1)$, $\gamma=0.8$

Solution

The model is known, so average over both landing states and overwrite the entry.

Average over the model

$$q_2(\text{g},\text{run})=0.5\,(7+0.8\cdot6)+0.5\,(5+0.8\cdot2)=9.2$$

Each landing state with its reward and its best old entry.

Answer $$\boxed{q_2(\text{good},\text{run})=9.2}$$
Check

The expected-reward form gives the same: $6+0.8\,(0.5\cdot6+0.5\cdot2)=9.2$.

No learning rate: the new entry replaces the old one.

B: Q-learning updates Q(good, run) from one observed day

A plant without the model of the machine holds $Q(\text{good},\text{run})=6$, $Q(\text{worn},\text{run})=2$ and $Q(\text{worn},\text{repair})=-1$, and observes (good, run, $5$, worn). With $\alpha=0.5$ and $\gamma=0.8$, update $Q(\text{good},\text{run})$.

Find$Q(\text{good},\text{run})$ after the update.
Given
  • observed: (good, run, $5$, worn)

  • $Q(\text{good},\text{run})=6$, $Q(\text{worn},\text{run})=2$, $Q(\text{worn},\text{repair})=-1$

  • $\alpha=0.5$, $\gamma=0.8$

Solution

Only one landing state was seen, so use it alone and move a fraction of the way.

One sample, then blend

$$\hat Q=5+0.8\cdot\max(2,\ -1)=6.6$$

The observed reward and the observed landing state.

$$Q(\text{g},\text{run})=0.5\cdot6+0.5\cdot6.6=6.3$$

Half of the old entry plus half of the target.

Answer $$\boxed{Q(\text{good},\text{run})=6.3}$$
Check

$6.3$ lies between the old $6$ and the target $6.6$.

A learning rate blends; one sample replaces the average over landing states.

Both targets add a reward to $\gamma$ times the best entry of a landing state. Value iteration averages over both landing states with the known $p$ and overwrites the entry, $9.2$; Q-learning uses the one landing state it saw and moves only a fraction $\alpha$ toward it, $6.3$.

How to tell them apart

If the problem gives $p(s'|s,a)$, sum over $s'$ and overwrite. If it gives an observed transition $(s,a,r,s')$ and a learning rate, update that one entry with $\alpha$.

A: the value of 'keep running when worn' at γ = 0.8

For the machine with $\gamma=0.8$, evaluate the policy that runs the machine in both states.

Find$v_{\pi_N}(\text{good})$ and $v_{\pi_N}(\text{worn})$.
Given
  • good, run: expected reward $6$; good or worn with $0.5$ each

  • worn, run: $+2$, stays worn

  • worn, repair: $-1$, lands in good (not used by this policy)

  • $\gamma=0.8$

Solution

A given policy: plug in its actions and solve linear equations, with no max.

Linear equations

$$v(\text{w})=2+0.8\,v(\text{w})\Rightarrow v(\text{w})=10$$

Running forever pays 2 per day.

$$v(\text{g})=6+0.8\,(0.5\,v(\text{g})+0.5\cdot10)\Rightarrow0.6\,v(\text{g})=10$$

Worn's equation had only $v(\text{w})$ as unknown, so it was solved first and now feeds into good's.

$$v(\text{g})=16.667$$

One linear equation with coefficient $0.6\ne0$, so the policy's value in good is unique.

Answer $$\boxed{v_{\pi_N}=(16.667,\ 10)}$$
Check

Plug back: $6+0.8\,(0.5\cdot16.667+0.5\cdot10)=6+10.667=16.667$.

A policy's value answers 'how good is this plan', not 'what is the best plan'.

B: the optimal values of the machine at γ = 0.8

For the machine with $\gamma=0.8$, check that $v_*=(20,\ 15)$ satisfies the optimality equations.

FindWhether the candidate is optimal.
Given
  • good, run: to good ($0.5$, reward $7$) or worn ($0.5$, reward $5$)

  • worn, run: stays worn, reward $2$

  • worn, repair: to good, reward $-1$

  • candidate $v_*=(20,\ 15)$

  • $\gamma=0.8$

Solution

Optimal values solve the same kind of equations with a max over actions, so test the max.

Max over actions

$$v(\text{w})=\max(2+0.8\cdot15,\ -1+0.8\cdot20)=\max(14,\ 15)=15\ \checkmark$$

Repair attains the max.

$$v(\text{g})=6+0.8\,(0.5\cdot20+0.5\cdot15)=20\ \checkmark$$

One action in good.

Answer $$\boxed{v_*=(20,\ 15)}$$
Check

$v_*\ge v_{\pi_N}$ in both states: $20\ge16.667$ and $15\ge10$, as optimality requires.

The optimal values are the largest values any policy can reach, state by state.

A given policy's values solve linear equations with its own actions plugged in; the optimal values solve the same equations with a max over actions, and they are at least as large in every state: $16.667\le20$ and $10\le15$.

How to tell them apart

'Value of this policy' means plug in the policy's actions, with no max. 'Optimal value' or 'best policy' means a max over actions, or value iteration.

Scaffolding comes off
The common skeleton
  1. List, for each state and each allowed action, the landing states with $p(s'|s,a)$ and the reward.

  2. Score each action with the old values: $r(s,a)+\gamma\sum_{s'}p(s'|s,a)\,v_k(s')$.

  3. Take the max over actions to get $v_{k+1}(s)$; the action that attains it is the greedy action.

  4. Compare $\max_s\lvert v_{k+1}(s)-v_k(s)\rvert$ with $\epsilon$ to decide whether to sweep again.

1 · fully worked

One sweep of the study plan from v1 = (3, 1)

A student is fresh or tired each evening. Fresh: study pays $+3$ and leaves the student fresh or tired with probability $0.5$ each; rest pays $0$ and stays fresh. Tired: study pays $+1$ and stays tired; rest pays $0$ and returns to fresh. With $\gamma=0.9$ and $v_1=(3,\ 1)$ for (fresh, tired), do one sweep with $\epsilon=0.5$.

Find$v_2$, the greedy actions and whether to stop.
Given
  • fresh: study ($+3$; fresh $0.5$, tired $0.5$), rest ($0$; fresh)

  • tired: study ($+1$; tired), rest ($0$; fresh)

  • $\gamma=0.9$, $v_1(\text{fresh})=3$, $v_1(\text{tired})=1$, $\epsilon=0.5$

Solution

Follow the skeleton: score every action with the old table, take the max, then check the change.

Score the actions

$$\text{fresh: study }3+0.9\,(0.5\cdot3+0.5\cdot1)=4.8,\ \ \text{rest }0+0.9\cdot3=2.7$$

Old values only; study averages over its two landing states.

$$\text{tired: study }1+0.9\cdot1=1.9,\ \ \text{rest }0+0.9\cdot3=2.7$$

Rest pays nothing now but lands in the more valuable state.

Max and argmax

$$v_2(\text{fresh})=4.8\ (\text{study}),\qquad v_2(\text{tired})=2.7\ (\text{rest})$$

The larger score in each state and the action attaining it.

Stopping check

$$\max(\lvert4.8-3\rvert,\ \lvert2.7-1\rvert)=1.8>0.5$$

The largest change exceeds $\epsilon=0.5$, so the values have not settled: sweep again.

Answer $$\boxed{v_2=(4.8,\ 2.7);\ \ \text{fresh: study, tired: rest};\ \ \text{continue}}$$
Check

Monotonicity: $v_1=(3,\ 1)$ is itself one sweep from $v_0=0$ (the best immediate rewards), and with non-negative rewards every later sweep can only raise the values; $4.8\ge3$ and $2.7\ge1$ hold.

The skeleton is the same in every value iteration question; only the table of landing states changes.

2 · you write the reasoning

Easier, and this time you write the reasons. States A and B with deterministic moves and $\gamma=0.5$; the current values are $v_k(A)=2$ and $v_k(B)=6$. In A, stay pays $+1$ and stays in A, go pays $0$ and moves to B. In B, back pays $+4$ and moves to A. Do one sweep with $\epsilon=0.5$.

  1. $\text{A: stay }1+0.5\cdot2=2,\qquad\text{go }0+0.5\cdot6=3$

    reasoning

    Each action is scored with the old values $v_k$: reward plus $0.5$ times the value of the cell it moves to.

  2. $v_{k+1}(A)=\max(2,\ 3)=3\quad(\text{go})$

    reasoning

    The new value is the larger score; go attains it, so go is the greedy action in A.

  3. $v_{k+1}(B)=4+0.5\cdot2=5$

    reasoning

    B has one action, so there is no max; it uses the old $v_k(A)=2$, not the new $3$.

  4. $\max(\lvert3-2\rvert,\ \lvert5-6\rvert)=1>0.5$

    reasoning

    The largest change is $1$, above $\epsilon$, so another sweep is needed.

3 · find the buried error

Harder, with two errors buried in the solution. A student does one sweep with $\gamma=0.8$ from $v_k=(4,\ 2,\ 6)$ for (X, Y, Z). Which two steps are wrong?

  • X: action a pays $+2$ and lands in X or Y with probability $0.5$ each; action b pays $0$ and lands in Z.
  • Y: action a pays $-1$ and lands in X; action b pays $+1$ and lands in Y with probability $0.8$ or Z with $0.2$.
  • Z: its only action c pays $+5$ and lands in X.
  1. Step 1. X, a: $2+0.8\,(0.5\cdot4+0.5\cdot2)=4.4$.

  2. Step 2. X, b: $0+0.8\cdot6=4.8$, so $v_{k+1}(X)=4.8$ with b greedy.

  3. Step 3. Y, a: $-1+0.8\cdot4.8=2.84$.

  4. Step 4. Y, b: $1+0.8\,(0.8\cdot2+0.2\cdot6)=3.24$, so $v_{k+1}(Y)=3.24$ with b greedy.

  5. Step 5. Z, c: $0.8\cdot5+0.8\cdot4=7.2$, so $v_{k+1}(Z)=7.2$.

  6. Step 6. The largest change is $\max(0.8,\ 1.24,\ 1.2)=1.24$.

the two buried errors (2)
⚠ step 3

It uses $v(X)=4.8$, computed in step 2 of this same sweep; a synchronous sweep must use the old $v_k(X)=4$.

Filling the table from top to bottom, the fresh number sits right above the line being written.

right

$-1+0.8\cdot4=2.2$. The value $v_{k+1}(Y)=3.24$ survives only because b wins either way.

⚠ step 5

It multiplies the immediate reward by $\gamma$: $0.8\cdot5$ instead of $5$.

Discounting every reward, the first one included, is the most common slip with returns.

right

$5+0.8\cdot4=8.2$, and the largest change becomes $\lvert8.2-6\rvert=2.2$.

4 · the bare problem
§14.5 — one sweep for a server

A server is busy or idle. Busy: serve pays $+2$ and stays busy with probability $0.6$ or becomes idle with $0.4$; shed pays $-1$ and becomes idle. Idle: serve pays $+1$ and becomes busy with probability $0.3$ or stays idle with $0.7$; sleep pays $0$ and stays idle. With $\gamma=0.9$ the current values are $v_k(\text{busy})=5$, $v_k(\text{idle})=10$.

Find
  1. (a) Find $v_{k+1}$ and the greedy action in each state.

  2. (b) Should the iteration stop with $\epsilon=0.5$?

Given
  • busy: serve ($+2$; busy $0.6$, idle $0.4$), shed ($-1$; idle)

  • idle: serve ($+1$; busy $0.3$, idle $0.7$), sleep ($0$; idle)

  • $\gamma=0.9$, $v_k=(5,\ 10)$ for (busy, idle), $\epsilon=0.5$

Hint 1/4

Follow the four-step skeleton: tabulate, score, max, check.

Hint 2/4

Score: $r(s,a)+\gamma\sum_{s'}p(s'|s,a)\,v_k(s')$; then $v_{k+1}(s)=\max_a$ of the scores.

Hint 3/4

Busy: serve $2+0.9\,(0.6\cdot5+0.4\cdot10)$, shed $-1+0.9\cdot10$. Idle: serve $1+0.9\,(0.3\cdot5+0.7\cdot10)$, sleep $0.9\cdot10$.

Hint 4/4

$v_{k+1}=(8.3,\ 9)$ with serve in busy and sleep in idle; the change $3.3>0.5$, so continue.

Show solution

The skeleton applies unchanged; each state has two actions, so four scores in total.

Busy

$$\text{serve: }2+0.9\,(0.6\cdot5+0.4\cdot10)=2+0.9\cdot7=8.3$$

Average of the old values over busy and idle.

$$\text{shed: }-1+0.9\cdot10=8\ \Rightarrow\ v_{k+1}(\text{busy})=8.3$$

$8.3>8$: serving beats shedding, by a little.

Idle

$$\text{serve: }1+0.9\,(0.3\cdot5+0.7\cdot10)=1+0.9\cdot8.5=8.65$$

Average over busy and idle.

$$\text{sleep: }0+0.9\cdot10=9\ \Rightarrow\ v_{k+1}(\text{idle})=9$$

$9>8.65$: sleeping beats serving when idle.

Stopping check

$$\max(\lvert8.3-5\rvert,\ \lvert9-10\rvert)=3.3>0.5$$

The largest change, $3.3$, exceeds $\epsilon=0.5$: keep sweeping.

Answer $$\boxed{v_{k+1}=(8.3,\ 9);\ \ \text{busy: serve, idle: sleep};\ \ \text{continue}}$$
Check

Every score lies between its reward plus $0.9\cdot5$ and its reward plus $0.9\cdot10$, the smallest and largest old values; all four do.

Nothing in the skeleton depends on the story; tabulate, score with old values, max, check.

Full exam-style question

An ad server: optimality equations, two sweeps, a Q-learning step and ε-greedyexam format

A website shows either an ad or an article to a visitor who is interested (I) or annoyed (A). Use $\gamma=0.9$.

  • Ad to an interested visitor: earns $4$; the visitor is then interested or annoyed with probability $0.5$ each.
  • Article to an interested visitor: earns $0$; the visitor stays interested.
  • Ad to an annoyed visitor: earns $1$, and the visitor leaves (terminal, value $0$).
  • Article to an annoyed visitor: earns $0$; the visitor is then interested or annoyed with probability $0.5$ each.
Find(a) The optimality equations.
(b) Two sweeps of value iteration from $v_0=0$, with the greedy actions.
(c) A check that $v_*=(22,\ 18)$ solves (a), and $\pi^*$.
(d) The Q-learning update of $Q(I,\text{ad})=20$ after $(I,\text{ad},4,A)$, with $Q(A,\text{ad})=1$, $Q(A,\text{article})=16$, $\alpha=0.1$.
(e) Under ε-greedy with $\epsilon=0.1$ and that table, the chance of an ad to an annoyed visitor.
Given
  • I, ad: $+4$; I or A with probability $0.5$ each. I, article: $0$; stays I

  • A, ad: $+1$; the visitor leaves (value $0$). A, article: $0$; I or A with probability $0.5$ each

  • $\gamma=0.9$

Solution

Each part uses one box of the section: (a) and (c) the optimality equations, (b) value iteration, (d) the Q-learning update, (e) the ε-greedy rule. Doing them in order reuses the numbers.

(a) Optimality equations

$$v_*(I)=\max\{\,4+0.9\,(0.5\,v_*(I)+0.5\,v_*(A)),\ 0.9\,v_*(I)\,\}$$

Ad averages over the two landing states; article stays in I.

$$v_*(A)=\max\{\,1,\ 0.9\,(0.5\,v_*(I)+0.5\,v_*(A))\,\}$$

The ad ends the visit, so there is no future term.

(b) Two sweeps from zero

$$v_1(I)=\max(4,\ 0)=4\ (\text{ad}),\qquad v_1(A)=\max(1,\ 0)=1\ (\text{ad})$$

With $v_0=0$ only immediate rewards count.

$$v_2(I)=\max(4+0.9\cdot2.5,\ 0.9\cdot4)=\max(6.25,\ 3.6)=6.25\ (\text{ad})$$

$0.5\cdot4+0.5\cdot1=2.5$ is the average old value.

$$v_2(A)=\max(1,\ 0.9\cdot2.5)=\max(1,\ 2.25)=2.25\ (\text{article})$$

The article now sees that the visitor can become interested again.

(c) Check the claimed optimal values

$$I:\ \max(4+0.9\cdot20,\ 0.9\cdot22)=\max(22,\ 19.8)=22\ \checkmark$$

The average claimed value of the two landing states is $0.5\cdot22+0.5\cdot18=20$.

$$A:\ \max(1,\ 0.9\cdot20)=\max(1,\ 18)=18\ \checkmark$$

Both equations hold, so the claim is optimal; the article wins in A because $18>1$, as the ad wins in I because $22>19.8$.

(d) One Q-learning update

$$\hat Q=4+0.9\max(1,\ 16)=18.4$$

The next state A has best entry 16, the article.

$$Q(I,\text{ad})\leftarrow0.9\cdot20+0.1\cdot18.4=19.84$$

Keep nine tenths of the old entry.

(e) ε-greedy in A

$$\arg\max(1,\ 16)=\text{article}\ \Rightarrow\ \Pr(\text{ad}\mid A)=\epsilon/2=0.05$$

Only heads can pick the non-greedy ad, one time in two.

Answer $$\boxed{\begin{aligned}&v_1=(4,\ 1),\quad v_2=(6.25,\ 2.25)\\&v_*=(22,\ 18),\quad\pi^*=(\text{ad},\ \text{article})\\&Q(I,\text{ad})=19.84,\quad\Pr(\text{ad}\mid A)=0.05\end{aligned}}$$
Check

For (c), $v_*(I)-v_*(A)=4$, and sweep 2 in (b) shows the same gap: $6.25-2.25=4$. Once the greedy actions are ad in I and article in A, both values share the continuation $0.9\,(0.5\,v(I)+0.5\,v(A))$, and I adds the ad's $4$ on top.

The five parts chained four boxes of this section, and later parts reused earlier numbers; keep every intermediate result.

Practice

A · concept 4 questions
1§14.6 — what Q-learning needs to know

A team wants to train a warehouse robot with Q-learning but has no model of how often its wheels slip.

Find(a) True or false: Q-learning cannot be used unless the transition probabilities $p(s'|s,a)$ are known.
GivenQ-learning update: $Q(S_t,A_t)\leftarrow(1-\alpha)Q(S_t,A_t)+\alpha\,[R_{t+1}+\gamma\max_{a'}Q(S_{t+1},a')]$
Hint 1/4

List the quantities that the update actually reads.

Hint 2/4

The update reads $Q(S_t,A_t)$, $R_{t+1}$, $S_{t+1}$, $\alpha$ and $\gamma$.

Hint 3/4

$R_{t+1}$ and $S_{t+1}$ come from watching the robot; $Q$, $\alpha$ and $\gamma$ belong to the agent.

Hint 4/4

No $p(s'|s,a)$ appears anywhere: false.

Show solution

Reading the formula term by term answers the question without any computation.

Inputs of the update

$$R_{t+1},\ S_{t+1}\ \text{observed};\quad Q,\ \alpha,\ \gamma\ \text{chosen by the agent}$$

The environment's probabilities act by producing the observed next state, never as numbers in the formula.

Answer $$\boxed{\text{False}}$$
Check

Contrast: a value iteration sweep needs $\sum_{s'}p(s'|s,a)\,v_k(s')$ and cannot be written down without $p$.

means the samples do the averaging that the known probabilities do in value iteration.

2§14.7 — the greedy action's share under ε-greedy

An agent uses ε-greedy with $\epsilon=0.1$ in a state with five actions.

Find(a) True or false: the greedy action is chosen with probability $0.9$.
Given
  • $\epsilon=0.1$

  • $\lvert\mathcal A\rvert=5$

Hint 1/4

Count every way the greedy action can be chosen.

Hint 2/4

Tails, probability $1-\epsilon$, takes it; heads, probability $\epsilon$, draws uniformly from all $\lvert\mathcal A\rvert$ actions, it included.

Hint 3/4

Here $1-\epsilon=0.9$ and $\epsilon/\lvert\mathcal A\rvert=0.1/5=0.02$.

Hint 4/4

$0.9+0.02=0.92$, so false.

Show solution

Add the two branches of the coin that lead to the greedy action.

Two branches

$$\Pr(a^\circ)=(1-\epsilon)+\epsilon\cdot\tfrac15=0.9+0.02=0.92$$

Tails always picks it; heads picks it one time in five.

Answer $$\boxed{\text{False: }0.92}$$
Check

The other four actions get $0.02$ each: $0.92+4\cdot0.02=1$.

Under ε-greedy the greedy action gets $1-\epsilon+\epsilon/\lvert\mathcal A\rvert$, never just $1-\epsilon$.

3§14.4 — what the existence theorem promises

A finite MDP has bounded rewards and a discount rate $\gamma<1$.

Find(a) Which statement is guaranteed for every such MDP?
Given
  • finite state and action sets

  • infinite horizon, $\gamma<1$

Hint 1/4

Recall what the slides' theorem says exists.

Hint 2/4

Some deterministic stationary Markov policy is optimal.

Hint 3/4

Deterministic: one action per state. Stationary Markov: the same rule at every step, using only the current state.

Hint 4/4

The guaranteed statement is that such a simple policy is optimal.

Show solution

The theorem is short, so check each statement against it and against one example.

Against the theorem

$$\exists\ \pi^*\ \text{deterministic, stationary, Markov}$$

This is exactly the theorem's claim.

$$\text{corridor: }\pi^*=\text{right everywhere}$$

A deterministic stationary policy that does not maximize the immediate reward in S0.

Answer $$\boxed{\text{a deterministic stationary Markov policy is optimal}}$$
Check

The corridor refutes the immediate-reward statement: left pays $+1$ now, yet right is optimal with $6.2$.

Exploration is a need of learning, not of acting on a known model.

4§14.4 — shifting every reward by a constant

A classmate wants to add the same constant $c$ to every reward of an MDP whose episodes end at terminal states, to make all rewards positive.

Find(a) True or false: adding the same constant $c$ to every reward never changes the optimal policy.
Given
  • every reward $R$ becomes $R+c$

  • test case, the corridor with $\gamma=0.9$: left is $+1$ and ends; right is $-1$, $-1$, $+10$ and ends

Hint 1/4

Look for a case where two plans collect the constant a different number of times.

Hint 2/4

A plan with $n$ rewards gains $c\,(1+\gamma+\dots+\gamma^{n-1})$, which depends on $n$.

Hint 3/4

Try $c=-5$ in the corridor: left becomes $1-5=-4$; right becomes $-6,\ -6,\ 5$, so $-6-5.4+0.81\cdot5$.

Hint 4/4

Right drops to $-7.35<-4$, so the optimal first move flips to left: false.

Show solution

One counterexample settles a 'never' claim; the corridor has plans of different lengths, which is where a constant can matter.

Shifted returns

$$G^{\text{left}}=1-5=-4$$

One reward, one shift.

$$G^{\text{right}}=-6+0.9\cdot(-6)+0.81\cdot5=-7.35$$

Three rewards, three shifts.

$$-7.35<-4$$

The pad now wins.

Answer $$\boxed{\text{False}}$$
Check

The difference of the shifts: right collects $-5\,(1+0.9+0.81)=-13.55$, left only $-5$, and $6.2-13.55=-7.35$.

In a task that never ends every policy collects $c/(1-\gamma)$ extra and the policy is safe; with terminal states, longer plans collect more of the shift.

B · computation 7 questions
1§14.1 — returns of one episode

An episode produces the rewards $R_1=2$, $R_2=0$, $R_3=-1$ and $R_4=4$, and then ends. The discount rate is $\gamma=0.9$.

Find
  1. (a) Find $G_2$.

  2. (b) Find $G_0$.

Given
  • $R_1=2,\ \allowbreak R_2=0,\ \allowbreak R_3=-1,\ \allowbreak R_4=4$; the episode ends after $R_4$

  • $\gamma=0.9$

Hint 1/4

Work backwards from the end of the episode.

Hint 2/4

$G_t=R_{t+1}+\gamma\,G_{t+1}$, with $G_4=0$ after the episode ends.

Hint 3/4

With $R_4=4$, $R_3=-1$, $R_2=0$, $R_1=2$ and $\gamma=0.9$: $G_3=4$, then $G_2=-1+0.9\cdot4$.

Hint 4/4

$G_2=2.6$, $G_1=2.34$ and $G_0=4.106$.

Show solution

The recursion reuses each return for the next one, so four short steps replace four separate sums.

Backwards

$$G_3=R_4=4$$

The episode ends after it.

$$G_2=-1+0.9\cdot4=2.6$$

Recursion at $t=2$.

$$G_1=0+0.9\cdot2.6=2.34$$

A zero reward passes the tail on, discounted.

$$G_0=2+0.9\cdot2.34=4.106$$

Recursion at $t=0$.

Answer $$\boxed{G_2=2.6,\qquad G_0=4.106}$$
Check

Directly: $G_0=2+0-0.81+0.729\cdot4=2-0.81+2.916=4.106$.

Backwards, each return costs one multiply and one add.

2§14.1 — a reward every other step

Stream A pays $+3$, $0$, $+3$, $0$, and so on forever, starting now. Stream B is the same but starts with the $0$: $0$, $+3$, $0$, $+3$, and so on. The discount rate is $\gamma=0.8$.

Find
  1. (a) Find the return of stream A.

  2. (b) Find the return of stream B.

Given
  • A: $R_{t+1}=3,\ \allowbreak R_{t+2}=0,\ \allowbreak R_{t+3}=3,\ \allowbreak \dots$

  • B: $R_{t+1}=0,\ \allowbreak R_{t+2}=3,\ \allowbreak R_{t+3}=0,\ \allowbreak \dots$

  • $\gamma=0.8$

Hint 1/4

Group the rewards so that the stream becomes one geometric series.

Hint 2/4

The nonzero rewards of A have weights $1,\gamma^2,\gamma^4,\dots$, and $\sum_j(\gamma^2)^j=\frac{1}{1-\gamma^2}$.

Hint 3/4

With $\gamma^2=0.64$: $G^A=\frac{3}{1-0.64}$, and B is A delayed by one step, so $G^B=0.8\,G^A$.

Hint 4/4

$G^A=8.333$ and $G^B=6.667$.

Show solution

Every other weight is zero, so the ratio of the series is $\gamma^2$, not $\gamma$.

Stream A

$$G^A=3\sum_{j\ge0}0.64^j=\frac{3}{0.36}=8.333$$

Only every second reward is nonzero, so each nonzero term is $\gamma^2=0.64$ times the one before and the sum is geometric.

Stream B

$$G^B=0+0.8\,G^A=6.667$$

B's first reward is $0$ and the rest is A one step later: the recursion $G_t=R_{t+1}+\gamma G_{t+1}$.

Answer $$\boxed{G^A=8.333,\qquad G^B=6.667}$$
Check

The two together pay $3$ every step, worth $3/(1-0.8)=15$, and $8.333+6.667=15$.

A delay of one step multiplies a return by the discount rate.

3§14.2 — expected reward from a transition table

Taking action $a$ in state $s$ leads to $s_1$, $s_2$ or $s_3$. The table gives $p(s_1|s,a)=0.2$ and $p(s_2|s,a)=0.5$; the entry for $s_3$ is smudged. The rewards are $r(s,a,s_1)=10$, $r(s,a,s_2)=2$ and $r(s,a,s_3)=-4$.

Find
  1. (a) Find $p(s_3|s,a)$.

  2. (b) Find the expected reward $r(s,a)$.

Given
  • $p(s_1|s,a)=0.2$, $p(s_2|s,a)=0.5$, $p(s_3|s,a)$ unknown

  • $r(s,a,s_1)=10$, $r(s,a,s_2)=2$, $r(s,a,s_3)=-4$

Hint 1/4

Use the one fact every row of a transition table must satisfy, then average.

Hint 2/4

$\sum_{s'}p(s'|s,a)=1$ and $r(s, \allowbreak a)=\sum_{s'}p(s'|s, \allowbreak a)\,r(s, \allowbreak a, \allowbreak s')$.

Hint 3/4

$p(s_3|s,a)=1-0.2-0.5$; then $r(s,a)=0.2\cdot10+0.5\cdot2+p(s_3|s,a)\cdot(-4)$.

Hint 4/4

$p(s_3|s,a)=0.3$ and $r(s,a)=2+1-1.2=1.8$.

Show solution

The row sum fixes the missing entry, and the expectation needs the complete row.

Missing probability

$$p(s_3|s,a)=1-0.2-0.5=0.3$$

A row of the transition table adds up to one.

Expected reward

$$r(s,a)=0.2\cdot10+0.5\cdot2+0.3\cdot(-4)=1.8$$

Weight each transition's reward by its probability.

Answer $$\boxed{p(s_3|s,a)=0.3,\qquad r(s,a)=1.8}$$
Check

Out of $10$ tries: $2$ pay $10$, $5$ pay $2$ and $3$ pay $-4$; the total $20+10-12=18$ gives $1.8$ per try.

Complete the row before averaging; a missing probability is never zero by default.

4§14.3 — value of a policy that cycles

A deterministic policy moves between two states forever: from A to B, paying $4$, and from B back to A, paying $0$. The discount rate is $\gamma=0.5$.

Find(a) Find $v_\pi(A)$ and $v_\pi(B)$.
Given
  • A $\to$ B with reward $4$; B $\to$ A with reward $0$

  • $\gamma=0.5$

Hint 1/4

Write one equation per state: reward of the move plus the discounted value of where it lands.

Hint 2/4

For a deterministic move $s\to s'$: $v_\pi(s)=r+\gamma\,v_\pi(s')$.

Hint 3/4

$v(A)=4+0.5\,v(B)$ and $v(B)=0+0.5\,v(A)$.

Hint 4/4

$v(A)=4+0.25\,v(A)$, so $v(A)=5.333$ and $v(B)=2.667$.

Show solution

A fixed policy gives linear equations; substituting one into the other leaves one unknown.

Equations

$$v(A)=4+0.5\,v(B),\qquad v(B)=0.5\,v(A)$$

One move per state.

Solve

$$v(A)=4+0.25\,v(A)\Rightarrow v(A)=\tfrac{4}{0.75}=5.333$$

B's equation gives $v(B)$ directly in terms of $v(A)$, so substituting it leaves one unknown.

$$v(B)=0.5\cdot5.333=2.667$$

B's equation gives its value from A's.

Answer $$\boxed{v_\pi(A)=5.333,\qquad v_\pi(B)=2.667}$$
Check

As a series from A: $4+0\cdot0.5+4\cdot0.25+0+4\cdot0.0625+\dots=\frac{4}{1-0.25}=5.333$.

Deterministic cycles give geometric series with ratio equal to the discount rate to the power of the cycle length.

5§14.5 — one more sweep in q form

For the machine with $\gamma=0.8$, value iteration in q form has reached $q_2(\text{good},\text{run})=9.2$, $q_2(\text{worn},\text{run})=3.6$ and $q_2(\text{worn},\text{repair})=3.8$.

Find
  1. (a) Compute $q_3$ for the three pairs.

  2. (b) Give the greedy action in worn and $\max\lvert q_3-q_2\rvert$ over the three pairs.

Given
  • good, run: expected reward $6$; lands in good or worn with probability $0.5$ each

  • worn, run: reward $2$, stays worn; worn, repair: reward $-1$, lands in good

  • $q_2=(9.2;\ 3.6,\ 3.8)$ for (good, run), (worn, run), (worn, repair); $\gamma=0.8$

Hint 1/4

First find the best entry of each landing state in the old table.

Hint 2/4

$q_3(s, \allowbreak a)=r(s, \allowbreak a)+\gamma\sum_{s'}p(s'|s, \allowbreak a)\max_{a'}q_2(s', \allowbreak a')$.

Hint 3/4

$\max_{a'}q_2(\text{good},a')=9.2$ and $\max_{a'}q_2(\text{worn},a')=3.8$; so $q_3(\text{g},\text{run})=6+0.8\,(0.5\cdot9.2+0.5\cdot3.8)$.

Hint 4/4

$q_3=(11.2;\ 5.04,\ 6.36)$, repair is greedy in worn, and the largest change is $2.56$.

Show solution

The max over next actions is taken once per landing state, so compute the two maxima first and reuse them.

Best old entries

$$\max_{a'}q_2(\text{g},a')=9.2,\qquad\max_{a'}q_2(\text{w},a')=3.8$$

Good has one action; in worn repair is ahead.

New entries

$$q_3(\text{g},\text{run})=6+0.8\,(0.5\cdot9.2+0.5\cdot3.8)=11.2$$

Running a good machine lands in good or worn with probability $0.5$ each, so the model weights the two best old entries equally.

$$q_3(\text{w},\text{run})=2+0.8\cdot3.8=5.04$$

Running keeps it worn, whose best old entry is $3.8$.

$$q_3(\text{w},\text{rep})=-1+0.8\cdot9.2=6.36$$

Lands in good.

Policy and stopping check

$$\max(5.04,\ 6.36)\Rightarrow\text{repair};\qquad\max(2,\ 1.44,\ 2.56)=2.56$$

The changes are $11.2-9.2$, $5.04-3.6$ and $6.36-3.8$.

Answer $$\boxed{q_3=(11.2;\ 5.04,\ 6.36),\ \text{repair},\ \text{change }2.56}$$
Check

The v form gives $v_3=(11.2,\ 6.36)$ for the same sweep, and $\max_aq_3$ reproduces it.

In q form, compute each landing state's best entry once and reuse it in every pair that lands there.

6§14.6 — two updates in a row

A robot moves between rooms A and B with actions go and stay. Its table holds $Q(A,\text{go})=2$, $Q(A,\text{stay})=1$, $Q(B,\text{go})=5$ and $Q(B,\text{stay})=3$. It observes $(A,\text{go},0,B)$ and then $(B,\text{stay},1,B)$; $\alpha=0.2$, $\gamma=0.9$.

Find
  1. (a) Find $Q(A,\text{go})$ after the first update.

  2. (b) Find $Q(B,\text{stay})$ after the second update.

Given
  • $Q(A,\text{go})=2$, $Q(A,\text{stay})=1$, $Q(B,\text{go})=5$, $Q(B,\text{stay})=3$

  • transitions: $(A,\text{go},0,B)$, then $(B,\text{stay},1,B)$

  • $\alpha=0.2$, $\gamma=0.9$

Hint 1/4

For each transition, form the target from the table as it stands, then blend.

Hint 2/4

$\hat Q=R_{t+1}+\gamma\max_{a'}Q(S_{t+1},a')$ and $Q\leftarrow(1-\alpha)Q+\alpha\hat Q$.

Hint 3/4

First: $\max(Q(B,\text{go}),Q(B,\text{stay}))=\max(5,3)=5$. Second: the next state is B again, with the same max $5$.

Hint 4/4

$Q(A,\text{go})=0.8\cdot2+0.2\cdot4.5=2.5$ and $Q(B,\text{stay})=0.8\cdot3+0.2\cdot5.5=3.5$.

Show solution

Each transition touches one entry; the second update reads the table after the first.

First transition

$$\hat Q=0+0.9\cdot5=4.5,\qquad Q(A,\text{go})=0.8\cdot2+0.2\cdot4.5=2.5$$

The best entry at B is go's $5$.

Second transition

$$\hat Q=1+0.9\cdot5=5.5,\qquad Q(B,\text{stay})=0.8\cdot3+0.2\cdot5.5=3.5$$

The max is still $5$; the first update changed only an entry of A.

Answer $$\boxed{Q(A,\text{go})=2.5,\qquad Q(B,\text{stay})=3.5}$$
Check

Gap form: $2+0.2\,(4.5-2)=2.5$ and $3+0.2\,(5.5-3)=3.5$; both lie between the old entry and the target.

The target's max ignores which action the agent takes next.

7§14.7 — four actions under two rules

In a state with four actions the table holds $Q(s, \allowbreak \cdot)=(3,\ \allowbreak 1,\ \allowbreak 1,\ \allowbreak 0)$.

Find
  1. (a) Give the action probabilities under ε-greedy with $\epsilon=0.2$.

  2. (b) Give them under Boltzmann exploration.

Given
  • $Q(s,a_1)=3,\ \allowbreak Q(s,a_2)=1,\ \allowbreak Q(s,a_3)=1,\ \allowbreak Q(s,a_4)=0$

  • $\epsilon=0.2$

  • $e\approx2.718$, $e^3\approx20.086$

Hint 1/4

Handle the greedy action and the others separately in (a); normalize exponentials in (b).

Hint 2/4

ε-greedy: $1-\epsilon+\epsilon/4$ for $a_1$, $\epsilon/4$ for each other. Boltzmann: $e^{Q(s,a)}/\sum_{a'}e^{Q(s,a')}$.

Hint 3/4

$\epsilon/4=0.05$. The exponentials are $20.086$, $2.718$, $2.718$ and $1$, with sum $26.522$.

Hint 4/4

(a) $(0.85,\ 0.05,\ 0.05,\ 0.05)$. (b) $(0.757,\ 0.102,\ 0.102,\ 0.038)$.

Show solution

Each rule is one formula; the only work is the normalizing sum.

ε-greedy

$$\Pr(a_1)=0.8+0.05=0.85,\qquad\Pr(a_2)=\Pr(a_3)=\Pr(a_4)=0.05$$

Heads spreads $0.2$ over all four.

Boltzmann

$$Z=20.086+2.718+2.718+1=26.522$$

Boltzmann divides each $e^{Q}$ by this sum, so the four shares add to $1$.

$$\tfrac{20.086}{26.522}=0.757,\quad\tfrac{2.718}{26.522}=0.102,\quad\tfrac{1}{26.522}=0.038$$

Equal $Q$ values give equal exponentials, so $a_2$ and $a_3$ get the same share.

Answer $$\boxed{(0.85,\ 0.05,\ 0.05,\ 0.05);\qquad(0.757,\ 0.102,\ 0.102,\ 0.038)}$$
Check

Both rows add to $1$ up to rounding, and Boltzmann's $\Pr(a_2)/\Pr(a_4)$ must be $e^{1}=2.718$; unrounded, $0.10248/0.03770=2.718$.

Tied entries always get tied probabilities under Boltzmann; ε-greedy ties everything that is not greedy.

C · exam level 5 questions
1§14.4 — does repairing still pay at γ = 0.5?

The machine of this section is operated with a smaller discount rate, $\gamma=0.5$. Two policies both run a good machine; $\pi_R$ repairs a worn one and $\pi_N$ keeps running it.

Find
  1. (a) Compute $v_{\pi_R}$ in both states.

  2. (b) Compute $v_{\pi_N}$ in both states.

  3. (c) Use the optimality equation at worn to decide which policy is optimal.

Given
  • good, run: to good ($0.5$, reward $7$) or worn ($0.5$, reward $5$); expected reward $6$

  • worn, run: stays worn, reward $2$; worn, repair: to good, reward $-1$

  • $\gamma=0.5$

Hint 1/4

Evaluate each policy with its own linear equations, then test the winner with a one-step look-ahead.

Hint 2/4

Fixed policy: $v(s)=r(s,\pi(s))+\gamma\sum_{s'}p(s'|s,\pi(s))\,v(s')$. Optimal: $v_*(s)=\max_a[r(s,a)+\gamma\sum_{s'}p\,v_*(s')]$.

Hint 3/4

$\pi_R$: $v(\text{g})=6+0.5\,(0.5v(\text{g})+0.5v(\text{w}))$, $v(\text{w})=-1+0.5v(\text{g})$. $\pi_N$: $v(\text{w})=2+0.5\,v(\text{w})$.

Hint 4/4

$v_{\pi_R}=(9.2,\ 3.6)$, $v_{\pi_N}=(9.333,\ 4)$, and $\pi_N$ passes the optimality test at worn.

Show solution

Evaluating a policy is linear algebra; checking optimality is one look-ahead per action with the candidate's values.

Repair when worn

$$v(\text{g})=6+0.25\,v(\text{g})+0.25\,(-1+0.5\,v(\text{g}))$$

Substitute the worn equation, $v(\text{w})=-1+0.5\,v(\text{g})$, to leave one unknown.

$$0.625\,v(\text{g})=5.75\Rightarrow v(\text{g})=9.2,\quad v(\text{w})=3.6$$

The equation is linear in $v(\text{g})$; once it is known, worn's equation $v(\text{w})=-1+0.5\,v(\text{g})$ gives the second value.

Keep running when worn

$$v(\text{w})=\tfrac{2}{1-0.5}=4$$

A worn machine that keeps running earns 2 forever.

$$v(\text{g})=6+0.25\,v(\text{g})+0.25\cdot4\Rightarrow v(\text{g})=\tfrac{7}{0.75}=9.333$$

Same equation for good, with the new worn value.

Optimality test at worn

$$\text{run: }2+0.5\cdot4=4,\qquad\text{repair: }-1+0.5\cdot9.333=3.667$$

Score both actions with $\pi_N$'s values.

$$\max(4,\ 3.667)=4=v_{\pi_N}(\text{w})$$

The equation holds; at good there is only one action, so $\pi_N$ is optimal.

Answer $$\boxed{v_{\pi_R}=(9.2,\ 3.6),\quad v_{\pi_N}=(9.333,\ 4),\quad\pi^*=\pi_N\ \text{at}\ \gamma=0.5}$$
Check

Consistency: $\pi_N$ is at least as good as $\pi_R$ in both states, $9.333\ge9.2$ and $4\ge3.6$, as an optimal policy must be.

A repair costs now and pays later, so a small discount rate makes it lose; at $\gamma=0.8$ it won.

2§14.6 — three updates, then act

A cleaning robot moves between the hall and a room. Its table holds $Q(\text{hall},\text{move})=0$, $Q(\text{hall},\text{wait})=2$, $Q(\text{room},\text{move})=4$, $Q(\text{room},\text{wait})=0$. It observes, in order, (hall, move, $1$, room), (room, move, $3$, hall), (hall, wait, $0$, hall). Here $\alpha=0.5$ and $\gamma=0.9$.

Find
  1. (a) Give the table after the three updates.

  2. (b) Give the greedy action in each of the two states, hall and room.

  3. (c) Under ε-greedy with $\epsilon=0.2$, what is the probability of wait in the hall?

Given
  • initial table: hall: move $0$, wait $2$; room: move $4$, wait $0$

  • transitions: (hall, move, $1$, room), (room, move, $3$, hall), (hall, wait, $0$, hall)

  • $\alpha=0.5$, $\gamma=0.9$

Hint 1/4

Process the transitions in order, each time reading the table as it stands.

Hint 2/4

$\hat Q=R_{t+1}+\gamma\max_{a'}Q(S_{t+1},a')$, $Q\leftarrow(1-\alpha)Q+\alpha\hat Q$; ε-greedy gives a non-greedy action $\epsilon/\lvert\mathcal A\rvert$.

Hint 3/4

Update 1 lands in the room, whose max is $4$. Update 2 lands in the hall, whose max is then $\max(\text{new move entry},\ 2)$. Update 3 also lands in the hall.

Hint 4/4

Table: hall move $2.3$, hall wait $2.035$, room move $4.535$, room wait $0$; greedy: move in both; wait in the hall has probability $0.1$.

Show solution

Order matters because each target reads the latest table, so go transition by transition.

Update 1: (hall, move, 1, room)

$$\hat Q=1+0.9\cdot\max(4,\ 0)=4.6$$

The room's best entry is move's $4$.

$$Q(\text{hall},\text{move})=0.5\cdot0+0.5\cdot4.6=2.3$$

Half of the old entry, half of the target.

Update 2: (room, move, 3, hall)

$$\hat Q=3+0.9\cdot\max(2.3,\ 2)=5.07$$

The hall's max now uses the entry just raised to $2.3$.

$$Q(\text{room},\text{move})=0.5\cdot4+0.5\cdot5.07=4.535$$

Blend with the old entry $4$.

Update 3: (hall, wait, 0, hall)

$$\hat Q=0+0.9\cdot\max(2.3,\ 2)=2.07$$

The max is taken before this entry changes.

$$Q(\text{hall},\text{wait})=0.5\cdot2+0.5\cdot2.07=2.035$$

Blend with the old entry $2$.

Act

$$\text{hall: }\max(2.3,\ 2.035)\to\text{move};\quad\text{room: }\max(4.535,\ 0)\to\text{move}$$

Greedy reads the final table.

$$\Pr(\text{wait}\mid\text{hall})=\epsilon/2=0.1$$

Only heads can pick the non-greedy action, one time in two.

Answer $$\boxed{\text{hall: }2.3,\ 2.035;\ \ \text{room: }4.535,\ 0;\ \ \text{move in both};\ \ 0.1}$$
Check

Each new entry lies between its old value and its target: $2.3\in[0,4.6]$, $4.535\in[4,5.07]$, $2.035\in[2,2.07]$.

In a trace, reread the table before every target; an entry changed one line earlier can change the max.

3§14.7 — why the table never reaches q*

An engineer runs Q-learning on a robot with the greedy rule and a constant $\alpha=0.1$. After $10\,000$ steps many entries are still exactly at their starting value $0$, and the learned policy is poor.

Find(a) Which single change addresses the cause?
Given
  • action rule: greedy, $A_t=\arg\max_aQ(S_t,a)$

  • $\alpha=0.1$ constant

  • many entries never changed from $0$

Hint 1/4

Ask why an entry would stay exactly at its starting value.

Hint 2/4

An entry changes only when its pair is tried; the convergence result needs every pair tried infinitely often.

Hint 3/4

Greedy tries only the current best action in each state, so pairs that look worse are never tried.

Hint 4/4

Add exploration, for example ε-greedy, so every pair keeps being tried.

Show solution

The symptom, entries exactly at their start value, points to pairs that were never updated, which is a visiting problem, not a step size problem.

Link symptom and condition

$$Q(s,a)\ \text{changes}\iff(s,a)\ \text{is tried}$$

Only the entry of the pair just tried is updated.

$$\text{greedy}\Rightarrow\text{some pairs tried }0\ \text{times}$$

The slides' condition, every pair infinitely often, fails.

Answer $$\boxed{\text{add exploration, e.g. }\epsilon\text{-greedy}}$$
Check

The corridor example: greedy with ties to left never tries (S0, right) and settles on the value $1$ instead of $6.2$.

Step sizes decide how fast tried entries settle; exploration decides which entries are tried at all.

4§14.5 — a ledge, a longer detour and value iteration

From the start cell a robot can take the ledge, which reaches the goal ($+10$) with probability $1-q$ and the pit ($-10$) with probability $q$, both ending the run. Or it takes a two-step detour: start to D1 ($-1$), D1 to D2 ($-1$), then D2 into the goal ($+10$). The discount rate is $\gamma=0.9$.

Find
  1. (a) Find the slip probability $q$ at which the two first moves tie.

  2. (b) For $q=0.3$, run value iteration from $v_0=0$ and find the first sweep after which the greedy action at the start is the detour.

Given
  • ledge: goal ($+10$) w.p. $1-q$, pit ($-10$) w.p. $q$; both terminal

  • detour: start $\to$ D1 ($-1$) $\to$ D2 ($-1$) $\to$ goal ($+10$), one action per detour cell

  • $\gamma=0.9$

Hint 1/4

Score both first moves with optimal values; then follow how many sweeps the goal needs to reach the start through the detour.

Hint 2/4

$q_*(\text{start},\text{ledge})=10-20q$; $q_*(\text{start},\text{detour})=-1+\gamma\,v_*(\text{D1})$ with $v_*(\text{D1})=-1+\gamma\cdot10$.

Hint 3/4

With $\gamma=0.9$: $v_*(\text{D1})=8$, the detour is worth $6.2$; at $q=0.3$ the ledge is worth $4$. Sweeps: $v_1(\text{D1})=-1$, $v_2(\text{D1})=8$.

Hint 4/4

(a) $q=0.19$. (b) Sweep $3$: the start's detour score is $-1$, then $-1.9$, then $6.2>4$.

Show solution

The ledge's landing cells are terminal, so its score never changes across sweeps; only the detour's score grows as the goal's value travels back.

Optimal scores

$$v_*(\text{D2})=10,\quad v_*(\text{D1})=-1+0.9\cdot10=8$$

One move each.

$$q_*(\text{start},\text{detour})=-1+0.9\cdot8=6.2,\quad q_*(\text{start},\text{ledge})=10-20q$$

The ledge averages two terminal outcomes.

$$10-20q=6.2\iff q=0.19$$

Set the two first moves equal and solve for $q$.

Sweeps at q = 0.3

$$k=1:\ v_1(\text{D2})=10,\ v_1(\text{D1})=-1;\ \ \text{start: }\max(4,\ -1)\to\text{ledge}$$

With $v_0=0$ the detour sees only its first $-1$.

$$k=2:\ v_2(\text{D1})=-1+9=8;\ \ \text{start: }\max(4,\ -1+0.9\cdot(-1))\to\text{ledge}$$

The start still uses $v_1(\text{D1})=-1$.

$$k=3:\ \text{start: }\max(4,\ -1+0.9\cdot8=6.2)\to\text{detour}$$

The goal's value has travelled back two cells.

Answer $$\boxed{q=0.19;\qquad\text{detour greedy from sweep }3}$$
Check

At $q=0.3$ the ledge is worth $0.7\cdot10+0.3\cdot(-10)=4<6.2$, so the detour must be optimal, and sweep 3 is the first time the start can see the goal through two detour cells.

Value iteration from zero needs as many sweeps as the path to the reward is long before distant rewards can change a decision.

5§14.6 — the first wrong line of a Q-learning trace

A student processes two transitions with Q-learning, $\alpha=0.5$ and $\gamma=0.9$, starting from the table in the given list, and writes four steps.

  1. Target of $(s_1,a,2,s_2)$: $2+0.9\max(2,\ 6)=7.4$.
  2. $Q(s_1,a)=0.5\cdot1+0.5\cdot7.4=4.2$.
  3. Target of $(s_2,a,-1,s_1)$: $-1+0.9\max(1,\ 3)=1.7$.
  4. $Q(s_2,a)=0.5\cdot2+0.5\cdot1.7=1.85$.
Find(a) Which step is the first one that is wrong?
Given
  • table: $Q(s_1,a)=1$, $Q(s_1,b)=3$, $Q(s_2,a)=2$, $Q(s_2,b)=6$

  • transitions in order: $(s_1,a,2,s_2)$, then $(s_2,a,-1,s_1)$

  • $\alpha=0.5$, $\gamma=0.9$

Hint 1/4

Check each line against the table as it stands at that moment, not as it was at the start.

Hint 2/4

$\hat Q=R_{t+1}+\gamma\max_{a'}Q(S_{t+1},a')$ reads the current table; $Q\leftarrow(1-\alpha)Q+\alpha\hat Q$ then changes one entry.

Hint 3/4

After step 2 the table holds $Q(s_1,a)=4.2$ and $Q(s_1,b)=3$, and the second transition lands in $s_1$.

Hint 4/4

Step 3 should use $\max(4.2,\ 3)=4.2$, so it is the first wrong step; the corrected target is $2.78$ and $Q(s_2,a)=2.39$.

Show solution

Each line depends on the lines before it, so check them in order and stop at the first disagreement.

Steps 1 and 2

$$2+0.9\cdot\max(2,\ 6)=7.4$$

The best entry at $s_2$ is $6$: step 1 is right.

$$0.5\cdot1+0.5\cdot7.4=4.2$$

Step 2 blends correctly and changes only this entry.

Step 3 against the current table

$$\max_{a'}Q(s_1,a')=\max(4.2,\ 3)=4.2$$

The entry changed in step 2 is the one this target needs.

$$\hat Q=-1+0.9\cdot4.2=2.78\ne1.7$$

So step 3 is the first wrong line.

Corrected step 4

$$Q(s_2,a)=0.5\cdot2+0.5\cdot2.78=2.39$$

The same blend with the corrected target.

Answer $$\boxed{\text{step 3};\qquad\text{corrected }Q(s_2,a)=2.39}$$
Check

$2.39$ lies between the old entry $2$ and the corrected target $2.78$; the student's $1.85$ moved the entry down, toward a target read from a table that no longer existed.

An error in one target spreads: every later update that reads the wrong entry inherits it.

D · interleaved 5 questions
1§14.6 — a news app keeps one number

A news app shows a sports story to new users; each showing ends the session and pays the reading time in minutes. The first five showings paid $3$, $7$, $5$, $9$ and $6$ minutes. The app keeps one number and after the $n$th showing replaces it by $(1-\frac1n)\cdot\text{old}+\frac1n\cdot\text{new}$, starting from $0$.

Find
  1. (a) What is the number after five showings?

  2. (b) What is its standard deviation after 100 showings?

  3. (c) What does the number converge to, and why?

Given
  • payoffs: $3,\ 7,\ 5,\ 9,\ 6$

  • update after showing $n$: $(1-\frac1n)\,\text{old}+\frac1n\,\text{new}$

  • reading times: independent, mean $6$, standard deviation $2$

Hint 1/4

Find out what this update computes, then use what you know about that quantity.

Hint 2/4

With weight $\frac1n$ the update keeps the running sample mean $\bar X_n=\frac1n\sum_{i\le n}X_i$; $\operatorname{Var}(\bar X_n)=\sigma^2/n$.

Hint 3/4

Here the payoffs add to $3+7+5+9+6=30$ over $5$ showings; $\sigma=2$ and $n=100$.

Hint 4/4

(a) $6$. (b) $2/\sqrt{100}=0.2$. (c) $6$, by the law of large numbers.

Show solution

Unrolling the update shows it is the sample mean, whose variance and limit are known from the probability review.

Unroll

$$3,\qquad\tfrac12\cdot3+\tfrac12\cdot7=5,\qquad\tfrac23\cdot5+\tfrac13\cdot5=5$$

The first update copies the first payoff; each later one keeps the mean so far.

$$\tfrac34\cdot5+\tfrac14\cdot9=6,\qquad\tfrac45\cdot6+\tfrac15\cdot6=6$$

The means of the first four and of all five payoffs are both $6$.

Spread and limit

$$\operatorname{sd}(\bar X_{100})=\frac{2}{\sqrt{100}}=0.2$$

Variance of a mean of independent variables: $\sigma^2/n$.

$$\bar X_n\to\mathbb E[X]=6$$

Law of large numbers.

Answer $$\boxed{6;\quad0.2;\quad\to6}$$
Check

$(3+7+5+9+6)/5=6$ directly, matching the last unrolled value.

Q-learning with $\alpha=1/n$ on a pair whose next state is terminal is exactly a sample mean.

2§14.6 — two classmates, one table entry

A table entry is $3$ and a new target value is $7$. Student A takes one gradient-descent step of size $0.25$ on the loss $\ell(Q)=\frac12(7-Q)^2$. Student B computes $(1-\alpha)Q+\alpha\cdot7$ with $\alpha=0.25$.

Find
  1. (a) Compute both results.

  2. (b) Show that the two rules agree for every $Q$, $T$ and step size.

  3. (c) In the neural network update $w(n+1)=w(n)-\eta\nabla l$, what plays $w$, $\eta$ and $l$?

Given
  • entry $Q=3$, target $T=7$

  • A: $Q\leftarrow Q-0.25\,\frac{d\ell}{dQ}$ with $\ell=\frac12(T-Q)^2$

  • B: $Q\leftarrow(1-\alpha)Q+\alpha T$, $\alpha=0.25$

Hint 1/4

Differentiate the loss, then compare the two formulas symbol by symbol.

Hint 2/4

$\frac{d}{dQ}\,\frac12(T-Q)^2=-(T-Q)$.

Hint 3/4

A: $3-0.25\cdot(-(7-3))$. B: $0.75\cdot3+0.25\cdot7$.

Hint 4/4

Both give $4$, since $Q+\eta(T-Q)=(1-\eta)Q+\eta T$.

Show solution

Rewriting both updates in the form 'old plus step times gap' shows they are the same expression.

Numbers

$$\text{A: }3-0.25\cdot(-(7-3))=3+1=4$$

The gradient of the loss at $Q=3$ is $-4$.

$$\text{B: }0.75\cdot3+0.25\cdot7=2.25+1.75=4$$

B's rule is the Q-learning update with $\alpha=0.25$; landing on the same $4$ as A hints that the rules coincide, which (b) proves.

Identity

$$Q-\eta\,\big(-(T-Q)\big)=Q+\eta\,(T-Q)=(1-\eta)\,Q+\eta\,T$$

Expand and collect the $Q$ terms.

Match with SGD

$$w\leftrightarrow Q(S_t,A_t),\quad\eta\leftrightarrow\alpha,\quad l\leftrightarrow\tfrac12(\hat Q-Q)^2$$

The target is treated as a constant while differentiating.

Answer $$\boxed{4=4;\qquad Q+\eta(T-Q)=(1-\eta)Q+\eta T}$$
Check

Check the identity at another point: $Q=0$, $T=10$, $\eta=0.5$ gives $5$ both ways.

This is why a deep Q network can be trained with ordinary gradient steps: each update is SGD on a squared error toward the target.

3§14.7 — an energy view of action choice

An agent chooses among three actions with probability proportional to $e^{Q(s,a)}$, where $Q(s,\cdot)=(1,\ 0,\ -1)$. A restricted Boltzmann machine assigns a configuration the probability $e^{-E}/Z$.

Find
  1. (a) What plays the energy $E$, and what is $Z$ here?

  2. (b) Compute the three probabilities.

  3. (c) If every entry of $Q(s,\cdot)$ increases by $5$, what happens to the probabilities?

Given
  • $Q(s,a_1)=1,\ Q(s,a_2)=0,\ Q(s,a_3)=-1$

  • $e\approx2.718$, $e^{-1}\approx0.368$

  • Boltzmann distribution: $p=e^{-E}/Z$, $Z=\sum e^{-E}$

Hint 1/4

Match the two formulas term by term.

Hint 2/4

$e^{Q}=e^{-E}$ means $E(a)=-Q(s,a)$; $Z=\sum_ae^{Q(s,a)}$.

Hint 3/4

The exponentials are $2.718$, $1$ and $0.368$, so $Z=4.086$.

Hint 4/4

(b) $(0.665,\ 0.245,\ 0.090)$. (c) Nothing changes: $e^5$ cancels between top and bottom.

Show solution

Writing the rule as $e^{-E}/Z$ makes the shift question a one-line cancellation.

Energy and Z

$$E(a)=-Q(s,a),\qquad Z=2.718+1+0.368=4.086$$

Lower energy, higher probability, as in the RBM.

Probabilities

$$\tfrac{2.718}{4.086}=0.665,\quad\tfrac{1}{4.086}=0.245,\quad\tfrac{0.368}{4.086}=0.090$$

Each term over the sum.

Shift by 5

$$\frac{e^{Q(s,a)+5}}{\sum_{a'}e^{Q(s,a')+5}}=\frac{e^5e^{Q(s,a)}}{e^5\sum_{a'}e^{Q(s,a')}}$$

The common factor cancels.

Answer $$\boxed{E=-Q,\ Z=4.086;\quad(0.665,\ 0.245,\ 0.090);\quad\text{unchanged}}$$
Check

These are the same probabilities as for $Q=(2,\ 1,\ 0)$ in the worked example: that table is this one shifted by $1$.

Only differences of entries matter to this rule, just as only energy differences matter in a Boltzmann machine.

4§14.2 — a trajectory as a directed graph

An agent follows a stationary Markov policy. Consider the variables $S_0,\ A_0,\ S_1,\ A_1,\ S_2$, with an arrow into each variable from each variable it is drawn from directly.

Find
  1. (a) List the arrows.

  2. (b) Find the Markov blanket of $S_1$.

  3. (c) Is $S_2$ independent of $S_0$ given $S_1$ and $A_1$?

Given
  • $A_t$ is drawn from $\pi(\cdot\mid S_t)$

  • $S_{t+1}$ is drawn from $p(\cdot\mid S_t,A_t)$

  • Markov blanket in a directed graph: parents, children and the children's other parents

Hint 1/4

Draw one arrow per 'is drawn from' relation, then read the blanket off the graph.

Hint 2/4

Parents of a node are what it is drawn from; the blanket adds children and co-parents; a node is independent of its non-descendants given its parents.

Hint 3/4

Arrows: $S_0\to A_0$, $S_0\to S_1$, $A_0\to S_1$, $S_1\to A_1$, $S_1\to S_2$, $A_1\to S_2$.

Hint 4/4

Blanket of $S_1$: $\{S_0,A_0,A_1,S_2\}$; and yes, $S_2$ is independent of $S_0$ given its parents $S_1,A_1$.

Show solution

The graph's parents are exactly the conditioning variables of the two model tables, so the graph can be written down without computing anything.

Arrows

$$S_0\to A_0,\ \ S_0\to S_1,\ \ A_0\to S_1,\ \ S_1\to A_1,\ \ S_1\to S_2,\ \ A_1\to S_2$$

Policy arrows into actions, transition arrows into states.

Markov blanket of S1

$$\text{parents }\{S_0,A_0\},\ \text{children }\{A_1,S_2\},\ \text{co-parent of }S_2:\ A_1$$

The co-parent is already a child, so nothing new is added.

Independence

$$S_2\perp\!\!\!\perp S_0\mid S_1,A_1$$

$S_0$ is a non-descendant of $S_2$, and $S_1,A_1$ are $S_2$'s parents.

Answer $$\boxed{\text{blanket}(S_1)=\{S_0,A_0,A_1,S_2\};\quad S_2\perp\!\!\!\perp S_0\mid S_1,A_1}$$
Check

Blocking check: every path from $S_0$ to $S_2$ passes through $S_1$ or $A_1$ as a chain or fork, and both are observed, so all paths are blocked.

The Markov property of an MDP is a conditional independence statement that the chain-shaped graph makes visible.

5§14.6 — a steady estimate or a quick one

The targets observed for one state-action pair are independent with mean $10$ and variance $9$. One agent uses $\alpha=1/n$; another uses a constant $\alpha=0.2$, whose estimate, once its start value is forgotten, is $\sum_{j\ge0}\alpha(1-\alpha)^jX_{n-j}$.

Find
  1. (a) Find the variance of agent 1's estimate after 100 updates.

  2. (b) Find the variance of agent 2's estimate.

  3. (c) Which agent should be used if the mean never changes, and which if it drifts over time?

Given
  • targets: independent, mean $10$, variance $9$

  • agent 1: $\alpha=1/n$; agent 2: $\alpha=0.2$

  • agent 2's estimate: $\sum_{j\ge0}\alpha(1-\alpha)^jX_{n-j}$

Hint 1/4

Both estimates are weighted sums of independent targets; add the squared weights.

Hint 2/4

$\operatorname{Var}(\sum_jw_jX_j)=\sigma^2\sum_jw_j^2$; for agent 2, $\sum_{j\ge0}\alpha^2(1-\alpha)^{2j}=\frac{\alpha^2}{1-(1-\alpha)^2}=\frac{\alpha}{2-\alpha}$.

Hint 3/4

Agent 1: $9/100$. Agent 2: $9\cdot\frac{0.2}{1.8}$.

Hint 4/4

(a) $0.09$. (b) $1$. (c) Agent 1 if the mean never changes; agent 2 if it drifts.

Show solution

Each estimate is a weighted sum of independent targets, so its variance is the target variance times the sum of the squared weights.

Agent 1

$$\operatorname{Var}=\tfrac{9}{100}=0.09$$

Equal weights $\frac1{100}$ on $100$ targets.

Agent 2

$$\sum_{j\ge0}0.04\cdot0.64^j=\frac{0.04}{0.36}=\frac{0.2}{1.8}$$

Squared weights $\alpha^2(1-\alpha)^{2j}$ form a geometric series.

$$\operatorname{Var}=9\cdot\tfrac19=1$$

The targets are independent, so a weighted sum has variance $\sum_j w_j^2$ times the common variance $9$.

Choice

$$\text{fixed mean: }\alpha=\tfrac1n;\qquad\text{drifting mean: }\alpha=0.2$$

Low variance against the ability to track change, the same trade-off as bias against variance.

Answer $$\boxed{0.09;\qquad1;\qquad\alpha=\tfrac1n\ \text{if fixed},\ \alpha=0.2\ \text{if drifting}}$$
Check

The weights of agent 2 add to $\sum_j0.2\cdot0.8^j=1$, so its estimate is unbiased for a fixed mean; only its variance, $1$ against $0.09$, is worse.

A constant learning rate never stops listening to new data: good when the world changes, noisy when it does not.

Mistake ledger (14 entries)
⚠ Discounting the first reward

It feels natural that every future reward is discounted, the next one included.

wrong$$G_t=\gamma R_{t+1}+\gamma^2R_{t+2}+\gamma^3R_{t+3}+\dots$$
right$$G_t=R_{t+1}+\gamma R_{t+2}+\gamma^2R_{t+3}+\dots$$
⚠ Treating γ = 1 as harmless in a task that never ends

The slides allow $\gamma=1$, and for tasks that always end it is fine.

wrong$$\gamma=1:\ \ 1+1+1+\dots\ \text{is a finite value}$$
right$$\sum_{k\ge0}\gamma^k=\frac{1}{1-\gamma}\ \text{needs}\ \gamma<1$$
⚠ Adding the transition table over the wrong index

Both indices are states, and the bar in $p(s'|s,a)$ is easy to read backwards.

wrong$$\sum_{s}p(s'|s,a)=1$$
right$$\sum_{s'}p(s'|s,a)=1\ \text{for every pair }(s,a)$$
⚠ Using the likeliest landing state's reward as the action's reward

The likeliest outcome feels like what the action pays.

wrong$$r(s,a)=r(s,a,s'_{\text{likeliest}})$$
right$$r(s,a)=\sum_{s'}p(s'|s,a)\,r(s,a,s')$$
⚠ Taking the maximum instead of the policy's average

The optimal value uses a max, and the two formulas sit side by side on the slides.

wrong$$v_\pi(s)=\max_a q_\pi(s,a)$$
right$$v_\pi(s)=\sum_a\pi(a|s)\,q_\pi(s,a)$$
⚠ Repeating the forced action in q

The name $q_\pi(s,a)$ mentions $a$, so it is tempting to keep playing $a$.

wrong$$q_\pi(s,a)=\text{return of always playing }a$$
right$$q_\pi(s,a):\ a\ \text{once, then}\ \pi\ \text{from the next step}$$
⚠ Putting the max after the average

Both $\max$ and $\sum$ appear in the equation, and their order looks cosmetic.

wrong$$v_*(s)=\sum_{s'}p(s'|s,a)\max_a\big[r+\gamma v_*(s')\big]$$
right$$v_*(s)=\max_a\sum_{s'}p(s'|s,a)\big[r(s,a,s')+\gamma v_*(s')\big]$$
⚠ Heading for the most valuable neighbour

Moving toward the state with the largest value feels like following the values.

wrong$$\pi^*(s)=\arg\max_{s'}v_*(s')$$
right$$\pi^*(s)=\arg\max_a\big[r(s,a)+\gamma\textstyle\sum_{s'}p(s'|s,a)\,v_*(s')\big]$$
⚠ Using a value from the current sweep

Filling the table from top to bottom, the fresh number is right there.

wrong$$v_2(\text{w})=\max(2+0.8\cdot2,\ -1+0.8\cdot\underbrace{9.2}_{v_2(\text{g})})=6.36$$
right$$v_2(\text{w})=\max(2+0.8\cdot2,\ -1+0.8\cdot\underbrace{6}_{v_1(\text{g})})=3.8$$
⚠ Extracting the policy from the immediate reward

At the end the values feel finished, so the look-ahead seems no longer needed.

wrong$$\pi(s)=\arg\max_ar(s,a)$$
right$$\pi(s)=\arg\max_a\big[r(s,a)+\gamma\textstyle\sum_{s'}p(s'|s,a)\,v_{k^*}(s')\big]$$
⚠ Swapping the two weights

Both weights add to one, so the formula looks symmetric.

wrong$$Q\leftarrow\alpha\,Q+(1-\alpha)\,\hat Q$$
right$$Q\leftarrow(1-\alpha)\,Q+\alpha\,\hat Q$$
⚠ Using the next action actually taken instead of the max

The agent's next move is the most visible thing at $S_{t+1}$.

wrong$$\hat Q=R_{t+1}+\gamma\,Q(S_{t+1},A_{t+1})$$
right$$\hat Q=R_{t+1}+\gamma\max_{a'}Q(S_{t+1},a')$$
⚠ Leaving the greedy action out of the random draw

Exploring sounds like trying something else, but the coin's draw on the slides is uniform over the whole action set.

wrong$$\Pr(a\ne a^\circ)=\frac{\epsilon}{\lvert\mathcal A\rvert-1}$$
right$$\Pr(a\ne a^\circ)=\frac{\epsilon}{\lvert\mathcal A\rvert}$$
⚠ Reading Boltzmann as a ratio of entries

Normalizing looks like dividing by a sum of entries, but it divides a sum of exponentials.

wrong$$\frac{\Pr(a_1)}{\Pr(a_2)}=\frac{Q(s,a_1)}{Q(s,a_2)}$$
right$$\frac{\Pr(a_1)}{\Pr(a_2)}=e^{\,Q(s,a_1)-Q(s,a_2)}$$
Formula card
Return
$$G_t=\sum_{k=0}^{\infty}\gamma^kR_{t+k+1}=R_{t+1}+\gamma\,G_{t+1}$$

$0\le\gamma<1$, or $\gamma=1$ if every episode ends; terminal rewards $0$

MDP tables
$$p(s'|s,a),\quad r(s,a)=\sum_{s'}p(s'|s,a)\,r(s,a,s')$$

Markov state; $\sum_{s'}p(s'|s,a)=1$

Values of a policy
$$v_\pi(s)=\mathbb E_\pi[G_t\mid S_t=s]=\sum_a\pi(a|s)\,q_\pi(s,a)$$

stationary Markov $\pi$

Bellman optimality
$$v_*(s)=\max_aq_*(s,a),\quad q_*(s,a)=r(s,a)+\gamma\sum_{s'}p(s'|s,a)\,v_*(s')$$

$\gamma<1$; $\pi^*(s)=\arg\max_aq_*(s,a)$ is optimal

Value iteration
$$v_{k+1}(s)=\max_a\Big[r(s,a)+\gamma\sum_{s'}p(s'|s,a)\,v_k(s')\Big]$$

$p$, $r$ known; $v_0=0$; stop when the largest change is at most $\epsilon$

Q-learning
$$Q(S_t,A_t)\leftarrow(1-\alpha)\,Q(S_t,A_t)+\alpha\big[R_{t+1}+\gamma\max_{a'}Q(S_{t+1},a')\big]$$

$0<\alpha\le1$; target from the table before the update; every pair tried infinitely often

ε-greedy and Boltzmann
$$\Pr(a^\circ)=1-\epsilon+\tfrac{\epsilon}{\lvert\mathcal A\rvert},\quad\Pr(a)=\frac{e^{Q(s,a)}}{\sum_{a'}e^{Q(s,a')}}$$

$a^\circ$ the greedy action; other actions get $\epsilon/\lvert\mathcal A\rvert$ under ε-greedy

Check yourself

Close the page and write from memory: the return and its recursion, the definitions of $v_\pi$ and $q_\pi$, the Bellman optimality equations, one value iteration sweep, the Q-learning update and the ε-greedy probabilities. Then compare with the formula card.

  • Compute $G_0$ for the rewards $-1,-1,+10$ at $\gamma=0.9$ and say why $R_{t+1}$ is undiscounted?

    c-return

  • Write the machine example as an MDP and fix a state that is not Markov?

    c-mdp

  • Solve the two linear equations for a given policy and average $q_\pi$ with $\pi$'s probabilities?

    c-policy-value

  • Check claimed optimal values state by state and read off $\pi^*$?

    c-bellman

  • Do two value iteration sweeps from zero, in both forms, without reading a value from the current sweep?

    c-value-iteration

  • Update a Q table from a list of observed transitions and say what the convergence result needs?

    c-q-learning

  • Give every action's probability under ε-greedy and under Boltzmann, and explain why greedy alone can get stuck?

    c-exploration

Glossary (28 terms)
reinforcement learningpekiştirmeli öğrenme

Learning to choose actions from the rewards that follow them, where the learner's own actions decide what it observes and the goal is the largest expected return.

agentajan

The learner and decision maker: it observes the state, picks an action and receives a reward.

environmentortam

Everything outside the agent: it answers each action with a reward and a next state.

rewardödül

The number $R_{t+1}$ the environment returns after the action taken at step $t$; costs are negative rewards.

return

The discounted sum of future rewards, $G_t=\sum_k\gamma^kR_{t+k+1}$.

discount rate

The factor $\gamma$ by which a reward loses weight for every step it arrives later.

episode

One run from a start state until a terminal state.

terminal state

A state that ends the episode; its value is $0$ and it has no actions.

policypolitika

A rule that maps what the agent knows to a distribution over actions.

stationary Markov policy

A policy $\pi(a|s)$ that uses only the current state, in the same way at every step.

deterministic policy

A policy that puts probability $1$ on one action in each state, written $\pi(s)$.

Markov propertyMarkov özelliği

Given the current state and action, the next state and reward do not depend on the earlier history.

Markov decision processMarkov karar süreci

A model with finite states, actions allowed in each state, transition probabilities $p(s'|s,a)$ and expected rewards $r(s,a,s')$.

transition probabilitygeçiş olasılığı

$p(s'|s,a)$, the probability that one step from state $s$ with action $a$ moves the process to $s'$.

durum değer fonksiyonu

$v_\pi(s)$, the expected return from $s$ when actions follow $\pi$.

action value functioneylem değer fonksiyonu

$q_\pi(s,a)$, the expected return after taking $a$ in $s$ and following $\pi$ afterwards.

optimal policyen iyi politika

A policy whose value is at least that of every other policy in every state.

Bellman optimality equation

$v_*(s)=\max_a[r(s,a)+\gamma\sum_{s'}p(s'|s,a)v_*(s')]$: the best value is that of the best first action followed by optimal play.

value iterationdeğer iterasyonu

Repeating the Bellman optimality update on a value table until it stops changing, then acting greedily.

Q-learning

A method that learns the optimal action values from observed transitions, without a model, by moving each tried entry toward a one-sample target.

learning rateöğrenme oranı

The step size $\alpha$ that sets how far each update moves an entry toward its target; $\eta$ in the neural networks section.

model-free

Learning that uses observed transitions instead of known transition probabilities.

explorationkeşif

Trying actions that do not look best at the moment, to learn their values.

sömürü

Taking the action that looks best according to the current estimates.

greedyaçgözlü

Always taking $\arg\max_aQ(s,a)$, with no exploration.

ε-greedy

With probability $\epsilon$ pick an action uniformly at random, otherwise the greedy one.

Boltzmann exploration

Pick action $a$ with probability proportional to $e^{Q(s,a)}$.

deep Q network

A neural network that replaces the Q table and is trained toward the Q-learning target with gradient steps.

What comes next
§15 · Multi-armed bandits and online learning

Here actions moved the agent between states, and a reward could pay off many steps later. Next there is one situation met over and over, such as choosing a route or an item to recommend, and the whole question is how to balance trying options against using the best one so far.

Sources
  • textbookT. Hastie, R. Tibshirani, J. Friedman, The Elements of Statistical Learning, Springer (course textbook) The weekly line names no chapter of the book, so no section numbers are cited here.
  • course materialEEE 485 lecture slides, chapter 14: Reinforcement learning Scope, order of topics and notation (S_t, A_t, R_{t+1}, γ, G_t, the history H_t, π, p(s′|s,a), r(s,a,s′), v_π, q_π, v_*, q_*, π*, v_k, q_k, ε, Q, Q̂, α and the coin C_t) follow the slides; all explanations, examples, figures and exercises here are original.
  • course materialEEE 485 syllabus page on STARS, Fall 2026-27, printed 21 September 2026 Assessment weights, the weekly topic list and the recommended books.
  • textbookR. S. Sutton, A. G. Barto, Reinforcement Learning: An Introduction, 2nd edition, MIT Press, 2018 Listed among the recommended books in the chapter 1 slides; the chapter 14 slides take their agent and environment figure and their recycling robot example from it. The authors' page links a free PDF.
  • standard resultM. L. Puterman, Markov Decision Processes: Discrete Stochastic Dynamic Programming, Wiley, 1994 Cited on the slides for the existence of a deterministic stationary optimal policy.
  • standard resultMDP robot grid-world example (video linked from the slides) Value iteration on a grid with a deterministic and a stochastic robot.
  • standard resultV. Mnih et al., Human-level control through deep reinforcement learning, Nature, 2015 The deep Q network that learned Atari games from screen pixels, the subject of the slides' last video.
  • standard resultGeometric series, conditional expectation and the law of large numbers Standard results used in the derivations.

Spotted something missing or wrong? tell us · share your own notes or an old exam.

Last updated .