๐ LunarLander โ Random vs Monte Carlo vs Q-Learning
LunarLander RL Demo
Author: Dr. Nishant Upadhyay
Contact: nishantup@gmail.com
What this is
An interactive comparison of three reinforcement learning approaches on the classic LunarLander-v3 control task from Gymnasium:
- Random policy โ the uniform-random baseline (no learning)
- Monte Carlo control โ learns from complete episodes by averaging returns
- Q-Learning (TD(0)) โ learns from individual transitions using bootstrapped targets
All three use a tabular representation over a discretized state space of 1,048,576 buckets. This lets us study classical RL mechanics without the black-box complexity of a neural network.
Checkpoint statistics
| Method | Episodes | Final train avg | Eval avg | Best episode | Success rate |
|---|---|---|---|---|---|
| Random | โ | โ | -192.1 | 17.0 | 0% |
| Monte Carlo | 30,000 | 8.6 | -6.9 | 277.6 | 10% |
| Q-Learning | 15,000 | -60.2 | -134.7 | 122.5 | 0% |
"Solved" = average reward โฅ 200 over 100 episodes. None of the tabular methods hit that bar on LunarLander โ the continuous physics really demands function approximation (DQN). That's by design: the point is to understand where tabular methods start to break down.
Hyperparameters
- Discount factor ฮณ = 0.99
- Q-Learning step size ฮฑ = 0.1
- Exploration: ฮต starts at 1.0, decays by factor 0.9995 per episode, floors at 0.05
- Max steps per episode: 1000
- Discretization bins: [8, 8, 8, 8, 8, 8, 2, 2]
How to use this demo
- Watch an Agent โ pick a method and a seed; watch a full episode.
- Compare All Three โ run all three on the same starting seed and see how dramatically outcomes can diverge.
- Statistics โ run many evaluation episodes to see the reward distribution.
- Training Curves โ inspect the pre-rendered learning curves from the full training runs.
- Inside the Q-Table โ peek at what the trained policy actually learned at specific meaningful states.
- Live Training Demo โ watch Q-Learning learn in real time on a capped budget (few minutes on CPU).
The reward function
Each step returns a sum of:
- Shaping rewards (small, every step) โ penalties for distance from landing pad, horizontal/vertical speed, and tilt angle
- Leg contact bonus โ +10 once per leg when it touches the ground
- Fuel cost โ โ0.3 for main engine, โ0.03 per side engine fire, every step
- Terminal โ +100 for a safe landing, โ100 for a crash
The "Reward breakdown" chart on the first tab decomposes these for any episode you run.
Code
The source code and training script are designed to be readable end-to-end. Key modules:
src/environment.pyโ state discretization, reward componentssrc/agents.pyโ Random / MC / Q-Learning agent wrapperssrc/evaluation.pyโ episode runner, video recordingsrc/training.pyโ live training generatorsrc/visualization.pyโ all plot helpersscripts/train_and_export.pyโ reproduce the trained Q-tablesscripts/generate_plots.pyโ re-render the training-curve PNGs
Acknowledgements
Built with Gymnasium, NumPy, Matplotlib, and Gradio. Inspired by Sutton & Barto, Reinforcement Learning: An Introduction, 2nd ed.
โ Dr. Nishant Upadhyay (nishantup@gmail.com)
LunarLander-v3 ยท Gymnasium ยท NumPy ยท Matplotlib ยท Gradio