AI Learns to Play Snake - Q Learning Explanation

Поділитися
Вставка
  • Опубліковано 12 вер 2024
  • I use a Q Learning algorithm in Python to train an agent to play the classic snake game 🐍. The goal of the game is to direct the snake toward eating apples, which allow the snake to grow longer. The snake also must avoid walls and its own body - crashing into these ends the game.
    While it is easy for a human to play this game fairly well, I wanted to see how good of a snake AI we can train using Q Learning. I begin the video by presenting the viewers with a Q learning explanation. Specifically, I explain what a Q value is, explain how the bellman equation works, go over the features & rewards of this game, explain exploration vs. exploitation, and justify my choice of using vanilla Q learning over deep Q learning.
    Afterwards, I show how well the snake game bot does after 10, 100, 200, 400, and 7,500 episodes of training. As expected, the agent starts out being terrible at the game but continuously improves as the Q table gets filled up with more and more accurate Q values.
    By the end of the video, the snake game AI ends up getting a score over 100 (meaning it ate more than 100 apples in one run before dying), which is very good given the limited set of features that the agent had to work with.
    In a future video, I will explore how Deep Q Learning can be applied to this game and give the agent many more features to work with, hopefully resulting in an improvement over the existing performance.
    If you enjoyed the video, consider subscribing to the channel!
    More about Q Learning: towardsdatasci...
    Code: github.com/tec...
    #snakegame #coding #programming

КОМЕНТАРІ • 6

  • @TechTribeCommunity
    @TechTribeCommunity  2 роки тому +7

    If you enjoyed the video, I recommend you subscribe! Also, feel free to ask any questions in the comments!

  • @SelyHaudy
    @SelyHaudy 5 місяців тому +1

    thank you for your tutorial and code. I learn a lot from it.

  • @jalnagnanvi5742
    @jalnagnanvi5742 Рік тому +1

    How do you run it?

  • @SamHall-cg9dq
    @SamHall-cg9dq Рік тому +1

    can you make this a website

  • @lilianchabane7695
    @lilianchabane7695 2 роки тому +1

    how do we run it ?

    • @eliezerpekar9223
      @eliezerpekar9223 4 місяці тому +1

      import pygame
      def main():
      # Initialize the Q-learning agent
      agent = SnakeQAgent()
      # Start the training process
      agent.train()
      # After training, optionally run a visual demonstration
      pygame.quit() # Ensure pygame is properly shutdown before restarting it in the demo
      demo(episode='10000') # You might want to specify which model/episode to demo
      def demo(episode):
      # Initialize the game with visuals
      game = VisualSnake()
      # Load and run the game using the trained Q-table
      game.run_game(episode)
      if __name__ == "__main__":
      main()