Level 3 — ProductionReinforcement Learning Foundations

Reinforcement Learning Foundations

45 minutes60 marksprintable — key stays hidden on paper

Level 3 — Production (from-scratch derivations, code-from-memory, explain-out-loud) Time limit: 45 minutes Total marks: 60

Answer all questions. Show all working. Where code is requested, pseudo-code with correct update rules and control flow is acceptable but must be self-consistent.


Question 1 — Bellman equations from scratch (12 marks)

(a) Starting from the definition of the return Gt=k=0γkRt+k+1G_t = \sum_{k=0}^{\infty}\gamma^k R_{t+k+1}, derive the Bellman expectation equation for the state-value function vπ(s)v_\pi(s) in terms of vπ(s)v_\pi(s'). State every assumption you use. (5)

(b) Write the Bellman expectation equation for the action-value function qπ(s,a)q_\pi(s,a), and show how vπ(s)v_\pi(s) and qπ(s,a)q_\pi(s,a) are related to each other (both directions). (4)

(c) Write the Bellman optimality equation for v(s)v_*(s) and explain in one sentence why it is nonlinear whereas the expectation equation is linear. (3)


Question 2 — Solving a 2-state MDP by hand (12 marks)

An MDP has two states {A,B}\{A, B\} and a single action available in each state (so the policy is fixed). Dynamics and rewards are deterministic:

  • From AA: reward +2+2, transition to BB.
  • From BB: reward +1+1, transition to AA.

Use discount factor γ=0.9\gamma = 0.9.

(a) Write the two linear Bellman equations for v(A)v(A) and v(B)v(B). (3)

(b) Solve them exactly for v(A)v(A) and v(B)v(B). (6)

(c) Verify your answer for v(A)v(A) by directly summing the infinite discounted return of the reward sequence generated from AA. (3)


Question 3 — Value iteration, coded from memory (10 marks)

(a) Write pseudo-code for the value iteration algorithm for a finite MDP with known PP and RR, including the stopping criterion and how the greedy policy is extracted at the end. (7)

(b) State the computational cost per sweep in terms of S|S|, A|A| and explain why value iteration converges (name the property). (3)


Question 4 — TD, SARSA, Q-learning contrast (12 marks)

(a) Write the TD(0) update for V(s)V(s) and define the TD error δt\delta_t. (3)

(b) Write the SARSA and Q-learning update rules for Q(s,a)Q(s,a) side by side, and clearly mark which term differs between them. (4)

(c) Explain out loud (in words) why SARSA is called on-policy and Q-learning off-policy, and describe one behavioural consequence on the classic cliff-walking task. (5)


Question 5 — Exploration and ϵ\epsilon-greedy (8 marks)

(a) Define the ϵ\epsilon-greedy policy formally: give the probability of selecting the greedy action and of selecting any specific non-greedy action, for A|A| actions. (3)

(b) With ϵ=0.1\epsilon = 0.1 and A=4|A| = 4, compute the probability of taking the greedy action and the probability of taking one particular non-greedy action. (3)

(c) Give one advantage of decaying ϵ\epsilon over training, in terms of the exploration–exploitation tradeoff. (2)


Question 6 — Monte Carlo return and TD target (6 marks)

An agent observes this single episode (γ=0.5\gamma = 0.5), terminating after the last reward:

S0R1=1S1R2=0S2R3=4terminalS_0 \xrightarrow{R_1=1} S_1 \xrightarrow{R_2=0} S_2 \xrightarrow{R_3=4} \text{terminal}

(a) Compute the Monte Carlo return G0G_0. (3)

(b) If the current estimate is V(S1)=3V(S_1) = 3, compute the TD(0) target for updating V(S0)V(S_0) using R1R_1 and V(S1)V(S_1). (3)


Answer keyMark scheme & solutions

Question 1 (12)

(a) Derivation. (5) vπ(s)=Eπ[GtSt=s].v_\pi(s)=\mathbb{E}_\pi[G_t\mid S_t=s]. Split the return: Gt=Rt+1+γGt+1G_t = R_{t+1} + \gamma G_{t+1} (1 mark, unrolling one step). vπ(s)=Eπ[Rt+1+γGt+1St=s].v_\pi(s)=\mathbb{E}_\pi[R_{t+1}+\gamma G_{t+1}\mid S_t=s]. By linearity of expectation and the Markov property (future depends only on St+1S_{t+1}) (1 mark for stating assumption): vπ(s)=aπ(as)s,rp(s,rs,a)[r+γvπ(s)].v_\pi(s)=\sum_a \pi(a\mid s)\sum_{s',r}p(s',r\mid s,a)\big[r+\gamma\,v_\pi(s')\big]. Marks: correct recursive split (1), Markov/linearity assumption stated (1), sum over aa with π\pi (1), sum over s,rs',r with pp (1), correct [r+γvπ(s)][r+\gamma v_\pi(s')] bracket (1).

(b) (4) qπ(s,a)=s,rp(s,rs,a)[r+γaπ(as)qπ(s,a)].q_\pi(s,a)=\sum_{s',r}p(s',r\mid s,a)\Big[r+\gamma\sum_{a'}\pi(a'\mid s')q_\pi(s',a')\Big]. (2) Relations (1 each):

q_\pi(s,a)=\sum_{s',r}p(s',r\mid s,a)[r+\gamma v_\pi(s')].$$ **(c)** **(3)** $$v_*(s)=\max_a\sum_{s',r}p(s',r\mid s,a)[r+\gamma v_*(s')].$$ (2) Nonlinear because of the $\max$ operator over actions, which is not a linear function of the value vector (whereas the expectation equation is a weighted average → linear). (1) --- ## Question 2 (12) **(a)** **(3)** $$v(A)=2+0.9\,v(B),\qquad v(B)=1+0.9\,v(A).$$ **(b)** **(6)** Substitute: $$v(A)=2+0.9(1+0.9v(A))=2+0.9+0.81v(A)=2.9+0.81v(A).$$ $$v(A)(1-0.81)=2.9\Rightarrow v(A)=\frac{2.9}{0.19}=15.2632\ (\approx).$$ $$v(B)=1+0.9(15.2632)=14.7368.$$ Marks: substitution (2), algebra to isolate (2), $v(A)$ value (1), $v(B)$ value (1). Exact: $v(A)=290/19$, $v(B)=280/19$. **(c)** **(3)** Reward sequence from $A$: $2,1,2,1,\dots$ $$G=\sum_{k=0}^\infty \gamma^{2k}(2+\gamma\cdot1)=\frac{2+0.9}{1-0.81}=\frac{2.9}{0.19}=15.2632.\checkmark$$ Marks: identify periodic reward pattern (1), geometric grouping (1), matches $v(A)$ (1). --- ## Question 3 (10) **(a)** Pseudo-code. **(7)** ``` Initialize V(s) = 0 for all s repeat Δ = 0 for each s in S: v_old = V(s) V(s) = max_a Σ_{s',r} p(s',r|s,a)[r + γ V(s')] Δ = max(Δ, |v_old - V(s)|) until Δ < θ # stopping criterion # extract greedy policy for each s: π(s) = argmax_a Σ_{s',r} p(s',r|s,a)[r + γ V(s')] ``` Marks: init (1), sweep over states (1), Bellman-optimality max update (2), Δ tracking + threshold stop (2), greedy argmax policy extraction (1). **(b)** **(3)** Cost per sweep $= O(|S|^2|A|)$ (each state: over $|A|$ actions and $|S|$ successors) (2). Converges because the Bellman optimality operator is a **$\gamma$-contraction** in the max-norm (contraction mapping / Banach fixed point) (1). --- ## Question 4 (12) **(a)** **(3)** $$V(S_t)\leftarrow V(S_t)+\alpha\,\delta_t,\qquad \delta_t=R_{t+1}+\gamma V(S_{t+1})-V(S_t).$$ Marks: update form (1), TD error definition (2). **(b)** **(4)** $$\text{SARSA: } Q(s,a)\leftarrow Q(s,a)+\alpha[r+\gamma Q(s',a')-Q(s,a)]$$ $$\text{Q-learning: } Q(s,a)\leftarrow Q(s,a)+\alpha[r+\gamma \max_{a'}Q(s',a')-Q(s,a)]$$ Differing term: SARSA uses $Q(s',a')$ (the actually-taken next action); Q-learning uses $\max_{a'}Q(s',a')$. Marks: SARSA (1.5), Q-learning (1.5), correctly marking the difference (1). **(c)** **(5)** SARSA is **on-policy**: its target uses the action $a'$ chosen by the same (exploratory) behaviour policy, so it evaluates/improves the policy it actually follows (2). Q-learning is **off-policy**: target uses $\max$, i.e. the greedy target policy regardless of the exploratory action actually taken (2). Consequence on cliff-walking: SARSA learns a **safer** path away from the cliff (accounting for exploratory falls), Q-learning learns the **optimal risky** path along the edge but incurs more falls during $\epsilon$-greedy training (1). --- ## Question 5 (8) **(a)** **(3)** With $|A|$ actions, greedy action prob $=1-\epsilon+\dfrac{\epsilon}{|A|}$ (1.5); each non-greedy action prob $=\dfrac{\epsilon}{|A|}$ (1.5). **(b)** **(3)** $\epsilon=0.1,|A|=4$: Greedy $=1-0.1+0.1/4 = 0.9+0.025 = 0.925$ (2). Non-greedy (each) $=0.1/4 = 0.025$ (1). **(c)** **(2)** Early high $\epsilon$ explores broadly to gather information; later low $\epsilon$ exploits the improved value estimates, so the agent converges toward greedy/optimal behaviour while having avoided premature commitment. (2) --- ## Question 6 (6) **(a)** **(3)** $\gamma=0.5$: $$G_0 = R_1+\gamma R_2+\gamma^2 R_3 = 1 + 0.5(0) + 0.25(4)=1+0+1=2.$$ Marks: formula (1), substitution (1), $G_0=2$ (1). **(b)** **(3)** TD(0) target $=R_1+\gamma V(S_1)=1+0.5(3)=1+1.5=2.5.$ Marks: formula (1), substitution (1), $2.5$ (1). ```verify [ {"claim":"v(A)=290/19 for 2-state MDP", "code":"vA,vB=symbols('vA vB'); sol=solve([Eq(vA,2+Rational(9,10)*vB),Eq(vB,1+Rational(9,10)*vA)],[vA,vB]); result=(sol[vA]==Rational(290,19))"}, {"claim":"v(B)=280/19", "code":"vA,vB=symbols('vA vB'); sol=solve([Eq(vA,2+Rational(9,10)*vB),Eq(vB,1+Rational(9,10)*vA)],[vA,vB]); result=(sol[vB]==Rational(280,19))"}, {"claim":"direct discounted sum from A equals 2.9/0.19", "code":"result=(simplify(Rational(29,10)/Rational(19,100))==Rational(290,19))"}, {"claim":"epsilon-greedy greedy prob 0.925, nongreedy 0.025 for eps=0.1,|A|=4", "code":"eps=Rational(1,10);A=4; g=1-eps+eps/A; ng=eps/A; result=(g==Rational(37,40) and ng==Rational(1,40))"}, {"claim":"MC return G0=2 and TD target 2.5", "code":"g=Rational(1,2); G0=1+g*0+g**2*4; td=1+g*3; result=(G0==2 and td==Rational(5,2))"} ] ```