Animation: Smarter Rockets

Why are they smart? These rockets find their way to the moon using a genetic algorithm - that's pretty smart.

This is the sequel to the previous animation called smart rockets. These are smarter because they have to dodge a wall on the way to the moon.

Each of these rockets has a list of instructions of when to fire it's thrusters and when to turn left and right. In the first generation, this list of instructions is randomly created for each rocket. If you click the "Reset" button, you can see that the first generation is really bad at flying.

At the end of each generation, each rocket is given a score based on how close it got to the moon. This score is called the fitness (the total fitness of the previous generation is shown in the animation). When we make a new generation, 2 parent rockets are chosen for each new rocket, and the fitter rockets (the rockets that got closest to the moon) are more likely to be parents. When the parents make a child rocket, they pass on their list of instructions of when to fire the thrusters to the child rocket. In this way, over time, the rockets learn and get better at getting closer to the moon.

This is a type of genetic algorithm. Genetic algorithms are a powerful form of machine learning that allows us to "evolve" solutions to a problem. The really cool thing about this is that the rockets don't really know anything about thrusters, moons or dodging walls. They are learning a solution without having to think about it - they are just trying and seeing what works over and over again.

The full screen option is probably most fun to watch although I tried to make this work at all scales, so even on a small mobile screen the rockets should have a difficult challenge because the gaps in the walls are smaller. Over time the rockets should learn a path to the moon.

The code for this and all my canvas animations can be found at github.

Read next