Two reinforcement learning approaches to Gymnasium CartPole-v1: the original tabular Q-Learning and a new neural-network PPO implementation. Despite the repository name, this is currently CartPole, not Pendulum-v1.
pip install "gymnasium[classic-control]" numpy
cd q-learn
python train_carpole.py
python test_carpole.pyThe Q-learning scripts use a relative path for q_table.npy, so run them from inside q-learn/.
Requirements: Python 3.10+, PyTorch, NumPy, Gymnasium. From the repository root:
pip install -r requirements-ppo.txt
python -m ppo.train --total-timesteps 100000 --seed 42
python -m ppo.evaluate --episodes 10
# Optional: see the agent balance the pole with a desktop window
python -m ppo.evaluate --episodes 1 --renderThe checkpoint is ppo/cartpole_ppo.pt and is excluded from version control. Smoke test (does not prove convergence):
python -m ppo.train --total-timesteps 2048 --rollout-steps 512
python -m ppo.evaluate --episodes 2PPO uses: a categorical actor policy, a critic predicting state values, fresh on-policy rollouts, GAE, a clipped policy objective, critic value loss, an entropy bonus and gradient clipping. A terminated episode does not bootstrap; a time-limit truncated episode bootstraps from the last observation. See ppo/agent.py for the hyperparameters.
| Q-Learning | PPO | |
|---|---|---|
| Learns | Tabular Q-values | Neural actor policy and critic |
| Observation | Discretized 4D state | Raw 4D state |
| Action selection | Epsilon-greedy | Stochastic categorical policy |
| Update | Bellman target | Clipped policy gradient + value loss |
| Data | Can be reused off-policy | Fresh on-policy trajectories |
| Trained result | q-learn/q_table.npy |
ppo/cartpole_ppo.pt |
Evaluate across several seeds and episodes before concluding either method is better. The default PPO training budget is a starting point, not a guaranteed target score.
python -m unittest discover -s tests -vSix tests cover sampled actions, rollout bootstrapping, terminal boundaries, truncation bootstrapping, PPO gradient updates, and checkpoint reload. The GitHub Actions workflow runs tests plus a short end-to-end CartPole smoke test.
q-learn/: existing Q-learning baseline and saved Q-tableppo/agent.py: PPO math and neural networksppo/train.py: PPO training CLIppo/evaluate.py: trained-policy evaluation CLItests/test_ppo.py: unit testsrequirements-ppo.txt: PPO dependencies