Teaching an AI Car to Drive on Procedurally Generated Track

Teaching an AI Car to Drive on Procedurally Generated Track

Table of Contents

  1. Introduction
  2. The Challenge of Teaching an AI to Drive
  3. Creating a Procedurally Generated Race Track
  4. Enhancing the Track with Scenery
  5. Developing the AI Car
  6. Training the AI Car
  7. Improving the AI Car's Performance
  8. Introducing Other Cars for Racing
  9. Conclusion

The Challenge of Teaching an AI to Drive 🚗

Teaching an AI to drive is no easy feat. The existing AI models in the market are usually trained on predefined courses. But what if we want an AI that can adapt to a dynamically generated track? In this article, we explore the challenges and steps involved in creating an AI that can drive on a procedurally generated race track.

Creating a Procedurally Generated Race Track 🛣️

To start off our project, we needed a race track model. After careful searching, we stumbled upon a fantastic racing model kit by Kenny. The straight and tight corner pieces in the kit had the same Dimensions, making them ideal for creating a procedurally generated track.

To create the track, we implemented a GRID and used the depth-first search algorithm to generate a random path. Each time the track was generated, we obtained unique layouts that added an exciting element of variation. We also added a custom start and finish line to complete the track.

Enhancing the Track with Scenery 🌳

While the procedurally generated track was functional, it lacked visual appeal. To address this, we leveraged another set of models from Kenny and created a simple scenery script. This script added random scenery elements such as clouds and mountains to give the scene depth and make it more visually appealing.

Developing the AI Car 🚀

Next on our agenda was creating a car that could navigate the track. We utilized a car model from the asset store and added a basic car controller, skid marks, and collision particle effects. To enable the AI agents to sense the environment, we incorporated 11 raycasts at the front of the car. The raycasts were programmed to detect other cars, walls, and invisible checkpoints.

Training the AI Car 🧠

The initial episodes of training revealed that the AI had zero understanding of driving and appeared incapable. However, after approximately 65 episodes, the AI learned the concept of moving forward. A few more episodes later, the AI grasped the art of turning, enabling it to navigate a significant portion of the Course.

The AI car's capabilities continued to improve over time. By episode 691, it could complete just over three-quarters of the track, though not consistently. However, after an additional 600 iterations, the AI's progress seemed stagnant. In a bid to reinvigorate the training, we decided to change the course.

Improving the AI Car's Performance 🏎️

The new track we introduced posed a greater challenge with closely packed corners. Surprisingly, the AI car made it almost halfway around the track from the start. However, it soon encountered difficulties with certain corners, getting consistently stuck.

To address this issue, we sent the agents to a "boot Camp" at episode 1961. Here, they faced every possible corner they could encounter on the track. Additionally, the agents were respawned in different positions along the track's width to familiarize themselves with varied circumstances. Gradually, the AI agents learned to navigate all possible corner scenarios.

With each episode, the AI car's performance improved. By episode 3770, the AI car was driving at a significantly faster speed. The training had paid off, and the AI car was becoming a competent driver.

Introducing Other Cars for Racing 🏁

Encouraged by the progress of individual AI agents, we decided it was time to up the ante and introduce other cars for racing. Initially, the agents struggled with frequent collisions, but at episode 3810, an agent successfully dodged past the other cars and made a break for the track.

Over time, the agents' driving skills improved, and by episode 4151, one of the cars managed to complete a full lap of the track. With a few more rounds of training, nearly all the cars were able to flawlessly navigate the track on their first attempt.

Conclusion 🏆

In just over 5000 episodes and 10 hours of training, we succeeded in teaching an AI car to drive on any procedurally generated track. The journey involved overcoming challenges, tweaking the course, and gradually improving the AI's performance. The results speak for themselves, showcasing the potential of AI in the field of autonomous driving.

We hope you found this article insightful. Don't forget to comment, rate, and subscribe to our Channel for more exciting content!

Find AI tools in Toolify

Join TOOLIFY to find the ai tools

Get started

Sign Up
App rating
4.9
AI Tools
20k+
Trusted Users
5000+
No complicated
No difficulty
Free forever
Browse More Content