CartPole is a uniquely useful benchmark for comparing reinforcement learning methods because the environment and the goal stay completely constant — only the way the agent learns changes. Whether the agent estimates action values, optimises a policy directly, or combines both into a single architecture, the challenge is identical: keep a pole balanced upright by nudging a cart left or right. Holding the environment fixed makes it possible to isolate what each algorithm actually contributes and to understand why reinforcement learning has developed several families of methods rather than converging on one universal solution.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/apursley2012/cartpole/llms.txt
Use this file to discover all available pages before exploring further.
The CartPole Control Problem
CartPole-v1 provides the agent with four continuous state values at every timestep:| State Value | Description |
|---|---|
| Cart position | Horizontal position of the cart on the track |
| Cart velocity | Speed and direction of cart movement |
| Pole angle | Angle of the pole relative to vertical |
| Pole angular velocity | Rate at which the pole is tipping |
Deep Q-Network (DQN)
DQN is a value-based method. The network predicts a Q-value for each action, and the agent usually chooses the action with the larger predicted value. Experiences are stored in replay memory and sampled later so the network can learn from earlier decisions instead of only the current step.Experience Replay
Experiences are stored in a replay memory buffer as tuples of(state, action, reward, next_state, done). During training the network samples random mini-batches from this buffer instead of learning from consecutive steps. This breaks the temporal correlations between successive observations, which would otherwise destabilise training and cause the network to overfit to recent events.
Epsilon-Greedy Exploration
Early in training the agent cannot trust its own Q-value estimates, so it explores randomly with probability epsilon (ε). As training progresses epsilon decays, shifting the balance from exploration toward exploitation of learned knowledge.Target Calculation
The Q-value target used to train the network combines the immediate reward with the best predicted future reward from the next state:GAMMA is set to 0.95, giving the agent strong weight on future outcomes. This is essential in CartPole because a move that looks beneficial immediately can cause the pole to fall several steps later — the agent must learn stability, not just instant reaction.
DQN is the method implemented in the live browser demo and the original Python notebook. REINFORCE and A2C are discussed here for comparison purposes; they are not running in the interactive simulation.
REINFORCE
REINFORCE is a policy-gradient method. Instead of predicting the value of each action, it learns a policy that produces a probability distribution over the available actions. The agent samples from that distribution to choose actions, and the goal is to shift the distribution so that actions that led to strong outcomes become more probable.How It Learns
REINFORCE is an episodic algorithm: the agent must complete a full episode before any learning can take place. Once the episode ends it calculates the discounted return for every step — how much cumulative reward followed from each decision — and normalises those returns before using them to update the policy.Advantages and Drawbacks
The method has a clear conceptual strength: the connection between an episode’s total return and the policy update is direct and interpretable. There is no Q-value approximation or Bellman equation involved. The main drawbacks are high variance and slow convergence. Because the agent must wait for an episode to finish before learning, and because a strong episode result may have been influenced by luck as much as skill, the gradient updates can be noisy and training progress can stall.Advantage Actor-Critic (A2C)
Advantage Actor-Critic uses two parts. The actor learns which action to choose, while the critic estimates how valuable the current state is. Rather than waiting for a full episode, A2C can update after each step.The Advantage
The signal used to update the actor is called the advantage — the difference between what actually happened and what the critic expected:Why It Helps
This usually makes training more stable than REINFORCE while keeping the flexibility of direct policy learning. The variance that plagues pure policy gradients is reduced because the critic’s value estimate provides a baseline, and step-level updates mean the agent does not have to wait for an episode to complete.Comparison Table
| Method | What it learns | When it updates | Main trade-off |
|---|---|---|---|
| DQN | Value of each action | From replayed experiences | Works well with discrete actions but depends on value estimates. |
| REINFORCE | Action policy | After a complete episode | Clear policy-gradient method, but updates can be slow and unstable. |
| A2C | Policy and state value | At each step | More efficient feedback, but requires both actor and critic networks. |
Key Update Mechanisms
- DQN
- REINFORCE
- A2C
DQN updates the Q-network by minimising the error between the current Q-value prediction and the Bellman target. Experience replay supplies random batches of past transitions so the update is stable and decorrelated.
Which Method to Use
DQN is a practical match for environments with discrete action spaces. CartPole has two discrete actions and the original implementation already uses replay memory and Q-value prediction, which is why DQN powers the browser demo. REINFORCE is the easiest algorithm to understand conceptually. If the goal is to learn how policy gradients work from first principles — with no value function involved — REINFORCE provides the clearest path. The cost is slow, high-variance training. A2C is the right choice when you want the flexibility of direct policy learning without the instability of pure policy gradients. The critic’s value baseline reduces variance, and step-level updates mean the agent learns from every interaction rather than waiting for episodes to end.The live CartPole // Balance Lab demo runs DQN only. All three methods can solve the CartPole-v1 problem, but DQN was chosen for the browser implementation because it maps cleanly onto the original Python notebook and works well with the two discrete actions the environment provides. REINFORCE and A2C are presented here as conceptual comparisons.