This project is a custom-built environment for testing and competing AI models in the classic Snake game. It is designed to run multiple game instances concurrently to visualize and compare different AI strategies.
- Multi-Agent Environment: Runs 3 concurrent Snake games in a single 900x600 window.
- Custom Implementation: Built from scratch using Python,
pygame, andnumpy. - High Performance: Optimized to run smoothly at 90 FPS.
- Extensible: Designed to easily plug in different AI models.
We will implement and compare three distinct AI approaches, each grounded in different mathematical frameworks.
This model treats the game grid as a graph
Mathematical Formulation:
Let
This model combines Neural Networks with Genetic Algorithms. Instead of backpropagation, we evolve the weights of the network over generations to maximize a fitness function (score).
Mathematical Formulation:
Neural Network:
Let
Where
Genetic Algorithm:
Let
The next generation
-
Selection: Choosing parents based on fitness probability
$p_i = \frac{F(\theta_i)}{\sum_j F(\theta_j)}$ . -
Crossover: Combining genomes of parents
$\theta_A, \theta_B$ : $$ \theta_{child} = \beta \cdot \theta_A + (1 - \beta) \cdot \theta_B, \quad \beta \sim U(0, 1) $$ -
Mutation: Adding random noise to weights with probability
$\mu$ : $$ \theta'{j} = \theta{j} + \epsilon, \quad \epsilon \sim \mathcal{N}(0, \sigma^2) $$
-
Clone the repository:
git clone https://github.com/Mathweuzz/Snake-IA.git cd Snake-IA -
Create a virtual environment (optional but recommended):
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
To run the multi-agent environment:
python main.py