This project implements a from-scratch CartPole environment using physics, visualizes it with matplotlib, and trains a reinforcement learning agent (PPO) to balance the pole. It also records videos of both random and trained agents for comparison.
cartpole_rl/
āāā env.py # Custom Gymnasium-compatible CartPole environment
āāā train.py # Trains PPO agent using Stable Baselines3
āāā visualize.py # Realtime cart-pole animation using matplotlib
āāā visualize_and_save.py # Saves trained agent animation to MP4 (with reward plot)
āāā record_random_policy.py# Saves random policy animation to MP4
āāā ppo_cartpole_custom.zip# Trained PPO model (after training)
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txtRequirements:
gymnasium
stable-baselines3
matplotlib
opencv-python
numpy
š Usage
- Train the PPO Agent
python train.py
- Visualize Trained Agent in Real-Time
python visualize.py
- Save Trained Agent Video (with rewards)
python visualize_trained.py
- Save Random Policy Video (for comparison)
python record_random_policy.py
š„ Output Videos
cartpole_rl.mp4 ā Trained agent balancing the pole
cartpole_random.mp4 ā Untrained (random) agent failing quickly
Each video shows:
Left: live cart-pole animation
Right: reward plot over time
š Concepts Demonstrated
Physics-based custom CartPole environment
Gymnasium API compliance
PPO reinforcement learning with Stable Baselines3
Live and saved visualizations
OpenCV + matplotlib integration