This program uses a genetic algorithm to design a two-dimensional car that is “optimal” for a particular terrain. The rules of the problem are as follows:

The optimality of a particular candidate solution (the fitness function) is determined by how far it travels before a) a mass touches the ground or b) time runs out.

Note that at the beginning, the algorithm doesn’t even know that wheels belong on the bottom. It is sometimes possible to see distinct species emerge and die - for instance, “unicycles” are often quite successful in the early stages of the algorithm’s progress.

The graph shows mean and maximum fitness values as a function of generation number.

This demo inspired Boxcar2D. Big shout out to Ruffle which is the mechanism by which this can run in your browser in 2023, nearly two decades after the code was originally written.

The GA could converge much faster than it does here. In choosing the fitness cutoff, population size etc. I have aimed for an interesting/fun simulation rather than a fast, useful optimization.

The timeout is fixed but arbitrary. It was a quick hack to remove solutions which get stuck without crashing.