Level 4 — ApplicationReinforcement Learning Foundations

Reinforcement Learning Foundations

60 minutes60 marksprintable — key stays hidden on paper

Level: 4 — Application (novel problems, no hints) Time limit: 60 minutes Total marks: 60

Answer all questions. Show full working. Use γ\gamma for the discount factor. State any assumptions.


Question 1 — Returns and the Discount Factor (10 marks)

A robot receives the infinite reward sequence starting at t=0t=0:

r0=2,r1=2,r2=2, (constant reward 2 forever)r_0=2,\quad r_1=2,\quad r_2=2,\ \dots \quad \text{(constant reward } 2 \text{ forever)}

except that at exactly one step t=3t=3 it receives a one-time penalty making r3=6r_3 = -6 (all other rewards are 22).

(a) Write the general formula for the discounted return G0G_0 and compute it symbolically as a function of γ\gamma. (6)

(b) Evaluate G0G_0 for γ=0.9\gamma = 0.9. (2)

(c) A colleague claims "with γ=0\gamma=0 the agent is myopic." State G0G_0 for γ=0\gamma=0 and explain in one sentence what behaviour this induces. (2)


Question 2 — Modelling as an MDP (12 marks)

A vending machine restocks a single product. Each morning the stock level is s{0,1,2}s\in\{0,1,2\} units. The manager chooses action a{order, wait}a\in\{\text{order},\ \text{wait}\}. "Order" adds 1 unit (capped at 2) with reward 1-1 (cost). During the day, demand consumes 1 unit with probability 0.70.7 and 0 units with probability 0.30.3; each unit sold gives reward +3+3. A stockout (demand arrives at stock 0) gives reward 2-2.

(a) Identify the state set, action set, and the discount interpretation appropriate for an ongoing daily process. (3)

(b) For state s=1s=1, action wait\text{wait}, write the full transition-and-reward distribution: list each next state ss', its probability, and the immediate reward. (5)

(c) Explain why the Markov property holds for this formulation, and give one realistic feature of the real machine whose inclusion would break it. (4)


Question 3 — Bellman Evaluation by Hand (14 marks)

Consider a 2-state MDP with states A,BA, B and a fixed policy π\pi. Under π\pi:

  • From AA: reward +1+1, then go to BB with prob 11.
  • From BB: reward +4+4, then go to AA with prob 0.50.5 and stay in BB with prob 0.50.5.

Use γ=0.5\gamma = 0.5.

(a) Write the two Bellman expectation equations for Vπ(A)V^\pi(A) and Vπ(B)V^\pi(B). (4)

(b) Solve the linear system exactly for Vπ(A)V^\pi(A) and Vπ(B)V^\pi(B). (8)

(c) Using your results, compute the action-value-style one-step consistency check: verify that Vπ(A)=1+γVπ(B)V^\pi(A) = 1 + \gamma V^\pi(B). (2)


Question 4 — TD, SARSA vs Q-learning Update (14 marks)

An agent uses learning rate α=0.5\alpha = 0.5, γ=0.9\gamma = 0.9. Current estimates:

Q(s,a1)=2,Q(s,a2)=5,Q(s,a1)=10,Q(s,a2)=6.Q(s,a_1)=2,\quad Q(s,a_2)=5,\quad Q(s',a_1)=10,\quad Q(s',a_2)=6.

The agent is in ss, takes a1a_1, receives reward r=3r=3, and lands in ss'. Its behaviour policy is ϵ\epsilon-greedy; on the next step it happens to select a2a_2 in ss'.

(a) Compute the updated Q(s,a1)Q(s,a_1) under the SARSA rule. Show the TD target and TD error. (5)

(b) Compute the updated Q(s,a1)Q(s,a_1) under the Q-learning rule. Show the TD target and TD error. (5)

(c) Explain precisely why the two results differ here, and state which one is on-policy. (4)


Question 5 — Exploration and Policy Design (10 marks)

An agent uses ϵ\epsilon-greedy action selection over A=4|\mathcal{A}|=4 actions.

(a) With ϵ=0.2\epsilon = 0.2, and a unique greedy action, give the probability of selecting the greedy action and the probability of selecting each specific non-greedy action. (4)

(b) The designer uses a decay schedule ϵk=11+k\epsilon_k = \tfrac{1}{1+k} for episode k=0,1,2,k=0,1,2,\dots. Compute ϵ0,ϵ1,ϵ9\epsilon_0, \epsilon_1, \epsilon_9, and explain in one sentence why decaying ϵ\epsilon supports convergence to a near-optimal policy. (4)

(c) State one situation where pure greedy (ϵ=0\epsilon=0) from the start would fail, referencing the exploration–exploitation tradeoff. (2)

Answer keyMark scheme & solutions

Question 1 (10 marks)

(a) Definition G0=t=0γtrtG_0=\sum_{t=0}^\infty \gamma^t r_t. (1) All rewards are 22 except r3=6r_3=-6, i.e. the actual value differs from an all-2 stream by (62)=8(-6-2)=-8 at t=3t=3.

G0=t=0γt2all-2 stream+γ3(8)G_0 = \underbrace{\sum_{t=0}^\infty \gamma^t\cdot 2}_{\text{all-2 stream}} + \gamma^3(-8)

The all-2 geometric series =21γ=\dfrac{2}{1-\gamma} (converges for γ<1|\gamma|<1). (3)

G0=21γ8γ3\boxed{G_0 = \frac{2}{1-\gamma} - 8\gamma^3} (2) (correct assembly)

(b) γ=0.9\gamma=0.9: 20.18(0.729)=205.832=14.168\dfrac{2}{0.1} - 8(0.729) = 20 - 5.832 = 14.168. (2)

(c) γ=0\gamma=0: only r0r_0 counts, G0=2G_0 = 2. The agent maximises immediate reward only, ignoring all future consequences. (2)


Question 2 (12 marks)

(a) States S={0,1,2}\mathcal S=\{0,1,2\}; actions A={order,wait}\mathcal A=\{\text{order},\text{wait}\}. For an ongoing (continuing, non-terminating) process a discount factor 0<γ<10<\gamma<1 is appropriate to keep returns finite and weight nearer days more. (3)

(b) State s=1s=1, action wait (no ordering cost). Demand =1=1 w.p. 0.70.7 → sells one unit (reward +3+3), stock 0\to 0. Demand =0=0 w.p. 0.30.3 → no sale (reward 00), stock stays 11.

ss' prob reward
00 0.70.7 +3+3
11 0.30.3 00

No stockout possible since stock \ge demand. (5) (2 for probabilities, 2 for rewards, 1 for correct next states)

(c) Markov property holds because next state and reward depend only on current stock and action, not on the history of previous days' demand. (2) A realistic breaker: demand that depends on recent sales history / day-of-week seasonality (a hidden trend), making the true state incomplete. (2)


Question 3 (14 marks)

(a) Bellman expectation equations, γ=0.5\gamma=0.5: (4)

V(A)=1+γV(B)V(A) = 1 + \gamma\,V(B) V(B)=4+γ(0.5V(A)+0.5V(B))V(B) = 4 + \gamma\big(0.5\,V(A) + 0.5\,V(B)\big)

(b) Substitute γ=0.5\gamma=0.5:

  • V(A)=1+0.5V(B)V(A) = 1 + 0.5\,V(B)
  • V(B)=4+0.5(0.5V(A)+0.5V(B))=4+0.25V(A)+0.25V(B)V(B) = 4 + 0.5(0.5 V(A) + 0.5 V(B)) = 4 + 0.25 V(A) + 0.25 V(B)

From second: 0.75V(B)=4+0.25V(A)V(B)=163+13V(A)0.75 V(B) = 4 + 0.25 V(A) \Rightarrow V(B) = \tfrac{16}{3} + \tfrac13 V(A). (3)

Substitute into first: V(A)=1+0.5(163+13V(A))=1+83+16V(A).V(A) = 1 + 0.5\left(\tfrac{16}{3} + \tfrac13 V(A)\right) = 1 + \tfrac{8}{3} + \tfrac16 V(A). V(A)16V(A)=11356V(A)=113.V(A) - \tfrac16 V(A) = \tfrac{11}{3} \Rightarrow \tfrac56 V(A) = \tfrac{11}{3}. V(A)=11365=6615=225=4.4.V(A) = \tfrac{11}{3}\cdot\tfrac65 = \tfrac{66}{15} = \tfrac{22}{5} = 4.4. (3)

Then V(B)=163+13(4.4)=5.3333+1.4667=6.8V(B) = \tfrac{16}{3} + \tfrac13(4.4) = 5.3333 + 1.4667 = 6.8. (2)

V(A)=4.4,V(B)=6.8\boxed{V(A)=4.4,\quad V(B)=6.8}

(c) Check: 1+0.5(6.8)=1+3.4=4.4=V(A)1 + 0.5(6.8) = 1 + 3.4 = 4.4 = V(A). ✓ (2)


Question 4 (14 marks)

α=0.5\alpha=0.5, γ=0.9\gamma=0.9, took a1a_1 in ss, r=3r=3, next action chosen a2a_2 in ss'.

(a) SARSA (uses the actually-chosen next action a2a_2):

  • TD target =r+γQ(s,a2)=3+0.9(6)=3+5.4=8.4= r + \gamma Q(s',a_2) = 3 + 0.9(6) = 3 + 5.4 = 8.4. (2)
  • TD error δ=8.4Q(s,a1)=8.42=6.4\delta = 8.4 - Q(s,a_1) = 8.4 - 2 = 6.4. (1)
  • Update: Q(s,a1)2+0.5(6.4)=2+3.2=5.2Q(s,a_1) \leftarrow 2 + 0.5(6.4) = 2 + 3.2 = 5.2. (2)

(b) Q-learning (uses maxaQ(s,a)=max(10,6)=10\max_{a'}Q(s',a')=\max(10,6)=10):

  • TD target =3+0.9(10)=3+9=12= 3 + 0.9(10) = 3 + 9 = 12. (2)
  • TD error δ=122=10\delta = 12 - 2 = 10. (1)
  • Update: Q(s,a1)2+0.5(10)=2+5=7Q(s,a_1) \leftarrow 2 + 0.5(10) = 2 + 5 = 7. (2)

(c) They differ because SARSA bootstraps on the action actually taken (a2a_2, value 6), while Q-learning bootstraps on the greedy/max action (a1a_1, value 10). Since a2a_2 was not greedy in ss', the targets differ (8.48.4 vs 1212). SARSA is on-policy (it evaluates the behaviour policy); Q-learning is off-policy. (4)


Question 5 (10 marks)

(a) Greedy action prob =1ϵ+ϵ/A=0.8+0.2/4=0.8+0.05=0.85= 1-\epsilon + \epsilon/|\mathcal A| = 0.8 + 0.2/4 = 0.8 + 0.05 = 0.85. (2) Each specific non-greedy action prob =ϵ/A=0.2/4=0.05= \epsilon/|\mathcal A| = 0.2/4 = 0.05. (2) (Check: 0.85+3(0.05)=1.0.85 + 3(0.05) = 1.)

(b) ϵ0=1/(1+0)=1\epsilon_0 = 1/(1+0)=1; ϵ1=1/2=0.5\epsilon_1 = 1/2 = 0.5; ϵ9=1/10=0.1\epsilon_9 = 1/10 = 0.1. (3) Early large ϵ\epsilon ensures broad exploration of all state-actions (needed for value estimates to be accurate), while shrinking ϵ\epsilon lets the agent increasingly exploit its improved estimates, approaching the greedy optimal policy. (1)

(c) With ϵ=0\epsilon=0 from the start, the agent commits to whatever action first looked best (possibly by chance/initialisation) and never tries alternatives, so it can get stuck in a suboptimal action having never explored the truly best one — pure exploitation with no exploration. (2)


[
  {"claim":"Q1b return equals 14.168 for gamma=0.9","code":"g=Rational(9,10); G=2/(1-g)-8*g**3; result=(G==Rational(14168,1000))"},
  {"claim":"Q3 solution V(A)=4.4 V(B)=6.8 satisfies Bellman eqs with gamma=0.5","code":"VA,VB=symbols('VA VB'); sol=solve([VA-(1+Rational(1,2)*VB), VB-(4+Rational(1,2)*(Rational(1,2)*VA+Rational(1,2)*VB))],[VA,VB]); result=(sol[VA]==Rational(22,5) and sol[VB]==Rational(34,5))"},
  {"claim":"Q4a SARSA update gives 5.2","code":"a=Rational(1,2); g=Rational(9,10); target=3+g*6; q=2+a*(target-2); result=(q==Rational(52,10))"},
  {"claim":"Q4b Q-learning update gives 7","code":"a=Rational(1,2); g=Rational(9,10); target=3+g*Max(10,6); q=2+a*(target-2); result=(q==7)"},
  {"claim":"Q5a epsilon-greedy probs sum to 1 with eps=0.2,4 actions","code":"eps=Rational(2,10); n=4; pg=1-eps+eps/n; po=eps/n; result=(pg==Rational(85,100) and po==Rational(5,100) and pg+3*po==1)"}
]