When an intelligent agent operates in a world where it does not know the underlying transition probabilities ( P(s'|s,a) ) or reward equations, dynamic programming cannot be applied. Q-Learning (Watkins, 1989) is a model-free, Temporal-Difference (TD) reinforcement learning algorithm that learns the optimal action-value function ( Q^*(s, a) ) directly from raw environmental experience.
In Q-Learning, the agent maintains a table of Q-values: ( Q(s, a) ). Whenever the agent executes action ( a ) in state ( s ), receives reward ( r ), and transitions to next state ( s' ), it updates the Q-table using the Temporal Difference Error (( delta )):
( alpha ): Learning rate (( 0 < alpha le 1 )).
( r + gamma max_{a'} Q(s', a') ): The TD Target (bootstrapped estimate of optimal return).
( delta = left[ r + gamma max_{a'} Q(s', a') - Q(s, a)
ight] ): The TD Error.
Off-Policy Nature: Q-learning is an off-policy algorithm because the target policy being evaluated is purely greedy (( max_{a'} Q(s', a') )), regardless of the exploratory behavior policy the agent actually followed to collect the experience.
The fundamental dilemma in reinforcement learning is balancing Exploitation (choosing the action with highest known Q-value to maximize current reward) with Exploration (trying untested actions to discover potentially superior strategies). The standard solution is the ( epsilon )-Greedy Strategy:
Tabular Q-learning requires storing a table of size ( |S| imes |A| ). In video games (like Atari) or robotics, states are high-dimensional continuous pixel tensors (e.g., ( 210 imes 160 imes 3 )), creating an uncountably infinite state space. DeepMind's Deep Q-Network (DQN) superseded tabular storage by parameterizing the Q-function with a deep convolutional neural network: ( Q(s, a; heta) approx Q^*(s, a) ).
The following snippet demonstrates Q-Learning training over 500 episodes in Python:
import numpy as np
# Environment: 5-state cliff environment
# States: 0, 1, 2, 3, 4 (Terminal Goal state with +10 reward)
n_states, n_actions = 5, 2 # Actions: 0 (Left), 1 (Right)
Q_table = np.zeros((n_states, n_actions))
alpha = 0.1 # Learning rate
gamma = 0.9 # Discount factor
epsilon = 0.2 # Exploration probability
for episode in range(500):
state = 0 # Start state
while state != 4:
# Epsilon-greedy action selection
if np.random.rand() < epsilon:
action = np.random.randint(n_actions)
else:
action = np.argmax(Q_table[state])
# Environment step transition
if action == 1:
next_state = min(4, state + 1)
else:
next_state = max(0, state - 1)
reward = 10.0 if next_state == 4 else -1.0
# Q-Learning Bellman Update
best_next_q = np.max(Q_table[next_state]) if next_state != 4 else 0.0
td_target = reward + gamma * best_next_q
td_error = td_target - Q_table[state, action]
Q_table[state, action] += alpha * td_error
state = next_state
print("Trained Optimal Q-Table (States x Actions [Left, Right]):")
print(np.round(Q_table, 2))
Broaden your foundational and advanced AI knowledge across our curriculum:
State transitions, reward signals, and the Bellman optimality equation.
Value-based methods vs policy gradient methods in reinforcement learning.
Using RL algorithms (PPO) to optimize language model generation.
Train a Q-learning agent on the OpenAI Gymnasium CartPole environment.