AlexDev

Join the Gang Gang and have the latest AI and Tech content.

Home page View on GitHub

Race AI

Posted on 29 July 2023.
javascript python game reinforcement learning

About

Race AI is a project I made to practice reinforcement learning techniques. In this project I trained a DQN agent to drive a car in a racing game.

You have to use wasd to drive the car. You can also try to enable AI mode and see how well the AI can play the game. You can enable debug mode to see all the AI inputs in the form of raycasts from the car to the edges of the track. And you can also change the track from the dropdown menu.

Quickstart

To be able to play around with the manual car you can start a http server in the race directory and open the browser at localhost:8000.

cd race
python -m http.server

To run the python training script you have to install the dependencies using venv and run the training script.

python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
python src/train.py

Reward Function

To change the default behaviour of the training you can also change the reward function given in race/rewardFunction.js. The reward function receives two arguments, the agent information and the track information. The trackInfo object will contain the width and waypoints properties. The width is the with of the track (from center to the edge). The waypoints variable is an array of engine.Point with the waypoints that make the racing track. The agentInfo object contains the player’s position (Point) and rotation (number), the waypoint index of the last passed waypoint waypointIndex and the index of the waypoint that the player has to reach nextWaypointIndex. The agentInfo object has some additional helper properties such as distanceFromTrack to determine the distance of the car from the center and progressDelta to determine the progress made by the player since the last call to the reward function.

Arguments

agentInfo

trackInfo

Returns

Torch Hyperparameters

To change the hyperparameters you can edit the config.json file with the following keys:

learning

environment

target_function

loss

optimizer

eps_scheduler

Conclusion

This was my first RL project that actually did something interesting by playing a game, and not just stuff I had to do for uni homeworks. It has also helped me learn more javascript and HTML.