An implementation of the classic Snake game where artificial intelligence agents learn to play using the NEAT (NeuroEvolution of Augmenting Topologies) algorithm.
This project demonstrates neuroevolution by training neural networks to control a snake agent. Instead of programming the snake's behavior directly, the algorithm evolves neural networks over multiple generations, gradually improving their ability to:
- Navigate towards food
- Avoid walls and boundaries
- Avoid colliding with their own body
- Maximize the length of the snake
NEAT is a genetic algorithm that evolves artificial neural networks. Unlike traditional neural networks with fixed architecture, NEAT:
- Starts with minimal networks
- Gradually adds neurons and connections through mutations
- Uses a speciation mechanism to protect innovative solutions
- Evolves both the topology (structure) and weights of the network
This allows complex behaviors to emerge without manual network design.
- Adaptive AI: Neural networks that evolve to play Snake
- Visualization: Real-time rendering of the best-performing snake
- Fitness Evaluation: Multiple reward/penalty mechanisms:
- Bonus for moving towards food
- Penalties for moving away or standing still
- Large reward for eating food
- Penalties for hitting walls or body
- Bonus for going straight towards adjacent food
- Population-based Training: Multiple snakes train simultaneously each generation
- Winner Playback: After training, watch the best snake play
- Python 3.x
- pygame
- neat-python
- Clone or download this project
- Install required dependencies:
pip install pygame neat-python
python main.py
The program will:
- Train the neural network population for 100 generations
- Display training statistics to the console
- After training completes, play the best-evolved snake agent
The snake's neural network receives 8 inputs about its current state:
- fruit_straight: Is food directly ahead?
- fruit_forward: Is food in the forward direction?
- fruit_left: Is food to the left?
- fruit_right: Is food to the right?
- danger_forward: Is there danger ahead (wall or body)?
- danger_left: Is there danger to the left?
- danger_right: Is there danger to the right?
- body_length: Current snake length (normalized by 50)
The neural network produces 3 outputs corresponding to:
- Turn left (-1)
- Go straight (0)
- Turn right (1)
Snakes are scored based on:
- +0.1: Going straight
- -0.2: Turning
- +1.0: Moving closer to food
- -0.2: Moving away from food
- +80 - steps_taken: Eating food (bonus for eating quickly)
- -30: Hitting wall or body
- Max steps: 80 moves per food (prevents infinite loops)
The NEAT algorithm is configured via config-feedforward.txt. Key parameters include:
- Population size
- Number of generations
- Mutation rates
- Activation functions
- Network structure constraints
Snake/ main.py # Main game and NEAT training logic config-feedforward.txt # NEAT configuration file README.md # This file
Represents the snake agent with:
- Position and direction tracking
- Body segments
- Movement and growth mechanics
- Collision detection
Represents the food target with:
- Random spawning
- Collision detection with snake
- Configurable difficulty levels
- Save/load trained networks
- Multiple game modes
- Statistics visualization
- Performance metrics tracking
- Adjustable simulation speed
This project is provided as-is for educational purposes.