Problems
Loading...
1 / 1

ε-Greedy Action Selection

Reinforcement Learning
Medium

Implement ε-greedy action selection for exploration in reinforcement learning.

Mathematical Definition

ε-Greedy Policy:

a={argmaxaQ(s,a),with probability 1ε,random action,with probability ε.a = \begin{cases} \arg\max_{a} Q(s, a), & \text{with probability } 1 - \varepsilon, \\[6pt] \text{random action}, & \text{with probability } \varepsilon. \end{cases}

where ε∈[0,1] controls the exploration rate.

Function Arguments

  • q_values: 1D array, shape (n_actions,) - Q(s, ·) for current state
  • epsilon: float in [0,1] - exploration probability
  • rng: optional np.random.Generator - for deterministic testing
Loading visualization...

Examples

Input: q_values=[1,2,0.5], ε=0

Output: action = 1

Input: q_values=[1,2,0.5], ε=1

Output: action ∈ {0,1,2} with equal probability

Hint 1

Generate a random number between 0 and 1. If it's less than ε, choose random action, else choose greedy.

Hint 2

Use np.argmax() for greedy action and rng.integers() or np.random.randint() for random action.

Requirements

  • If ε=0 always return greedy action (argmax)
  • If ε=1 always return random action
  • Use rng if provided, else use np.random
  • Return Python int action index
  • Do not modify q_values

Constraints

  • 1 ≤ len(q_values) ≤ 10,000
  • NumPy only
  • Time limit: 200ms
Try Similar Problems