Skip to content

GSAUC3/Snake-Ai

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Snake Ai (using Genetic Algorithm)

Libraries Required:

  • Pygame: to create the snake game
  • PyTorch: to create the "brain" of the snake a.k.a a neural net

Demo

What is Genetic Algorithm is all about?

Genetic algorithm is a guided search algorithm, which is inspired by Darwin's theory of natural selection. In this context, we are searching for that one snake 🐍, that can survive in any given environment.

Steps involved in the algorithm:

  • POPULATION | initialize a population
  • FITNESS | calculate the fitness of each and every snake
  • SELECTION | select the snakes with higher fitness value
  • CROSSOVER | after selection, pick randomly two snakes and produce a child using crossover method
  • MUTATION | then the child will go through a mutation process, based on a mutation rate, usually lower than 0.1

Flowchart:

Neural Network Architecture

Snakenet(
  (brain): Sequential(
    (0): Linear(in_features=13, out_features=16, bias=False)
    (1): ReLU()
    (2): Linear(in_features=16, out_features=8, bias=False)
    (3): ReLU()
    (4): Linear(in_features=8, out_features=4, bias=False)
  )
)

Click the image below to see it on youtube: