← Open Source
greerviau

SnakeAI

Train a Neural Network to play Snake using a Genetic Algorithm.

TutorialsML/AI fundamentalsProcessing
Open on GitHub
Momentum
+-1stars in 24 hours-0.0%
2.39k
Stars
556
Forks
+0
This week
1
Contributors
Created 2018-11-01 · Updated 2026-10-15 · #18704 today
Top developers

greerviau/SnakeAI is ranked 18,704 with 2,389 stars.

Same-category comparison

greerviau/SnakeAI is ranked 18,704 among these 8 peers; adithya-s-k/AI-Engineering.academy is ranked 18,705.

Same-category comparison
NameRankMetrics
PaddlePaddle/book18,663
Language:
Jupyter Notebook
Stars:
2,751
Forks:
1,301
openai/openai-quickstart-node18,677
Language:
JavaScript
Stars:
2,644
Forks:
1,936
Description:
Node.js example app from the OpenAI API quickstart tutorial.
Project-MONAI/tutorials18,685
Language:
Jupyter Notebook
Stars:
2,569
Forks:
808
Description:
MONAI Tutorials.
zackchase/mxnet-the-straight-dope18,687
Language:
Jupyter Notebook
Stars:
2,563
Forks:
705
adithya-s-k/AI-Engineering.academy18,705
Language:
Jupyter Notebook
Stars:
2,383
Forks:
286
Description:
Mastering Applied AI, One Concept at a Time.
addyosmani/gemini-cli-tips18,709
Stars:
2,360
Forks:
105
Description:
Gemini CLI Tips and Tricks.
hzwer/ICCV2019-LearningToPaint18,718
Language:
Python
Stars:
2,315
Forks:
312
Description:
ICCV2019 - Learning to Paint With Model-based Deep Reinforcement Learning.
mrgoonie/claudekit-skills18,728
Language:
Python
Stars:
2,226
Forks:
421
Description:
All powerful skills of ClaudeKit.cc.
Key parameters and specifications
Key parameters and specifications
ParameterValue
LanguageProcessing
CategoryTutorials
SubcategoryML/AI fundamentals
KeywordsRL
Stars2,389
Forks556
Contributors1
Created date2018-11-01
Updated date2026-10-15
Stars in 1 day-1
Stars in 7 days0
Rank18,704
Previous rank8,724
Rank change-9,980
7-day stars rank12,049
Previous 7-day stars rank9,441
7-day stars rank change-2,608
Total stars rank5,472
Previous total stars rank5,465
Total stars rank change-7
CountryUSA
README summary

SnakeAI Download and Run To run the program you will need Processing YouTube Video Snake Neural Network Each snake contains a neural network. The neural network has an input layer of 24 neurons, 2 hidden layers of 16 neurons, and one output layer of 4 neurons. Note: Network can now be customized with the number of hidden layers as well as the number of neurons in the hidden layers. Vision The snake can see in 8 directions. In each of

these directions the snake looks for 3 things: Distance to food Distance to its own body Distance to a wall 3 x 8 directions = 24 inputs. The 4 outputs are simply the directions the snake can move. Evolution Natural Selection Each generation a population of 2000 snakes is created. For the first generation, all of the neural nets in each of the snakes are initialized randomly. Once the entire population is dead, a fitness score is calculated for each of

the snakes. Using these fitness scores, some of the best snakes are selected to reproduce. In reproduction two snakes are selected and the neural nets of each are crossed and then the resulting child is mutated. This is repeated to create a new population of 2000 new snakes. Fitness A snakes fitness is dependant on how long the snake stays alive as well as its score. However they are not equally important, having a higher score is rewarded more than

a snake who simply stays alive. There is the possibility however that a snake may evolve a strategy where it loops in a certain pattern and never dies. Even though having a high score is prioritized more, if a snake can stay alive forever then that is a clear problem. To avoid this each snake is giving 200 starting moves at the beginning of its life. Every time it eats a piece of food it gains 100 more moves, with

a maximum of 500 moves. This means that snakes who evolve to go in loops will eventually die and snakes who go for the food will not only have a higher score, but stay alive longer. Crossover & Mutation When two snakes are selected for reproduction, what happens is that the snakes brains are crossed with each other. What this means is that part of one parents brain is mixed with part of the second parents and the resulting brain is

assigned to the child. After the crossover the brain is also mutated according to a mutation rate. The mutation rate determines how much of the brain will be randomly altered. Data Collection Graphing The graph represents the score of the best snake from each generations. In some generations the graph may dip bellow the previous, this is because even though the score may have been worse, some trait allowed the snake to live longer and gain a higher overall fitness

. Save & Load Models can be saved and loaded in order to test a model in new situations. The weights for

Frequently asked questions

How many stars and forks are recorded?

greerviau/SnakeAI has 2,389 stars and 556 forks.

What is the repository rank?

greerviau/SnakeAI is ranked 18,704.

What language and category are recorded?

greerviau/SnakeAI has 2,389 stars, uses Processing and belongs to Tutorials.

Which repository is closest in rank?

greerviau/SnakeAI is compared with adithya-s-k/AI-Engineering.academy, ranked 18,705 with 2,383 stars.

README

SnakeAI

Download and Run

To run the program you will need Processing

YouTube Video

Snake

Neural Network

Each snake contains a neural network. The neural network has an input layer of 24 neurons, 2 hidden layers of 16 neurons, and one output layer of 4 neurons. Note: Network can now be customized with the number of hidden layers as well as the number of neurons in the hidden layers.

Vision

The snake can see in 8 directions. In each of these directions the snake looks for 3 things:

  • Distance to food
  • Distance to its own body
  • Distance to a wall

3 x 8 directions = 24 inputs. The 4 outputs are simply the directions the snake can move.

Evolution

Natural Selection

Each generation a population of 2000 snakes is created. For the first generation, all of the neural nets in each of the snakes are initialized randomly. Once the entire population is dead, a fitness score is calculated for each of the snakes. Using these fitness scores, some of the best snakes are selected to reproduce. In reproduction two snakes are selected and the neural nets of each are crossed and then the resulting child is mutated. This is repeated to create a new population of 2000 new snakes.

Fitness

A snakes fitness is dependant on how long the snake stays alive as well as its score. However they are not equally important, having a higher score is rewarded more than a snake who simply stays alive. There is the possibility however that a snake may evolve a strategy where it loops in a certain pattern and never dies. Even though having a high score is prioritized more, if a snake can stay alive forever then that is a clear problem. To avoid this each snake is giving 200 starting moves at the beginning of its life. Every time it eats a piece of food it gains 100 more moves, with a maximum of 500 moves. This means that snakes who evolve to go in loops will eventually die and snakes who go for the food will not only have a higher score, but stay alive longer.

Crossover & Mutation

When two snakes are selected for reproduction, what happens is that the snakes brains are crossed with each other. What this means is that part of one parents brain is mixed with part of the second parents and the resulting brain is assigned to the child. After the crossover the brain is also mutated according to a mutation rate. The mutation rate determines how much of the brain will be randomly altered.

Data Collection

Graphing

The graph represents the score of the best snake from each generations. In some generations the graph may dip bellow the previous, this is because even though the score may have been worse, some trait allowed the snake to live longer and gain a higher overall fitness.

Save & Load

Models can be saved and loaded in order to test a model in new situations. The weights for each connection are saved in a CSV file. The evolution graph is also saved in order to view the evolution progress of the model.

Nearby in the ranking
Similar items