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 instead of . SARSA applies the TD idea to them: after taking action in , receiving , landing in and choosing the next action , nudge towards . The name lists the five things the update uses: State, Action, Reward, State, Action.
The policy is ε-greedy in the current , so as 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 with its expectation under the policy, , removes the randomness of sampling . 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
- 1Initialise Q(s, a) = 0. For each episode, start in s and choose a ε-greedily from Q.
- 2Take a; observe r, s′. Choose a′ ε-greedily from Q(s′, ·).
- 3Update (use r alone if s′ is terminal).
- 4Set s ← s′, a ← a′; repeat until the episode ends.
The maths
SARSA update
Expected SARSA target
ε-greedy probabilities
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…
Check your understanding
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).