Skip to content

A Deep Q-Learning implementation to train and evaluate an agent on the classic CartPole-v1 environment using OpenAI Gym. Includes training scripts, pre-trained models, and tools for visualizing learning progress.

License

Notifications You must be signed in to change notification settings

alienx5499/CartPole

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

40 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🌟 CartPole: OpenAI Gym Reinforcement Learning 🌟

Master the CartPole-v1 environment with Deep Q-Learning and Visualization

Build Passing Views Contributions Welcome License: MIT Security ⭐ GitHub stars 🍴 GitHub forks Commits πŸ› GitHub issues πŸ“‚ GitHub pull requests πŸ’Ύ GitHub code size

πŸ”— Visit the Live Demo | πŸ“‘ Explore Documentation


🎒 What is CartPole?

CartPole is a classic reinforcement learning environment from OpenAI Gym where the goal is to balance a pole on a moving cart. This repository includes:

  • CartPole.py: A script for rendering and evaluating a trained DQN agent.
  • CartPoleAlgorithm.py: Deep Q-Learning implementation to train the agent on the CartPole-v1 environment.
  • Visualize_q_table.py: Tools for analyzing and visualizing training metrics like rewards, losses, and epsilon decay.
  • ModelData/: A folder containing:
    • Pre-trained DQN model (cartpole_dqn_optimized.pth).
    • Training logs (training_logs.json) for insights.

"Master the art of balancing with Deep Q-Learning!"


πŸ“š Table of Contents

  1. ✨ Features
  2. πŸ› οΈ Tech Stack
  3. πŸ“Έ Screenshots
  4. βš™οΈ Setup Instructions
  5. 🚨 Resource Warning
  6. 🎯 Target Audience
  7. 🀝 Contributing
  8. 🌟 Awesome Contributors
  9. πŸ“œ License

✨ Features

  • πŸ€– Deep Q-Learning: Train a neural network to approximate the Q-value function for optimal policy learning.
  • πŸ“Š Training Logs: Analyze rewards, epsilon decay, and losses to understand the learning process.
  • πŸ“ˆ Visualization Tools: View training progress with clear and detailed graphs.
  • πŸ–₯️ Pre-Trained Model: Quickly test and render the pre-trained model for instant results.
  • πŸ’» Modular Codebase: Separate scripts for training, evaluation, and visualization.

πŸ› οΈ Tech Stack

🌐 Python Technologies

  • Reinforcement Learning: OpenAI Gym
  • Deep Learning: PyTorch
  • Visualization: Matplotlib, NumPy
  • Code Management: JSON for logs, Torch for saving models

πŸ› οΈ Scripts and Files

  • CartPole/: Folder containing:
    • CartPole.py: Script for rendering the trained agent and observing its performance.
    • CartPoleAlgorithm.py: Core DQN training implementation.
    • Visualize_q_table.py: Tools to visualize training metrics and analyze learning progress.
  • ModelData/: Folder containing:
    • cartpole_dqn_optimized.pth: Pre-trained DQN model.
    • training_logs.json: Saved training metrics for detailed analysis.

πŸ“Έ Screenshots

Here are visualizations showcasing the training process and results:

  1. Total Rewards Per Episode
    Visualizes the total rewards collected by the agent over episodes, showing trends and improvement over time.
    Total Rewards Per Episode

  2. Training Loss Over Time
    Displays the loss curve over the course of training, providing insights into model convergence.
    Training Loss Over Time

  3. Epsilon Decay During Training
    Highlights how the epsilon value decreases during training, balancing exploration and exploitation.
    Epsilon Decay During Training

  4. CartPole Agent in Action
    Watch the trained agent perform in the CartPole-v1 environment as it successfully balances the pole.
    CartPole Agent in Action


βš™οΈ Setup Instructions

  1. Clone the Repository
    git clone https://github.com/alienx5499/CartPole.git
  2. Navigate to the Project Directory
    cd CartPole
  3. Install Dependencies
    pip install -r requirements.txt
  4. Run Training Script
    python CartPoleAlgorithm.py
  5. Visualize Training Metrics
    python Visualize_q_table.py
  6. Render the Trained Agent
    python CartPole.py

🚨 Resource Warning

This project includes a pre-trained model trained for 10,000 episodes, which is resource-intensive and can be CPU/GPU consuming.

If you have limited hardware resources, we recommend:

  • Training with fewer episodes (e.g., 2000–5000 episodes) to achieve reasonable performance.
  • Ensure your system can handle training workloads to prevent hardware strain.

Alternative: If you prefer not to train the model, you can use the pre-trained model and logs provided in the ModelData/ folder, which contains data for 10,000 episodes.


🎯 Target Audience

  1. Reinforcement Learning Enthusiasts: Dive deep into Deep Q-Learning and OpenAI Gym.
  2. AI Researchers: Analyze and experiment with the classic CartPole environment.
  3. Students and Educators: Use as a learning tool for understanding reinforcement learning.
  4. Developers: Expand the repository with new features or algorithms.

🀝 Contributing

We ❀️ open source! Contributions are welcome to make this project even better.

  1. Fork the repository.
  2. Create your feature branch.
    git checkout -b feature/new-feature
  3. Commit your changes.
    git commit -m "Add a new feature"
  4. Push to the branch and open a pull request.

Refer to our CONTRIBUTING.md for detailed contribution guidelines.


Awesome Contributors

Thank you for contributing to our repository



πŸ“œ License

This project is licensed under the MIT License. See the LICENSE file for details.


πŸ“¬ Feedback & Suggestions

We value your input! Share your thoughts through GitHub Issues.

πŸ”— Visit the Live Demo | πŸ“‘ Explore Documentation


πŸ’‘ Let's master the CartPole challenge together!

About

A Deep Q-Learning implementation to train and evaluate an agent on the classic CartPole-v1 environment using OpenAI Gym. Includes training scripts, pre-trained models, and tools for visualizing learning progress.

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages