№ 002Interactive
The fantastic journey of neural network evolution: 50 birds teach themselves Flappy Bird
Nobody shows these birds how to fly. Give each one a brain of six weights, add survival of the fittest, and a few dozen generations later they glide through every pipe.
- Published
- Reading time
- 5 min
On this page
- What is neuroevolution?
- Bird brain anatomy, simplified
- From rookie to bird champion: the secret of evolution
- Step 1: a random free-for-all
- Step 2: eugenics (not the evil kind)
- Step 3: mutate genes, create the future
- AI bird behaviour studies: strange but effective strategies
- Why does something this simple work?
- What's distinctive in the implementation
- Conclusion: AI dancing with Darwin
- Further reading
When you see those little birds miraculously navigating through obstacles on your screen, you might wonder: "How did these cyber birds get so smart?" Today, let's lift the veil on Flappy Bird and follow the evolutionary journey that turns clumsy beginners into elegant flyers.
The fifty birds below are evolving in your browser right now. Watch them hit the wall for a few generations, then turn the speed up.
The leader's brain
━ positive weight━ negative weight
Pipes cleared by each generation (100 and the flock graduates)
The first generation is still flying
What is neuroevolution?
If you've never heard of neuroevolution, don't worry. It sounds like something from a sci-fi movie, but the concept is surprisingly simple.
Imagine this:
- Neural networks: computer programs mimicking the brain
- Genetic algorithms: processes mimicking Darwin's survival of the fittest
- Neuroevolution: the brilliant idea of combining the two
Simply put, you let a group of electronic bird brains compete against each other and allow the best performers to pass their genes to the next generation. Just like real biological evolution, but millions of times faster!
Bird brain anatomy, simplified
Our AI birds have an extremely simple brain, the one drawn on the right of the instrument above:
- Input: just two neurons, receiving the bird's height and the height of the next opening
- Hidden layer: two neurons to process that
- Output: one neuron; above 0.5, the bird flaps
That's it! Six weights in total, no deep learning, no convolutions. But don't underestimate this bird brain. It can work miracles!
From rookie to bird champion: the secret of evolution
Step 1: a random free-for-all
// Start with 50 completely random birds
const population = new Population({
shape: [2, 2, 1], // 2 inputs, 2 hidden neurons, 1 output
size: 50,
});Initially, all birds are rookies, acting completely at random. It's like throwing 50 beginners into a pole-dancing class and seeing who lasts longest without breaking a leg. Most will crash tragically, but a few will get lucky.
Step 2: eugenics (not the evil kind)
elitism: 0.2, // keep the best 20% unchanged
randomRate: 0.2, // make 20% completely random (avoids local optima)
// the remaining 60% are children of good birdsWe do a few things:
- Keep the best-performing birds (long live elitism!)
- Introduce some brand-new random birds (fresh blood!)
- Let elite birds "mate" to produce offspring (don't overthink it… we only mix their neural weights)
Step 3: mutate genes, create the future
mutationRate: 0.1, // each weight has a 10% chance of mutating
mutationRange: 0.5, // by at most ±0.5To avoid inbreeding, we randomly alter some connection weights. This is like genetic mutation in nature: sometimes it creates superpowers, sometimes… well, disasters.
AI bird behaviour studies: strange but effective strategies
After dozens of generations, you'll notice these birds develop some interesting habits:
- Panic bird: always jumps at the last moment, as if it had a serious procrastination problem.
- Super-conservative bird: stays in the middle of the screen, like an accountant who never takes risks.
- Rhythm-master bird: develops an almost musical rhythm that makes you want to add a soundtrack.
- Cheating bird?: sometimes it looks as if they've found a loophole in the game, but it's only the algorithm optimising.
Why does something this simple work?
You might ask: "Can an AI really learn a game this easily?"
Yes! Because:
- The problem is straightforward: Flappy Bird has a simple state and very few actions.
- Mass trial and error: a computer can try thousands of times in a short while, which beats you practising for hundreds of hours.
- Survival of the fittest: one of nature's oldest learning rules works just as well on virtual birds.
Evolution doesn't guarantee success, though. If the score refuses to climb, press Restart and begin with a new set of ancestors.
What's distinctive in the implementation
private breed(a: Float32Array, b: Float32Array): Float32Array {
const child = Float32Array.from(a); // copy the first parent's genes
for (let i = 0; i < child.length; i++) {
if (rng() <= 0.5) child[i] = b[i]; // crossover: 50% chance of the second parent's gene
if (rng() <= mutationRate) child[i] += rng() * mutationRange * 2 - mutationRange; // mutation
}
return child;
}This code is the system's DNA laboratory, mixing the genes of two successful networks to produce the next generation. It's like blending the brain wiring of two smart birds and adding a dash of mutation seasoning.
Conclusion: AI dancing with Darwin
Next time you see those little birds gliding through pipes, remember: they weren't programmed to do that, they learned to. Through countless generations of failure and selection, from random flailing to precise control, these virtual creatures demonstrate a simple yet profound truth:
Sometimes the secret to success isn't clever design, but enough chances to try and the ability to adapt.
If a flock of pixel birds can learn a game through simple evolution, do we humans learn our skills in a similar way? Perhaps each of us is a neuroevolution experiment run by our own genes and experiences…
All right, deep-thinking time is over. Go back up, set the speed to max, and let's see how good those little guys get after a few more generations!
Further reading
If you're interested in the mathematics behind neuroevolution and more technical detail:
- Kenneth O. Stanley's NEAT paper: NeuroEvolution of Augmenting Topologies
- The Quest for Artificial Intelligence, Nils J. Nilsson
- Evolutionary Optimization Algorithms, Dan Simon
This article first appeared on my previous site in March 2025. The interactive parts were rewritten from scratch in TypeScript when it moved into this notebook.