This project is a Proof of Concept for a 2D car simulation with AI. The goal is to create a simulation where the AI learns to drive a car on a track. The circuits and game mechanisms have been created by NeuralNine :
- GitHub: https://github.com/NeuralNine/ai-car-simulation
- Youtube: https://www.youtube.com/watch?v=Cy155O5R1Oo
The project is divided in two parts: the simulation and the AI. The simulation is a 2D game where the player can drive a car on a track. The AI is a neural network that learns to drive the car on the track.
- Python >3.10
We highly recommand you to use a virtual environment to run the project. For this project we used the virtual env feature of PyCharm.
- Clone the repository
- Install the dependencies with
pip install -r requirements.txt
To run the simulation, run the Main.py
file.
You can change the parameters of the simulation in the main function of the Main.py
file.
-
NB_CARS: Number of cars in the simulation
This constant must be equal or lower than your number of CPU cores. If you have 4 cores, you can set this constant to 4. -
ID_MAP: Id of the map to load
This constant must be between 1 and 5. There are 5 maps in the game. Feel free to create your own maps and add them to theassets
folder. -
ALGO: The algorithm to use for the training.
The available algorithms areA2C
andPPO
.
if __name__ == '__main__':
NB_CARS = 8
ID_MAP = 4
ALGO = "A2C"
# Start Race Server
race = threading.Thread(target=thread_race, args=(NB_CARS, ID_MAP))
race.start()
if NB_CARS == 1:
# Start Training with Mono Client
train_monoproccess(ALGO, ID_MAP)
else:
# Start Training with AI Clients
train_multiproccess(ALGO, ID_MAP, NB_CARS)
For more information about how it works and how to use it, please refer to the documentation of stable-baselines and the video of NeuralNine
The car is a 2D sprite that can move on the map. It has a speed and a direction. The car can move forward and backward. It can also turn left and right.
Here are the maps available in the game:
We do not use this map in our tests because it is too difficult for the AI and does seems relevant for our tests.
The simulation is a server that runs on a thread. The AI is another server which create workers. each worker is a client that connects to the server. The server sends the state of the game to the client. The client sends the action to the server. The server applies the action to the game and sends the new state to the client. The client receives the new state and sends the new action to the server. And so on...
Here is a diagram of the process:
This project has been created by a group of students from the ESEO Engineering School in Angers, France. We are 3 students in the 5th year of the school, in the Software Engineering and Artificial Intelligence specialization.