liveBook · Manning

Epsilon-greedy

As explained in Build a Robo-Advisor with Python (From Scratch)

Epsilon-greedy is an action-selection strategy that chooses a random action with probability epsilon and a currently best-known action otherwise.

Epsilon-greedy balances exploration and exploitation during reinforcement learning. At each state, it selects a random action with probability ε and the action with the highest expected cumulative reward Q(W,t,a) with probability 1−ε. Pure exploitation can miss better policies, while pure exploration wastes existing knowledge. section 6.2.2

Selecting and updating an action

With ε = 0.5, the agent explores half the time on average. Otherwise it selects an action maximizing Q; ties are broken randomly. After transitioning to the next wealth state, it updates the selected value using α, γ, and the maximum next-state Q value. section 6.2.2

Listing 6.5 Updating \(Q\)
def Update_Q(w_idx, t, Q):
    if np.random.uniform() < epsilon:  #1
        a = np.random.randint(nA)
    else:
        A_array = Q[w_idx, t, :]
        a = np.where(A_array == A_array.max())[0]
        if (len(a)>1):  #2
            a = np.random.choice(a)
            
    W_tplus1_idx = transition(W[w_idx,t], t, a)
    Q[w_idx, t, a] += alpha*(gamma*Q[W_tplus1_idx, t+1, :].max() 
                             - Q[w_idx, t, a])  #3
    return W_tplus1_idx, t+1, Q

Inspect the Python implementation for ε-based exploration, greedy selection, random tie-breaking, state transitions, and Q-value updates. section 6.2.2

Listing 6.6 Generating episodes
from tqdm import tqdm

n_episodes = 1000000
epsilon = 0.5
alpha = 0.1
gamma = 1.0

Q = np.zeros((nW, T+1, nA))
for j in range(nW):
    if W[j, T]>G:
        Q[j, T, :] = 1

for i in tqdm(range(n_episodes), desc="Running..."):
    W_idx = 0
    t = 0
    while t<=(T-1):
        W_idx, t, Q = Update_Q(W_idx, t, Q)

Review the episode-generation loop, including ε, α, γ, Q initialization, terminal values, and repeated learning paths. section 6.2.2

How Build a Robo-Advisor with Python (From Scratch) covers it

How deep it goes
Explains probabilities, trade-offs, tie-breaking, Q updates, and episode generation, while deferring more sophisticated exploration schedules.
Examples
Uses wealth-state transitions, action-value arrays, random tie-breaking, and one million generated episodes.
What it assumes
Assumes states, actions, rewards, Q-values, transitions, and Q-learning are available concepts.
What you can do after
Apply it via Python to generate exploratory Q-learning paths and update action values.
How it connects
Connects reinforcement learning, Q-learning, Q-values, exploration-exploitation, policies, states, actions, and rewards.

Also explained in

Compare how 3 books explain epsilon-greedy →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Robo-Advisor with Python (From Scratch). The section links point to the parts of the book it draws on.