A small reinforcement learning project that trains and compares two Deep Q-Network (DQN) agents to play Snake in a headless 20×20 grid environment.
This project includes:
- a custom Snake game environment
- a pure DQN agent with experience replay and a target network
- a heuristic-enhanced DQN agent that avoids immediate death and prefers safer moves
- a side-by-side visual demo for comparing both agents
- training scripts to save model checkpoints to the
models/folder
The environment is implemented in game.py and models the classic Snake game as a reinforcement learning task.
- Grid size: 20 × 20
- State vector: 11 features describing danger, movement direction, and food direction
- Actions: 0 = straight, 1 = turn right, 2 = turn left
- Reward design:
- eating food: +10
- moving closer to food: +0.1
- moving farther from food: -0.1
- crash / starvation: large negative penalty
The agent logic is split between:
model.py: neural network architecture for the DQNagent.py: replay buffer, pure DQN agent, and heuristic DQN agenttrain.py: training loop and model savingdemo.py: pygame visualization for playing the agents side by side
snake/
├── agent.py # DQN agents and replay buffer
├── demo.py # Pygame visual comparison demo
├── game.py # Snake environment logic
├── model.py # Neural network model definition
├── models/ # Trained model checkpoints
├── requirements.txt # Python dependencies
├── train.py # Training script
└── README.md # Project documentation
The PureDQNAgent uses a basic DQN architecture with:
- epsilon-greedy exploration
- replay memory
- target network updates
- Bellman-style TD learning
This agent learns from rewards with no domain-specific guidance.
The HeuristicDQNAgent extends the same model with two safety heuristics:
- Death masking: it avoids moves that would instantly crash if another safe move exists.
- Flood-fill bias: when multiple safe actions are available, it prefers the move with more open space.
This improves training quality and gives the demo a stronger-looking agent.
Create a virtual environment and install dependencies:
python -m venv .venv
source .venv/bin/activate # macOS / Linux
# or .venv\Scripts\activate # Windows
pip install -r requirements.txtTrain both models with the default settings:
python train.pyTrain only the pure DQN agent:
python train.py --pure-onlyTrain only the heuristic DQN agent:
python train.py --heuristic-onlyCustomize the number of episodes:
python train.py --episodes 800The models are saved into:
models/pure_dqn.pthmodels/heuristic_dqn.pth
Run the visual comparison demo:
python demo.pyThe demo shows two games side by side:
- left panel: Pure DQN
- right panel: Heuristic DQN
Controls:
- Up / Down arrow: increase or decrease game speed
- Space: pause / resume
- Esc: quit
- The demo will exit early with a message if the saved model files are missing, so it is best to train once before launching the visualizer.
- Training is designed to run in a headless environment and is intentionally lightweight enough for CPU usage.
- The project uses
pygamefor visualization,torchfor deep learning, andnumpyfor state handling.
python train.py
python demo.pyThis gives you a trained Snake AI baseline and a visual comparison between raw DQN learning and heuristic-assisted DQN learning.