Training neural network to solve the best way to play Snake using Genetic Algorithm. Program creates X (population) NN's, testing their performance in game and calculates score of each one using fitness function. NN with the best fitness value is mixed with second best NN and mutated X times to fill out population vector. The best progressing NN's are saved to file named with their fitness and generation.
Neural network is designed like this:
16 - Input Layer ( Every update Snake (his head) is sending rays in 8 directions (like in compass: N, NE, E, SE, S.. etc.)
8 for walls/borders and 8 for food if ray is coliding with object like Food-Apple or border then it takes that distance to neuron with 1/distance. If ray sent for Food is not pointing for it Neuron Value is set to 0
{ - Hidden Layer 8 - first with 8 neurons 6 - second with 6 neurons }
4 - Output Layer (4 output neurons(move): left, right, up, down)
- libsfml-dev
- cmake
- clang
Clone repository
git clone https://github.com/danihek07/SnakeNN
Navigate to it
cd SnakeNN
Use cmake
cmake .
Create executable with make
make
[SPACE] - fast/slow simulation
[0] and [9] - increasing and decreasing speed of simulation
[Q] - no drawing
[R] - draw best snake/draw all snakes
[U] - show input and output of best NN's
[T] - drawing raycasts on/off
[Y] - drawing neural network on/off
//If game mode is set to player you can move snake with arrows