Reinforcement Learning
AI Lunar Lander
Case study: A lunar landing mission
An AI taught itself, by trial and error, to land a lunar module gently on its target pad while saving fuel.
Launch a landingThe problem
Case study: a lunar landing mission needs an autopilot that puts the module down between two flags, softly and without wasting fuel. Nobody can write the right sequence of engine burns by hand for every starting position. The idea is to let an agent learn it through reinforcement learning: it tries, gets a score, and slowly keeps what works.
The test environment is LunarLander-v3 from Gymnasium. The simulator gives points for getting closer to the pad and touching down gently, +100 for a landing, -100 for a crash, and small penalties for every engine burst. The mission's bar: an average score of at least 200 over 100 landings in a row.
What the demo does
"Launch a landing" plays one episode flown by the trained agent, chosen at random. You see the module, the engine flames when the agent fires them, and the score changing step by step.
The duel puts the two agents I trained, PPO and DQN, on the same terrain. Over a few runs the difference is easy to see: one lands run after run, the other sometimes ends in a crash.
The learning curve shows the score during training. PPO climbs steadily. DQN goes up and down.
The landings you see were flown by the trained models in the real physics simulator, then replayed in your browser.
Results
- 258Average score over 100 landings
- The simulator treats 200 as a solved landing. The trained pilot averages 258, and its scores stay close together (standard deviation 24).
- 99/100Landings above 200
- Out of 100 test runs, 99 cleared the bar. The other algorithm I tested, DQN, cleared it 74 times.
- +150Worst of the 100 landings
- Even its worst run scored +150. The best DQN agent's worst run was -310, a crash.
The final agent is a PPO model trained for 1 million steps. Over 100 evaluation landings:
- average 258.1, standard deviation 23.8, median 261.4;
- 99 landings above 200;
- worst landing +150.
For comparison:
- random actions: average -165.1;
- DQN with the library's default settings, 100,000 steps: -100.6. It crashes less violently than random actions, but only 5 landings out of 100 clear 200;
- best tuned DQN, 400,000 steps: average 201.5. It passes the bar on average, but with a standard deviation of 116.2, only 74 landings above 200 and a worst run of -309.9, a real crash.
That is why I kept PPO. A 201 average built on a mix of perfect landings and crashes does not meet a brief that asks for a reliable pilot.
Limits: each configuration was trained with a single random seed, and results can move by tens of points from one run to another. The 100-landing evaluation is solid for the final agent; the ranking between the intermediate DQN settings is less certain.
How it's built
Architecture, stack and technical choices.
Diagram as text
flowchart LR
M["ppo_lunarlander.zip"] --> API["api.py (FastAPI)<br/>the only file that loads the model<br/>and runs the simulation"]
API -->|"/play, /episode"| GUI["gui.py<br/>one landing, step by step"]
API -->|"/evaluate"| DASH["dashboard.py<br/>reliability, precision, fuel"]
API --> VID["generer_video.py<br/>landing video"]Stack
- Python
- Gymnasium
- Stable-Baselines3
- PyTorch
- TensorBoard
- FastAPI
- Streamlit
- uv
How I got there:
- Baseline: random actions, then DQN with Stable-Baselines3 defaults on 100,000 steps.
- Ablation: I changed one hyperparameter at a time (learning rate, gamma, replay buffer size, target update interval, network size) on the same budget. Only one setting mattered: how often the target network is refreshed. With the default of 10,000 steps it is copied only 10 times in the whole run, so the agent keeps chasing a frozen target. Setting it to 250 took the average from -100.6 to +75.5. The learning rate change alone made things worse, so the settings do not simply add up.
- Combined settings (the published RL Zoo configuration) on the same budget, then 4 times longer. The average passed 200, but the training curve kept oscillating: the agent forgot and relearned what it knew.
- PPO, with the RL Zoo configuration: 16 environments in parallel, 1 million steps. Counted in steps, DQN learns faster. Counted in wall-clock time, PPO's million steps took less time than DQN's 400,000, and its curve rises steadily.
Everything is logged to TensorBoard so the curves can be overlaid.
The application follows one strict rule from the brief: all the reinforcement learning stays in the API. api.py loads the model and runs the simulation; the two Streamlit pages (a step-by-step landing viewer and a dashboard that measures reliability, landing precision and fuel use) only call the API and display what it returns. FastAPI rejects a malformed observation through its type declarations, and its /docs page lets you try every route without writing a client.
Next steps
- Train each configuration with 3 to 5 seeds and compare the averages. With one seed per setting, some of my intermediate comparisons are within the noise.
- Compare the algorithms at equal wall-clock time as well as equal steps, from the start, instead of explaining the difference afterwards.
- Tune PPO myself rather than relying on the published settings, even if the gain would be small. That would make the comparison with DQN fairer.
- Test the agent in harder conditions. The environment has wind and turbulence options, and an autopilot that has only seen calm weather is not ready for much.
- Send trajectories rather than images. By default the original API returns every frame as a PNG, while a browser can draw the scene from a few numbers per step.