Skip to content

Repository files navigation

🐍 Snake Game AI with Deep Q-Learning

A reinforcement learning project where an AI agent learns to master the classic Snake game using Deep Q-Learning (DQN) and a simple feedforward neural network.


📚 Table of Contents


✨ Overview

The AI agent is trained using Q-Learning with function approximation via a neural network. It learns from its environmental interactions, predicting the best actions to take based on the current state to maximize cumulative reward (i.e., eat more food and survive longer!).


⚙️ Installation

Make sure you have Python 3.7+ installed.

Install the required dependencies:

pip install -r requirements.txt

🚀 Usage

To begin training the agent:

python agent.py
  • Training progress and events are logged to agent.log.
  • Models are saved periodically to the ./model/ directory.

📁 Project Structure

.
├── agent.py           # Contains the Agent class and training logic
├── model.py           # Defines the neural network and QTrainer
├── game.py            # Implements the Snake game mechanics (not included here)
├── helper.py          # Helper functions like plotting (not included here)
├── agent.log          # Log file for training progress
├── model/             # Saved models (checkpoints)
└── requirements.txt   # Python dependencies

🤖 Agent Details

The Agent class handles:

  • Initializing the neural network and trainer
  • Extracting game state features (11 inputs)
  • Choosing actions via an exploration/exploitation policy
  • Storing short- and long-term memories
  • Training the model based on rewards received

🧠 Model Details

The model is a simple feedforward neural network:

  • Input Layer: 11 neurons representing game state
  • Hidden Layer: 256 neurons with ReLU activation
  • Output Layer: 3 neurons (straight, left, right)

Implemented in the Linear_QNet class inside model.py.


🎯 Training Process

The agent trains by repeatedly playing the game and improving based on its mistakes:

  • Short-term memory: Train immediately on the latest step
  • Long-term memory: Sample batches from past experiences and train

Training is managed by the train() function in agent.py.


💾 Checkpointing & Fine-Tuning

✅ Saving the Model

Models are checkpointed periodically and when new high scores are achieved:

def save(self, file_name='model.pth', n_games=-1):
    ...
    torch.save(self.state_dict(), file_name)

🔁 Loading a Saved Model

You can resume training or fine-tune from an existing model:

def load(self, file_name='model90.pth'):
    ...
    self.load_state_dict(torch.load(file_name))

💡 This improves training efficiency and resilience to interruptions.


📋 Logging

Training events, scores, model saves, and errors are logged to agent.log.

Use the log file to track progress or debug unexpected behavior.


🖼️ Screenshots

🎮 Gameplay Snapshot

The AI agent in action learning to survive longer and eat more food:

Game-Play-snapshot.png

📈 Rewards over Games

Visualizing the agent’s improving performance:

Rewards-Curve


🤝 Contributing

Contributions are welcome! To contribute:

  1. Fork the repo
  2. Create your feature branch: git checkout -b feature/my-feature
  3. Commit your changes: git commit -m "Add my feature"
  4. Push to the branch: git push origin feature/my-feature
  5. Open a Pull Request 🎉

📝 License

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


Built with ❤️ by an RL enthusiast. Enjoy watching your AI learn to slither like a pro! 🐍

About

A reinforcement learning project where an AI agent learns to master the classic Snake game using Deep Q-Learning (DQN) and a simple feedforward neural network.

Resources

Stars

Watchers

Forks

Releases

Packages

Used by

Contributors

Languages