Foundations — SARSA algorithm
Before you can read a single line of the SARSA update, you need to own a small pile of symbols. This page builds every one of them from nothing, in an order where each rests on the previous. Nothing is used before it is drawn.
1. The agent and the world (the picture everything sits on)
Look at the grid figure below. Each cell is a state. The little robot sits in one cell. The arrows leaving it are the actions it could pick. When it moves, a number pops out — that is the reward.
Why the topic needs this. SARSA is entirely a bookkeeping system over "state → action → reward → new state." If you cannot see that loop as a picture, every later symbol is floating in air.
2. Symbols for state and action: , , and the time subscript
Think of as frame numbers in a flip-book. In frame the robot is in state and picks action . Then the world advances the flip-book to frame , where the robot is now in state .
Why the topic needs this. The whole experience tuple is just these letters strung along the clock. Reading it fluently is reading SARSA.
3. Reward over time:
Picture it as: you push a button at tick , and the coin drops at tick . The coin belongs to the transition, so we label it with the arriving moment.
Why the topic needs this. SARSA's whole job is to predict the total future reward. It starts from the one reward it can see right now, , and folds in a guess about everything after.
4. The Q-value:
Picture a giant spreadsheet: rows are states, columns are actions, each cell holds one number. See the figure.
The letter just names that spreadsheet. reads off the one cell for "the state I'm in now, the action I just chose."
Why the topic needs this. SARSA is a rule for editing one cell of this table at a time. Everything else is machinery to decide the new number.
5. Total future reward and the discount factor
Rewards keep arriving forever. Adding infinitely many numbers can blow up, and a reward far in the future is less certain than one right now. So we shrink each future reward before adding it.
- If : only the immediate reward counts (the robot is totally short-sighted).
- If close to : rewards far away still matter a lot (far-sighted).
- : no shrinking at all — only safe when episodes end (like the cliff grid).
Why the topic needs this. The SARSA target has exactly this shape: one reward now, plus a discounted estimate of everything later. The is the shrink knob.
6. The policy and -greedy exploration
A greedy policy always picks the action with the biggest . But if you only ever pick your current favourite, you never try alternatives that might be better. So we add a pinch of randomness.
Why the topic needs this. This is the beating heart of the on-policy idea. Because the robot sometimes moves randomly, it might stumble off a cliff. SARSA learns using the moves it actually makes (random ones included), so it learns to stay away from cliffs. That is the entire "safety" story of the parent note.
7. Expectation:
If flipping a coin pays \1$0\mathbb{E}[\text{pay}] = 0.50.5$ on one flip — it's the long-run average.
Why the topic needs this. The world (and the -greedy policy) is random, so a single move's outcome is noisy. The Bellman equation defines as an expectation — the true long-run average. SARSA can't compute that average directly, so it samples one outcome and nudges toward it repeatedly; over many nudges the noise averages out.
8. The learning rate
Picture walking toward a target: is the fraction of the remaining distance you cover each step. Small = slow but steady (averages out noise); big = fast but jumpy.
Why the topic needs this. In the update , the decides how much of the "surprise" actually changes the table cell.
9. The TD error — the surprise
- : reality beat the guess → nudge the cell up.
- : reality was worse → nudge down.
- : perfect guess → leave it alone.
Why the topic needs this. Every SARSA update is literally "old value plus times surprise." Once you own , the update rule reads itself:
10. The operator (so you can tell SARSA from Q-learning)
Why the topic needs this. Q-learning uses (the best possible next value). SARSA instead uses (the value of the action it actually takes). Knowing what does lets you see exactly the one symbol that separates the two algorithms.
Prerequisite map
Equipment checklist
State what each symbol means without peeking, then reveal:
What does mean and why the subscript?
Why is the reward written instead of ?
In one sentence, what is ?
What does the discount factor control, and what does do?
Explain -greedy in two chances.
Why does the Q-value use an expectation ?
What is and what does its sign tell you?
What role does play in the update?
Which single symbol separates SARSA from Q-learning?
Return to the parent when these are solid: SARSA algorithm.