Using a genetic algorithm model, the program teaches each generation of cars that tries to drive a route created by the user in unity.
Cars start at the blue point and try to get to the red point. If they drive into a white block, their turn ends. The next generation begins after a percentage of the population has finished their turn. The next generation gets some of the genes of the best cars from the previous generation. To prevent blockages, there is a mutation factor that causes the car to run erratically. Each of the given values can be changed in the file: Assets/Code/PopulationController.cs
.
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.
You need to have a Unity installed, preferably version 2021.3.20f1
or up.
You can download files from github or from this link, remember to unzip it.
First open Unity Hub (remeber to have a right version installed), then click Add project from disk
option in Open
button, choose GeneticCars
folder that you downloaded and unziped, then open the newly created project and you're good to go.
A window with an example scene should open. Now with the Move Tool
and Rect Tool
, you can edit the position of all objects (remember that the spawn and target points are hidden in PopulationController layer, and are not merged with colored blocks). If you want to add new walls, copy and paste them where you want.
After setting the scene, click the Play
button at the top of the screen and watch the cars try to find their way.
The code is in Assets/Code/
, in order to edit variables, enter the file PopulationController.cs
.
Below are the generation steps in an example scene.
1st Generation:
5th Generation:
400th Generation:
600th Generation:
1000th Generation: