Launch the interactive walkthrough to discover each component, including the 3D Q-Sphere Lab & Python in Colab.
Master the mathematical engine of Q-learning: TD error, learning rate alpha, discount factor gamma, and the Bellman optimality update.
| Symbol / Term | Meaning | Beginner Interpretation |
|---|---|---|
| Current state-action value | Old belief | |
| Learning rate (0 to 1) | How much to adjust | |
| Immediate reward | Points received right now | |
| Discounted max future value | Best payoff from next state |
Old Q = 5.0, alpha = 0.5, reward r = 2.0, gamma = 0.9, max future Q(s') = 10.0.
Result: Q-value updates from 5.0 to 8.0
Agent takes action a in state s, lands in s', and receives reward r.
Find maximum Q-value in the next state: max_a' Q(s', a').
TD Error = r + gamma * max_future_Q - current_Q.
Add alpha * TD_Error to current Q(s,a).
Problem: Automated Guided Vehicles (AGVs) navigating factory floors with changing obstacles.
Solution: Q-value updates continuously propagate path congestion delays into route selection.
Interactive activity component
Execute a single Bellman update step and verify numerical convergence.
For Google Colab or Jupyter notebooks, run this shell command to install the required dependencies:
!pip install numpy1 import numpy as np
import numpy as np → NumPy math
1 # Parameters: 2 alpha = 0.2 # Learning rate 3 gamma = 0.9 # Discount factor 4 5 # Before step: 6 current_q = 4.0 7 reward = 2.0 8 next_state_q_values = np.array([3.0, 8.0, 5.0]) # Q(s', :) 9 10 # Bellman update step: 11 max_next_q = np.max(next_state_q_values) # 8.0 12 td_target = reward + gamma * max_next_q 13 td_error = td_target - current_q 14 new_q = current_q + alpha * td_error 15 16 print(f"TD Target: {td_target:.2f}") 17 print(f"TD Error: {td_error:.2f}") 18 print(f"Updated Q-Value: from {current_q:.2f} -> {new_q:.2f}")
# Parameters:
Hyperparameters
alpha = 0.2 # Learning rate
Alpha
gamma = 0.9 # Discount factor
Gamma
# Before step:
Initial state
current_q = 4.0
Q(s, a)
reward = 2.0
Immediate reward r
next_state_q_values = np.array([3.0, 8.0, 5.0]) # Q(s', :)
Next state actions
# Bellman update step:
Computation
max_next_q = np.max(next_state_q_values) # 8.0
Max future Q
td_target = reward + gamma * max_next_q
Target = 2 + 0.9*8 = 9.2
td_error = td_target - current_q
Error = 9.2 - 4.0 = 5.2
new_q = current_q + alpha * td_error
Updated Q = 4 + 0.2*5.2 = 5.04
print(f"TD Target: {td_target:.2f}")
Print target
print(f"TD Error: {td_error:.2f}")
Print error
print(f"Updated Q-Value: from {current_q:.2f} -> {new_q:.2f}")
Print updated Q
Click "Copy for Colab", open Google Colab, paste into a single cell, and run.
1 # ========================================================= 2 # Single Bellman Q-Update Step in Python 3 # Prepared for Google Colab (Run with Shift + Enter) 4 # ========================================================= 5 6 # Step 1: Install required packages 7 !pip install numpy 8 9 # Step 2: Import libraries 10 import numpy as np 11 12 # Step 3: Run Demonstration 13 # Parameters: 14 alpha = 0.2 # Learning rate 15 gamma = 0.9 # Discount factor 16 17 # Before step: 18 current_q = 4.0 19 reward = 2.0 20 next_state_q_values = np.array([3.0, 8.0, 5.0]) # Q(s', :) 21 22 # Bellman update step: 23 max_next_q = np.max(next_state_q_values) # 8.0 24 td_target = reward + gamma * max_next_q 25 td_error = td_target - current_q 26 new_q = current_q + alpha * td_error 27 28 print(f"TD Target: {td_target:.2f}") 29 print(f"TD Error: {td_error:.2f}") 30 print(f"Updated Q-Value: from {current_q:.2f} -> {new_q:.2f}")
Extracts max Q in s' = 8.0.
2.0 + 0.9 * 8.0 = 9.2.
Q-value shifts from 4.00 to 5.04 toward the target.
Current Q=4.0, Reward=2.0, Next Qs=[3, 8, 5]
TD Target: 9.20 TD Error: 5.20 Updated Q-Value: from 4.00 -> 5.04
Through successive Bellman updates, Q-values propagate backwards from terminal rewards to early states.
Q-learning, Q-table updates, DQN architectures, and exploration-exploitation trade-offs.
Test your understanding with these questions. Remember, this is for learning, not examination!