Skip to content
Lambdian

SARSA: on-policy TD control

Learn action values for the policy you are actually following, exploration included.

Animated explanation

The idea

To control (choose actions), we learn action values Q(s,a)Q(s,a) instead of V(s)V(s). SARSA applies the TD idea to them: after taking action aa in ss, receiving rr, landing in s′s' and choosing the next action a′a', nudge Q(s,a)Q(s,a) towards r+γQ(s′,a′)r + \gamma Q(s', a'). The name lists the five things the update uses: State, Action, Reward, State, Action.

The policy is ε-greedy in the current QQ, so as QQ improves the policy improves. Because SARSA's target uses the action the agent will actually take, including exploratory ones, it learns the value of its own behaviour. Near a cliff, where a random step is disastrous, that makes SARSA cautious.

On-policy versus off-policy

On-policy methods (SARSA) evaluate and improve the policy that generates the data. Off-policy methods (Q-learning) learn about a different target policy, typically the greedy one, from exploratory data. On-policy learning is safer while learning; off-policy learning can reuse old data and learns the optimal policy directly. With ε decayed to zero, SARSA also converges to the optimal policy.

Expected SARSA

Replacing Q(s′,a′)Q(s',a') with its expectation under the policy, ∑a′π(a′∣s′)Q(s′,a′)\sum_{a'}\pi(a'\mid s')Q(s',a'), removes the randomness of sampling a′a'. Expected SARSA usually performs at least as well as both SARSA and Q-learning, and it becomes Q-learning when the target policy is greedy.

The algorithm, step by step

  1. 1
    Initialise Q(s, a) = 0. For each episode, start in s and choose a ε-greedily from Q.
  2. 2
    Take a; observe r, s′. Choose a′ ε-greedily from Q(s′, ·).
  3. 3
    Update Q(s,a)←Q(s,a)+α[r+γQ(s′,a′)−Q(s,a)]Q(s,a) \leftarrow Q(s,a) + \alpha[r + \gamma Q(s',a') - Q(s,a)] (use r alone if s′ is terminal).
  4. 4
    Set s ← s′, a ← a′; repeat until the episode ends.

The maths

SARSA update

Q(s,a)←Q(s,a)+α[r+γQ(s′,a′)−Q(s,a)]Q(s,a) \leftarrow Q(s,a) + \alpha[r + \gamma Q(s',a') - Q(s,a)]

Expected SARSA target

r+γ∑a′π(a′∣s′) Q(s′,a′)r + \gamma\sum_{a'}\pi(a'\mid s')\,Q(s',a')

ε-greedy probabilities

π(a∣s)=ε∣A∣+(1−ε)1[a=arg⁡max⁡a′Q(s,a′)]\pi(a\mid s) = \frac{\varepsilon}{|A|} + (1-\varepsilon)\mathbb{1}[a = \arg\max_{a'}Q(s,a')]

Strengths and weaknesses

Strengths

  • Simple, online, model-free control
  • Safer behaviour during learning (accounts for exploration)
  • Converges with decaying ε

Weaknesses

  • Learns the ε-greedy policy's values, not the optimal ones, unless ε decays
  • Cannot learn from other policies' data
  • Tabular version needs small discrete spaces

Reach for it when…

Online learning where mistakes during training are costly (robots, real systems), or as a stable on-policy baseline.

Check your understanding

Question 1

Q1Which quantity does SARSA use in its target?

Question 2

Q2On the cliff, why does SARSA take a longer, safer path than Q-learning?

Question 3

Q3Expected SARSA replaces Q(s′, a′) with…

Lambdian Lab

SARSA on the cliff

A 4 × 8 cliff world is provided. Implement:

  • eps_greedy(q_row, eps, rng): random action with probability eps, otherwise argmax (lowest index on ties).
  • sarsa(episodes, alpha, eps, gamma, seed) → (Q, episode_returns); Q has shape (32, 4).
  • expected_sarsa(...) with the same interface.
  • greedy_path(Q): list of states visited greedily from START (stop at the goal, after 50 steps, or on falling).
Open the labPython + NumPy runs in your browser. Sign-in keeps your code and attempts.