Genetic Algorithm Snake Simulator

Neural-network snakes evolve over generations using a genetic algorithm. Configure the parameters, run the evolution, then replay the best individual from any generation.

0

Generation

Best fitness

Avg fitness

Best food

Replay gen:Select a generation and press Replay

Fitness over generations

Run at least 2 generations to see the chart
How it works

Each snake is controlled by a small neural network (11 inputs → 16 hidden → 4 outputs). The inputs encode danger in three directions, the current heading, and food location relative to the head.

Fitness = steps alive + food eaten × board size × 2. Surviving longer is the primary goal; eating food provides a significant bonus and resets the starvation counter.

Each generation: evaluate all snakes → rank by fitness → carry elite individuals unchanged → fill the rest with crossover + mutation.

Click any row in the history table to select that generation, then press ▶ Replay to watch the best snake from that generation play.