I created snake game and trained neural network to beat the game because why not.
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.
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.
I decided to give these features below for the NN input.
Output (label) will be collected after the snake moves 1 step every time.
- 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
- 1 - Snake survived and moved to the right direction (direction to the food)
- 0 - Snake survived but the direction was wrong
- -1 - Snake died
2 hidden layers of Relus, and linear layer as an output layer.
I let the snake moves randomly and see how they make difference in their movement.
Let's see how snake evolves in every generation.
As a result, I got 13 million steps of data from 100K generation of snakes.
Here's the average score of every generation.
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.
Movement of snakes appears to be gradually becoming more linear.