AI driven snake game using Reinforcement Learning and Deep Q Learning.
The game of Snake actually has a trivial, unbeatable solution. It can be driven by Simple Non-ML Technique by just traversing every block of the board, this gives the unbeateablre solution but it is very time taking and very brute force approach.
But we will be using reinforcement learning techinque.
The first question arises in mind that why we are using reinforcement learning instead of supervised machine learning, the answer is, in supervised ML algorithms need to be trained with an input and a “correct answer” called target.In this example, we don’t know what the best action to take at each stage of the game is, so a traditional approach would not be effective.
In Reinforcement Learning, we have two main components: the environment (our game) and the agent (our Snake.. or to be correct, the Deep Neural Network that drives our Snake’s actions). Every time the agent performs an action, the environment gives a reward to the agent, which can be positive or negative depending on how good the action was from that specific state.
I will explain the implementation of this SnakeAI step by step.
A simple snake board game which is user controlled is designed using pygame module is here https://github.com/vedantgoswami/SnakeGameAI/blob/main/snake_game.py
We have snake and food on the board randomly placed.
- calculate the state of the snake using the 11 values
- Now this current state is passed to the RL Model for the next state.
- After executing the next state calculate the reward. Rewards are defined as below:
- Eat food : +10
- Game Over : -10
- Else : 0
- Update the Q value and Train the Model.
After analysing the algorithm now we have to build the idea to proceed for coding this algorithm.
Our Project will be divided into three Modules named Agent, Game and Model
Initial Epochs After 100th Epochs