This repository is a hands-on learning project for core Reinforcement Learning (RL) ideas through three practical demos:
- Tabular Q-learning on FrozenLake
- REINFORCE (policy gradient) on CartPole
- PPO on CartPole using Stable-Baselines3
It is designed to connect theory and implementation: you can read concepts, run training, evaluate policies, and generate demo videos.
demos/q_learning_frozenlake.py: value-based, model-free RL with a Q-tabledemos/reinforce_cartpole.py: direct policy optimization with Monte Carlo returnsdemos/gymnasium_ppo_cartpole.py: practical PPO workflow withstable-baselines3docs/rl_explanations.md: full code-referenced theory walkthrough (EN)docs/rl_explanations.es.md: full code-referenced theory walkthrough (ES)videos/: recorded evaluation runsmodels/: saved model artifacts (for PPO)
- Agent, environment, state, action, reward, policy
- Objective: maximize expected discounted return
- Model-based vs model-free RL, and why this repo focuses on model-free methods
.
Q-learning learns action values argmax.
Update rule used by the demo:
Implemented concepts in the script:
- Epsilon-greedy exploration with epsilon decay
- Bellman/TD update
- Terminal handling (no bootstrap on terminal state)
- Greedy post-training evaluation (success rate)
REINFORCE optimizes a stochastic policy
Training loss used conceptually:
Implemented concepts in the script:
- Sampling actions from a policy network
- Discounted return computation
- Return normalization for lower-variance updates
- Episode-level policy gradient optimization
PPO is a stable policy optimization method implemented here via stable-baselines3.
Implemented workflow in the demo:
- Create and train PPO agent (
MlpPolicy) on CartPole - Save model under
models/ - Evaluate deterministic policy performance
- Optional human rendering and optional video recording
- Model: predicts environment dynamics/rewards
- Policy: chooses actions from states
- Value function: estimates expected return
In this repo:
- Q-learning primarily learns a value function (
Q-table) - REINFORCE/PPO primarily optimize policies (PPO also estimates value internally)
- Python 3.10 or 3.11
uvinstalled
From project root:
uv pip install -r requirements.txtpython demos/q_learning_frozenlake.pyUseful options:
python demos/q_learning_frozenlake.py --slippery
python demos/q_learning_frozenlake.py --render-eval --render-episodes 5
python demos/q_learning_frozenlake.py --record-video --video-episodes 5 --video-dir videos/q_learning_frozenlake
python demos/q_learning_frozenlake.py --record-and-render --render-episodes 5 --video-episodes 5python demos/reinforce_cartpole.pyUseful options:
python demos/reinforce_cartpole.py --episodes 1500 --lr 0.0005 --gamma 0.99
python demos/reinforce_cartpole.py --render-eval --render-episodes 3
python demos/reinforce_cartpole.py --record-video --video-episodes 3 --video-dir videos/reinforce_cartpole
python demos/reinforce_cartpole.py --record-and-render --render-episodes 3 --video-episodes 3python demos/gymnasium_ppo_cartpole.pyUseful options:
python demos/gymnasium_ppo_cartpole.py --render-demo
python demos/gymnasium_ppo_cartpole.py --render-eval --render-episodes 3
python demos/gymnasium_ppo_cartpole.py --record-video --video-episodes 3 --video-dir videos/ppo_cartpole
python demos/gymnasium_ppo_cartpole.py --record-and-render --render-episodes 3 --video-episodes 3- RL foundations + model-free context
- Q-learning demo and Bellman updates
- REINFORCE demo and policy gradient intuition
- PPO demo as a practical modern baseline
- Wrap-up: model vs policy vs value function
For the full code-referenced explanation, see docs/rl_explanations.md.