Skip to content

mehmetkahya0/ai-flappy-bird

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 

Repository files navigation

🧠 AI Flappy Bird - Neural Evolution

An advanced AI implementation of Flappy Bird using neural networks and genetic algorithms.

🤖 How the AI Learns

The AI learns to play Flappy Bird through evolutionary learning - a process inspired by natural selection:

The Learning Process:

  1. Start with Random Birds: Each bird has a neural network "brain" with random connections
  2. Play the Game: All birds try to play Flappy Bird simultaneously
  3. Survival of the Fittest: Birds that survive longer and pass more pipes get higher scores
  4. Create Next Generation: The best birds' "brains" are combined and mutated to create smarter offspring
  5. Repeat: Each generation gets better as successful strategies are passed down

What the AI Learns:

  • Timing: When to flap to avoid pipes
  • Positioning: How to navigate through gaps
  • Strategy: Balancing speed and precision
  • Adaptation: Adjusting to changing pipe patterns

Within 50 generations, the AI typically masters the game, achieving scores over 1000 points consistently!

🎯 Performance Metrics

The AI typically achieves:

  • Generation 1-10: Learning basic controls (0-100 points)
  • Generation 10-30: Developing strategy (100-500 points)
  • Generation 30-50: Optimizing performance (500-1000 points)
  • Generation 50+: Mastering the game (1000+ points consistently)

Analytics Insights

  • Survival Rate: Tracks population health over time
  • Decision Confidence: Measures AI certainty in decisions
  • Neural Complexity: Monitors brain development
  • Genetic Diversity: Ensures population variety
  • Learning Progress: Phase-based training advancementWatch as AI agents learn to play the game through evolutionary processes!

🚀 Features

Core AI System

  • Enhanced Neural Networks: 6-input, 12-hidden-node, 1-output architecture with ReLU and Sigmoid activations
  • Advanced Genetic Algorithm: Multiple crossover strategies, adaptive mutation rates, and elitism
  • Smart Bird Behavior: Improved decision-making with enhanced input features
  • Dynamic Difficulty: Adaptive pipe spacing and speed based on generation performance

Visual Enhancements

  • Beautiful Graphics: Gradient backgrounds, detailed pipes, and animated clouds
  • Real-time Neural Visualization: Live display of neural network activations
  • Performance Tracking: Comprehensive statistics and generation analytics
  • Best Bird Highlighting: Visual indicators for top-performing agents
  • Advanced Analytics Dashboard: 12+ real-time graphs and visualizations

Optimization Features

  • Performance Optimized: 3x speed multiplier for faster learning
  • Elitism Strategy: Top 10% of birds preserved each generation
  • Tournament Selection: Better diversity through competitive selection
  • Adaptive Mutations: Mutation rates adjust based on fitness scores

🎮 Controls

Key Action
SPACE Pause/Resume the simulation
R Reset the entire simulation
S Toggle statistics display

📈 Dashboard Features

The analytics dashboard provides real-time insights into the AI learning process:

  • Live Neural Networks: Watch the AI's "brain" make decisions
  • Performance Metrics: Track fitness, survival, and learning progress
  • Genetic Analytics: Monitor population diversity and evolution
  • Decision Confidence: See how confident the AI is in its choices
  • Training Progress: Follow the learning journey through phases

🧬 How It Works

Neural Network Architecture

Inputs (6 nodes):
├── Bird Y position (normalized)
├── Bird velocity (normalized) 
├── Distance to next gap (normalized)
├── Gap center position (normalized)
├── Height difference from gap center
└── Frames since last flap

Hidden Layer (12 nodes):
└── ReLU activation function

Output (1 node):
└── Flap decision (Sigmoid activation)

🧮 Matrix System in Neural Networks

The neural network in AI Flappy Bird relies on matrix operations for efficient computation of the bird's decisions. This system uses linear algebra to process inputs through the network layers.

Weight Matrices Structure

The network consists of two weight matrices that define the connections between neurons:

  1. Input-Hidden Matrix (6×12): Connects 6 input features to 12 hidden neurons
  2. Hidden-Output Matrix (12×1): Connects 12 hidden neurons to 1 output neuron

Forward Propagation Process

The decision-making happens through matrix multiplication:

Hidden_Activations = Input_Vector × Input_Hidden_Matrix
Hidden_Output = ReLU(Hidden_Activations)
Final_Output = Sigmoid(Hidden_Output × Hidden_Output_Matrix)

Detailed Example

Let's walk through a concrete example with sample values:

Input Features (normalized between 0-1):

Bird Y Position:     0.5
Bird Velocity:       0.2
Distance to Gap:     0.8
Gap Center Y:        0.6
Height Difference:   0.1
Frames Since Flap:   0.3

Input Vector (1×6):

[0.5, 0.2, 0.8, 0.6, 0.1, 0.3]

Sample Input-Hidden Weights (6×12 matrix, showing first 3 columns):

┌                     ┐
│ 0.12  -0.45  0.78  │
│ 0.34   0.67 -0.23  │
│-0.56   0.89  0.12  │
│ 0.45  -0.34  0.56  │
│ 0.78   0.23 -0.67  │
│-0.12   0.45  0.89  │
└                     ┘

Hidden Layer Calculation (first 3 neurons):

Hidden[0] = (0.5×0.12) + (0.2×0.34) + (0.8×-0.56) + (0.6×0.45) + (0.1×0.78) + (0.3×-0.12) = 0.23
Hidden[1] = (0.5×-0.45) + (0.2×0.67) + (0.8×0.89) + (0.6×-0.34) + (0.1×0.23) + (0.3×0.45) = 0.67
Hidden[2] = (0.5×0.78) + (0.2×-0.23) + (0.8×0.12) + (0.6×0.56) + (0.1×-0.67) + (0.3×0.89) = 0.89

After ReLU Activation (negative values become 0):

Hidden_Output = [0.23, 0.67, 0.89, ...]  (12 values)

Hidden-Output Weights (12×1 matrix, sample values):

┌    ┐
│ 0.45 │
│-0.23 │
│ 0.67 │
│ 0.12 │
│-0.78 │
│ 0.34 │
│ 0.56 │
│-0.45 │
│ 0.89 │
│ 0.23 │
│-0.67 │
│ 0.12 │
└    ┘

Final Output Calculation:

Raw_Output = Σ(Hidden_Output[i] × Hidden_Output_Weights[i]) = 0.76
Flap_Decision = Sigmoid(0.76) = 0.68

Decision: Since 0.68 > 0.5, the bird decides to flap!

This matrix system allows the AI to process complex relationships between game state variables and make intelligent decisions in real-time.

Genetic Evolution of Matrices

Through generations, the genetic algorithm evolves these weight matrices:

  • Crossover: Combines weight matrices from parent birds
  • Mutation: Randomly adjusts matrix values
  • Selection: Preserves matrices from high-performing birds

Genetic Algorithm Process

  1. Population: 150 birds per generation
  2. Selection: 70% tournament selection, 30% roulette wheel
  3. Elitism: Top 10% preserved unchanged
  4. Crossover: Uniform, single-point, and blend strategies
  5. Mutation: Adaptive rates with Gaussian distribution
  6. Fitness: Multi-objective function considering survival, distance, and pipes passed

Learning Progression

  • Early Generations: Random behavior, learning basic survival
  • Mid Generations: Developing timing and spatial awareness
  • Advanced Generations: Optimized performance with precise control

📊 Analytics Dashboard

The AI Flappy Bird features a comprehensive analytics dashboard with 12+ real-time visualizations:

Core Performance Graphs

  • Fitness Graph: Tracks population fitness over generations
  • Survival Rate: Shows percentage of birds surviving each generation
  • Performance Histogram: Distribution of scores across the population
  • Training Progress: Overall learning progress and phase tracking

Neural Network Analytics

  • Neural Activity Heatmap: Real-time brain activation patterns
  • Decision Confidence: Average confidence in flap decisions
  • Complexity Graph: Neural network complexity over time
  • Gene Pool Diversity: Genetic diversity metrics

Evolutionary Metrics

  • Species Graph: Population clustering and speciation
  • Learning Rate: Adaptive mutation rate tracking
  • Diversity Graph: Population diversity measurements
  • Confidence Levels: Decision-making confidence trends

Real-time Visualizations

  • Best Bird Neural Network: Live brain visualization of top performer
  • Population Neural Network: Current generation's neural patterns
  • Dynamic Statistics: Live updates of all performance metrics

🔧 Technical Improvements

Neural Network Enhancements

  • Xavier weight initialization for better learning
  • Numerical stability improvements in activation functions
  • Enhanced input feature engineering
  • Multi-strategy crossover operations

Game Engine Optimizations

  • Efficient collision detection with margin adjustments
  • Dynamic pipe generation based on performance
  • Optimized rendering with gradient backgrounds
  • Real-time FPS monitoring
  • Comprehensive error handling and crash prevention
  • Advanced analytics dashboard with 12+ visualizations

Genetic Algorithm Upgrades

  • Tournament selection for better diversity
  • Adaptive mutation rates based on fitness
  • Gaussian mutations for smoother changes
  • Multi-objective fitness function

🎯 Performance Metrics

The AI typically achieves:

  • Generation 1-10: Learning basic controls (0-100 points)
  • Generation 10-30: Developing strategy (100-500 points)
  • Generation 30-50: Optimizing performance (500-1000+ points)
  • Generation 50+: Mastering the game (1000+ points consistently)

🌟 Advanced Features

  • Speciation Support: Framework for population clustering (future enhancement)
  • Neural Visualization: Real-time display of network activations
  • Performance Analytics: Detailed statistics and trend tracking
  • Dynamic Environments: Adaptive difficulty based on AI performance
  • Advanced Dashboard: 12+ real-time graphs and analytics
  • Error Recovery: Robust error handling and crash prevention
  • Performance Monitoring: FPS tracking and optimization
  • Genetic Diversity: Advanced population diversity metrics

🚀 Getting Started

  1. Open index.html in a modern web browser
  2. Watch the AI learn to play automatically
  3. Use keyboard controls to interact with the simulation
  4. Observe neural network activations in real-time
  5. Monitor the analytics dashboard for learning insights

🔧 Latest Features

Version 2.0 Enhancements

  • Advanced Analytics Dashboard: 12+ real-time graphs and visualizations
  • Improved Neural Networks: Enhanced architecture with better learning
  • Error Recovery: Robust crash prevention and error handling
  • Performance Monitoring: Real-time FPS and optimization tracking
  • Enhanced UI: Modern design with gradient backgrounds and animations
  • Genetic Diversity: Advanced population diversity metrics
  • Decision Confidence: Real-time confidence measurements
  • Training Progress: Phase-based learning progression tracking

🧪 Experimentation

Try modifying these parameters in main.js:

  • POP_SIZE: Population size (default: 150)
  • MUTATION_RATE: Base mutation rate (default: 0.15)
  • PIPE_GAP: Initial gap size (default: 200)
  • SPEED_MULTIPLIER: Simulation speed (default: 3)
  • GRAVITY: Bird gravity (default: 0.6)
  • JUMP: Bird jump strength (default: -12)

Advanced Parameters

  • HIDDEN_NODES: Neural network complexity (default: 12)
  • INPUT_NODES: Input features (default: 6)
  • ELITISM_RATE: Elite population percentage (default: 0.1)

🏆 Achievement Goals

  • Survive 1000+ frames consistently
  • Achieve all-time best scores over 2000
  • Maintain stable performance across generations
  • Observe emergent intelligent behaviors

Built with: Vanilla JavaScript, HTML5 Canvas, and advanced AI algorithms Inspired by: NEAT (NeuroEvolution of Augmenting Topologies) and genetic programming principles: How the System Works

Introduction

Welcome! This is a fun project where we teach computers to play Flappy Bird using artificial intelligence. We'll explain everything step by step, like you're learning it for the first time. No fancy words without explanations!

What is Flappy Bird?

Flappy Bird is a simple game where a bird flies through pipes. The bird falls down due to gravity, and you tap to make it flap up. The goal is to go through as many pipes as possible without hitting them.

In our version, no human plays - the computer controls everything!

Artificial Intelligence (AI)

AI means making computers think and learn like humans. Here, we use two main ideas:

  1. Neural Networks - Like a tiny brain inside each bird
  2. Evolution - Birds get better over time, like animals in nature

Neural Networks: The Bird's Brain

Each bird has a tiny "brain" that decides when to flap. Think of it as a decision-making machine.

How It Works

The brain takes inputs (information) and gives outputs (decisions).

Inputs (4 pieces of information):

  • Where the bird is on the screen (up/down)
  • How fast the bird is moving (up or down)
  • Height of the top pipe
  • Height of the bottom pipe

Output (1 decision):

  • Flap or don't flap (yes/no)

The Brain's Structure

Inputs → Hidden Layer → Output
   4   →     8       →   1

Neural Network Diagram:

Input Layer     Hidden Layer     Output Layer
   [I1]           [H1]              [O1]
   [I2]           [H2]              (Flap?)
   [I3]           [H3]
   [I4]           [H4]
                 [H5]
                 [H6]
                 [H7]
                 [H8]

Connections: I1→H1, I1→H2, ..., I4→H8, H1→O1, ..., H8→O1
  • Input layer: 4 numbers (the information above)
  • Hidden layer: 8 "thinking" units
  • Output layer: 1 number (0 = don't flap, 1 = flap)

Mathematics: How the Brain Thinks

The brain uses simple math to make decisions:

  1. Multiplication: Each connection has a "weight" (like importance)

    Input × Weight = Signal
    
  2. Addition: Add up all signals

    Signal1 + Signal2 + Signal3 + ... = Total
    
  3. Sigmoid Function: Squish the total into a number between 0 and 1

    Sigmoid(x) = 1 / (1 + e^(-x))
    
    • If x is big positive: result ≈ 1
    • If x is big negative: result ≈ 0
    • If x is zero: result ≈ 0.5

Sigmoid Function Graph:

Output
  1.0 |          .--------
      |        .'
      |      .'
  0.5 |....'
      |   .'
      |  .'
  0.0 |.'          --------
      +-------------------- Input
        -3  -2  -1   0   1   2   3

This gives us a number between 0 and 1. If > 0.5, the bird flaps!

Evolution: Getting Smarter Over Time

Birds start with random brains and get better through "evolution" (like natural selection).

How Evolution Works

  1. Generation: A group of 100 birds plays the game
  2. Fitness: How well each bird did (score = how many pipes passed)
  3. Selection: Pick the best birds to be "parents"
  4. Crossover: Mix parents' brains to make babies
  5. Mutation: Add small random changes
  6. Repeat: New generation with improved brains

Mathematics in Evolution

Fitness Calculation:

Fitness = Score × Score

(Better scores get much higher fitness - rewards good birds more)

Selection:

  • Calculate total fitness of all birds
  • Pick randomly, but better birds more likely to be chosen
  • Like a lottery where good birds have more tickets

Crossover:

  • Take half the "weights" from mom, half from dad
  • Creates a mix of both parents' brains

Mutation:

  • 10% chance to change each weight
  • Add small random number (-0.1 to +0.1)
  • Prevents getting stuck, adds new ideas

Fitness Distribution Chart:

Fitness
  High |     ████  (Best birds)
        |   ████████
        | ████████████
  Low   |██████████████████  (Most birds)
        +--------------------
          Score: 0 → 1000

Selection Process:

Bird A (Score: 100) - Fitness: 10,000
Bird B (Score: 200) - Fitness: 40,000  ← More likely to be picked!
Bird C (Score: 50)  - Fitness: 2,500

The Complete System

  1. Start: 100 birds with random brains
  2. Play: Each bird uses its brain to decide when to flap
  3. Die: Birds hit pipes or go off screen
  4. Evolve: Best birds make new generation
  5. Repeat: Birds get better each time!

Evolution Progress Over Time:

Generation
   20 |          ██████████ (Birds playing forever!)
      |
   15 |       ████████
      |
   10 |    ██████
      |
    5 |  ████
      |
    1 | ██  (Random - crash immediately)
      +---------------------------------- Time
        Gen 1    5    10    15    20

Score Improvement:

Score
 1000 |          ██████████
      |
  750 |       ████████
      |
  500 |    ██████
      |
  250 |  ████
      |
    0 | ██
      +---------------------------------- Generation
        1    5    10    15    20

Visualizing the Brain

In the top corners, you can see the neural networks "thinking":

  • Best Bird Neural Network: The smartest bird so far
  • Population Neural Network: Current generation's neural patterns

Colors show how active each part is:

  • Red: Very active
  • Yellow: Medium active
  • Blue: Not active

Neural Activity Example:

Input Layer:    [🔵] [🔵] [🟡] [🔴]  ← Bird position, velocity, pipes
Hidden Layer:   [🟡] [🔴] [🟡] [🔵] [🟡] [🔵] [🟡] [🔴]  ← Processing
Output Layer:   [🔴]  ← FLAP! (High activation = flap)

Activity Levels:

0.0 - 0.3: 🔵 Blue (Inactive)
0.3 - 0.7: 🟡 Yellow (Medium)
0.7 - 1.0: 🔴 Red (Very Active)

Analytics Dashboard

The game includes a comprehensive analytics dashboard with multiple graphs:

Performance Tracking

  • Fitness Graph: Shows how well the population is doing over time
  • Survival Rate: Percentage of birds that survive each generation
  • Training Progress: Overall learning progress through different phases

Neural Network Insights

  • Activity Heatmap: Real-time visualization of brain activity
  • Decision Confidence: How confident the AI is in its decisions
  • Complexity Graph: Measures neural network development

Evolutionary Metrics

  • Gene Pool Diversity: Genetic variety in the population
  • Learning Rate: How fast the AI is adapting
  • Species Graph: Population clustering patterns

Why This Works

  • Trial and Error: Birds try different strategies
  • Learning: Good strategies survive and spread
  • Improvement: Each generation is better than the last
  • No Human Help: The computer figures it out itself!

Fun Facts

  • Birds start terrible (crash immediately)
  • After 10-20 generations, they get pretty good
  • The best birds can go forever!
  • Each bird's brain has 6×12 + 12×1 = 84 connections
  • That's 84 numbers the computer has to learn!
  • The analytics dashboard shows 12+ different metrics
  • Decision confidence helps track AI learning progress
  • Neural complexity increases as birds get smarter

Try It Yourself

  1. Open https://mehmetkahya0.github.io/ai-flappy-bird
  2. Watch the birds learn
  3. See the neural activity change
  4. Notice how scores improve over generations

Isn't it amazing how simple math can create intelligent behavior? 🤖🦅

Contact!

mail: mehmetkahyakas5@gmail.com

github: @mehmetkahya0

website: https://mehmetkahya0.github.io/

About

An advanced AI implementation of Flappy Bird using neural networks and genetic algorithms. Watch as AI agents learn to play the game through evolutionary processes!

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors