Back to Articles Reinforcement Learning • 24 min read

Reinforcement Learning: From Q-Learning to Deep RL

Robot arm learning to pick objects

In 2016, AlphaGo defeated 18-time world champion Lee Sedol at Go — a game with more board configurations than atoms in the observable universe. In 2019, AlphaStar reached Grandmaster level at StarCraft II, beating 99.8% of human players. In 2022, the same principles that powered these game-playing agents were used to align ChatGPT with human values. The common thread? Reinforcement Learning.

📋 What You'll Learn
  • What makes RL fundamentally different from supervised learning
  • The Markov Decision Process (MDP) mathematical framework
  • Value functions, the Bellman equation, and dynamic programming
  • Q-Learning with complete Python implementation
  • Deep Q-Networks and how DeepMind beat Atari
  • Policy gradient methods: REINFORCE, Actor-Critic, PPO
  • How RLHF powers ChatGPT and Claude

1. Why RL Is Different

Supervised learning is like studying from solved textbook problems. Unsupervised learning is like exploring data without a guide. Reinforcement learning is like learning to ride a bike — you try things, fall down, try again, and gradually improve through trial and error. The key differences:


2. The Markov Decision Process (MDP)

The formal mathematical framework for RL is the Markov Decision Process. An MDP is defined by a 5-tuple (S, A, P, R, γ):

The "Markov" property states that the future is conditionally independent of the past given the present state. Knowing the current state is sufficient — you don't need the full history. This critical assumption makes the MDP mathematically tractable.


3. Value Functions

The central quantity in RL is the value function — a measure of "how good" it is to be in a given state (or to take a given action).

State Value Function V(s)

V(s) is the expected total discounted return an agent will receive starting from state s and following policy π thereafter:

Vπ(s) = Eπ[ Σ γt rt | s0 = s ]

Action Value Function Q(s,a)

Q(s,a) is the expected return starting from state s, taking action a first, then following policy π. Q-values allow the agent to compare actions: choose the action with the highest Q-value.

Qπ(s,a) = Eπ[ Σ γt rt | s0=s, a0=a ]

The Bellman Equation

The Bellman equation gives us a recursive relationship that unlocks practical computation of value functions:

Q(s,a) = R(s,a) + γ · maxa' Q(s', a')

Read: "The value of taking action a in state s equals the immediate reward plus the discounted value of the best action I can take from the resulting state s'." This recursive definition is the foundation of Q-learning.


4. Q-Learning: The Algorithm

Q-Learning is a model-free, off-policy algorithm that learns Q-values by iteratively applying the Bellman equation as a target for a running estimate:

Q(s,a) ← Q(s,a) + α · [ r + γ · maxa'Q(s',a') − Q(s,a) ]

The term in brackets is the TD error (temporal difference error): the gap between the current Q estimate and the target. Learning rate α controls how aggressively we update toward the new target.

import numpy as np

# Grid World: 4x4 grid, goal at (3,3), holes at (1,1) and (2,3)
# States: 0-15, Actions: 0=Up, 1=Down, 2=Left, 3=Right

class GridWorld:
    def __init__(self):
        self.n_states = 16
        self.n_actions = 4
        self.goal = 15
        self.holes = [5, 11]

    def step(self, state, action):
        row, col = state // 4, state % 4
        if action == 0 and row > 0: row -= 1   # Up
        elif action == 1 and row < 3: row += 1  # Down
        elif action == 2 and col > 0: col -= 1  # Left
        elif action == 3 and col < 3: col += 1  # Right
        next_state = row * 4 + col
        if next_state == self.goal: return next_state, 1.0, True
        if next_state in self.holes: return next_state, -1.0, True
        return next_state, -0.01, False  # Small step penalty

# Q-Learning
env = GridWorld()
Q = np.zeros((env.n_states, env.n_actions))
alpha, gamma, epsilon = 0.1, 0.99, 0.1
n_episodes = 5000

for ep in range(n_episodes):
    state = 0  # Start top-left
    done = False
    while not done:
        # Epsilon-greedy action selection
        if np.random.random() < epsilon:
            action = np.random.randint(env.n_actions)
        else:
            action = np.argmax(Q[state])
        next_state, reward, done = env.step(state, action)
        # Bellman update
        td_target = reward + gamma * np.max(Q[next_state]) * (not done)
        Q[state, action] += alpha * (td_target - Q[state, action])
        state = next_state

print("Learned Q-Table (action values per state):")
print(Q.reshape(4, 4, 4).round(2))
print("\nOptimal Policy (0=Up, 1=Down, 2=Left, 3=Right):")
print(np.argmax(Q, axis=1).reshape(4,4))
            

5. Exploration vs. Exploitation

A critical challenge in RL is that the agent must explore to discover good strategies, but must also exploit what it knows to earn rewards. Key strategies:


6. Deep Q-Networks (DQN): Atari from Pixels

Classic Q-learning uses a table — one entry per (state, action) pair. For complex environments (Atari games with millions of pixel-state combinations), this is impossible. Deep Q-Networks replace the Q-table with a deep neural network that approximates Q(s,a) from raw pixel inputs.

DeepMind's 2013 DQN paper introduced two critical innovations that make neural Q-learning stable:

  1. Experience Replay: Store transitions (s, a, r, s') in a replay buffer. Sample random mini-batches to train on. This breaks temporal correlations that otherwise destabilize training.
  2. Target Network: Maintain two copies of the network: the online network (updated every step) and the target network (updated every N steps). Use the target network to compute the Bellman target — this prevents the target from chasing itself and causing divergence.

With these innovations, a single DQN agent trained purely on 84×84 pixel frames achieved superhuman performance on 29 of 49 Atari games — with no game-specific engineering, just raw pixels and the score.


7. Policy Gradient Methods

Q-learning learns the value of actions. Policy gradient methods take a fundamentally different approach: directly optimize the parameters of the policy itself. The policy πθ(a|s) is a neural network outputting action probabilities.

REINFORCE

The simplest policy gradient algorithm. Run an episode, collect the return G (total discounted reward), then update policy parameters to increase the probability of actions that led to high returns and decrease the probability of actions that led to low returns:

θ ← θ + α · Gt · ∇θ log πθ(at|st)

Proximal Policy Optimization (PPO)

PPO is the most widely used policy gradient algorithm in modern RL. It improves on earlier methods by constraining the policy update to a "trust region" — preventing updates so large that the policy collapses. PPO uses a clipped surrogate objective that effectively limits how much the policy changes per update. PPO is the algorithm used to train the ChatGPT and Claude reward models in the RLHF pipeline.


8. Famous RL Milestones

YearAchievementSignificance
1992TD-Gammon masters BackgammonFirst superhuman RL agent for a complex board game
2013DQN plays 49 Atari gamesFirst RL agent to learn from raw pixels at superhuman level
2016AlphaGo defeats Lee SedolMastered Go — long considered impossible for AI
2019AlphaStar reaches GrandmasterReal-time strategy game with imperfect information
2021AlphaFold2 solves protein folding50-year biology grand challenge solved with RL+deep learning
2022ChatGPT via RLHFPPO used to align LLMs with human preferences

9. Real-World Applications Beyond Games

📚 Further Reading
  • Reinforcement Learning: An Introduction — Sutton & Barto (free at incompleteideas.net)
  • Spinning Up in Deep RL — OpenAI's free RL tutorial with code (spinningup.openai.com)
  • DeepMind's free "Introduction to RL" lecture series on YouTube
  • Stable-Baselines3: production-ready PPO, SAC, TD3 implementations in PyTorch

Reviewed by the Synapse Editorial Team

Last Updated: July 2026. Our content is rigorously reviewed by computer science educators and industry professionals to ensure accuracy, objectivity, and educational value.