Skip to content

πŸš— Draw a circuit and watch self-driving cars evolve thanks to the NEAT evolutionary algorithm.

License

Notifications You must be signed in to change notification settings

marcpinet/neat-cars

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

32 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Neat Cars

πŸ“ Description

Neat cars allows you to draw a track, choose a starting point and watch the magic happen: the cars will drive themselves and constantly improve their driving skills.
You will also be able to observe the Artificial Neural Network of the best car from the generation.

The project is based on a genetic algorithm called NEAT (NeuroEvolution of Augmenting Topologies).

πŸŽ₯ Demo

Live neural network in the top left corner

demo

Track 1: With sensors - Infinite track

6f2NHLh.mp4

Track 2: Without sensors - Finite track

qmsJrvZ.mp4

πŸ’‘ How to use

Prerequisites

  • Python 3.7.0+

Get a copy of the Project. Assuming you have git installed, open your Terminal and enter:

git clone 'https://github.com/marcpinet/neat-cars.git'

To install all needed requirements run the following command in the project directory:

pip install -r requirements.txt

Running

After that, you can proceed to start the program by running main.py.

Controls and tweaks

Instructions are displayed in the window's title.

title

You can also see the stats of the current generation in the title...

title2

...and the full stats inside the console.

cli_output

Feel free to tweak the parameters inside the ai/config.txt but also the static variables inside the Car, CarAI and Engine classes. For example, you can disable the rendering of the car's sensors by setting DRAW_SENSORS to False in the Car class.

βš™οΈ How the AI works

The neural network is trained using the NEAT algorithm. The NEAT algorithm is a genetic algorithm which evolves over time from a basic neural network to a more advanced and complex one depending on your fitness function by going further and further. Check the neat-python documentation for more infos.

neat

Also, you can find the full mathematic approach and details directly in the NEAT paper.

Inputs

The main informations the car will use to drive are the distance to the walls in front and next of it. The car has 5 sensors :

  • In front,
  • 2 in the diagonals
  • 2 on each side

The sensors are represented by a green line in the rendering. Red means the sensor is detecting a wall.

inputs

Outputs

The outputs are obviously the car's actions. The car has 4 possible actions:

  • Turn left
  • Turn right
  • Accelerate
  • Brake

Note that we have a minimum speed to respect so that the car doesn't stop completely nor drives too slowly.

We get, as a starting point for our neural network, something like this:

nn

The algorithm will then create itself the necessary connections (increasing their weight over time) and eventually add hidden layers in the process.

Fitness

The fitness is quite simple: the more the car drives, the better it is. The fitness is calculated by the distance the car has driven. The car is therefore penalized if it crashes.

πŸ› Known issues

  • Nothing yet!

πŸ₯… TO-DO List

  • Find a way to allow 8-like tracks

✍️ Authors

πŸ“ƒ License

This project is licensed under the MIT License - see the LICENSE.md file for details

About

πŸš— Draw a circuit and watch self-driving cars evolve thanks to the NEAT evolutionary algorithm.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages