5.1.12 · D1Reinforcement Learning Foundations

Foundations — SARSA algorithm

1,876 words9 min readBack to topic

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

States and actions s a

Time counter t

Reward R at t plus 1

Q table Q of s a

Discount gamma

Policy pi and epsilon greedy

Expectation E

TD error delta

Learning rate alpha

SARSA update rule

max over a


Equipment checklist

State what each symbol means without peeking, then reveal:

What does mean and why the subscript?
The specific state that actually occurred at clock tick ; the subscript pins it to a moment in the episode.
Why is the reward written instead of ?
The reward only arrives after the world responds to your action at time , i.e. at the next tick.
In one sentence, what is ?
The expected total (discounted) future reward if you take action in state , stored as one cell of a state-by-action table.
What does the discount factor control, and what does do?
How much future rewards are shrunk before adding them; makes the agent care only about the immediate reward.
Explain -greedy in two chances.
With probability take the best- action (exploit); with probability take a random action (explore).
Why does the Q-value use an expectation ?
Outcomes are random, so is defined as the long-run average return under policy ; SARSA approximates it by sampling and nudging.
What is and what does its sign tell you?
The TD error (reality minus guess); positive means nudge up, negative means nudge down.
What role does play in the update?
It sets how big a fraction of the surprise actually changes the stored value.
Which single symbol separates SARSA from Q-learning?
SARSA uses (actual next action); Q-learning uses (best possible action).

Return to the parent when these are solid: SARSA algorithm.