Welcome to the AI Plays Snake Game project! This repository demonstrates how an AI agent learns to play the classic Snake game using Reinforcement Learning, specifically the Deep Q-Learning (DQN) algorithm.
This project utilizes Deep Q-Learning, a type of reinforcement learning, to train an AI agent to play the Snake game. The agent interacts with the game environment, learns from its actions, and improves over time by optimizing its policy to maximize the reward.
The Snake game environment includes:
- A grid where the snake moves.
- A food item that the snake needs to consume to grow.
- Walls and the snake's own body, which must be avoided.
The AI agent learns by trial and error and uses its experiences to improve its decision-making.
- Custom Snake Game Environment: Built using Python with the Pygame library.
- Deep Q-Learning Implementation: Efficient training with experience replay and neural networks.
- Visualization: Real-time display of the game and AI performance.
- Checkpoints: Save and load trained models.
snakeai.mp4
Deep Q-Learning combines Q-learning with deep neural networks to approximate the Q-value function for large state-action spaces.
-
Environment:
- The Snake game environment provides states, rewards, and transitions based on the agent's actions.
-
State Representation:
- The state includes information about the snake's position, food location, and potential obstacles.
-
Action Space:
- The agent can choose from four actions: move up, down, left, or right.
-
Reward Function:
- Positive reward for eating food (+10).
- Negative reward for collisions (game over) (-10).
-
Deep Q-Network (DQN):
- A neural network approximates the Q-value for each state-action pair.
- Input: Current state.
- Output: Q-values for all possible actions.
-
Experience Replay:
- Stores past experiences (state, action, reward, next state) in a replay buffer.
- Samples mini-batches from the buffer for training to break temporal correlations.
-
Target Network:
- A separate network updated periodically to stabilize training.
- Initialize the replay buffer and neural network.
- For each episode:
- Reset the game environment.
- Observe the initial state.
- For each step in the episode:
- Choose an action using an epsilon-greedy policy.
- Perform the action and observe the reward and next state.
- Store the experience in the replay buffer.
- Sample a mini-batch from the buffer and update the Q-network using the Bellman equation: [ Q(s, a) \leftarrow Q(s, a) + \alpha \big[r + \gamma \max_a' Q'(s', a') - Q(s, a)\big] ]
- Update the target network periodically.
- Clone this repository:
git clone https://github.com/bhavya0304/SnakeAI.git cd SnakeAI - Install the required dependencies:
pip install -r requirements.txt
- Train the AI agent:
python ./SnakeAI_Final/agent.py
The AI agent starts by making random moves but gradually learns to optimize its actions, achieving higher scores over time. Training results are logged and can be visualized to observe the improvement in performance.
Contributions are welcome! Feel free to open issues or submit pull requests to enhance the project.