The development record presents the original Python notebook contents in readable editor panels — from workspace setup and model configuration through parameter experiments and recorded run results. All source code and terminal output shown here is drawn directly from the preservedDocumentation 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.
Cartpole.ipynb notebook, which remains available via the “Open Notebook ↗” link in the site navigation.
This page documents the Python/Keras source implementation. For the browser demo’s JavaScript equivalent, see Original Notebook. For the full hyperparameter reference, see Model Configuration.
Workspace and Dependencies
The notebook imports Gym for the environment, NumPy for state and value operations, Keras for the dense network and optimizer, and a localScoreLogger utility for tracking run history against the solve threshold.
Cartpole.ipynb — imports
Training Configuration
These nine constants define the baseline DQN profile. They are stored in the notebook and reproduced verbatim in the browser demo’s DQN MODEL PROFILE panel.Cartpole.ipynb — configuration
DQNSolver Architecture
The solver uses two 24-unit ReLU hidden layers and a two-value linear output layer. The output neurons represent the predicted Q-value of moving left (action 0) and moving right (action 1). MSE loss and the Adam optimizer are used throughout training.Cartpole.ipynb — DQNSolver.__init__
Action Selection (act)
The agent follows an epsilon-greedy policy. When a random draw falls below exploration_rate, a random action is returned. Otherwise, the model predicts Q-values for both actions and the highest one is selected.
Cartpole.ipynb — act()
Experience Replay (experience_replay)
At each replay step, a batch of BATCH_SIZE transitions is sampled randomly from memory. For each sampled transition the Bellman target is computed, the relevant Q-value is updated in-place, and the model is fit on the single sample. After the batch, exploration_rate is decayed toward EXPLORATION_MIN.
Cartpole.ipynb — experience_replay()
Q-Value Update Equation
The central update rule combines the immediate step reward with the best possible Q-value available from the next state, discounted byGAMMA. For terminal states (pole fallen), only the immediate reward is used.
Cartpole.ipynb — Bellman target
GAMMA = 0.95, the agent gives strong weight to future outcomes — necessary in CartPole because a correction that appears stable now can destabilize the pole several steps later. The discounted future term ensures the agent learns to plan ahead rather than react greedily.
Parameter Test: Gamma
The first experiment increasedGAMMA from 0.95 to 0.995, giving future rewards greater influence over each Q-value update.
gamma-test.py — Experiment 01
Terminal — GAMMA 0.995
Parameter Test: Learning Rate
A second experiment raisedLEARNING_RATE to 0.1 — 100× the baseline — to compare the effect of a much larger update step on convergence speed and stability.
learning-rate-test.py — Experiment 02
Parameter Test: Minimum Exploration
A third experiment increasedEXPLORATION_MIN from 0.01 to 0.02, keeping all other baseline values unchanged.
exploration-min-test.py — Experiment 03
Terminal — Experiment 03
Completed Run Results
The notebook preserves output from two additional solved runs at baseline configuration, both reaching the CartPole-v1 maximum score of 500.Terminal — Recorded Run 01
Terminal — Recorded Run 02