PPO Agent playing LunarLander-v3

This is a trained model of a PPO agent playing LunarLander-v3 using the stable-baselines3 library.

Usage (with Stable-baselines3)


import gymnasium as gym
from google.colab import drive
from huggingface_sb3 import load_from_hub, package_to_hub
from huggingface_hub import (
    notebook_login,
) 

from stable_baselines3 import PPO
from stable_baselines3.common.env_util import make_vec_env
from stable_baselines3.common.evaluation import evaluate_policy
from stable_baselines3.common.monitor import Monitor

env = gym.make("LunarLander-v3")
env.reset()
print("_____OBSERVATION SPACE_____ \n")
print("Observation Space Shape", env.observation_space.shape)
print("Sample observation", env.observation_space.sample())

print("\n _____ACTION SPACE_____ \n")
print("Action Space Shape", env.action_space.n)
print("Action Space Sample", env.action_space.sample())  # Take a random action

env = make_vec_env("LunarLander-v3", n_envs=16) #staking env for more hindsights


model = PPO(
    policy="MlpPolicy",
    env=env,
    n_steps=1024,
    batch_size=256,
    n_epochs=10,
    gamma=0.999,
    gae_lambda=0.98,
    ent_coef=0.01,
    learning_rate=3e-4,
    verbose=1,
)


drive.mount('/content/drive')


#function to save the model to drive 

def save_model(model, model_name="ppo_LunarLander"):
    path = f"/content/drive/MyDrive/{model_name}"
    model.save(path)
    print(f" Model saved to {path}")
    return path


model_name = "ppo-LunarLander-v3"

model.learn(total_timesteps=3000000)
model_name = "ppo_LunarLander-v3"

save_model(model , "ppo_LunarLander")


eval_env = Monitor(gym.make("LunarLander-v3"))
mean_reward , std_reward = evaluate_policy(model , eval_env , n_eval_episodes = 10 , deterministic = True)




#result = mean_reward - std_reward

if mean_reward - std_reward > 200:
  print("Passed , you can push")


package_to_hub(...)

...
Downloads last month
-
Video Preview
loading

Evaluation results