Reinforcement learning playgroundMiddleLesson 2926 min read

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.

Lesson in motion

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.

1/4
In simple words
In a mango orchard, there is a small guava tree right next to the gate, and a huge mango tree at the far end. If you always pick from the first tree that gave you fruit, you will eat guavas for ever and never find the mangoes. But if you wander randomly every single day, you will waste a lot of time. The smart plan: wander a lot at first, then settle down.
Every RL agent faces this trade-off. Exploitation means using what you already know to collect reward now. Exploration means trying something that might be worse, to learn whether it is better. You need both.

Three ways to set epsilon

ScheduleWhile learningFinal result
Never explore (epsilon = 0)Grabs the first reward it findsOften stuck on a small prize
Always explore a lot (epsilon = 1)Mostly random: low reward all through trainingThe table may be good, but the agent played badly for the whole time
Decay: start high, end lowExplores widely early, then uses what it learnedUsually finds the big prize and plays well by the end

Epsilon decay

An epsilon schedule is a function that says what epsilon to use in each episode. Two common shapes:
Python: two decay schedulespython
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}")
Both start near 1 (almost all exploring) and end small (mostly using what was learned). Keeping a small floor, such as 0.05, means the agent never stops checking completely.

Why the small prize is a trap

The guava is close, so the agent finds it early and its Q value goes up. With little exploring, the greedy choice then always walks toward the guava. The mango's reward never gets a chance to spread back to the start, because the agent almost never walks that far. This is a local optimum: good enough to stop searching, but not the best.

Comparing runs fairly

RL results are noisy: the same code with a different random seed can learn quite differently. So never judge a setting from one run.
  1. 1

    Fix the seeds

    Run each setting with the same list of seeds, such as 0 to 4.
  2. 2

    Change one thing

    Only the schedule changes between runs, not the map or alpha.
  3. 3

    Measure two things

    The final greedy result, and the average reward during training.
  4. 4

    Report all runs

    "4 of 5 seeds found the mango" is honest. The best seed alone is not.
Note
There are smarter ways to explore than random actions: giving a bonus to rarely visited states, being optimistic about unknown actions, or rewarding the agent for surprises (sometimes called curiosity). They matter in big games where random wandering almost never finds the prize. The trade-off is the same.
Real example
Arjun picks a coaching centre. He could join the first one his friend mentions (exploit). Or he could attend free demo classes at many centres first (explore). He tries three demos in the first week, then commits. That is epsilon decay in real life.
Try this. In the lab, move the mango one square further away. Which schedules still find it? What do you have to change in your schedule to find it again?

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.

~30 min

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.

  1. 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.
  2. 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.
  3. Run again. Your schedule should find the mango on most seeds and earn a higher average training reward than "always 1.0".
  4. Press Check.
  5. 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.

Output

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.

Please sign in to continue.

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.

Sign in to take the test and save your progress