From 841cea948f48036d2b48c96fa5ca6adb7e5870d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Gruszczy=C5=84ski?= Date: Thu, 1 Jun 2023 01:17:41 +0200 Subject: [PATCH] Update README.md --- README.md | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/README.md b/README.md index 4a4f93c..93590e0 100644 --- a/README.md +++ b/README.md @@ -9,3 +9,58 @@ Learning process video with only 100 snakes: https://github.com/Morph21/Snake-AI-Neural-Network/assets/38075691/55d8d79f-c861-4cd6-9b94-21da6ee2e102 + +# How it all works + +## Natural selection of snakes: +* The population of snakes is 2000 +* First generation have they neural network randomized +* After each generation each snake will be scored based on fitness function +* Then snakes are sorted based on fitness +* Only 50% of best snakes will survive +* Best snake won't be crossed over between generations, only mutation will happen for him +* mutation rate is set on 3% that means that 3% of all neural network values will be randomized +* every other snake have 90% chance to cross over with randomized one to change genes +* every 100 generations best snake will populate all population (you could say it will be copied 2000 times) + +## Fitness function +I tried few fitness function, this one did work best since fitness sometimes was higher for snakes with lower scores + +``` +if (snakeScore.getScore() < 10) { + fitness = floor(lifetime * lifetime) * pow(2, snakeScore.getScore()); +} else { + fitness = floor(lifetime * lifetime); + fitness *= pow(2, 10); + fitness *= (snakeScore.getScore() - 9); +} +``` + +## What snake can see and what is very important + +* there are 12 vision inputs of snake +* First 2 are head direction and tail direction +* Snake can see in 3 directions Left, Right, In front of snake +* Each direction takes up 3 vision inputs: +* first is for seeing apples which is binary 1/0 value (I tested 1/distance but it was a lot worse) +* second is distance to snake body if any was find in direction 1/distance rounded to 2 decimal points +* third is distance to walls 1/distance rounded to 2 decimal points + +I was experimenting with snake looking behind himself but there wasn't any significant improvements there + +### The most important thing with inputs is that you need to think where snake is looking and not where you look. +### 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 +### 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 +### head and tail direction is important after some point. Without these inputs my snake best score was 112 + +## Why snakes have different colours? +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 :)) + +# How to run best snake for yourself + +* download SnakeAi.jar from release +* download best.ser from release +* run SnakeAi.jar then press "l" (small L on keyboard) +* select best.ser file +* wait until it's loaded +* press "r" to resume snake movements \ No newline at end of file