Skip to content

kasundularaam/snake_ai_q_learning

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

4 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ Snake AI - Q-Learning Training Dashboard

Python Pygame License AI Platform

A beautiful, modern implementation of the classic Snake game with an AI agent that learns to play using Q-Learning reinforcement learning. Features a professional training dashboard with real-time statistics and performance monitoring.

Screenshot

โœจ Features

  • ๐Ÿค– Q-Learning AI Agent - Learns to play Snake through trial and error
  • ๐ŸŽจ Beautiful Modern Interface - Professional training dashboard with real-time stats
  • ๐Ÿ“Š Real-time Performance Monitoring - Track learning progress, exploration rate, and performance trends
  • ๐Ÿ’พ Model Persistence - Save and load trained models
  • ๐ŸŽฎ Interactive Controls - Pause, reset, and save during training
  • ๐Ÿ“ˆ Visual Learning Analytics - Progress bars, trend indicators, and performance metrics

๐ŸŽฏ What You'll See

The interface features:

  • ๐ŸŽฎ Large, clear game area with enhanced graphics
  • ๐Ÿ“‹ Real-time statistics panel showing score, episodes, and learning progress
  • ๐Ÿ” Exploration rate visualization with progress bars
  • ๐Ÿ“ˆ Performance trend indicators (Improving/Declining/Stable)
  • โฑ๏ธ Training progress tracking
  • ๐ŸŒ™ Modern dark theme with professional color scheme

๐Ÿš€ Installation

๐Ÿ“‹ Prerequisites

  • ๐Ÿ Python 3.7 or higher
  • ๐Ÿ“ฆ pip package manager

๐Ÿ“š Dependencies

Install the required libraries:

pip install pygame numpy

Or install them individually:

pip install pygame
pip install numpy

๐Ÿ  Using Virtual Environment (Recommended)

# Create virtual environment
python -m venv snake_ai_env

# Activate it (Windows)
snake_ai_env\Scripts\activate

# Activate it (macOS/Linux)
source snake_ai_env/bin/activate

# Install dependencies
pip install pygame numpy

๐ŸŽฎ Usage

โ–ถ๏ธ Running the Training

python snake_ai.py

๐ŸŽ›๏ธ Controls

  • โธ๏ธ SPACE - Pause/Resume training
  • ๐Ÿ”„ R - Reset training (clear all progress)
  • ๐Ÿ’พ S - Save model manually
  • โŒ ESC - Exit application

๐ŸŒŸ First Run

On first run, the AI will start learning from scratch. The model will be automatically saved as snake_q_table.pkl every 100 episodes and when you exit the program.

๐Ÿ“‚ Loading Existing Model

If a saved model (snake_q_table.pkl) exists in the same directory, it will be automatically loaded when you start the program.

๐Ÿง  How It Works

๐Ÿค– Q-Learning Algorithm

The AI uses Q-Learning, a model-free reinforcement learning algorithm that learns the quality of actions (Q-values) for different states:

  1. ๐ŸŽฏ State Representation: The game state includes:

    • โš ๏ธ Danger detection (walls and snake body in each direction)
    • ๐ŸŽ Food direction relative to snake head
    • โžก๏ธ Current movement direction
    • ๐Ÿ“Š This creates a 12-dimensional boolean state space
  2. ๐ŸŽฎ Actions: The snake can take 3 actions:

    • โฌ†๏ธ Continue straight (0)
    • โ†—๏ธ Turn right (1)
    • โ†–๏ธ Turn left (2)
  3. ๐Ÿ† Rewards:

    • โœ… +10 for eating food
    • โŒ -10 for collision (game over)
    • โฐ -0.1 for each step (encourages efficiency)
    • ๐Ÿ”„ -5 for taking too long without eating (prevents infinite loops)
  4. โš™๏ธ Learning Parameters:

    • ๐Ÿ“š Learning rate (ฮฑ): 0.1
    • ๐Ÿ”ฎ Discount factor (ฮณ): 0.95
    • ๐ŸŽฒ Exploration rate (ฮต): Starts at 1.0, decays to 0.01

๐Ÿ”„ Training Process

  1. ๐ŸŽฒ Exploration Phase: Initially, the AI explores randomly (high ฮต value)
  2. ๐Ÿ“š Learning Phase: As training progresses, the AI gradually exploits learned knowledge
  3. ๐ŸŽฏ Convergence: Eventually, the AI develops optimal strategies for playing Snake

๐Ÿ“ Project Structure

snake_ai/
โ”‚
โ”œโ”€โ”€ ๐Ÿ snake_ai.py           # Main game and AI implementation
โ”œโ”€โ”€ ๐Ÿ’พ snake_q_table.pkl     # Saved Q-table (created after training)
โ”œโ”€โ”€ ๐Ÿ“ธ screenshot.png        # Project screenshot
โ””โ”€โ”€ ๐Ÿ“– README.md            # This file

๐Ÿ—๏ธ Code Structure

๐Ÿงฉ Main Classes

  • ๐ŸŽฎ SnakeGame - Game logic and environment
  • ๐Ÿค– QLearningAgent - AI agent with Q-learning implementation
  • ๐ŸŽจ GameRenderer - Modern UI and visualization

๐Ÿ”ง Key Methods

  • get_state() - Converts game situation to state representation
  • choose_action() - Epsilon-greedy action selection
  • update() - Q-value updates using Bellman equation
  • render() - Beautiful dashboard rendering

๐Ÿ“Š Performance Metrics

The dashboard displays several key metrics:

  • ๐Ÿ† Score - Current game score
  • ๐ŸŽฌ Episode - Number of games played
  • ๐Ÿง  States - Number of unique states learned
  • ๐ŸŽฒ Exploration Rate - Current ฮต value (exploration vs exploitation)
  • ๐Ÿ“ˆ Average Score - Running average of last 100 games
  • ๐Ÿ‘‘ Best Score - Highest score achieved
  • ๐Ÿ“Š Performance Trend - Whether AI is improving, declining, or stable

๐Ÿ’ก Training Tips

  1. ๐ŸŒฑ Initial Learning: The first few hundred episodes will show poor performance as the AI explores
  2. ๐Ÿ“ˆ Improvement Phase: Around episodes 500-1000, you should see steady improvement
  3. ๐ŸŽฏ Convergence: After 1000+ episodes, performance should stabilize at a high level
  4. โณ Patience: Q-Learning can take time to converge - let it run for several thousand episodes

๐ŸŽ›๏ธ Customization

โš™๏ธ Adjusting Learning Parameters

In the QLearningAgent class constructor:

agent = QLearningAgent(
    alpha=0.1,        # ๐Ÿ“š Learning rate
    gamma=0.95,       # ๐Ÿ”ฎ Discount factor
    epsilon=1.0,      # ๐ŸŽฒ Initial exploration rate
    epsilon_decay=0.995,  # ๐Ÿ“‰ Exploration decay rate
    epsilon_min=0.01  # ๐ŸŽฏ Minimum exploration rate
)

๐ŸŽฎ Modifying Game Settings

In the constants section:

GRID_SIZE = 40        # ๐Ÿ“ Size of each grid cell
GRID_WIDTH = 10       # โ†”๏ธ Number of cells horizontally
GRID_HEIGHT = 10      # โ†•๏ธ Number of cells vertically
WINDOW_WIDTH = 1200   # ๐Ÿ–ฅ๏ธ Window width
WINDOW_HEIGHT = 700   # ๐Ÿ–ฅ๏ธ Window height

๐ŸŽจ Changing Visualization

The GameRenderer class contains all visual settings including colors, fonts, and layout parameters.

๐Ÿ”ง Technical Details

  • ๐Ÿ’ป Language: Python 3.7+
  • ๐ŸŽฎ Graphics: Pygame
  • ๐Ÿค– AI Algorithm: Q-Learning (Tabular)
  • ๐Ÿ“Š State Space: Discrete (2^12 possible states)
  • ๐ŸŽฏ Action Space: Discrete (3 actions)
  • ๐Ÿ’พ Model Persistence: Pickle format

๐Ÿ”ง Troubleshooting

โš ๏ธ Common Issues

  1. ๐Ÿ“ฆ Module not found: Make sure pygame and numpy are installed
  2. ๐ŸŒ Slow performance: Try reducing render_every parameter or window size
  3. ๐Ÿ“‰ No improvement: Increase training episodes or adjust learning parameters
  4. ๐Ÿ’พ Save/load errors: Ensure write permissions in the project directory

โšก Performance Optimization

  • ๐Ÿ–ฅ๏ธ Reduce render_every to update display less frequently
  • ๐Ÿ“ฑ Decrease window size for faster rendering
  • โšก Adjust epsilon_decay for faster convergence

๐Ÿ“„ License

This project is open source and available under the MIT License.

๐Ÿ™ Credits

  • ๐Ÿ Built with Python and Pygame
  • ๐Ÿค– Uses Q-Learning reinforcement learning algorithm
  • ๐ŸŽจ Modern UI design inspired by professional ML dashboards

๐Ÿค Contributing

Feel free to fork this project and submit pull requests for improvements. Some areas where contributions would be welcome:

  • ๐Ÿง  Additional AI algorithms
  • โšก Performance optimizations
  • ๐ŸŽจ UI/UX improvements
  • ๐Ÿ“Š Better state representations
  • ๐Ÿ“š Documentation improvements

๐ŸŽ‰ Happy Learning! Watch your AI master the game of Snake through the power of reinforcement learning! ๐Ÿ๐Ÿค–

About

Q-Learning reinforcement learning implementation for Snake game with modern training dashboard and real-time performance monitoring.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages