This project implements a Deep Q-Network (DQN) agent to learn how to play the classic game of Snake. The agent learns through reinforcement learning, aiming to maximize its score by eating food while avoiding collisions.
There are two main ways to interact with this project:
This method allows you to train and visualize the DQN agent directly in your browser using Google Colab, a free cloud-based notebook environment. This is the best way to see the reinforcement learning in action.
Steps:
- Open Google Colab: Go to https://colab.research.google.com/ and sign in with your Google account.
- Create a New Notebook: Click on
File > New notebook. - Upload the Code:
- Click on the folder icon on the left sidebar (
Filessection). - Click the
Upload to session storageicon (looks like a folder with an arrow pointing up). - Upload the
snake_visualization.pyfile from this repository. - If you have a pre-trained model (e.g.,
snake_weights.keras), upload it to the same directory. The notebook is designed to load existing weights if found, otherwise, it will start training from scratch.
- Click on the folder icon on the left sidebar (
- Copy and Paste the Code: Copy the entire content of the
snake_visualization.pyfile into the first code cell of your new Colab notebook. - Install Dependencies: The provided code includes an
install_packagefunction to automatically installpydotandgraphvizfor visualizing the model architecture. Ensure this runs correctly. - Run the Notebook: Execute all cells in the notebook. You can do this by clicking
Runtime > Run all. - Interact with Prompts: The notebook will prompt you for:
- "How many episodes to run?": Enter the number of training episodes you want. More episodes mean more training time but potentially better performance.
- "Batch size (positive integer)?": Enter the batch size for training the neural network (e.g.,
16or32).
- Observe Training: As the code runs, you will see:
- A live plot of the agent's scores over episodes.
- If
watch = Trueis set in the code, a real-time visualization of the Snake game as the agent plays during training. - Console output showing the episode number, score, and epsilon value (exploration rate).
- Output Files: After training, the notebook will generate:
snake_weights.keras: The trained model weights (saved periodically and at the end).scores.json: A JSON file containing the scores from each training episode.training_scores.png: A plot visualizing the training scores.model_architecture.png: An image of the neural network's architecture.
For algorithms beyond the Deep Q-Network, you can use a local Python environment or online platforms like Pygame Trinket.
Steps:
- Choose a File: Select a Python file from this repository (e.g.,
snakebot.py). - Open Pygame Trinket: Go to https://trinket.io/features/pygame or set up a local Python environment with Pygame installed.
- Copy and Paste: Copy the code from your chosen Python file and paste it into the Trinket editor or run it in your local environment.
- Execute: Run the code to see the algorithm play the Snake game.
- Model Saving: The Deep Q-Network model weights are saved in the
.kerasformat (e.g.,snake_weights.keras). This is a modern format for Keras models and is fully compatible with TensorFlow 2.x. - Exploration vs. Exploitation (
self.epsilon): In theDDQNAgentclass, theself.epsilonparameter controls the balance between exploration (trying new actions) and exploitation (using learned actions).- Values closer to
1mean more exploration. - Values closer to
0mean more exploitation. Theepsilonvalue decays over time during training, allowing the agent to explore initially and then increasingly exploit its learned knowledge.
- Values closer to
- TensorFlow Tutorials: Reinforcement Learning (DQN)
- Keras Documentation
- Python Official Documentation
- Denny Britz's Reinforcement Learning Blog
- Python Machine Learning GitHub
Thank you for exploring this project! Contributions and feedback are welcome if you find this interesting.