This project applies Deep Q-learning to train an agent to play the classic Snake game. The goal is to enable the agent to improve over time and eventually perform as well as, or better than, a human player.
Machine Learning (ML)
To train an agent that learns through reinforcement to excel at playing the Snake game after substantial training.
Leverage Deep Q-learning to train the system agent to play a custom Snake game implemented from scratch.
- Python
- PyTorch (for building and training neural networks)
- Pygame (for game development)
- Matplotlib (for visualizing results)
- Ipython (for interactive sessions)
- Random (for random number generation)
- Numpy (for array operations)
- Collections (for data structures)
- Enum (for enumerations)
- Clone the repository and navigate to the project directory:
git clone https://github.com/gouravanirudh05/Reinforcementlearning-snakegame.git cd Reinforcementlearning-snakegame
- Install the required dependencies:
pip install -r requirements.txt
- Run the main file to start the game and training process:
python agent.py
- Two windows will open: 1.The game window, displaying the Snake game. 2.A graph window, showing the current score and a rolling average of scores across games.
- The agent will learn over the course of many games (recommended: ~150 games). Watch the agent improve its performance over time.
This approach can be generalized to other games by adjusting:
- Game state parameters
- Reward functions
- Complexity of the neural network We chose the Snake game to focus on learning and implementing everything from scratch in our first hackathon. However, the framework could be extended to more complex games.
- UI Enhancement: The game's interface could be made more visually appealing.
- Game Elements: Adding features like bonus points, score multipliers, or additional obstacles for greater gameplay depth.
- Model Optimization:
- More layers could be added to the neural network for increased accuracy.
- The reward function could be fine-tuned.
- Better handling of the exploration-exploitation tradeoff, with more exploration at the beginning, could further improve learning.
Despite its simplicity compared to other projects, we built everything from the ground up, including the game, and focused on the core concepts of reinforcement learning. This contrasts with others who may have used pre-trained models and simply combined them with a fancy UI. We believe our project is more aligned with the Machine Learning track as it applies core ML principles.
Check out a demo of the game here.
This project is licensed under the MIT License. See the LICENSE file for details.