Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/soheil-mp/auto-pilot-gaming

A deep reinforcement learning framework for training AI agents in classic control and Atari environments. ๐Ÿš€
https://github.com/soheil-mp/auto-pilot-gaming

Last synced: about 1 month ago
JSON representation

A deep reinforcement learning framework for training AI agents in classic control and Atari environments. ๐Ÿš€

Awesome Lists containing this project

README

        

# ๐ŸŽฎ Auto Pilot Gaming

[![Python](https://img.shields.io/badge/Python-3.8%2B-blue?style=for-the-badge&logo=python)](https://www.python.org/)
[![PyTorch](https://img.shields.io/badge/PyTorch-Latest-red?style=for-the-badge&logo=pytorch)](https://pytorch.org/)
[![License](https://img.shields.io/badge/License-MIT-green?style=for-the-badge)](LICENSE)
[![Gymnasium](https://img.shields.io/badge/Gymnasium-Latest-orange?style=for-the-badge)](https://gymnasium.farama.org/)


A deep reinforcement learning framework for training AI agents in classic control and Atari environments. ๐Ÿš€

[Features](#features) โ€ข [Installation](#-installation) โ€ข [Usage](#-usage) โ€ข [Documentation](#-documentation) โ€ข [Contributing](#-contributing)

---

## ๐Ÿ“ Project Structure

```
adaptive-game-ai/
โ”œโ”€โ”€ src/
โ”‚ โ”œโ”€โ”€ agents/
โ”‚ โ”‚ โ””โ”€โ”€ dqn_agent.py # DQN implementation
โ”‚ โ”œโ”€โ”€ utils/
โ”‚ โ”‚ โ”œโ”€โ”€ preprocessing.py # Frame processing utilities
โ”‚ โ”‚ โ”œโ”€โ”€ replay_buffer.py # Experience replay implementation
โ”‚ โ”‚ โ””โ”€โ”€ visualization.py # Training visualization
โ”‚ โ”œโ”€โ”€ config/
โ”‚ โ”‚ โ””โ”€โ”€ hyperparameters.py # Training configurations
โ”‚ โ””โ”€โ”€ play.py # Script to run trained agents
โ”œโ”€โ”€ models/ # Saved model checkpoints
โ”œโ”€โ”€ logs/ # Training logs
โ”œโ”€โ”€ videos/ # Recorded gameplay videos
โ”œโ”€โ”€ tests/ # Unit tests
โ””โ”€โ”€ requirements.txt # Project dependencies
```

## โœจ Features

### ๐Ÿง  Core Features
- Deep Q-Learning (DQN) implementation
- Experience replay mechanism
- Frame stacking & preprocessing
- ฮต-greedy exploration strategy
- Real-time training visualization
- Video recording of trained agents

### ๐ŸŽฏ Capabilities
- Multi-game support (both classic control and Atari games)
- Model checkpointing with best model saving
- Real-time visualization of training progress
- Performance metrics tracking
- Gameplay video recording
- Support for both image-based and vector-based environments

## ๐Ÿ› ๏ธ Tech Stack

### Core Dependencies
- Python 3.8+
- PyTorch 2.0+
- Gymnasium[atari] 0.29+
- NumPy 1.24+
- OpenCV 4.8+
- Matplotlib 3.7+

### Optional Tools
- CUDA-capable GPU
- MoviePy (for video recording)
- Pygame (for environment rendering)

## ๐Ÿ“‹ Prerequisites

Before you begin, ensure you have the following:
- โœ… Python 3.8 or higher installed
- โœ… pip (Python package manager)
- โœ… Virtual environment (recommended)
- โœ… CUDA-capable GPU (optional, for faster training)

## ๐Ÿš€ Installation

1. **Clone the repository:**

```bash
git clone [repository-url]
cd adaptive-game-ai
```

2. **Set up virtual environment:**

```bash
python -m venv venv

# Windows
.\venv\Scripts\activate

# Unix/MacOS
source venv/bin/activate
```

3. **Install dependencies:**

```bash
pip install -r requirements.txt

# For video recording support
pip install "gymnasium[other]" moviepy
```

## ๐ŸŽฎ Usage

### Training Your AI

```bash
python -m src.agents.dqn_agent --env CartPole-v1 \
--episodes 1000 \
--save-path models/cartpole_dqn.pt \
--target-reward 195
```

Available Training Options:

| Option | Description | Default |
|--------|-------------|---------|
| `--env` | Environment name | Required |
| `--episodes` | Number of training episodes | 1000 |
| `--save-path` | Model save location | Required |
| `--device` | Training device (cuda/cpu) | auto |
| `--target-reward` | Target reward to consider solved | 195.0 |

### Playing Games

```bash
python -m src.play --env CartPole-v1 \
--model models/cartpole_dqn.pt \
--episodes 5 \
--record \
--video-dir videos/cartpole
```

Available Play Options:

| Option | Description | Default |
|--------|-------------|---------|
| `--env` | Environment name | Required |
| `--model` | Path to trained model | Required |
| `--episodes` | Number of episodes | 5 |
| `--render` | Enable visualization | True |
| `--record` | Record gameplay videos | False |
| `--video-dir` | Directory to save videos | videos |

### Video Recording

The agent's gameplay can be recorded using the `--record` flag. Videos are saved in MP4 format with the following naming convention:
```
{env_name}_{timestamp}-episode-{episode_number}.mp4
```

Example video directory structure:
```
videos/
โ””โ”€โ”€ cartpole_test/
โ”œโ”€โ”€ CartPole-v1_20240101_120000-episode-0.mp4
โ”œโ”€โ”€ CartPole-v1_20240101_120000-episode-1.mp4
โ””โ”€โ”€ CartPole-v1_20240101_120000-episode-2.mp4
```

## ๐Ÿงช Implementation Details

Our implementation leverages state-of-the-art techniques in deep reinforcement learning:

- ๐Ÿ”„ DQN with experience replay buffer
- ๐Ÿ–ผ๏ธ CNN architecture for image processing
- ๐Ÿ“Š Advanced reward shaping
- ๐ŸŽฏ Frame stacking (4 frames)
- ๐Ÿ” Epsilon-greedy exploration
- ๐Ÿ“ˆ Real-time training visualization
- ๐ŸŽฅ Gameplay video recording

## ๐Ÿค Contributing

Contributions are welcome! Here's how you can help:

1. Fork the repository
2. Create your feature branch (`git checkout -b feature/AmazingFeature`)
3. Commit your changes (`git commit -m 'Add some AmazingFeature'`)
4. Push to the branch (`git push origin feature/AmazingFeature`)
5. Open a Pull Request

## ๐Ÿ“„ License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.