Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Chapter 14 — Lab: Reinforcement Learning

Open in Colab

Lab Overview

ExerciseTopicPoints
1Discounted returns10
2Value iteration on a model20
3ε-greedy action selection10
4The Q-learning and SARSA updates20
5Learning to cross the cliff20
6The REINFORCE loss20
ChallengeUCB for a bandit+10

Complete the code where you see # TODO, then run the check cell under it.


Exercise 1: Discounted Returns (10 points)

Complete returns_to_go(rewards, gamma): for a list of rewards r1,…,rTr_1, \dots, r_T of one episode, return the array of returns Gt=rt+1+γrt+2+…G_t = r_{t+1} + \gamma r_{t+2} + \dots from every time step. Work backwards: Gt=rt+1+γGt+1G_t = r_{t+1} + \gamma G_{t+1}.


Exercise 2: Value Iteration (20 points)

A model is given as a dictionary P[s][a]: a list of (probability, next_state, reward, done) tuples (the format used by the Gymnasium library). Complete value_iteration(P, gamma, theta): repeat V(s)←max⁡a∑p [r+γV(s′)]V(s) \leftarrow \max_a \sum p\,[r + \gamma V(s')] (with V(s′)=0V(s') = 0 when done) over all states until the largest change is below theta; then return V and the greedy policy (a dict state → action).


Exercise 3: ε-Greedy (10 points)

Complete epsilon_greedy(q_values, eps, rng): with probability eps return a uniformly random action index, otherwise the index of the largest value (use rng.random() and rng.integers).


Exercise 4: The Q-Learning and SARSA Updates (20 points)

Q is a NumPy array indexed Q[state, action]. Complete both updates, in place, and return the new value. When done is true, the target is just r.

  • Q-learning: target r+γmax⁡a′Q(s′,a′)r + \gamma \max_{a'} Q(s', a')

  • SARSA: target r+γQ(s′,a′)r + \gamma Q(s', a') for the action a′a' actually chosen next


Exercise 5: Crossing the Cliff (20 points)

The cliff of the chapter: a 4×12 grid, start bottom-left, goal bottom-right, the cells between them are the cliff (−100 and back to start), −1 per step. Complete train(method, episodes, alpha, eps, seed) (γ = 1) using your functions from Exercises 3 and 4. For SARSA, choose the next action before the update and then take it. Return the Q table.

Question. In one or two sentences: why does each method choose its path, and which would you use to train a real robot?

Your answer:


Exercise 6: The REINFORCE Loss (20 points)

Complete reinforce_loss(log_probs, rewards, gamma): compute the returns-to-go of the episode (Exercise 1), normalise them (subtract their mean, divide by their standard deviation plus 1e-8), and return −∑tG^tlog⁡π(at∣st)-\sum_t \hat G_t \log\pi(a_t \mid s_t) as a PyTorch scalar. log_probs is a 1-D tensor that requires gradients; the minus sign turns gradient descent on the loss into ascent on the return.

Question. After normalisation, the early steps of a long episode get a positive weight and the last steps a negative one. What does that mean for the actions taken just before the pole fell?

Your answer:


Challenge: UCB (+10 points)

Complete ucb_action(Q, N, t, c): if some action has never been tried (N[a] == 0), return the first such action; otherwise return arg⁡max⁡a[Q(a)+cln⁡t/N(a)]\arg\max_a \big[Q(a) + c\sqrt{\ln t / N(a)}\big]. Then run it on 300 random 10-armed bandits of 1000 pulls (code given) and compare its average reward with ε-greedy.


Lab Report

Submit this notebook with every check cell showing ✅ and your answers to the two questions.