Reinforcement Learning
stable-baselines3
LunarLander-v3
deep-reinforcement-learning
Eval Results (legacy)
Instructions to use Shivam3002/ppo-LunarLander-v3 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- stable-baselines3
How to use Shivam3002/ppo-LunarLander-v3 with stable-baselines3:
from huggingface_sb3 import load_from_hub checkpoint = load_from_hub( repo_id="Shivam3002/ppo-LunarLander-v3", filename="{MODEL FILENAME}.zip", ) - Notebooks
- Google Colab
- Kaggle
Add detailed README with training config, results, and usage
Browse files
README.md
CHANGED
|
@@ -16,22 +16,173 @@ model-index:
|
|
| 16 |
type: LunarLander-v3
|
| 17 |
metrics:
|
| 18 |
- type: mean_reward
|
| 19 |
-
value:
|
| 20 |
name: mean_reward
|
| 21 |
verified: false
|
| 22 |
---
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
This is a trained model of a **PPO** agent playing **LunarLander-v3**
|
| 26 |
-
using the [stable-baselines3 library](https://github.com/DLR-RM/stable-baselines3).
|
| 27 |
|
| 28 |
-
|
| 29 |
-
TODO: Add your code
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
```python
|
| 33 |
-
|
|
|
|
| 34 |
from huggingface_sb3 import load_from_hub
|
| 35 |
|
| 36 |
-
.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
type: LunarLander-v3
|
| 17 |
metrics:
|
| 18 |
- type: mean_reward
|
| 19 |
+
value: 269.06 +/- 19.17
|
| 20 |
name: mean_reward
|
| 21 |
verified: false
|
| 22 |
---
|
| 23 |
|
| 24 |
+
# PPO Agent β LunarLander-v3 π
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
A Proximal Policy Optimization (PPO) agent trained to land a spacecraft on the Moon using [Stable-Baselines3](https://github.com/DLR-RM/stable-baselines3) and [Gymnasium](https://gymnasium.farama.org/).
|
|
|
|
| 27 |
|
| 28 |
+
> **Mean Reward: 269.06 Β± 19.17** over 10 evaluation episodes β exceeds the 200-point solve threshold.
|
| 29 |
+
|
| 30 |
+
---
|
| 31 |
+
|
| 32 |
+
## Environment
|
| 33 |
+
|
| 34 |
+
| Property | Value |
|
| 35 |
+
|---|---|
|
| 36 |
+
| Environment | `LunarLander-v3` |
|
| 37 |
+
| Observation space | Box(8,) β position, velocity, angle, angular vel, leg contacts |
|
| 38 |
+
| Action space | Discrete(4) β do nothing, fire left, fire main, fire right |
|
| 39 |
+
| Solved threshold | β₯ 200 mean reward |
|
| 40 |
+
|
| 41 |
+
### Reward breakdown
|
| 42 |
+
- Closer to landing pad β higher reward
|
| 43 |
+
- Slower movement β higher reward
|
| 44 |
+
- Tilted angle β penalty
|
| 45 |
+
- Each leg touching ground β +10
|
| 46 |
+
- Side engine firing β β0.03/frame
|
| 47 |
+
- Main engine firing β β0.3/frame
|
| 48 |
+
- Crash β β100 | Safe landing β +100
|
| 49 |
+
|
| 50 |
+
---
|
| 51 |
+
|
| 52 |
+
## Training
|
| 53 |
+
|
| 54 |
+
| Hyperparameter | Value |
|
| 55 |
+
|---|---|
|
| 56 |
+
| Algorithm | PPO |
|
| 57 |
+
| Policy | MlpPolicy (2 Γ 64 Tanh layers) |
|
| 58 |
+
| Total timesteps | 1,000,000 |
|
| 59 |
+
| Parallel envs | 16 (vectorized) |
|
| 60 |
+
| n_steps | 1024 |
|
| 61 |
+
| batch_size | 64 |
|
| 62 |
+
| n_epochs | 4 |
|
| 63 |
+
| gamma | 0.999 |
|
| 64 |
+
| gae_lambda | 0.98 |
|
| 65 |
+
| ent_coef | 0.01 |
|
| 66 |
+
| learning_rate | 3e-4 (default) |
|
| 67 |
+
| Device | CPU |
|
| 68 |
+
|
| 69 |
+
Training time: ~8 minutes on Apple M-series CPU.
|
| 70 |
+
|
| 71 |
+
---
|
| 72 |
+
|
| 73 |
+
## Results
|
| 74 |
+
|
| 75 |
+
| Metric | Value |
|
| 76 |
+
|---|---|
|
| 77 |
+
| Mean reward (10 episodes) | **269.06** |
|
| 78 |
+
| Std reward | Β±19.17 |
|
| 79 |
+
| Training timesteps | 1,000,000 |
|
| 80 |
+
| Final ep_rew_mean (training) | ~268 |
|
| 81 |
+
|
| 82 |
+
---
|
| 83 |
+
|
| 84 |
+
## Usage
|
| 85 |
+
|
| 86 |
+
### Load and run
|
| 87 |
+
|
| 88 |
+
```python
|
| 89 |
+
from stable_baselines3 import PPO
|
| 90 |
+
from huggingface_sb3 import load_from_hub
|
| 91 |
+
from stable_baselines3.common.evaluation import evaluate_policy
|
| 92 |
+
from stable_baselines3.common.monitor import Monitor
|
| 93 |
+
import gymnasium as gym
|
| 94 |
+
|
| 95 |
+
# Load from Hub
|
| 96 |
+
checkpoint = load_from_hub(
|
| 97 |
+
repo_id="shivam3002/ppo-LunarLander-v3",
|
| 98 |
+
filename="ppo-LunarLander-v3.zip",
|
| 99 |
+
)
|
| 100 |
+
model = PPO.load(checkpoint)
|
| 101 |
+
|
| 102 |
+
# Evaluate
|
| 103 |
+
eval_env = Monitor(gym.make("LunarLander-v3", render_mode="human"))
|
| 104 |
+
mean_reward, std_reward = evaluate_policy(
|
| 105 |
+
model, eval_env, n_eval_episodes=10, deterministic=True
|
| 106 |
+
)
|
| 107 |
+
print(f"mean_reward={mean_reward:.2f} +/- {std_reward:.2f}")
|
| 108 |
+
eval_env.close()
|
| 109 |
+
```
|
| 110 |
+
|
| 111 |
+
### Render a single episode
|
| 112 |
|
| 113 |
```python
|
| 114 |
+
import gymnasium as gym
|
| 115 |
+
from stable_baselines3 import PPO
|
| 116 |
from huggingface_sb3 import load_from_hub
|
| 117 |
|
| 118 |
+
checkpoint = load_from_hub("shivam3002/ppo-LunarLander-v3", "ppo-LunarLander-v3.zip")
|
| 119 |
+
model = PPO.load(checkpoint)
|
| 120 |
+
|
| 121 |
+
env = gym.make("LunarLander-v3", render_mode="human")
|
| 122 |
+
obs, _ = env.reset()
|
| 123 |
+
done = False
|
| 124 |
+
total_reward = 0
|
| 125 |
+
|
| 126 |
+
while not done:
|
| 127 |
+
action, _ = model.predict(obs, deterministic=True)
|
| 128 |
+
obs, reward, terminated, truncated, _ = env.step(action)
|
| 129 |
+
total_reward += reward
|
| 130 |
+
done = terminated or truncated
|
| 131 |
+
|
| 132 |
+
print(f"Episode reward: {total_reward:.2f}")
|
| 133 |
+
env.close()
|
| 134 |
+
```
|
| 135 |
+
|
| 136 |
+
---
|
| 137 |
+
|
| 138 |
+
## Training code
|
| 139 |
+
|
| 140 |
+
```python
|
| 141 |
+
from stable_baselines3 import PPO
|
| 142 |
+
from stable_baselines3.common.env_util import make_vec_env
|
| 143 |
+
from stable_baselines3.common.evaluation import evaluate_policy
|
| 144 |
+
from stable_baselines3.common.monitor import Monitor
|
| 145 |
+
import gymnasium as gym
|
| 146 |
+
|
| 147 |
+
# Vectorized training env
|
| 148 |
+
env = make_vec_env("LunarLander-v3", n_envs=16)
|
| 149 |
+
|
| 150 |
+
model = PPO(
|
| 151 |
+
policy="MlpPolicy",
|
| 152 |
+
env=env,
|
| 153 |
+
n_steps=1024,
|
| 154 |
+
batch_size=64,
|
| 155 |
+
n_epochs=4,
|
| 156 |
+
gamma=0.999,
|
| 157 |
+
gae_lambda=0.98,
|
| 158 |
+
ent_coef=0.01,
|
| 159 |
+
verbose=1,
|
| 160 |
+
)
|
| 161 |
+
|
| 162 |
+
model.learn(total_timesteps=1_000_000)
|
| 163 |
+
model.save("ppo-LunarLander-v3")
|
| 164 |
+
|
| 165 |
+
# Evaluate
|
| 166 |
+
eval_env = Monitor(gym.make("LunarLander-v3", render_mode="rgb_array"))
|
| 167 |
+
mean_reward, std_reward = evaluate_policy(model, eval_env, n_eval_episodes=10, deterministic=True)
|
| 168 |
+
print(f"mean_reward={mean_reward:.2f} +/- {std_reward:.2f}")
|
| 169 |
+
```
|
| 170 |
+
|
| 171 |
+
---
|
| 172 |
+
|
| 173 |
+
## Dependencies
|
| 174 |
+
|
| 175 |
+
```
|
| 176 |
+
gymnasium[box2d]>=1.0
|
| 177 |
+
stable-baselines3>=2.0
|
| 178 |
+
huggingface_sb3
|
| 179 |
+
torch
|
| 180 |
```
|
| 181 |
+
|
| 182 |
+
---
|
| 183 |
+
|
| 184 |
+
## References
|
| 185 |
+
|
| 186 |
+
- [Gymnasium LunarLander-v3 docs](https://gymnasium.farama.org/environments/box2d/lunar_lander/)
|
| 187 |
+
- [Stable-Baselines3 PPO docs](https://stable-baselines3.readthedocs.io/en/master/modules/ppo.html)
|
| 188 |
+
- [HuggingFace Deep RL Course β Unit 1](https://huggingface.co/deep-rl-course/unit1/introduction)
|