Midnapore College (Autonomous) : Snake Game Using AI

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

MIDNAPORE COLLEGE(AUTONOMOUS)

Snake Game using AI

Name-Sakshi Kumari
Roll - 3373
Sem- 6th
Subject: COSC(HONS)
PROJECT: Literature review on implementation of AI in Snake game using Genetic Algorithm and
Neural network
1

Overview

Each snake contains a neural network. The neural network has an input layer of 8
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.

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

The goal of this project is to develop an AI Bot able to learn how to play the popular
game Snake from scratch. In order to do it, I implemented a Deep Reinforcement
Learning algorithm. This approach consists in giving the system parameters related to
its state, and a positive or negative reward based on its actions. No rules about the
game are given, and initially the Bot has no information on what it needs to do. The goal
for the system is to figure it out and elaborate a strategy to maximize the score - or the
reward.

Additionally, it is possible to run the Bayesian Optimization method to find the optimal
parameters of the Deep neural network, as well as some parameters of the Deep RL
approach.
2

Specifications

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.
3

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.

Research Based on Snake Game Using Neural Networks &


Genetic Algorithm:

In a paper, researchers from the University of Technology, Poland used a neural


network structure that decides what action to take from any given input. The
Neural Network is called DNA by the researchers. The DNA class is the most
important part of the snake as it is the “brain” that makes every decision. The
class has matrixes with weights and separate ones with bias, which represent
each layer of the neural network. The next step is followed by creating a function
that allows calculating its performance, where the performance includes the
number of moves the snake executed without dying and scores. The researchers
used neural networks.The genetic algorithm to find out which method and
parameters are the best. At first, they randomly generated the population of
snakes with an optimum size of 2000.
4

Then they let the snakes play in order to understand how many steps were
executed and how many apples the snake ate. This led to the calculation of
fitness of each snake that helps to see which one performed the best and which
one should have a higher probability of being chosen for breeding. For the
Selection part, the researchers chose a pair of snakes (parents) that will give
DNA to the new snake (child) where the probability of being chosen is based on
fitness. After choosing the parents, the researchers crossover their DNA by
taking some of the weights from the father and some from the mother and
applying it to their child. After selection, the next step is a mutation which follows
when every new snake inherits the neural network from parents. Then the
playing and mutation processes are repeated in order to get the best results.

Languages
Python 100.0%
5

Game Rules
Avoid Walls
If a snake leaves the last tile of the board, they will die.
6

Eat Food
Eating a food pellet will make snakes one segment longer. Snakes grow out of their tail:
new tail segment will appear in the same square that the tail was in the previous turn.

Eating a food pellet will restore snakes' health-points to 100.

The amount of food can vary from game to game, but within the same game it will
always stay the same. As soon as a piece of food is eaten, it will respawn at a random,
unoccupied location on the next turn.

Don't Starve
Every turn snakes will loose one health-point. Health-points serve like the snake's
hunger bar, and if it reaches zero, the snake will starve and die. Eating food will restore
snake's health to one-hundred points on the next turn.

Don't Collide with Snakes' Tails


If a snake collides with itself or any other snakes' tails, it dies.

REFERENCES:
https://en.wikipedia.org/wiki/Artificial_intelligence
https://builtin.com/artificial-intelligence
https://towardsdatascience.com/training-a-snake-game-ai-a-literature-review-1cdddcd1862 f

You might also like