{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "source": [
        "# Install Libraries\n",
        "!pip install stable-baselines3 gymnasium\n",
        "\n",
        "# Import Libraries\n",
        "import gymnasium as gym\n",
        "from stable_baselines3 import PPO"
      ],
      "metadata": {
        "id": "_BWz5WTw9PkY"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Create the Environment\n",
        "env = gym.make(\"CartPole-v1\")\n",
        "\n",
        "# Initialize the PPO Model\n",
        "model = PPO(\n",
        "    policy=\"MlpPolicy\",\n",
        "    env=env,\n",
        "    learning_rate=3e-4,\n",
        "    n_steps=2048,\n",
        "    batch_size=64,\n",
        "    gamma=0.99,\n",
        "    verbose=1\n",
        ")"
      ],
      "metadata": {
        "id": "TP6NfVVu7FYt"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Train the Agent\n",
        "model.learn(total_timesteps=20000)\n",
        "\n",
        "#  Save the Trained Model\n",
        "model.save(\"ppo_cartpole\")\n",
        "\n",
        "# Test the Trained Agent\n",
        "obs, info = env.reset()\n",
        "\n",
        "for _ in range(500):\n",
        "\n",
        "    action, _ = model.predict(obs, deterministic=True)\n",
        "\n",
        "    obs, reward, terminated, truncated, info = env.step(action)\n",
        "\n",
        "    done = terminated or truncated\n",
        "\n",
        "    env.render()\n",
        "\n",
        "    if done:\n",
        "        obs, info = env.reset()\n",
        "\n",
        "env.close()\n",
        "print(\"Training Complete!\")"
      ],
      "metadata": {
        "id": "tPXYyHXo7KGx"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}