Reinforcement Learning Foundations
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 , derive the Bellman expectation equation for the state-value function in terms of . State every assumption you use. (5)
(b) Write the Bellman expectation equation for the action-value function , and show how and are related to each other (both directions). (4)
(c) Write the Bellman optimality equation for 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 and a single action available in each state (so the policy is fixed). Dynamics and rewards are deterministic:
- From : reward , transition to .
- From : reward , transition to .
Use discount factor .
(a) Write the two linear Bellman equations for and . (3)
(b) Solve them exactly for and . (6)
(c) Verify your answer for by directly summing the infinite discounted return of the reward sequence generated from . (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 and , 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 , 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 and define the TD error . (3)
(b) Write the SARSA and Q-learning update rules for 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 -greedy (8 marks)
(a) Define the -greedy policy formally: give the probability of selecting the greedy action and of selecting any specific non-greedy action, for actions. (3)
(b) With and , 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 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 (), terminating after the last reward:
(a) Compute the Monte Carlo return . (3)
(b) If the current estimate is , compute the TD(0) target for updating using and . (3)
Answer keyMark scheme & solutions
Question 1 (12)
(a) Derivation. (5) Split the return: (1 mark, unrolling one step). By linearity of expectation and the Markov property (future depends only on ) (1 mark for stating assumption): Marks: correct recursive split (1), Markov/linearity assumption stated (1), sum over with (1), sum over with (1), correct bracket (1).
(b) (4) (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))"} ] ```