Car AI steered with a Neural Network that is trained by a Genetic Algorithm in P5.js.
Play with it! - https://kmoszczyc.github.io/Car-AI/
- Play and pause the simulation
- Play the simulation without starting the next generation
- Show sensors of neural networks
- Restart the simulation
algorithm:
- start new generation (40 cars)
- wait for some time
- calculate cars fitness
- roulette selection - more fitness -> bigger chance of making offspring
- mutation of genes (randomly changing weights in neural network)
- go to step 1
The best car from previous generation is copied to the next one without mutation, so that it's valuable genes don't get lost during the selection. All the rest of the cars are deleted from the previous generation.
fitness = distance_traveled + max_dist_from_start_point*3 + life_time
So this rewards cars that live long, travel longest distance but most importantly get as far away from the start as possible. It's supposed to fight the problem of spinning cars at the starting point, but it has a downside. For specific tracks it may get the population stuck in a local minimum being a sharp corner that is indeed far from the start, but to go further down the track the car has to get closer to the start sometimes, lowering it's fitness. This could be fixed with fitness gates, gates that would be positioned on the track and passing each one would increase car's fitness. Spinning cars would get 0 fitness score then. Although current naive fitness function is good enough.
- clear current racetrack
- save and load racetracks (local storage)
- delete racetracks
- visualize best neural network
- zoom and move around with mouse
- Lock and unlock the camera
Neural network library based on: https://www.youtube.com/watch?v=XJ7HLz9VYz0&list=PLRqwX-V7Uu6aCibgK1PTWWu9by6XFdCfh by Dan Shiffman