AI Learns to play Snake! Chrispresso Views: 77790 Like: 3044 Using a Genetic Algorithm and Neural Network, a population of snakes are able to gradually learn to play the game with no prior knowledge! Link to code: Blog: Music: 14.04.2022
i Love these AI Channels they're always so Fun to watch
If you listen closely, you can hear Code Bullet wailing and having an aneurism off in the distance.
I have never felt so outsmarted by a snake in my life,
Also maybe give the snakes a reward for being quicker to each apple to encourage a speedier strategy for larger grids
Meanwhile I can't even get 3 apples
Back in the 80s there was a multiplayer version of Snake for the Commodore 64 which was a type-in game in RUN Magazine, although the setup was that each snake would have a rule set for which direction to move given the surrounding 8 cells, and the point of the game was to program your snake with a rule set that made it outlast all the other snakes. Being the kind of nerdy kid I was I decided to see what happened if I randomly combined the rule sets of the two best-performing snakes, basically coming up with genetic algorithms on my own, and it got pretty good… then I let it also use the human player’s rule sets, and it got scary good.
So anyway I appreciate this video.
This was amazing.
isn't it a little misleading to say "no prior knowledge" when they inherited the knowledge from more successful snake player parents?
I found a typo at 1:53
Amazing, the AI discovered that the brute force approach is in fact the best approach. That kind of generalization is pretty crazy.
How are you calculating the fitness? Is it just how many apples it’s eaten or a function of apples eaten and time alive?
nice job man! it was harder than I thought making the representation. Really clever solution getting the direction account so that the snake knows that when the head goes to where the tail is currently it will not crash into itself 🙂
Shit this was good.
Amazing, works flawlessly! However when i want to save the file with the save_snake function, it wants to make the same folder over and over again after each generation, and blocks saving further generations. The other issue is that i did not find anything in my created save folder, but the folder itself and the json file had been created… Maybe I did something wrong. Without the saving functionality, i did not have any patience to wait and see the whole evolution, so please, can you give me short details where to insert the function? I did it into snake_app.py, under line 159. And how can i load the data? If you have a few minutes, a description here or to zed.tylor@(gmail(dot)com) would be really appreciated! Great and amazing work tho, your channel will be visited on every upload from now on! 🙂
I wonder if an AI could learn how to play MegaMan 3.
this is a cheat algorithm!
Could you please describe a little closer how does the binary vision for self work? Does it look only on one pixel ahead for self vision or all the way to the wall?
i wonder if its possible for it to learn that once the snake takes up half of the map, it should stay as close to its body as possible(otherwise it spends a increasing amount of time navigating) maybe something like "the closer it is to its body, the more likely it is to stay close to itself" which would work like "as the map fills up with its body, the weight of an input increases, once the map is 50% filled, the input is at 50% and then becomes active, triggering a change in the model. This would take forever to train normally, but if you start the snake in a map that is 49% full it wouldnt take long before the new part triggers so it could be tested and mutated quickly. it would also need to be able to keep functioning or it would die before it got to that 50%.
Me : nothing scares me, but those blue red lines, those scares me
It would be really interesting to take the genetic makeup that succeeded in winning the game and training it with a fitness function that prioritizes speed/effeciency and then try again on the large grids.
The computer is playing with itself…. give it some privacy! oO
The weight of neural network are consider as chromosome?
How did you visualize it so beautifully? 🙂
This stuff is so amazing. It learned – and then taught me – that the best algorithm is to stack itself in neat columns with patience to find the apple. Actually by the end it has learned to just scan vertically until it happens to find the apple and then is biased to return to an average scanning position at the top left of the domain.
It would be interesting to introduce a decaying apple that requires finding before disappearing because the snake needs to "eat". I'd guess that the most interesting behaviors could be developed by specifying the snake find the apple in X moves to stay alive + grow and if >X moves grows but loses energy and needs to find the next apple sooner or else die.
love it 👌👌❤️😂
So; it figured out hamiltonian on its own.
The first thing I see here is that the snake ended up following a hamilton cycle. It could be faster, too. Very interesting
I like how the goal of this game is to create an ouroboros
What a great video!! :O :')
After 1453 generations, it defaulted to Hamiltonian Cycle
Why would’ve give the whole grid in the input layer instead of only 8 directions? What different result could we have then?
But what about efficiency?
The snake AI : we don't do that here!!
Interesting, the latest iteration on the huge grid appears somewhat close to an optimal strategy.
how do you crossover neural networks without totally breaking their logic ?
I can say this snake has smart moves
Would have been a greater video with speech!
I especially appreaciate the part where you explained the input nodes. Most of the videos are missing that part.
this snake gonna take over eventually
Snake evolves into Ouroboros.
Such a stupid algorithm lol
Fascinating! Didn't that bigger board bear the same issue Code-Bullet ran into?
This is freaking awesome
Thank you! Great explaination!
Amazing!! I am speechless!!
I wish I know how you came up with the formula & the training layers. The only two parts that were not explained. Thank you very much. It feel like watching a baby walk and go to college then graduates. I felt very connected to the snake.