Learning by trial and error
Nobody tells the agent the right answer. It tries something, gets a score, and slowly changes what it does. That is reinforcement learning.
In 60 seconds
Learning by trial and error
Nobody tells the agent the right answer. It tries something, gets a score, and slowly changes what it does. That is reinforcement learning.
The loop that everything in this track uses
- 1
Look
The agent sees the current situation, called the state. - 2
Act
It picks an action, such as "move left" or "bowl a yorker". - 3
Get feedback
The environment moves to a new state and gives a reward. - 4
Learn
The agent changes its future choices a little, based on the reward. - 5
Repeat
Thousands of times. Good habits grow, bad ones fade.
Three ways to learn, side by side
| Kind of learning | What the computer gets | Everyday example |
|---|---|---|
| Learning from examples (supervised) | Inputs with the correct answer | Past exam papers with the answer key |
| Finding patterns (unsupervised) | Inputs with no answers at all | Sorting a pile of photos into groups that look alike |
| Reinforcement learning | Only a score after it acts | A game where you see your points but nobody tells you the best move |
Why is this hard?
- The reward is only a number. It says how well you did, not what you should have done instead.
- Rewards can come late. In a cricket match, the result comes at the end. Which of the 120 balls won it?
- The agent's actions change what it sees next. A bad early move can lead to a worse part of the game.
- It must try things to learn, and some tries will be bad. This is called exploration, and it has its own lesson.
Trial and error in ten lines
import random
rng = random.Random(42) # seeded, so you get the same result every run
TARGET = 7.0
def reward(strength):
landed = strength * 1.4 # the game's physics, hidden from the player
return -abs(landed - TARGET) # 0 is perfect, more negative is worse
strength = 1.0
best = reward(strength)
for attempt in range(1, 41):
trial = strength + rng.uniform(-1.0, 1.0) # try a small change
r = reward(trial)
if r > best: # keep it only if it helped
strength, best = trial, r
if attempt % 5 == 0:
print(f"attempt {attempt:2}: strength {strength:.2f}, miss {-best:.2f} m")
Where you may have heard of RL
TARGET to 10 and the step size 1.0 to 0.1 (in both places). Does the player still get close in 40 attempts? What does a tiny step size cost you?Watch and read more
Lab
A lock-cracking agent that opens a 6-dial combination lock in a few hundred tries, using only a score.
The problem
A combination lock has 6 dials, each showing 0 to 9. That is a million possible codes. The lock will not tell you which dials are right. It only gives a score: how many dials are right. That score is your only reward.
- Press Run. The starter guesses completely at random 600 times. Look at the best score it reaches.
- Rewrite
crackso that it learns by trial and error: start with one guess, change one dial at a time, and keep a change only when the score goes up. - Use at most
triescalls toscore. Stop early when all dials are right. - Press Check. It tests your
crackon three new locks it has never seen. - Write two sentences: why is keeping the good changes so much better than guessing from scratch each time?
Practice lab
Edit the code and run it. Python runs in your browser — nothing is sent to our server.
No output. Add print(...) to see values.
You are done when
Hard questions
Try to answer before you reveal. If you can answer these, you understood the lesson.
Q1Can you open a 6-dial lock in about 55 tries, every time, with no randomness?Reveal
Yes. Start with all zeros and note the score. For the first dial, try 1 to 9 while keeping the others fixed: the score goes up by one exactly when that dial is right, and down by one if you just broke a dial that was already right at 0. So you learn each dial with at most 10 tries, about 60 in total. This uses the structure of the score. RL agents usually cannot do this because the world is not so tidy, which is why they need more tries.
Q2Hill climbing can get stuck on a "small hill". Would that happen with this lock?Reveal
Not with this lock, because improving one dial never makes another dial worse, so there is always an uphill step until the lock opens. In many real problems, changes interact: a move can look worse now but lead somewhere much better. Then pure hill climbing gets stuck, and you need exploration, which the next lessons add.
Questions people ask
Is reinforcement learning the same as a reward in a school?
It borrows the word from psychology, where rewards shape behaviour. In computing, the reward is just a number the program tries to make large. The program does not feel happy. It only follows maths.
Why not just tell the agent the right answer?
Often nobody knows it. What is the best move in a new game position, or the best way for a robot to balance? We can still score the result, so RL lets the agent find good behaviour we could not write down.
Does RL need a lot of tries?
Usually yes, far more than a person would need. That is why many RL systems practise in games and simulators, where a million tries are cheap and nobody gets hurt.
Do I need a big computer for this track?
No. Every lab uses tiny games that run in your browser in seconds. The ideas are the same ones used at a larger scale.
Lesson test
5 questions. Get 3 right (60%) to pass and complete this lesson.