{"id":14963955,"url":"https://github.com/ssusnic/machine-learning-flappy-bird","last_synced_at":"2025-05-16T06:07:07.678Z","repository":{"id":37561666,"uuid":"99936517","full_name":"ssusnic/Machine-Learning-Flappy-Bird","owner":"ssusnic","description":"Machine Learning for Flappy Bird using Neural Network and Genetic Algorithm","archived":false,"fork":false,"pushed_at":"2017-12-19T10:22:15.000Z","size":485,"stargazers_count":1827,"open_issues_count":6,"forks_count":401,"subscribers_count":88,"default_branch":"master","last_synced_at":"2025-05-16T06:06:59.755Z","etag":null,"topics":["ai","ai-tutorial","artificial-evolution","artificial-intelligence","flappy-bird","flappybird","game-programming","genetic-algorithm","genetic-algorithms","html5","javascript","machine-intelligence","machine-learning","machine-learning-algorithm","machinelearning","neural-network","neural-networks","neuroevolution","phaser","phaser-tutorial"],"latest_commit_sha":null,"homepage":"http://www.askforgametask.com/tutorial/machine-learning-algorithm-flappy-bird","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ssusnic.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-08-10T15:14:19.000Z","updated_at":"2025-05-07T07:30:48.000Z","dependencies_parsed_at":"2022-08-08T21:00:09.528Z","dependency_job_id":null,"html_url":"https://github.com/ssusnic/Machine-Learning-Flappy-Bird","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ssusnic%2FMachine-Learning-Flappy-Bird","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ssusnic%2FMachine-Learning-Flappy-Bird/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ssusnic%2FMachine-Learning-Flappy-Bird/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ssusnic%2FMachine-Learning-Flappy-Bird/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ssusnic","download_url":"https://codeload.github.com/ssusnic/Machine-Learning-Flappy-Bird/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254478190,"owners_count":22077676,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ai","ai-tutorial","artificial-evolution","artificial-intelligence","flappy-bird","flappybird","game-programming","genetic-algorithm","genetic-algorithms","html5","javascript","machine-intelligence","machine-learning","machine-learning-algorithm","machinelearning","neural-network","neural-networks","neuroevolution","phaser","phaser-tutorial"],"created_at":"2024-09-24T13:32:23.074Z","updated_at":"2025-05-16T06:07:02.667Z","avatar_url":"https://github.com/ssusnic.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Machine Learning for Flappy Bird using Neural Network and Genetic Algorithm\n\nHere is the source code for a HTML5 project that implements a machine learning algorithm in the Flappy Bird video game using neural networks and a genetic algorithm. The program teaches a little bird how to flap optimally in order to fly safely through barriers as long as possible.\n\nThe complete tutorial with much more details and demo you can find here:  \n[http://www.askforgametask.com/tutorial/machine-learning-algorithm-flappy-bird](http://www.askforgametask.com/tutorial/machine-learning-algorithm-flappy-bird)\n\nHere you can also watch a short video with a simple presentation of the algorithm:  \n[https://www.youtube.com/watch?v=aeWmdojEJf0](https://www.youtube.com/watch?v=aeWmdojEJf0)\n\nAll code is written in HTML5 using [Phaser framework](http://phaser.io/) and [Synaptic Neural Network library](https://synaptic.juancazala.com) for neural network implementation.\n\n![Flappy Bird Screenshot](https://raw.githubusercontent.com/ssusnic/Machine-Learning-Flappy-Bird/master/screenshots/flappy_10.png \"Flappy Bird Screenshot\")\n\n## Neural Network Architecture\n\nTo play the game, each unit (bird) has its own neural network consisted of the next 3 layers:\n1. an input layer with 2 neurons presenting what a bird sees:\n     \n     ```\n     1) horizontal distance between the bird and the closest gap\n     2) height difference between the bird and the closest gap\n     ```\n     \n2. a hidden layer with 6 neurons\n3. an output layer with 1 neuron used to provide an action as follows:\n     \n     ```\n    if output \u003e 0.5 then flap else do nothing\n     ```\n     \n![Flappy Bird Neural Network](https://raw.githubusercontent.com/ssusnic/Machine-Learning-Flappy-Bird/master/screenshots/flappy_06.png \"Flappy Bird Neural Network\")\n\n\nThere is used [Synaptic Neural Network library](https://synaptic.juancazala.com) to implement entire artificial neural network instead of making a new one from the scratch.\n\n## The Main Concept of Machine Learning\n\nThe main concept of machine learning implemented in this program is based on the neuro-evolution form. It uses evolutionary algorithms such as a genetic algorithm to train artificial neural networks. Here are the main steps:\n\n1. create a new population of 10 units (birds) with a **random neural network** \n2. let all units play the game simultaneously by using their own neural networks\n3. for each unit calculate its **fitness** function to measure its quality as:\n\n    ```\n    fitness = total travelled distance - distance to the closest gap\n    ```\n \n    ![Flappy Bird Fitness](https://raw.githubusercontent.com/ssusnic/Machine-Learning-Flappy-Bird/master/screenshots/flappy_08.png \"Flappy Bird Fitness\")\n\n    \n4. when all units are killed, evaluate the current population to the next one using **genetic algorithm operators** (selection, crossover and mutation) as follows:\n\n    ```\n    1. sort the units of the current population in decreasing order by their fitness ranking\n    2. select the top 4 units and mark them as the winners of the current population\n    3. the 4 winners are directly passed on to the next population\n    4. to fill the rest of the next population, create 6 offsprings as follows:\n        - 1 offspring is made by a crossover of two best winners\n        - 3 offsprings are made by a crossover of two random winners\n        - 2 offsprings are direct copy of two random winners\n    5. to add some variations, apply random mutations on each offspring.\n    ```\n    \n5. go back to the step 2\n\n## Implementation\n\n### Requirements\n\nSince the program is written in HTML5 using [Phaser framework](http://phaser.io/) and [Synaptic Neural Network library](https://synaptic.juancazala.com) you need these files:\n\n- **phaser.min.js**\n- **synaptic.min.js**\n\n### gameplay.js \nThe entire game logic is implemented in **gameplay.js** file. It consists of the following classes:\n\n- `App.Main`, the main routine with the following essential functions:\n\t- _preload()_ to preload all assets\n\t- _create()_ to create all objects and initialize a new genetic algorithm object\n\t- _update()_ to run the main loop in which the Flappy Bird game is played by using AI neural networks and the population is evolved by using genetic algorithm\n\t- _drawStatus()_ to display information of all units\n\t\n- `TreeGroup Class`, extended Phaser Group class to represent a moving barrier. This group contains a top and a bottom Tree sprite.\n\n- `Tree Class`, extended Phaser Sprite class to represent a Tree sprite.\n\n- `Bird Class`, extended Phaser Sprite class to represent a Bird sprite.\n\n- `Text Class`, extended Phaser BitmapText class used for drawing text.\n\n### genetic.js \n\nThe genetic algorithm is implemented in **genetic.js** file which consists of the following class:\n\n- `GeneticAlgorithm Class`, the main class to handle all genetic algorithm operations. It needs two parameters: **_max_units_** to set a total number of units in population and **_top_units_** to set a number of top units (winners) used for evolving population. Here are its essential functions:\n\n   - _reset()_ to reset genetic algorithm parameters\n   - _createPopulation()_ to create a new population\n   - _activateBrain()_ to activate the AI neural network of an unit and get its output action according to the inputs\n   - _evolvePopulation()_ to evolve the population by using genetic operators (selection, crossover and mutations)\n   - _selection()_ to select the best units from the current population\n   - _crossOver()_ to perform a single point crossover between two parents\n   - _mutation()_ to perform random mutations on an offspring\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fssusnic%2Fmachine-learning-flappy-bird","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fssusnic%2Fmachine-learning-flappy-bird","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fssusnic%2Fmachine-learning-flappy-bird/lists"}