PIXELBANKv9.1.0
Menu

SARSA, Q-Learning and Expected SARSA Targets

Problem Statement

Implement the three action-value control updates side by side. They differ only in how the next state is valued.

Background

Every one of these has the same skeleton — move Q(s,a) a fraction alpha toward a target — and the entire on-policy/off-policy distinction lives in one term.

SARSA uses the action the behaviour policy actually took next:

Q(St,At)←Q(St,At)+α[Rt+1+γQ(St+1,At+1)−Q(St,At)]Q(S_t,A_t) \leftarrow Q(S_t,A_t) + \alpha\big[R_{t+1} + \gamma Q(S_{t+1},A_{t+1}) - Q(S_t,A_t)\big]

It is on-policy: it learns the value of the policy you are running, exploration mistakes included. That is why SARSA learns the "safe" path near a cliff — it knows an epsilon-greedy agent will occasionally fall off.

Q-learning ignores what happened next and uses the greedy value:

Q(St,At)←Q(St,At)+α[Rt+1+γmax⁡a′Q(St+1,a′)−Q(St,At)]Q(S_t,A_t) \leftarrow Q(S_t,A_t) + \alpha\big[R_{t+1} + \gamma \max_{a'} Q(S_{t+1},a') - Q(S_t,A_t)\big]

It is off-policy: it learns the optimal policy's values while behaving with exploration. The price is maximisation bias — a max over noisy estimates is systematically optimistic.

Expected SARSA replaces the sample or the max with the exact expectation under the target policy:

Q(St,At)←Q(St,At)+α[Rt+1+γ∑a′π(a′∣St+1)Q(St+1,a′)−Q(St,At)]Q(S_t,A_t) \leftarrow Q(S_t,A_t) + \alpha\Big[R_{t+1} + \gamma \sum_{a'} \pi(a' \mid S_{t+1}) Q(S_{t+1},a') - Q(S_t,A_t)\Big]

Removing the sampling of At+1A_{t+1} removes its variance for the cost of one extra sum, which is usually free. With an epsilon-greedy π\pi and epsilon = 0 it is Q-learning; with larger epsilon it drifts toward the on-policy answer.

Your Task

Implement all three. Each returns the new scalar value of Q[s][a]; do not mutate Q.

def sarsa_update(Q, s, a, r, s2, a2, alpha, gamma):
    ...

def q_learning_update(Q, s, a, r, s2, alpha, gamma):
    ...

def expected_sarsa_update(Q, s, a, r, s2, alpha, gamma, epsilon):
    ...

expected_sarsa_update weights the successor action values by an **epsilon-greedy distribution over **Q[s2]****, splitting the greedy probability 1 - epsilon evenly across all tied maxima.

Input / Output Format

Q is a nested list of floats. Each function returns a single float, rounded to 4 decimals by the grader.

Sample

Q = [[0.0, 0.0], [2.0, 6.0]]
print(round(sarsa_update(Q, 0, 0, 1.0, 1, 0, 0.5, 0.9), 4))
print(round(q_learning_update(Q, 0, 0, 1.0, 1, 0.5, 0.9), 4))
print(round(expected_sarsa_update(Q, 0, 0, 1.0, 1, 0.5, 0.9, 0.2), 4))

Output:

1.4
3.2
3.02

All three start from Q[0][0] = 0.0 and move half way (alpha = 0.5) toward their target. SARSA bootstraps off the action actually taken, Q[1][0] = 2.0, for a target of 1.0 + 0.9*2.0 = 2.8. Q-learning bootstraps off max(Q[1]) = 6.0, for a target of 6.4. Expected SARSA bootstraps off the epsilon-greedy expectation 0.12.0 + 0.96.0 = 5.6, for a target of 1.0 + 0.9*5.6 = 6.04.

Example:

Input:
Q = [[0.0, 0.0], [2.0, 6.0]]; sarsa_update(Q, 0, 0, 1.0, 1, 0, 0.5, 0.9), q_learning_update(Q, 0, 0, 1.0, 1, 0.5, 0.9), expected_sarsa_update(Q, 0, 0, 1.0, 1, 0.5, 0.9, 0.2)
Output:
1.4
3.2
3.02
Reasoning:

All three move Q(0,0)=0 half way toward their target because alpha=0.5. SARSA uses the action actually taken next, a2=0, so its target is 1.0 + 0.92.0 = 2.8 and the result is 1.4. Q-learning uses max(Q[1]) = 6.0 for a target of 6.4, giving 3.2. Expected SARSA weights the successor values by the epsilon-greedy distribution: with epsilon=0.2 over 2 actions the greedy action gets 0.8 + 0.1 = 0.9 and the other gets 0.1, so the expectation is 0.12.0 + 0.96.0 = 5.6, the target is 1.0 + 0.95.6 = 6.04, and the result is 3.02.

Constraints:

  • 1 <= n_states <= 500, 1 <= n_actions <= 50
  • 0.0 <= alpha <= 1.0, 0.0 <= gamma <= 1.0, 0.0 <= epsilon <= 1.0
  • Q must not be modified — return the new scalar only.
  • Ties for the max in the epsilon-greedy distribution split 1 - epsilon evenly.
  • Do not round inside the functions.
solution.py

Test Results

0/0
Run code to see test results.
SARSA, Q-Learning and Expected SARSA Targets - Medium | PixelBank