Build linear and exponential epsilon decay schedules

Why it matters DQN-style agents and many bandit systems in production use exactly these two schedules for exploration.

An exploring agent starts by acting almost randomly and slowly trusts its own estimates more. The exploration rate epsilon is lowered by a schedule.

Write epsilon_schedule(steps, eps_start, eps_end, decay_steps, kind). steps is a list of non-negative ints. Return a list of floats, one epsilon per step:
- kind "linear": eps = eps_start - (eps_start - eps_end) * t / decay_steps, never going below eps_end (once t passes decay_steps it stays at eps_end).
- kind "exp": eps = eps_end + (eps_start - eps_end) * exp(-t / decay_steps).

Signaturedef epsilon_schedule(steps, eps_start, eps_end, decay_steps, kind)

Example

epsilon_schedule([0, 50, 100, 200], 1.0, 0.1, 100, "linear") gives [1.0, 0.55, 0.1, 0.1].

Understand the idea

Epsilon-greedy picks a random action with probability epsilon and the best known action otherwise. Early on the agent knows nothing, so a high epsilon is good. Later, random moves just waste reward, so epsilon should shrink. Think of a new cook trying random spice mixes at first, then sticking to what works.

Linear example with start 1.0, end 0.1 over 100 steps: at step 50 we are halfway, so 1.0 - 0.9 * 0.5 = 0.55. At step 200 the line would give -0.8, so we clamp to 0.1.

Rules:
linear: eps = max(end, start - (start - end) * t / T)
exponential: eps = end + (start - end) * exp(-t / T)

The linear form reaches the floor at a known step; the exponential form slows down smoothly. Atari DQN used a linear decay over the first million frames.

solution.pyPython idle
Ctrl/Cmd + Enter runs sample tests

Run checks the first 2 tests. Submit checks all 4.

  • Sample 1: exampleepsilon_schedule([0, 50, 100, 200], 1.0, 0.1, 100, 'linear')
  • Sample 2: expepsilon_schedule([0, 10, 100, 1000], 1.0, 0.05, 100, 'exp')
  • Test 3: past the endepsilon_schedule([300, 5000], 0.9, 0.02, 1000, 'linear')
  • Test 4: flatepsilon_schedule([0, 7], 0.3, 0.3, 10, 'linear')