Projects
Evolutionary Algorithm

Neat Snake

This project aims to demonstrate the NEAT (NeuroEvolution of Augmenting Topologies) algorithm to solve the classic Snake game. For more details, feel free to visit the repository (opens in a new tab).

Snake Neat algorithm

NEAT Algorithm

NEAT (NeuroEvolution of Augmenting Topologies) is an evolutionary algorithm that generates and optimizes neural networks. Much like reinforcement learning, NEAT can be used to solve complex tasks by optimizing behavior through a fitness function. However, unlike traditional reinforcement learning techniques that often rely on backpropagation,

Mutation

The NEAT algorithm handles this process a little differently, NEAT evolves both the neural network weights and the network architecture itself by gene mutation. Using a process very similar to natural evolution — random mutations. These mutations come in the following three forms:

  1. Add a connection between two unconnected nodes.
  2. Add a node between two nodes that are already connected, disabling the old connection & creating two new ones to preserve the structure of the network.
  3. Change the weight of a connection.

Neat Mutation

Mutation explanation and image from Robert MacWha (opens in a new tab)

Snake Environment

The Snake game environment features a 10x10 grid map created using a custom Python class. The snake starts with just its head and grows a tail each time it consumes an apple. Apples are spawned randomly across the map.

Training

The model is trained until it reaches a fitness score of 20. To expedite the training process, multi-processing is employed. Each population runs 10 times, and the mean fitness is calculated. The gene with the highest fitness score is retained and passed on to the next generation. This process is repeated until the target fitness score is satisfied.

Saving Model

Every 10 generations, a checkpoint is created. By using the 'save' argument command, a winner model is generated. This model can be saved and used later for further testing or real-time demonstration.

Rendering

The game is rendered using Pygame. It displays the map, the snake, the apple, and the neural network's neuron activation. Rendering is available only when using the test environment or running a trained model. Feel free to check out the project repository and experiment with the NEAT algorithm to evolve your own Snake bot!