Flappy-Bat-AI

· Python · pygame · Source on GitHub

Flappy-Bat-AI game: bats flying between pipes
The game
The evolved neural network: five inputs, four hidden nodes, one output
The network

The goal of this post is to demonstrate the three requirements for machine learning and use my old project as a case study for them.

The story

It was the summer of 2022 and I had just completed my first courses on data structures and algorithms, and I was eager to get deeper into programming and practice my new skills with a project. Two things collided fruitfully that summer. I encountered a video about neural networks, and I realised that I could use the tools from my courses to create recursive graphs, which act as a neural network. Filled with enthusiasm, I began my first machine learning project to see if I could create and train a neural network from first principles.

The result of this project is called Flappy-bat-AI, which constitutes a version of the popular flappy bird game and a somewhat unique model optimizer/architecture combination. Below is a video of the model training process. Each generation produces 100 variations of last generations best performing bat model. The model controls the vertical acceleration of the model, making the task more challenging than in the original game, because small mistakes compound quickly.

Flappy-bat-ai training to solve the game. The arrow moving up and down visualizes the output of a random model. The bats were drawn by my wife.

Three components of machine learning

The first thing you need for machine learning is a model architecture. There are several options available and each have their own unique and useful use cases. The architecture best known by contemporary culture is the neural network but there are others like decision trees, regression models and convolutional networks.

My Flappy-Bat-AI uses a neural network, but unlike the usual networks, it can change its topology, meaning it can evolve its structure. In the video where I got my inspiration, the author uses NeuroEvolution of Augmenting Topologies (NEAT) and I wanted to program my version of that freely available library.

How Flappy-Bat-AI network evolves structure to find better solutions for the game
Made with Claude Opus 5.5

There are good reasons to change the topology, the best one being that you can potentially find a maximally simple network that can solve the problem at hand. However, for most machine learning there are two downsides that are too significant to make this viable. First is that these kinds of ML models struggle with more complicated tasks, because it is hard to find an evolutionary path from a simple to a complicated network capable of solving the problem.

The other reason relates to the second core component of ML — it requires an optimizer. A randomly created model is going to be useless, unless it is optimized. Optimization requires some method for updating the parameters of the model in such a manner that its output and the desired result is minimal. A common method for optimization in deep learning is stochastic gradient descent which amounts to navigating to lower points in a high-dimensional landscape.

Below you can try being an optimizer yourself! The simplest ML model is a straight line in 2d space and you can try to change the model parameters so that the line matches the data points as closely as possible. A line has just two variables, intercept and slope, and below you can play with those values to get the ideal model. See if you can find the the position of the line that minimizes error.

Error: 57.8Bad

Error 57.8, Bad

Model: Linear regression. Error is the squared distance of points to the straight line.
Made with Claude Opus 5.5

Optimizer efficiency is a central problem in ML and often involves sophisticated mathematics. But there are supremely simple optimizers available even if they are slow and unreliable. One of them is inspired by nature — evolution! Here the idea is simply to try a bunch of models, take the best one, produce many slightly edited copies of it, and repeat.

My Flappy-bat-AI uses evolutionary search and the reason is that finding better models when the model complexity can change is a complicated task — changing the number of parameters essentially means that the search space dimension changes. Also, implemeting evolution is trivially easy, since all you need to do is mutate the models and keep trying. The downside is that evolutionary search constitutes a random search and does not guarantee finding a solution.

The last component required for ML is the learning criterion, which is often called the loss function. In essence, the loss function just measures how good the model is, and it is specific for the given task. In the above line fitting example, the loss function is the sum of point distances from the line and the optimizer should minimize the error. For Flappy-Bat-AI, the loss function is the game score that the model receives, which is being maximized.

Most modern machine learning involves data and a lot of it. It seems that all the big AI companies are doing everything in their power to get access to more data. The role of the data essentially comes in with the loss function, where there function needs the model and data to evaluate how good a given model is. In supervised ML, the data is split into features that the model gets access to, and the target which the model tries to predict. However, data can be sometimes replaced with simulations and games, as my Flappy-Bat-AI does.

So, there you have it. Machine learning requires an architecture, a model optimizer and a loss function. This project uses an evolving neural network as architecture, evolution for optimizer and a simulated game for the loss function. The result is a model that can play the game flawlessly. Please enjoy the trained bat flying through a hundred obstacles.

The trained model playing the game.

Network evolution animation (and its still at the top of the page) and interactive line-fitting demo made with help from Claude Opus 5.5. I wanted to use the original project for the game but the recordings I got were of poor quality. In order to improve the educational value, I applied my significantly improved clean coding and CI practices and instructed Opus 5.5 to create a testing suite and a major refactor of the project. We then added visualisation and performance improvements as well as recording capacity. Those improvements live on vtuomas/opus-refactor branch and I will leave them there.