Project Introduction: Machine-Learning-Flappy-Bird
The Machine-Learning-Flappy-Bird project showcases an innovative approach to incorporating machine learning into the Flappy Bird video game using neural networks and a genetic algorithm. The project is all about teaching a virtual bird how to fly skillfully through obstacles for as long as possible by learning from its own experiences.
What is Machine-Learning-Flappy-Bird?
This project utilizes HTML5, the Phaser framework, and the Synaptic Neural Network library. The machine learning algorithm allows a small bird to navigate the Flappy Bird game environment autonomously by learning optimal flapping patterns through trial and error.
For those interested in learning more, the complete tutorial is available on Ask For Game Task, and a demonstrative video offers a simple presentation of the algorithm's application.
Neural Network Architecture
The core of this project is a neural network comprising three essential layers for each bird:
-
Input Layer:
- Contains 2 neurons that represent:
- Horizontal distance to the closest gap.
- Height difference from the closest gap.
- Contains 2 neurons that represent:
-
Hidden Layer:
- Contains 6 neurons that process information from the input layer.
-
Output Layer:
- Contains 1 neuron that decides the bird's action:
- If the output is greater than 0.5, the bird flaps its wings; otherwise, it does nothing.
- Contains 1 neuron that decides the bird's action:
The Synaptic Neural Network library is used to create this neural network.
Machine Learning Concept
The primary concept of this project is to apply neuro-evolution, where evolutionary algorithms like genetic algorithms train the birds' neural networks. Here's how it works:
-
Population Creation:
A population of 10 bird units is initialized with random neural networks. -
Simulation:
All birds play the game simultaneously, each using its own neural network. -
Fitness Calculation:
Each bird's performance is evaluated using a fitness function:fitness = total travelled distance - distance to the closest gap
-
Evolution with Genetic Algorithm:
Once all birds fail to continue:- Birds are ranked by fitness.
- The top 4 birds become the breeding winners.
- New population is generated by:
- Directly passing the 4 winners.
- Creating 6 offsprings through crossover and mutation of winners.
-
Reiteration:
Steps 2-4 are repeated for continuous learning and improvement.
Implementing the Project
Requirements
The project requires:
- phaser.min.js: For the game framework.
- synaptic.min.js: For the neural network framework.
Gameplay Logic
All the game logic resides in gameplay.js
, featuring classes like:
- App.Main: Handles game initialization, asset loading, and game updating, integrating AI and genetic algorithms.
- TreeGroup Class: Represents moving obstacles.
- Tree Class: Represents individual tree sprites.
- Bird Class: Represents bird sprites.
- Text Class: For displaying text information.
Genetic Algorithm Logic
The genetic.js
file contains the GeneticAlgorithm
class responsible for all genetic operations, like population creation, selection, crossover, and mutations. It helps in the continuous evolution of the bird's neural network, making it smarter over time.
This innovative project is a fascinating exploration of how machine learning can animate a simple game character, demonstrating advancements in AI and game development strategies.