Launch the interactive walkthrough to discover each component, including the 3D Q-Sphere Lab & Python in Colab.
Understand the Q-Table lookup structure: mapping discrete states (rows) and actions (columns) to estimated cumulative rewards.
| Symbol / Term | Meaning | Beginner Interpretation |
|---|---|---|
| Optimal action | Best move | |
| Q-value in row s, column a | Expected reward |
Row for State 2: Q(2, Left) = 4.2, Q(2, Right) = 9.8. Max is 9.8.
Result: Greedy policy picks Right
Create zero matrix of size (num_states, num_actions).
Current state index s identifies row Q[s, :].
Apply epsilon-greedy selection across row entries.
Problem: Solving 4x4 slippery grid mazes.
Solution: A 16x4 Q-table learns optimal paths avoiding ice holes and reaching the frisbee.
Interactive activity component
Train an agent to navigate the FrozenLake environment using a 16x4 Q-table, epsilon-greedy exploration, and the Bellman temporal difference update equation.
For Google Colab or Jupyter notebooks, run this shell command to install the required dependencies:
!pip install gymnasium numpy1 import numpy as np 2 import gymnasium as gym
import numpy as np → NumPy for 2D Q-matrix math
import gymnasium as gym → Gymnasium reinforcement learning environment suite
1 # Create FrozenLake environment (4x4 grid = 16 states, 4 actions) 2 env = gym.make("FrozenLake-v1", is_slippery=False) 3 state_size = env.observation_space.n 4 action_size = env.action_space.n 5 6 # Initialize Q-table with zeros 7 Q_table = np.zeros((state_size, action_size)) 8 9 # Hyperparameters 10 alpha = 0.1 # Learning rate 11 gamma = 0.99 # Discount factor 12 epsilon = 1.0 # Initial exploration rate 13 epsilon_decay = 0.995 14 epsilon_min = 0.01 15 episodes = 2000 # Number of games 16 17 # Training Loop 18 for episode in range(episodes): 19 state = env.reset()[0] 20 done = False 21 22 while not done: 23 # Epsilon-greedy action selection 24 if np.random.rand() < epsilon: 25 action = np.random.choice(action_size) 26 else: 27 action = np.argmax(Q_table[state]) 28 29 # Take action and observe transition 30 next_state, reward, done, _, _ = env.step(action) 31 32 # Update Q-table using the Bellman equation 33 Q_table[state, action] = Q_table[state, action] + alpha * ( 34 reward + gamma * np.max(Q_table[next_state]) - Q_table[state, action] 35 ) 36 37 state = next_state 38 39 # Decay epsilon to shift from exploration to exploitation 40 epsilon = max(epsilon * epsilon_decay, epsilon_min) 41 42 print("Training complete! Non-zero learned Q-values:\n", np.round(Q_table[Q_table > 0][:5], 4))
# Create FrozenLake environment (4x4 grid = 16 states, 4 actions)
Environment creation
env = gym.make("FrozenLake-v1", is_slippery=False)
Deterministic 4x4 grid
state_size = env.observation_space.n
16 discrete states
action_size = env.action_space.n
4 discrete actions: Left, Down, Right, Up
# Initialize Q-table with zeros
Initialize Q-table
Q_table = np.zeros((state_size, action_size))
16x4 matrix
# Hyperparameters
Parameters
alpha = 0.1 # Learning rate
Alpha step size
gamma = 0.99 # Discount factor
Gamma future discount
epsilon = 1.0 # Initial exploration rate
Exploration probability
epsilon_decay = 0.995
Decay rate per episode
epsilon_min = 0.01
Minimum exploration floor
episodes = 2000 # Number of games
Training duration
# Training Loop
Training loop
for episode in range(episodes):
Loop over episodes
state = env.reset()[0]
Reset environment to start state
done = False
Episode flag
while not done:
Step through episode
# Epsilon-greedy action selection
Action choice
if np.random.rand() < epsilon:
Explore
action = np.random.choice(action_size)
Sample random action
else:
Exploit
action = np.argmax(Q_table[state])
Greedy action from Q-table
# Take action and observe transition
Environment step
next_state, reward, done, _, _ = env.step(action)
Step transition
# Update Q-table using the Bellman equation
Q-value update
Q_table[state, action] = Q_table[state, action] + alpha * (
Temporal difference update
reward + gamma * np.max(Q_table[next_state]) - Q_table[state, action]
TD error calculation
)
Close update
state = next_state
Transition to next state
# Decay epsilon to shift from exploration to exploitation
Epsilon decay
epsilon = max(epsilon * epsilon_decay, epsilon_min)
Reduce epsilon
print("Training complete! Non-zero learned Q-values:\n", np.round(Q_table[Q_table > 0][:5], 4))
Display learned values
Click "Copy for Colab", open Google Colab, paste into a single cell, and run.
1 # ========================================================= 2 # Q-Table Optimization on Gymnasium FrozenLake-v1 3 # Prepared for Google Colab (Run with Shift + Enter) 4 # ========================================================= 5 6 # Step 1: Install required packages 7 !pip install gymnasium numpy 8 9 # Step 2: Import libraries 10 import numpy as np 11 import gymnasium as gym 12 13 # Step 3: Run Demonstration 14 # Create FrozenLake environment (4x4 grid = 16 states, 4 actions) 15 env = gym.make("FrozenLake-v1", is_slippery=False) 16 state_size = env.observation_space.n 17 action_size = env.action_space.n 18 19 # Initialize Q-table with zeros 20 Q_table = np.zeros((state_size, action_size)) 21 22 # Hyperparameters 23 alpha = 0.1 # Learning rate 24 gamma = 0.99 # Discount factor 25 epsilon = 1.0 # Initial exploration rate 26 epsilon_decay = 0.995 27 epsilon_min = 0.01 28 episodes = 2000 # Number of games 29 30 # Training Loop 31 for episode in range(episodes): 32 state = env.reset()[0] 33 done = False 34 35 while not done: 36 # Epsilon-greedy action selection 37 if np.random.rand() < epsilon: 38 action = np.random.choice(action_size) 39 else: 40 action = np.argmax(Q_table[state]) 41 42 # Take action and observe transition 43 next_state, reward, done, _, _ = env.step(action) 44 45 # Update Q-table using the Bellman equation 46 Q_table[state, action] = Q_table[state, action] + alpha * ( 47 reward + gamma * np.max(Q_table[next_state]) - Q_table[state, action] 48 ) 49 50 state = next_state 51 52 # Decay epsilon to shift from exploration to exploitation 53 epsilon = max(epsilon * epsilon_decay, epsilon_min) 54 55 print("Training complete! Non-zero learned Q-values:\n", np.round(Q_table[Q_table > 0][:5], 4))
Creates a 16-state FrozenLake grid and initializes a 16x4 Q-table with zeros.
Agent balances exploratory random actions with exploitative argmax actions, decaying epsilon from 1.0 to 0.01.
Whenever the agent reaches the goal (reward=1), the reward propagates backwards across the Q-table, revealing the optimal trajectory.
16x4 FrozenLake environment trained over 2,000 episodes
Training complete! Non-zero learned Q-values: [0.5832 0.6591 0.7451 0.8428 0.9510]
The Q-table converges to the optimal navigation policy, ensuring the agent takes the safest, shortest route across the frozen lake to reach the goal.
Q-learning, Bellman optimality equation, Q-table updates, and exploration-exploitation balance.
Test your understanding with these questions. Remember, this is for learning, not examination!