# Evolving 2D Cars By Using Genetic Algorithms

Tech

Genetic algorithms mimic the process of natural selection and they are mostly used to generate solutions to optimization problems. Such as the problem of building two-dimensional cars to navigate a bumpy race track.

Here you can see an example car that I generated with 3 wheels jumping across the race track. To the top is the current generation, which is 12. It means that our car belongs to the 12th generation and that there were over 200 cars tested before it (since a generation consists of 20 cars).

Cars with an extra wheel to the front tend to perform better after hitting a bump and getting airborne.

After the program tests a generation consisting of 20 cars, it considers the cars that belonged to that generation for selections, crossovers and mutations; which is another way of saying that it selects them by considering their scores, it does some crossovers between them and it does a degree of mutation to them. These result in a new generation of cars that may perform better in the racing track.

The cars aren’t perfect to begin with. At the start, they may not even have wheels (reinventing the wheel anyone?) and they roll over in seconds. However as generations pass, the better performing cars are found and the program goes ahead to use them more often since they score a lot more than the other weirdly shaped cars. The race track also gets trickier and bumpier after a while, which helps the further evolution of cars. In the following image you can see the same car slowing down to a stop while trying to ascend a steep slope.

BoxCar crawling down to a stop, it was a good ride

You can also share a car that performs great with others or see other people’s cars. The algorithm is also explained to a degree for those interested. One problem is that after a while the generations tend to take a longer time since the cars perform better and longer. A solution is to get a bumpier road, by changing the track while keeping your generation.

The name of the program is BoxCar2D, check it out yourself. It’s pretty fun in the beginning, then you can make it run in the background for a while and check it later to see a better generation. Rinse and repeat to find the ultimate car.