Skip to content

Car AI steered with a Neural Network that is trained by a Genetic Algorithm in P5.js.

License

Notifications You must be signed in to change notification settings

KMoszczyc/Car-AI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

58 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Car-AI

Car AI steered with a Neural Network that is trained by a Genetic Algorithm in P5.js.

Car sensors

car

Neural Network architecture

architecture

Features

  • Play and pause the simulation
  • Play the simulation without starting the next generation
  • Show sensors of neural networks
  • Restart the simulation

Genetic Algorithm

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 Function (naive)

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.

Making the race track

create-race-track-speed

First generations

first_generations

Little help..

little_help

Generation 35

generation_35

Generation 207

generation_207

Update!

Now the UI looks like that!

image

New features:

  • 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

https://p5js.org/

About

Car AI steered with a Neural Network that is trained by a Genetic Algorithm in P5.js.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages