Skip to content

Reinforcement Learning and Deep Q-Learning (DQN) agent that learns to play Snake using the Bellman equation and neural networks.

Notifications You must be signed in to change notification settings

Alireza2317/snake_rl_agent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

87 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Snake-RL: Reinforcement Learning from Scratch 🐍

Python NumPy Pygame License

A Reinforcement Learning agent that masters the classic Snake game using Deep Q-Learning (DQN).

Key Differentiator: This project does not use ML frameworks like PyTorch or TensorFlow. The Neural Network (backpropagation, optimization, and dense layers) is implemented entirely from scratch using NumPy math in nn.py.

📸 Demo

Snake RL Demo

🧠 Key Concepts

  • Custom ML Engine: A raw implementation of Feed Forward networks and Gradient Descent.
  • Deep Q-Learning: Using the custom engine to approximate Q-values.
  • Experience Replay: Storing past game states to train the custom network in mini-batches.

🚀 Getting Started

1. Installation

Clone the repo and set up the environment:

git clone https://github.com/Alireza2317/snake_rl_agent
cd Snake_RL

# Create virtual env (Optional)
python -m venv env
source env/bin/activate  # Windows: env\Scripts\activate

# Install dependencies (Mainly Pygame & NumPy)
pip install -r requirements.txt

2. Usage

Run the main application. The agent will start with random moves and progressively learn from the custom neural net.

python app.py

Check configs in app.py to tweak hyperparameters like learning rate or epsilon decay.

📂 Project Structure

.
├── app.py          # Entry point: Orchestrates training loop and game rendering
├── agent.py        # The DQN Agent logic
├── snake.py        # The Game Environment (Pygame logic)
├── nn.py           # ⚠️ CUSTOM ENGINE: Neural Net implementation from scratch

🛠️ Tech Stack

  • Language: Python
  • Math Backend: NumPy (Matrix operations)
  • Game Engine: Pygame

About

Reinforcement Learning and Deep Q-Learning (DQN) agent that learns to play Snake using the Bellman equation and neural networks.

Topics

Resources

Stars

Watchers

Forks

Languages