5.1.13 · D1Reinforcement Learning Foundations

Foundations — Q-learning algorithm

2,534 words12 min readBack to topic

Before you can read the parent note Q-learning algorithm, every squiggle in it must feel obvious. This page takes each symbol it uses, gives it a plain-words meaning, ties it to a picture, and says why the topic can't live without it. We go in build order: nothing appears before the thing it rests on.


0. The world we are in: an agent poking at an environment

Everything starts with a loop between two characters.

Figure — Q-learning algorithm

This loop is the stage. Every symbol below is just a name for one arrow or one box in that picture. This picture is formalised by Markov Decision Processes — but you don't need that page yet; the picture is enough.


1. State —

  • Picture: one dot on the grid in the figure above — the agent's current spot.
  • Why we need it: the whole point is that the right move depends on where you are. Going "right" is great near the goal and terrible near a wall. Without a name for "where you are", you can't say "the best move here". is that name.

The set of all possible states is written (a capital letter for the whole collection). "" is read " is one of the states in the set ". The symbol just means "is a member of".

The special state where the episode ends (you reached the goal, you died, time ran out) is called a terminal state. Nothing happens after it.


2. Action —

  • Picture: one of the four amber arrows leaving the agent's dot below.
  • Why we need it: the agent's only power over the world is which action it picks. Learning "which action is best in each state" is the whole job.

The set of all actions is . "" (read "a-prime") is just a different action — we use the little tick mark whenever we mean "some other action, usually in the next situation". Get comfortable with the prime mark: throughout the topic, plain letters mean "now", primed letters (, ) mean "one step later".

Figure — Q-learning algorithm

3. Reward — and the reward idea

  • Picture: the amber number that pops up on the arrow the agent just followed.
  • Why we need it: reward is the only signal telling the agent what we want. No maps, no instructions — just "here's how many points that earned". Everything Q-learning learns, it squeezes out of these numbers.

The parent also writes — the "reward function", the rule that decides the reward. The lowercase is one observed number from experience; the capital is the underlying rule that generated it. Q-learning is called model-free precisely because it never learns or the transition rule — it only ever uses the sampled it actually saw.


4. The transition — the world's hidden dice

  • Picture: dashed arrows from your dot to the possible next dots, each with a probability tag; sometimes "right" slips and you go up instead.
  • The bar is read "given": " given and ". It's the notation for "this probability depends on the situation and the move".
  • Why we mention it: so we can say the magic: Q-learning never touches . It's hidden dice the agent never gets to see. Instead of computing averages over , Q-learning just plays and watches. Remember this — it's the reason Step 2 of the parent's derivation ("turn expectation into sampling") exists.

5. Return, and why the future is discounted —

The agent doesn't care about one reward; it cares about the total it will collect from now on. Add up all future rewards:

That sum could run forever and blow up to infinity. Also, a reward now should count for more than the same reward in a thousand years. Both problems are fixed by one knob.

Figure — Q-learning algorithm
  • : only the immediate reward matters (short-sighted).
  • : distant rewards matter almost as much as now (far-sighted).
  • Why it must be for the theory: the same shrinking that tames the sum is exactly what makes the update "contract" toward one answer (see the convergence theorem in the parent). Hold onto that.

6. The Q-value — , the star of the show

Now we can name the thing Q-learning actually stores.

  • Picture: a table. One row per state, one column per action, a number in every cell. That grid of numbers is the agent's brain.
  • The letter Q stands for "Quality" of a state-action pair. It's not a mysterious symbol — it's a lookup table.
  • Why we need it: if you know , choosing well is trivial — in each state, read the row and pick the biggest number. So learning the whole problem reduces to filling in this table correctly.
Figure — Q-learning algorithm

The parent writes (with a star). The star means the optimal values — the true, correct table you'd have if you played perfectly forever. Plain is the agent's current guess, which starts wrong and creeps toward .


7. The max —

Read this as: "look at the next state , scan across all its actions , and take the biggest Q-value in that row."

  • Picture: in the next-state row of the table, put your finger on the tallest bar.
  • The subscript under means "the maximisation ranges over all next-actions ". The result is a single number: the value of the best thing you could do next.
  • Why this exact tool? We want to learn how good it is to act optimally. Optimal play means "in the next state, do the best move" — and "the best move's value" is literally the maximum of that row. That single is what makes Q-learning learn the optimal policy even while the agent wanders around randomly. This is the off-policy property. (Its cousin SARSA uses the actual next action instead of the max, which is why SARSA is on-policy.)

The max is also the villain in "Mistake 2" of the parent: taking the max of noisy guesses tends to grab overestimates — the reason Double Q-learning exists.


8. Expectation —

  • Picture: roll the world's hidden dice a thousand times, record the number each time, take the average.
  • Why the topic needs it: the world is random ( from §4). Doing "right" might land you in different next states. is defined as an average over all those possibilities — hence the .
  • The punchline of the whole algorithm: we can't compute this average, because we can't see . So Q-learning replaces "average over all outcomes" with "the one outcome we just experienced", and repairs the noise by only nudging a little each time. That nudge fraction is our last symbol.

9. Learning rate — — and the update arrow

  • Picture: a slider between "old value" and "new target". tells you how far along the slider to move.
  • The arrow in is assignment: "replace the left value with the right value". It is not an equals sign; it's "update to become".
  • Why we need it: one sample is noisy (§8). A small averages many noisy samples into a steady estimate.

Putting the last four symbols together gives the parent's update rule — and now every piece is earned:


10. The Greek and symbol cheat-sheet

Symbol Say it Means
"ess, ess-prime" state now / state next step
"ay, ay-prime" action now / action next step
"arr" reward just received (a number)
"gamma" discount factor,
"alpha" learning rate,
"Q of ess ay" current value guess for (state, action)
"Q star" the true optimal values
"max over ay-prime" biggest value across next actions
"expected value" average over randomness
"delta" TD error = target − old guess
"gets / becomes" overwrite the left with the right
"in" is a member of

How these feed the topic

Agent and Environment loop

State s

Action a

Reward r

Transition P hidden

Return G

Discount gamma

Q value of s and a

Expectation E average

max over next actions

TD target

Learning rate alpha

Update rule

Q-learning algorithm

Every arrow says "you need the left box before the right box makes sense". The bottom node is the whole parent topic.


Equipment checklist

Cover the right side and test yourself. If any answer is fuzzy, reread that section before opening the parent note.

What does a state represent?
The complete description of the agent's current situation — the one thing the best action depends on.
What does the prime mark mean in and ?
"One step later" — the next state and a next-step action, versus the un-primed "now".
Why does reward matter — what is it the only source of?
It's the sole signal of what we want; everything the agent learns is squeezed from these numbers.
What is and does Q-learning use it?
The probability of landing in after doing in ; Q-learning is model-free and never uses it — it samples instead.
Why do we multiply future rewards by ?
To make far rewards matter less and keep the infinite sum finite; and is what makes the update converge.
In one sentence, what is ?
The expected discounted total reward from doing in and then playing optimally forever.
What is the difference between and ?
is the current guess (starts wrong); is the true optimal table it converges toward.
Why and not the actual next action?
The max encodes "act optimally next", which lets Q-learning learn the optimal policy off-policy while exploring randomly.
What does do and why can't we compute it directly here?
It averages over the random environment; we can't compute it because is hidden, so we sample one outcome instead.
What does the arrow mean, and why not ?
"Overwrite / becomes" — an update step, not an algebraic equality.
What does control?
The step size toward each new target — small averages noise slowly, large leaps and is noisy.
What is the TD error ?
target minus old guess: — "how wrong we were".