Reinforcement Learning Foundations
Level: 5 (Mastery — cross-domain: math + coding + proof/build) Time limit: 90 minutes Total marks: 60
Notation: MDP . Return . Value functions , ; optimal , .
Question 1 — Contraction, convergence, and a numeric MDP (24 marks)
(a) Define the Bellman optimality operator on the space of value functions by Prove that is a -contraction in the sup-norm , i.e. . Hence state why value iteration converges to a unique fixed point. (8)
(b) Consider a 2-state MDP with states , a single available action in each state (so the policy is fixed), , deterministic transitions and rewards:
- From : go to , reward .
- From : go to , reward .
Write the two linear Bellman evaluation equations for and and solve them exactly. (8)
(c) Starting from , perform two synchronous value-iteration (here just policy-evaluation) sweeps and report . Using part (a), give an upper bound on in terms of and , and check numerically that your bound holds. (8)
Question 2 — SARSA vs Q-learning: derive, code, and reason (22 marks)
(a) Write the update rules for tabular SARSA and Q-learning given a transition . Explain precisely, with reference to the target term, why SARSA is on-policy and Q-learning is off-policy. (6)
(b) Implement a single agent step (in Python-like pseudocode) for an -greedy Q-learning agent, including action selection and the Q-update. Your code must be runnable logic (no hand-waving) using a dictionary/array Q. (8)
(c) A cliff-walking-style task has a shortcut path with high per-step reward but a risk of catastrophic penalty under exploration. Argue which of SARSA / Q-learning learns the safer path during training and which learns the optimal greedy path, and connect this to the exploration–exploitation tradeoff and the discount. (8)
Question 3 — Monte Carlo vs Temporal Difference estimation (14 marks)
(a) Given the episode of rewards (single trajectory, ): compute the return (from the first state). (6)
(b) Suppose current estimate , and after the first transition we observe and next state with . Compute the TD(0) target and the TD update to with learning rate , . Contrast the bias/variance of the MC target (part a) vs this TD target in one or two sentences. (8)
Answer keyMark scheme & solutions
Question 1
(a) Contraction proof (8)
Fix states; for any , where . (1)
Use the inequality . (2)
= \max_a \gamma\sum_{s'}P(s'|s,a)\big(u(s')-v(s')\big).$$ **(2)** Since $\sum_{s'}P=1$ and $|u(s')-v(s')|\le \lVert u-v\rVert_\infty$: $$\le \gamma \lVert u-v\rVert_\infty.$$ By symmetry the same bound holds for $(Tv)(s)-(Tu)(s)$, so $|(Tu)(s)-(Tv)(s)|\le\gamma\lVert u-v\rVert_\infty$ for all $s$, giving $\lVert Tu-Tv\rVert_\infty\le\gamma\lVert u-v\rVert_\infty$. **(2)** Since $\gamma<1$, $T$ is a contraction; by the Banach fixed-point theorem it has a unique fixed point $v_*$ and iterates $v_{k+1}=Tv_k$ converge to it geometrically. **(1)** ### (b) Exact solve (8) Bellman evaluation equations (deterministic single-action): $$v(A) = 5 + 0.9\,v(B), \qquad v(B) = -1 + 0.9\,v(A).$$ **(3)** Substitute: $$v(A) = 5 + 0.9(-1 + 0.9 v(A)) = 5 - 0.9 + 0.81 v(A) = 4.1 + 0.81 v(A).$$ **(2)** $$0.19\,v(A) = 4.1 \Rightarrow v(A) = \frac{4.1}{0.19} = 21.578947\ldots$$ **(2)** $$v(B) = -1 + 0.9(21.5789\ldots) = 18.4210\ldots$$ Exact: $v(A)=\tfrac{410}{19}$, $v(B)=\tfrac{350}{19}$. **(1)** ### (c) Two sweeps + bound (8) Sweep 1 from $v_0=(0,0)$: $$v_1(A)=5+0.9(0)=5,\qquad v_1(B)=-1+0.9(0)=-1.$$ **(2)** Sweep 2: $$v_2(A)=5+0.9(-1)=4.1,\qquad v_2(B)=-1+0.9(5)=3.5.$$ **(3)** Bound: for a $\gamma$-contraction, $\lVert v_2-v_*\rVert_\infty \le \dfrac{\gamma}{1-\gamma}\lVert v_2-v_1\rVert_\infty$, or via the one-step form $\lVert v_{k+1}-v_*\rVert_\infty\le\frac{\gamma^{k}}{1-\gamma}\lVert v_1-v_0\rVert_\infty$. Here $\lVert v_1-v_0\rVert_\infty=5$, so $$\lVert v_2-v_*\rVert_\infty \le \frac{\gamma^{1}}{1-\gamma}\cdot 5 = \frac{0.9}{0.1}\cdot 5 = 45.$$ **(2)** Check: actual $\lVert v_2-v_*\rVert_\infty=\max(|4.1-21.5789|,|3.5-18.4211|)=17.4789\le 45$. ✓ **(1)** ## Question 2 ### (a) Update rules + on/off-policy (6) SARSA (on-policy): **(2)** $$Q(s,a)\leftarrow Q(s,a)+\alpha\big[r+\gamma Q(s',a') - Q(s,a)\big],$$ where $a'$ is the action **actually taken** by the current ($\varepsilon$-greedy) policy at $s'$. Q-learning (off-policy): **(2)** $$Q(s,a)\leftarrow Q(s,a)+\alpha\big[r+\gamma \max_{a''} Q(s',a'') - Q(s,a)\big].$$ Reasoning: SARSA's target uses the value of the action the behaviour policy will execute, so it evaluates/improves the same policy it follows → on-policy. Q-learning's target uses $\max$, i.e. the greedy (target) policy, regardless of the exploratory action taken → it learns about a policy different from behaviour → off-policy. **(2)** ### (b) Code (8) ```python import numpy as np, random # Q: np.ndarray shape (n_states, n_actions) def q_step(Q, s, alpha, gamma, eps, env): # eps-greedy action selection if random.random() < eps: a = random.randrange(Q.shape[1]) # explore (2) else: a = int(np.argmax(Q[s])) # exploit (2) s_next, r, done = env.step(s, a) # environment transition target = r + (0.0 if done else gamma * np.max(Q[s_next])) # off-policy max (2) Q[s, a] += alpha * (target - Q[s, a]) # TD update (2) return s_next, r, done ``` Marks: correct $\varepsilon$-branch (2), argmax exploit (2), correct max/terminal target (2), correct update form (2). ### (c) Cliff reasoning (8) - **SARSA learns the safer path.** Because its target $\gamma Q(s',a')$ includes the value of exploratory actions, the expected penalty from occasionally stepping off the cliff during $\varepsilon$-exploration is baked into the estimates near risky states. SARSA therefore assigns lower value to the risky shortcut and prefers a margin-of-safety path. **(3)** - **Q-learning learns the optimal greedy path.** Its $\max$ target ignores exploratory missteps, converging to $q_*$ which prefers the shorter high-reward shortcut; but during training its actual online returns are worse because $\varepsilon$-exploration triggers the catastrophe on the cliff edge. **(3)** - **Tradeoff/discount link:** With $\varepsilon>0$ exploration is mandatory to discover values but incurs real cost; a smaller $\gamma$ shortens the effective horizon and downweights distant penalties/rewards, reducing the perceived danger of far-off cliffs and pushing both algorithms toward greedier/shorter routes. As $\varepsilon\to0$, SARSA's estimates approach Q-learning's since the exploratory term vanishes. **(2)** ## Question 3 ### (a) Return $G_0$ (6) $$G_0 = R_1 + \gamma R_2 + \gamma^2 R_3 + \gamma^3 R_4$$ **(2)** $$= 2 + 0.5(0) + 0.25(-4) + 0.125(8)$$ **(2)** $$= 2 + 0 - 1 + 1 = 2.0.$$ **(2)** ### (b) TD(0) target and update (8) TD target: $r + \gamma V(s_1) = 2 + 0.5(3.0) = 3.5.$ **(3)** TD error: $\delta = 3.5 - V(s_0) = 3.5 - 1.0 = 2.5.$ **(2)** Update: $V(s_0)\leftarrow 1.0 + 0.1(2.5) = 1.25.$ **(2)** Bias/variance: the MC target $G_0=2.0$ is an unbiased sample of $v(s_0)$ but high-variance (accumulates randomness of all 4 rewards); the TD target $3.5$ uses a bootstrap estimate $V(s_1)$ so it is lower-variance but biased while $V(s_1)$ is inaccurate. **(1)** ```verify [ {"claim":"Q1b exact values", "code":"vA,vB=symbols('vA vB'); sol=solve([Eq(vA,5+Rational(9,10)*vB),Eq(vB,-1+Rational(9,10)*vA)],[vA,vB]); result=(sol[vA]==Rational(410,19) and sol[vB]==Rational(350,19))"}, {"claim":"Q1c two sweeps", "code":"v0A=0;v0B=0;v1A=5+0.9*v0B;v1B=-1+0.9*v0A;v2A=5+0.9*v1B;v2B=-1+0.9*v1A; result=(abs(v2A-4.1)<1e-9 and abs(v2B-3.5)<1e-9)"}, {"claim":"Q1c bound holds", "code":"actual=max(abs(4.1-Rational(410,19)),abs(Rational(7,2)-Rational(350,19))); bound=Rational(9,10)/(Rational(1,10))*5; result=bool(actual<=bound)"}, {"claim":"Q3a return G0", "code":"G0=2+Rational(1,2)*0+Rational(1,4)*(-4)+Rational(1,8)*8; result=(G0==2)"}, {"claim":"Q3b TD update", "code":"target=2+Rational(1,2)*3; V=1+Rational(1,10)*(target-1); result=(target==Rational(7,2) and V==Rational(5,4))"} ] ```