Skip to content

Neural network learns to play snake game using reinforcement learning algorithm. Built with tf.keras and pygame.

Notifications You must be signed in to change notification settings

kei5uke/snake-game-ai

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

snake-game-ai

I created snake game and trained neural network to beat the game because why not.

Base Game (Pygame)

I used the Pygame library for creating the base snake game.
Snake at the start and apple(food) at each moment will be placed on the map randomly.

Neural Network

To make snake smart, we need to give knowledge to it.
Considering the main tasks of the game are survive and earn score, the best way to make the snake smart is giving which direction is safe and close to the food.

Features as an input

I decided to give these features below for the NN input.
Output (label) will be collected after the snake moves 1 step every time.

Input

  • Blocked direction [ UP, RIGHT, DOWN, LEFT ] True : 1 / False : 0
  • Distance of the food
  • Distance of walls
  • Normalized angle between snake's movement direction and direction to an apple
  • Suggested direction [ UP, RIGHT, DOWN, LEFT ] True : 1 / False : 0

Output

  • 1 - Snake survived and moved to the right direction (direction to the food)
  • 0 - Snake survived but the direction was wrong
  • -1 - Snake died

NN Architecture

2 hidden layers of Relus, and linear layer as an output layer.

Experiment

I let the snake moves randomly and see how they make difference in their movement.
Let's see how snake evolves in every generation.

Data collection and result

As a result, I got 13 million steps of data from 100K generation of snakes.
Here's the average score of every generation.
avg_score
As you can see, the average score goes higher as the amount of data increases. Especially from the gen 5k to 400k.
Above gen 600k decrease avg score so probably they are overfitting and need some parameter tuning.

Gen 5000

gen5k

Gen 10000

10k

Gen 100000

100k

Gen 200000

200k

Gen 400000

400k

Gen 600000

600k

Gen 800000

800k
Movement of snakes appears to be gradually becoming more linear.

About

Neural network learns to play snake game using reinforcement learning algorithm. Built with tf.keras and pygame.

Resources

Stars

Watchers

Forks

Languages