Skip to content

Latest commit

 

History

History
152 lines (119 loc) · 5.09 KB

README.md

File metadata and controls

152 lines (119 loc) · 5.09 KB

Self-Learning Snake AI using Neural Network

This project implements a self-learning Snake AI game using HTML, Sketch.js (or p5.js), and TensorFlow.js for the neural network. The AI-controlled snake learns to navigate the game environment by avoiding walls and optimizing its movements to achieve higher scores over time.

Table of Contents

Features

  • HTML5 Canvas & Sketch.js: Renders the game in the browser for smooth animations.
  • TensorFlow.js Neural Network: Implements a neural network using TensorFlow.js for real-time learning.
  • Self-Learning AI: The snake learns to avoid walls and optimize its path through reinforcement learning.
  • Interactive Controls: Pause, step through, and observe the learning process.
  • Visualization of Neural Network: Real-time rendering of the neural network's decision-making process.

How It Works

Game Mechanics

  • Snake Movement: The snake moves on a grid, consuming food to grow longer.
  • Collision Detection: The snake must avoid colliding with walls or its own body.
  • Scoring System: Points are awarded for each piece of food consumed.

Neural Network Architecture

  • Inputs: The neural network takes a state vector of four elements representing:
    • Wall Up: Whether there's a wall above the snake.
    • Wall Down: Whether there's a wall below the snake.
    • Wall Right: Whether there's a wall to the right of the snake.
    • Wall Left: Whether there's a wall to the left of the snake.
  • Hidden Layer: Consists of 4 neurons with ReLU activation.
  • Outputs: A vector of four probabilities corresponding to the actions:
    • Move Up
    • Move Down
    • Move Left
    • Move Right

Learning Process

  • State Evaluation: The snake assesses its environment using the state vector.
  • Action Selection: The neural network predicts the best action based on the current state.
  • Reward System: The snake receives rewards or penalties:
    • Positive Reward: For moving without hitting a wall.
    • Negative Reward: For colliding with a wall (-1).
  • Q-Learning Algorithm: Updates the neural network using temporal difference learning.

Getting Started

Prerequisites

  • Web Browser: A modern browser like Google Chrome or Firefox.
  • Local Server (Optional): For improved performance, especially with TensorFlow.js.

Installation

  1. Clone the Repository
    git clone https://github.com/sharik709/snake-game.git
  2. Navigate to the Project Directory
    cd snake-game
  3. View Game
    • open index.html in moderm browser.

Running the Game

  • Without a Server: Open index.html in your browser.
  • With a Server: Navigate to http://localhost:8000 (or the port you specified).

Customization

Adjusting Game Parameters

  • Frame Rate (FR): Controls the speed of the game.
    var FR = 100; // Higher value speeds up the game
  • Exploration Rate (EXPLORATION): Balances exploration and exploitation.
    var EXPLORATION = 0.1; // Lower value reduces random actions
  • Learning Rate (LEARNING_RATE): Affects how quickly the AI learns.
    const LEARNING_RATE = 0.01; // Increase for faster learning
  • Gamma (GAMMA): The discount factor for future rewards.
    const GAMMA = 0.9; // Closer to 1 considers future rewards more

Modifying the Neural Network

  • Change Input Size: Modify if you add more environmental factors.
    this.inputs = 4; // Number of input neurons
  • Adjust Hidden Layers: Increase or decrease complexity.
    this.hidden = 4; // Number of neurons in the hidden layer
  • Alter Activation Functions: Experiment with different activation functions.
    activation: 'relu', // Options: 'sigmoid', 'tanh', etc.

Demo

Unavailable. Please clone this repository and open index.html in modern browser.

Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the Repository
  2. Create a Feature Branch
    git checkout -b feature/YourFeature
  3. Commit Your Changes
    git commit -m "Add Your Feature"
  4. Push to the Branch
    git push origin feature/YourFeature
  5. Open a Pull Request

License

This project is licensed under the MIT License. See the LICENSE file for details.


Note: Make sure to replace placeholders like your-username and demo links with your actual information.