Simple Algorithm
Reinforcement Learning · Exploration · 16 min

What Is Reinforcement Learning?

Learning to act from trial, error and reward.

Suppose you had to teach a skill without ever showing the right move, only handing out points afterward. That is the deal in reinforcement learning, and it has produced champion game players, controllers for fusion plasma and the final polish on chat assistants. You start this lesson as the learner yourself, then watch agents plan, explore and learn, and finish with a map of the whole course.

What you’ll learn

  • Explain what makes reinforcement learning different from learning from labeled examples: the feedback is a score, it is often delayed, and it depends on the learner's own actions.
  • Use the words of the agent–environment loop correctly: state, action, reward, return, policy and value.
  • Compute a discounted return and explain why rewards further in the future count less.
  • Explain the exploration–exploitation dilemma, and the difference between planning with a model and learning from experience.
  • Place the ten topics of this course on one map and say which question each one answers.

You are the agent

Here are five slot machines. Each one pays a point with its own fixed probability, and nobody will tell you which is best. There is no manual and no teacher, only the points you collect. Pull a few dozen times and try to win as much as you can.

Learning to act from a score, by trial and error, is reinforcement learning (RL). In a moment you will name what you just did. First, play.

Think first On every pull you faced the same choice. What was it, and why is there no free lunch?

Pull the machine with the best record so far, or try one you know less about. The first is exploitation: cashing in on what you know. The second is exploration: paying a little now for information that may pay off later. Explore too little and you can lock onto a mediocre machine forever; explore too much and you waste pulls on machines you already know are bad. This exploration–exploitation dilemma appears in every RL problem, and slot machines, the multi-armed bandit, are its purest form.

Check your understanding

After a few pulls, machine A has paid 6 times out of 7 and machine B once out of 3. What is the soundest conclusion?

  1. A is certainly better, so B should never be pulled again
  2. B is due for a win, so pull B next
  3. A looks better, but three pulls of B say little: B could still be the best machine, so trying it again has value
  4. Pull A and B in strict alternation from now on
Show the answer

C. A looks better, but three pulls of B say little: B could still be the best machine, so trying it again has value

Three pulls cannot pin down B's rate: a machine that pays 60% of the time still pays only once in three pulls about 29% of the time. Estimates from few tries are uncertain, and good strategies keep exploring in proportion to that uncertainty. Thompson sampling does exactly that, which is why its regret curve flattens while greedy's can keep climbing.

The agent–environment loop

Every RL problem has the same shape. An agent, the learner and decision maker, interacts with an environment, which is everything else, in discrete steps. At step tt the agent observes the state StS_t and chooses an action AtA_t; the environment answers with a number, the reward Rt+1R_{t+1}, and a new state St+1S_{t+1}. Then it repeats.

S0, A0, R1, S1, A1, R2, S2, A2, R3, …S_0,\ A_0,\ R_1,\ S_1,\ A_1,\ R_2,\ S_2,\ A_2,\ R_3,\ \dots
A trajectory. The reward carries the index t+1t+1 because it arrives together with the next state. A task that ends, like a game, splits into episodes.

The agent's rule for choosing actions is its policy, written π\pi: in each state it says which action to take, or with what probabilities. The goal is not the next reward but the total collected over the long run, the return. That is what makes RL hard: a move can look bad now and turn out brilliant later.

ProblemStateActionsReward
GoThe board positionLegal moves+1+1 for a win, −1-1 for a loss, 0 until the game ends
A robot arm picking up a cupJoint angles, a camera imageMotor commands+1+1 when the cup is lifted
Heating and cooling a buildingTemperatures, occupancy, weatherHeater and chiller settingsMinus the energy cost, minus a penalty for discomfort
Video recommendationsThe user's recent historyWhich video to showWatch time, a stand-in for satisfaction
Fine-tuning a chat assistantThe conversation so farThe next piece of textA score from a model of human preferences
One loop fits all five. Notice how often the reward is only a stand-in for what we actually want.

Compare this with learning from labeled examples, the supervised learning behind spam filters and image classifiers, where every example comes with the right answer. In RL:

  • the feedback is a score, not an answer: a reward of 3 says how well things went, never what you should have done;
  • the feedback is delayed: a game is won or lost only at the end, so the agent must work out which of its many moves deserve the credit, the credit assignment problem;
  • the data depend on the agent: what it sees next depends on what it chose, so a timid agent never learns what lies beyond its comfort zone.
Check your understanding

Which of these is a reinforcement learning problem?

  1. Predicting tomorrow's temperature from ten years of weather records
  2. Labeling photos as cats or dogs, given 10,000 labeled photos
  3. Grouping customers by their shopping habits
  4. Learning when to heat and cool a building, judged only by the energy bills and comfort complaints that follow
Show the answer

D. Learning when to heat and cool a building, judged only by the energy bills and comfort complaints that follow

The building controller's choices change what happens next and are judged only by a delayed score, with no one saying which setting would have been right. The other three are prediction from labeled data (the first two) or finding structure without labels (the third).

Rewards, returns and the price of waiting

If an agent simply added up all its future rewards, a task that never ends could have an infinite total. RL usually discounts instead: a reward kk steps ahead is multiplied by γk\gamma^k, where the discount factor γ\gamma is a number between 0 and 1.

Gt=Rt+1+γRt+2+γ2Rt+3+⋯G_t = R_{t+1} + \gamma R_{t+2} + \gamma^2 R_{t+3} + \cdots
The discounted return from step tt. With γ=0.9\gamma = 0.9, a reward 20 steps away counts about an eighth as much as one right now: 0.920≈0.120.9^{20} \approx 0.12.

There are three reasons to discount:

  • the future is uncertain: if the world might end, or change, at each step with probability 1−γ1-\gamma, a reward kk steps away arrives only with probability γk\gamma^k;
  • sooner is more useful: a reward now can be put to work, just as money now earns interest;
  • the math stays finite: with rewards of at most 1, the return is at most 1+γ+γ2+⋯=1/(1−γ)1 + \gamma + \gamma^2 + \cdots = 1/(1-\gamma).

That last number says roughly how far ahead the agent looks, its effective horizon: 10 steps for γ=0.9\gamma = 0.9, 100 steps for γ=0.99\gamma = 0.99.

Predict before you look

A robot pays a cost of 1 for every step (a reward of −1-1) and reaches its charger, worth +10+10, on the third step: its rewards are −1,−1,+10-1, -1, +10. With γ=0.9\gamma = 0.9, what is its return G0G_0?

Show the answer

6.2

G0=−1+0.9×(−1)+0.92×10=−1−0.9+8.1=6.2G_0 = -1 + 0.9 \times (-1) + 0.9^2 \times 10 = -1 - 0.9 + 8.1 = 6.2. A route that reached the charger one step sooner would be worth −1+0.9×10=8-1 + 0.9 \times 10 = 8: the step costs and the discount both reward getting there quickly.

Rewards are how you tell an agent what you want, not how to do it. Sutton and Barto's example: reward a chess program for winning, not for capturing pieces. Rewarded for captures, it may learn to win material and lose games. An agent optimizes exactly the number you give it, and it finds loopholes you did not imagine.

Policies and values: planning with a map

How should an agent choose? The key quantity is value: the expected return from a state when you follow a given policy from there on. A state can be valuable without paying anything itself, because of where it leads. If you know the rules of the world, how each action moves you and what it pays, you can compute values without taking a single real step. That is planning with a model, and its classic method is dynamic programming, developed by Richard Bellman in the 1950s.

The whole idea fits in one sentence, the Bellman equation: the value of where you stand is the reward of your best move plus the discounted value of where that move takes you (averaged over the possible landing places when moves can slip). Value iteration repeats that calculation for every state, sweep after sweep, and each sweep carries news of the rewards one cell further.

Think first With no living reward, the +1+1 exit is five moves from the start, and 0.95≈0.590.9^5 \approx 0.59. Why is the start worth only about 0.49?

Moves slip sideways 20% of the time, so the trip takes more than five moves on average, and every extra move multiplies the eventual +1+1 by another 0.9. (Bumping into a wall costs nothing but that lost step.) The optimal route keeps away from the pit, so the −1-1 barely enters. Set the noise to 0 and the start is worth exactly 0.95≈0.590.9^5 \approx 0.59.

Learning from experience

Real agents rarely get the map. A robot does not know its own exact physics, and a recommender cannot write down how people will respond. Two families of methods cope:

  • Model-based RL learns a model of how the world works from experience, then plans with it.
  • Model-free RL skips the model and learns values or a policy directly from experience.

Q-learning (Watkins, 1989) is the classic model-free method. For every state and action it keeps an estimate Q(s,a)Q(s,a) of the return, an action value. After each step it compares that estimate with what it just saw, the reward plus its own estimate of the best next action, and moves part of the way toward it.

Q(St,At)←Q(St,At)+α[Rt+1+γmax⁡aQ(St+1,a)−Q(St,At)⏟the surprise]Q(S_t, A_t) \leftarrow Q(S_t, A_t) + \alpha\big[\underbrace{R_{t+1} + \gamma \max_{a} Q(S_{t+1}, a) - Q(S_t, A_t)}_{\text{the surprise}}\big]
The step size α\alpha sets how far each surprise moves the estimate. Learning a guess from a later guess is temporal-difference learning, and the surprise is the TD error.
Predict before you look

In the maze below the goal pays +1+1, moves never slip, and the shortest route from the start takes 13 moves. With γ=0.9\gamma = 0.9, what value should the start's best action settle at once the agent has learned a shortest route?

Show the answer

0.25

0.913≈0.2540.9^{13} \approx 0.254: the +1+1 loses a factor of 0.9 for each move. After training, read the start cell's largest Q-value. It climbs toward 0.25 as the agent's route becomes a shortest one.

A short history, and RL at work

WhereWhat RL does thereThe honest caveat
GamesExpert or superhuman play in backgammon, Go, chess, Atari games and StarCraft II, mostly learned through self-playNeeds a perfect simulator and an enormous amount of play
RoboticsLegged locomotion, dexterous manipulation and drone racing, usually trained in simulationTransfer from simulation to the real machine (sim-to-real) is fragile, and real trials are slow and can break hardware
Control of physical systemsShaping the plasma in an experimental fusion reactor, a tokamak (2022), and steering stratospheric balloons (2020)Built on accurate simulators and safety checks: landmark demonstrations, not yet routine practice
RecommendationChoosing what to show next to raise long-term engagementThe reward is a proxy (clicks, watch time) that can drift from what users value, and most learning is from logged data
Language modelsRL from human feedback and from checkable answers fine-tunes pretrained modelsHow much RL adds beyond pretraining is debated, and optimizing a learned reward can exploit its flaws (reward hacking)
Where reinforcement learning is used, as of the mid-2020s.

The map of this course

This course is a state space with ten regions, each explored on three levels. Exploration lessons like this one build intuition with live agents. Bootstrapping lessons give the algorithms, the math and the code. Fixed Point lessons prove the theorems and reach the research frontier.

TopicThe question it answers
  1. What Is Reinforcement Learning?
What problem do all RL methods solve, and in what language? (You are here.)
  1. Bandits & the Explore–Exploit Dilemma
How much should you experiment before you commit, when every trial costs something?
  1. Planning with a Model: Dynamic Programming
If you know the rules of the world, how do you compute the best behavior?
  1. Learning from Experience: Monte Carlo & TD
How do you learn values from samples alone, without the rules?
  1. Function Approximation & Deep Q-Learning
How do you generalize when there are far too many states to visit, as in Atari from pixels?
  1. Policy Gradients & Actor–Critic
How do you improve behavior directly, by following the gradient of the expected return?
  1. Model-Based RL: Learning to Plan
How do you learn how the world works, and then plan, search or practice inside that model?
  1. Learning from Demonstrations & Data
How do you learn from experts and from logged data, without trial and error?
  1. Multi-Agent RL & Games
What changes when several learners share one world, from poker to traffic?
  1. RL at the Frontier
How is RL used to train language models, keep agents safe and within bounds, organize behavior into reusable skills, and build agents that learn to learn?
Each topic has one Exploration lesson, four Bootstrapping lessons and twelve Fixed Point deep dives.

The rest of this topic has four Bootstrapping lessons. Agents, Environments & Rewards makes the interaction loop, returns and time limits precise. Markov Decision Processes builds the formal model of states, actions and transition probabilities. Value Functions & the Bellman Equations derives the equations that pin values down and solves them. The Map of RL Algorithms lays out the families you will meet, value-based or policy-based, model-free or model-based, on- or off-policy, online or offline, and when to reach for each.

Check your understanding

A hospital wants a better treatment policy, but it can only learn from the records of past patients: no new trials are allowed. Which topic addresses this?

  1. Bandits & the Explore–Exploit Dilemma
  2. Learning from Demonstrations & Data
  3. Multi-Agent RL & Games
  4. Planning with a Model: Dynamic Programming
Show the answer

B. Learning from Demonstrations & Data

Learning a policy from a fixed log of past decisions, without new interaction, is offline RL. It belongs to Learning from Demonstrations & Data, together with imitation learning and estimating a new policy's value from old records.

Key takeaways

  • Reinforcement learning learns to act from rewards: feedback that scores outcomes instead of naming the right action, often arrives late, and depends on what the agent chose.
  • The loop: observe a state, act, receive a reward and a new state. A policy picks the actions; the goal is the return, the discounted sum of rewards.
  • Discounting by γ\gamma makes later rewards count less, keeps returns finite, and sets an effective horizon of about 1/(1−γ)1/(1-\gamma) steps.
  • Every agent must balance exploration (gaining information) against exploitation (using it).
  • Values measure long-run worth. With a model they can be computed by planning (dynamic programming); without one they are learned from experience (Q-learning), at the price of many samples.
  • Rewards specify what to achieve, not how, and agents exploit every loophole in a badly specified reward.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

A program learns chess from a database in which every position is labeled with a grandmaster's move. Why is this not reinforcement learning?

  1. Because chess is a game
  2. Because it is told the correct move for each position instead of learning from a score of its own play
  3. Because it does not use a neural network
  4. Because chess has too many positions
Show the answer

B. Because it is told the correct move for each position instead of learning from a score of its own play

Being told the right action is supervised learning, here imitation of an expert. RL gets only evaluative feedback, such as winning or losing, on actions it chose itself, and must discover which moves were responsible.

Question 2

Plan A pays 10 right away. Plan B pays nothing now and 15 one step later. For which discount factors does plan B have the larger return?

  1. For every γ\gamma
  2. For γ>0.5\gamma > 0.5
  3. For γ>2/3\gamma > 2/3
  4. For no γ\gamma below 1
Show the answer

C. For γ>2/3\gamma > 2/3

Plan B is worth 15γ15\gamma and plan A is worth 10, so B wins when γ>10/15=2/3\gamma > 10/15 = 2/3. A patient agent (large γ\gamma) waits for the bigger reward; an impatient one takes the 10.

Question 3

What does the value of a state measure?

  1. The reward received on entering it
  2. The number of steps to the nearest exit
  3. How often the agent has visited it
  4. The expected return from that state onward, when following a given policy
Show the answer

D. The expected return from that state onward, when following a given policy

Value looks ahead: it is the expected discounted sum of all future rewards from the state under a policy. In the gridworld, most cells pay nothing themselves yet have positive value because of where they lead.

Question 4

Once the maze agent has reached the goal a few times, why does it still need some random moves?

  1. Its estimates come only from routes it has tried, so a shorter or safer route can be found only by trying actions its current estimates do not favor
  2. Random moves make the Q-values on the route it already knows converge faster
  3. Without random moves the agent would stop moving
  4. Random moves are needed to make the rewards random
Show the answer

A. Its estimates come only from routes it has tried, so a shorter or safer route can be found only by trying actions its current estimates do not favor

Exploitation follows the best route known so far, which need not be the best route. A little exploration keeps testing alternatives, and good exploration strategies (the Bandits topic) aim it where the agent is most uncertain.

Question 5

Which statement correctly contrasts value iteration and Q-learning?

  1. Value iteration learns by trial and error; Q-learning needs the transition probabilities
  2. Both need the full rules of the world
  3. Value iteration needs the rules (a model) but no experience; Q-learning needs experience but no model
  4. Q-learning works only in games
Show the answer

C. Value iteration needs the rules (a model) but no experience; Q-learning needs experience but no model

Value iteration is planning: it computes values from known transition probabilities and rewards. Q-learning is model-free learning: it estimates action values from the transitions it experiences. Model-based RL sits in between, learning a model from experience and planning with it.

Go deeper

Further reading

  • R. S. Sutton & A. G. Barto, Reinforcement Learning: An Introduction, ch. 1 and 3 (MIT Press, 2nd ed., 2018).
  • D. Silver et al., "Mastering the game of Go with deep neural networks and tree search," Nature 529, 484–489 (2016).