Exploration versus exploitation
Explore too little and you miss the best prize. Explore too much and you play badly for ever. Learn to decay epsilon, and compare runs fairly.
In 60 seconds
Exploration versus exploitation
Explore too little and you miss the best prize. Explore too much and you play badly for ever. Learn to decay epsilon, and compare runs fairly.
Three ways to set epsilon
| Schedule | While learning | Final result |
|---|---|---|
| Never explore (epsilon = 0) | Grabs the first reward it finds | Often stuck on a small prize |
| Always explore a lot (epsilon = 1) | Mostly random: low reward all through training | The table may be good, but the agent played badly for the whole time |
| Decay: start high, end low | Explores widely early, then uses what it learned | Usually finds the big prize and plays well by the end |
Epsilon decay
def linear(ep, n, start=1.0, end=0.05, frac=0.6):
# falls in a straight line over the first 60% of episodes, then stays at 'end'
t = min(1.0, ep / (frac * n))
return start + (end - start) * t
def exponential(ep, n, start=1.0, end=0.05, rate=0.99):
# multiplies by 'rate' every episode, but never goes below 'end'
return max(end, start * rate ** ep)
n = 500
for ep in (0, 50, 100, 200, 300, 499):
print(f"episode {ep:3}: linear {linear(ep, n):.3f} exponential {exponential(ep, n):.3f}")
Why the small prize is a trap
Comparing runs fairly
- 1
Fix the seeds
Run each setting with the same list of seeds, such as 0 to 4. - 2
Change one thing
Only the schedule changes between runs, not the map or alpha. - 3
Measure two things
The final greedy result, and the average reward during training. - 4
Report all runs
"4 of 5 seeds found the mango" is honest. The best seed alone is not.
Watch and read more
Lab
An epsilon decay schedule that finds the far mango tree and plays well, backed by a fair comparison over five seeds.
The problem
An orchard path: guava tree G (+1, ends the episode) is 2 steps left of the start S, and the mango tree M (+10, ends the episode) is 6 steps right. The Q-learning agent is ready. You write the exploration schedule.
- Press Run. The comparison shows "never explore", "always 0.3" and "always 1.0". Note which tree each ends up at, and the average reward during training.
- Write
epsilon_at(episode, n_episodes): start close to 1.0, go down as episodes pass, never go up, and end at 0.1 or less. - Run again. Your schedule should find the mango on most seeds and earn a higher average training reward than "always 1.0".
- Press Check.
- Write three sentences comparing the four schedules, using the numbers you saw.
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.
Q1"Always 1.0" also ends at the mango. So why is it worse than your schedule?Reveal
Because it plays randomly for all 500 episodes. Q-learning is off-policy, so the table still learns the best route, and the greedy route at the end is right. But during training the agent often walked into the guava or wandered, so its average reward was low. If an agent learns while it is being used (a robot at work, an app with real users), that poor behaviour during learning is a real cost.
Q2Why does "always 0.3" fail here, even though it explores 30% of the time?Reveal
To reach the mango by accident, the agent must go right six times from the start. When the greedy choice points left, each step right needs a random move (chance 0.3 x 0.5 = 0.15), and six in a row is very unlikely. So the mango is almost never found. Exploration that is random step by step is weak at reaching far-away rewards, which is why big games need smarter exploration or a schedule that starts near 1.
Questions people ask
Is there one best epsilon schedule?
No. It depends on the game, the number of episodes and how far away the good rewards are. That is why you compare schedules with several seeds on your own problem.
Why not keep epsilon at 1 during training and use the greedy policy only at the end?
For a tiny game that can work, because Q-learning learns from random play. But in bigger games, random play rarely reaches the interesting parts, so the agent must use what it has learned to get deeper into the game. And if learning happens while the agent is being used, random play has a real cost.
What is a random seed and why does it matter?
A seed fixes the sequence of "random" numbers, so a run can be repeated exactly. Different seeds give different luck. Testing several seeds shows whether a result is real or just lucky.
Does exploration matter outside games?
Yes. A recommendation system that only shows what already works may never learn about new items. A robot that never tries a new grip never finds a better one. But real exploration must stay within safe limits.
Lesson test
5 questions. Get 3 right (60%) to pass and complete this lesson.