How It Works: NeuroEvolution
This simulation combines Neural Networks (the Brain) with Genetic Algorithms (Evolution) to create an AI that learns to play the game without any human instruction. It starts with zero knowledge and learns purely through trial and error.
1. The Brain (Neural Network)
Each bird has its own unique "Brain", which is a simple Perceptron Neural Network. It takes information about the world as inputs and decides whether to jump or not.
- Y Position: Height of the bird.
- Velocity: Is it falling or rising?
- Pipe Distance: Horizontal distance to the next opening.
- Top Pipe Y: Height of the ceiling obstacle.
- Bottom Pipe Y: Height of the floor obstacle.
The network outputs a single value between 0 and 1.
Bird.jump();
}
2. The Evolution (Genetic Algorithm)
We spawn 200 birds at once. Each bird has a random brain initially. Most will crash immediately.
We track the "Fitness" of every bird. Fitness is simply how long the bird stays alive and how many pipes it passes. The better it performs, the higher its score.
When all birds die, we create the next generation:
- Elitism: We keep the absolute best bird from the previous generation unchanged (The Champion).
- Selection: We pick parents for the new generation. Birds with higher fitness have a higher chance of being picked.
- Mutation: We copy the parent's brain but tweak the "synapses" (weights) randomly slightly. This introduces new behavior.
3. Convergence
Over generations, behaviors that cause crashing are weeded out, and behaviors that dodge pipes survives. Eventually, the population converges on a "perfect" strategy.