Skip to content

Commit 841cea9

Browse files
committed
Update README.md
1 parent 2b6b7e7 commit 841cea9

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,58 @@ Learning process video with only 100 snakes:
99

1010
https://github.com/Morph21/Snake-AI-Neural-Network/assets/38075691/55d8d79f-c861-4cd6-9b94-21da6ee2e102
1111

12+
13+
# How it all works
14+
15+
## Natural selection of snakes:
16+
* The population of snakes is 2000
17+
* First generation have they neural network randomized
18+
* After each generation each snake will be scored based on fitness function
19+
* Then snakes are sorted based on fitness
20+
* Only 50% of best snakes will survive
21+
* Best snake won't be crossed over between generations, only mutation will happen for him
22+
* mutation rate is set on 3% that means that 3% of all neural network values will be randomized
23+
* every other snake have 90% chance to cross over with randomized one to change genes
24+
* every 100 generations best snake will populate all population (you could say it will be copied 2000 times)
25+
26+
## Fitness function
27+
I tried few fitness function, this one did work best since fitness sometimes was higher for snakes with lower scores
28+
29+
```
30+
if (snakeScore.getScore() < 10) {
31+
fitness = floor(lifetime * lifetime) * pow(2, snakeScore.getScore());
32+
} else {
33+
fitness = floor(lifetime * lifetime);
34+
fitness *= pow(2, 10);
35+
fitness *= (snakeScore.getScore() - 9);
36+
}
37+
```
38+
39+
## What snake can see and what is very important
40+
41+
* there are 12 vision inputs of snake
42+
* First 2 are head direction and tail direction
43+
* Snake can see in 3 directions Left, Right, In front of snake
44+
* Each direction takes up 3 vision inputs:
45+
* first is for seeing apples which is binary 1/0 value (I tested 1/distance but it was a lot worse)
46+
* second is distance to snake body if any was find in direction 1/distance rounded to 2 decimal points
47+
* third is distance to walls 1/distance rounded to 2 decimal points
48+
49+
I was experimenting with snake looking behind himself but there wasn't any significant improvements there
50+
51+
### The most important thing with inputs is that you need to think where snake is looking and not where you look.
52+
### So when snake is going to the right side of monitor and is looking up/front it means that it is looking to your right
53+
### It feels obvious but believe me it isn't and after I changed how snake see things I saw the biggest improvement in learning speed
54+
### head and tail direction is important after some point. Without these inputs my snake best score was 112
55+
56+
## Why snakes have different colours?
57+
Colour of snake is based on some math in matrix (neural network) and it determines the snake colour. So basically in theory you can see how similar to each other snakes are (I wouldn't base any real informations on that :))
58+
59+
# How to run best snake for yourself
60+
61+
* download SnakeAi.jar from release
62+
* download best.ser from release
63+
* run SnakeAi.jar then press "l" (small L on keyboard)
64+
* select best.ser file
65+
* wait until it's loaded
66+
* press "r" to resume snake movements

0 commit comments

Comments
 (0)