AI Learns to play Snake! -

AI Learns to play Snake!

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:




  1. i Love these AI Channels they're always so Fun to watch

  2. If you listen closely, you can hear Code Bullet wailing and having an aneurism off in the distance.

  3. 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

  4. 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.

  5. isn't it a little misleading to say "no prior knowledge" when they inherited the knowledge from more successful snake player parents?

  6. Amazing, the AI discovered that the brute force approach is in fact the best approach. That kind of generalization is pretty crazy.

  7. How are you calculating the fitness? Is it just how many apples it’s eaten or a function of apples eaten and time alive?

  8. 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 🙂

  9. 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, 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! 🙂

  10. I wonder if an AI could learn how to play MegaMan 3.

  11. 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?

  12. 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%.

  13. Me : nothing scares me, but those blue red lines, those scares me

  14. 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.

    Awesome vid.

  15. The computer is playing with itself…. give it some privacy! oO

  16. The weight of neural network are consider as chromosome?

  17. How did you visualize it so beautifully? 🙂

  18. 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.

    Nevertheless, incredible.

  19. So; it figured out hamiltonian on its own.

  20. The first thing I see here is that the snake ended up following a hamilton cycle. It could be faster, too. Very interesting

  21. I like how the goal of this game is to create an ouroboros

  22. After 1453 generations, it defaulted to Hamiltonian Cycle

  23. Why would’ve give the whole grid in the input layer instead of only 8 directions? What different result could we have then?

  24. But what about efficiency?
    The snake AI : we don't do that here!!

  25. Interesting, the latest iteration on the huge grid appears somewhat close to an optimal strategy.

  26. how do you crossover neural networks without totally breaking their logic ?

  27. Would have been a greater video with speech!

  28. I especially appreaciate the part where you explained the input nodes. Most of the videos are missing that part.

  29. Fascinating! Didn't that bigger board bear the same issue Code-Bullet ran into?

  30. 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.

Leave a Reply

Your email address will not be published.