Interleaved — Phase 5

AI-ML interleaved practice

printable — key stays hidden on paper

Instructions: Work each problem independently. These deliberately mix RL fundamentals with deep RL algorithms in random order — part of your task is deciding which framework or update rule applies before computing. Show all reasoning. Total: 50 marks.


Problem 1. (5 marks) An agent in a 2-state MDP is in state ss. The immediate reward for the next step is R1=2R_1 = 2, then R2=3R_2 = 3, then R3=1R_3 = 1, and every reward thereafter is 00. With discount factor γ=0.9\gamma = 0.9, compute the return G0G_0 observed from the start of this trajectory.


Problem 2. (5 marks) You observe the following single transition: state ss, action aa, reward r=5r = 5, next state ss'. Current estimates are Q(s,a)=10Q(s,a) = 10, and for ss' the values are Q(s,a1)=8Q(s', a_1) = 8, Q(s,a2)=12Q(s', a_2) = 12. The agent's policy at ss' actually selected a1a_1. Using α=0.5\alpha = 0.5, γ=1.0\gamma = 1.0: (a) Compute the Q-learning update for Q(s,a)Q(s,a). (b) Compute the SARSA update for Q(s,a)Q(s,a). (c) State the one conceptual reason the two answers differ.


Problem 3. (5 marks) An ϵ\epsilon-greedy policy over 4 actions uses ϵ=0.2\epsilon = 0.2. The greedy action is a\*a^\*. Give the probability of selecting a\*a^\* and the probability of selecting any one specific non-greedy action.


Problem 4. (6 marks) A 3-state MDP has a fixed policy π\pi. Under π\pi the dynamics give deterministic transitions: s1s2s_1 \to s_2 (reward 0), s2s3s_2 \to s_3 (reward 0), s3s3s_3 \to s_3 (reward 1, absorbing self-loop). Discount γ=0.5\gamma = 0.5. Using the Bellman expectation equation, compute Vπ(s1)V^\pi(s_1), Vπ(s2)V^\pi(s_2), Vπ(s3)V^\pi(s_3).


Problem 5. (5 marks) For a stochastic policy, an episode gives return G=4G = 4 following action aa taken in state ss, where πθ(as)=0.25\pi_\theta(a|s) = 0.25. In the REINFORCE update, the parameter step is θθ+αGθlnπθ(as)\theta \leftarrow \theta + \alpha\, G\, \nabla_\theta \ln \pi_\theta(a|s). If θlnπθ(as)=3\nabla_\theta \ln \pi_\theta(a|s) = 3 and α=0.1\alpha = 0.1, compute the size of the update Δθ\Delta\theta. Why do we use lnπ\ln \pi rather than π\pi?


Problem 6. (5 marks) Explain concisely why Double DQN was introduced over vanilla DQN. Write the target used in vanilla DQN and the target used in Double DQN, clearly showing which network selects and which evaluates the action.


Problem 7. (6 marks) Monte Carlo prediction: three episodes visit state ss once each, producing returns G={6,2,10}G = \{6, 2, 10\}. (a) Give the first-visit MC estimate of V(s)V(s). (b) A new episode gives return G=4G = 4. Update V(s)V(s) using the incremental MC rule V(s)V(s)+α(GV(s))V(s) \leftarrow V(s) + \alpha (G - V(s)) with α=0.1\alpha = 0.1 starting from your answer in (a). (c) State one reason to prefer TD(0) over MC here.


Problem 8. (4 marks) Match each algorithm to the property that distinguishes it: (i) SARSA, (ii) Q-learning, (iii) A2C, (iv) SAC — against — (A) off-policy, maximizes reward plus an entropy term; (B) on-policy TD control; (C) off-policy control using the max over next-state values; (D) on-policy actor-critic using an advantage estimate.


Problem 9. (5 marks) In experience replay with a target network, explain: (a) what problem experience replay solves, and (b) what problem the separate target network solves. State how often the target network parameters are typically updated relative to the online network.


Problem 10. (4 marks) PPO uses a clipped surrogate objective. Given the probability ratio rt(θ)=πθ(atst)πθold(atst)=1.4r_t(\theta) = \frac{\pi_\theta(a_t|s_t)}{\pi_{\theta_{old}}(a_t|s_t)} = 1.4, advantage A^t=+2\hat{A}_t = +2, and clip parameter ϵ=0.2\epsilon = 0.2, compute the value of the clipped term min(rtA^t, clip(rt,1ϵ,1+ϵ)A^t)\min\big(r_t \hat{A}_t,\ \text{clip}(r_t, 1-\epsilon, 1+\epsilon)\hat{A}_t\big).

Answer keyMark scheme & solutions

Problem 1Subtopic 5.1.6 (Discount factor & returns). G0=R1+γR2+γ2R3=2+0.9(3)+0.81(1)=2+2.7+0.81=5.51G_0 = R_1 + \gamma R_2 + \gamma^2 R_3 = 2 + 0.9(3) + 0.81(1) = 2 + 2.7 + 0.81 = \boxed{5.51}. Why this method: a pure return computation — no bootstrapping, just geometric discounting of a finite reward sequence.


Problem 2Subtopics 5.1.13 (Q-learning) vs 5.1.12 (SARSA). (a) Q-learning target =r+γmaxaQ(s,a)=5+1.0(12)=17= r + \gamma \max_{a'} Q(s',a') = 5 + 1.0(12) = 17. Update: Q10+0.5(1710)=10+3.5=13.5Q \leftarrow 10 + 0.5(17 - 10) = 10 + 3.5 = \boxed{13.5}. (b) SARSA uses the actual next action a1a_1: target =5+1.0(8)=13= 5 + 1.0(8) = 13. Update: Q10+0.5(1310)=10+1.5=11.5Q \leftarrow 10 + 0.5(13 - 10) = 10 + 1.5 = \boxed{11.5}. (c) Q-learning is off-policy (uses max\max, the greedy target), SARSA is on-policy (uses the action the behavior policy actually took). The choice hinges on spotting "policy actually selected a1a_1" → SARSA must use it.


Problem 3Subtopic 5.1.8 (ε-greedy). P(a\*)=1ϵ+ϵ/4=0.8+0.05=0.85P(a^\*) = 1 - \epsilon + \epsilon/4 = 0.8 + 0.05 = \boxed{0.85}. Each non-greedy action: ϵ/4=0.2/4=0.05\epsilon/4 = 0.2/4 = \boxed{0.05}. Check: 0.85+3(0.05)=1.00.85 + 3(0.05) = 1.0. ✓ Why: standard ε-greedy split — greedy gets the leftover mass plus its share of exploration.


Problem 4Subtopics 5.1.5 (Bellman) + 5.1.3/5.1.4 (value functions). Work backward from absorbing state. V(s3)=1+γV(s3)V(s3)(10.5)=1V(s3)=2V(s_3) = 1 + \gamma V(s_3) \Rightarrow V(s_3)(1-0.5) = 1 \Rightarrow V(s_3) = 2. V(s2)=0+0.5V(s3)=0.5(2)=1V(s_2) = 0 + 0.5 \cdot V(s_3) = 0.5(2) = 1. V(s1)=0+0.5V(s2)=0.5(1)=0.5V(s_1) = 0 + 0.5 \cdot V(s_2) = 0.5(1) = 0.5. V(s1)=0.5, V(s2)=1, V(s3)=2\boxed{V(s_1)=0.5,\ V(s_2)=1,\ V(s_3)=2}. Why: deterministic policy → Bellman expectation collapses to a chain; solve the self-loop first as a geometric fixed point.


Problem 5Subtopic 5.2.6 (REINFORCE). Δθ=αGθlnπ=0.1×4×3=1.2\Delta\theta = \alpha\, G\, \nabla_\theta \ln \pi = 0.1 \times 4 \times 3 = \boxed{1.2}. Log used because lnπ=π/π\nabla \ln \pi = \nabla\pi / \pi; the log-derivative (score function) trick turns the policy-gradient expectation into a sample-averageable form E[Glnπ]\mathbb{E}[G\nabla\ln\pi], and dividing by π\pi correctly weights actions inversely to their sampling probability.


Problem 6Subtopic 5.2.4 (Double DQN). Vanilla DQN overestimates values because the same network both selects and evaluates the max action, and max\max over noisy estimates is biased upward. Vanilla target: y=r+γmaxaQθ(s,a)y = r + \gamma \max_{a'} Q_{\theta^-}(s',a'). Double DQN target: y=r+γQθ ⁣(s, argmaxaQθ(s,a))y = r + \gamma\, Q_{\theta^-}\!\big(s',\ \arg\max_{a'} Q_{\theta}(s',a')\big) — the online net θ\theta selects the action, the target net θ\theta^- evaluates it, decoupling selection from evaluation to reduce overestimation bias.


Problem 7Subtopic 5.1.10 (Monte Carlo), contrasted with 5.1.11 (TD). (a) V(s)=(6+2+10)/3=18/3=6V(s) = (6 + 2 + 10)/3 = 18/3 = \boxed{6}. (b) V6+0.1(46)=60.2=5.8V \leftarrow 6 + 0.1(4 - 6) = 6 - 0.2 = \boxed{5.8}. (c) TD(0) bootstraps, so it can update online per-step (no need to wait for episode end) and typically has lower variance than MC (which needs full returns).


Problem 8Subtopics 5.1.12, 5.1.13, 5.2.8, 5.2.11. (i) SARSA → (B) on-policy TD control. (ii) Q-learning → (C) off-policy, max over next values. (iii) A2C → (D) on-policy actor-critic with advantage. (iv) SAC → (A) off-policy with entropy-augmented objective.


Problem 9Subtopics 5.2.2 (replay) + 5.2.3 (target networks). (a) Experience replay breaks the temporal correlation between consecutive samples (and reuses data), giving more i.i.d.-like minibatches and better sample efficiency. (b) The target network provides a stationary/fixed target for a number of steps, preventing the moving-target instability where the network chases its own rapidly changing predictions. Update cadence: target net is updated to the online net's weights periodically (every CC steps, e.g. every few thousand) or via slow Polyak averaging.


Problem 10Subtopic 5.2.9 (PPO). Since A^t=+2>0\hat{A}_t = +2 > 0, clip caps the ratio at 1+ϵ=1.21+\epsilon = 1.2. Unclipped: rtA^t=1.4×2=2.8r_t \hat{A}_t = 1.4 \times 2 = 2.8. Clipped: clip(1.4,0.8,1.2)2=1.2×2=2.4\text{clip}(1.4, 0.8, 1.2)\cdot 2 = 1.2 \times 2 = 2.4. min(2.8,2.4)=2.4\min(2.8, 2.4) = \boxed{2.4}. Why: the clip removes incentive to push the ratio beyond 1.21.2 for a positive advantage, limiting destructive policy updates.

[
  {"claim":"P1 return G0 = 5.51",
   "code":"G = 2 + 0.9*3 + 0.9**2*1\nresult = abs(G - 5.51) < 1e-9"},
  {"claim":"P2 Q-learning update gives 13.5 and SARSA gives 11.5",
   "code":"ql = 10 + 0.5*((5 + 1.0*12) - 10)\nsa = 10 + 0.5*((5 + 1.0*8) - 10)\nresult = (ql == 13.5) and (sa == 11.5)"},
  {"claim":"P10 PPO clipped term equals 2.4",
   "code":"r=1.4; A=2; eps=0.2\nclip = min(max(r,1-eps),1+eps)\nval = min(r*A, clip*A)\nresult = abs(val - 2.4) < 1e-9"}
]