# The Best Path a DeepRacer Can Learn

Source: Deep Learning on Medium

# The Best Path a DeepRacer Can Learn

## How using the ideal racing line could improve DeepRacer’s performance time by over 10%.

I joined Myplanet about three months ago. Shortly after I joined, we had a team compete in—and win!—the Canadian edition of the 2019 Deep Racer league. I didn’t know much about DeepRacer at the time, but I’m deeply challenge motivated and hearing what they accomplished was all the fuel I needed to stoke my competitive fires. I immediately joined my colleagues in their quest to grapple with this new and dynamic machine learning opportunity.

I learned as much as possible as quickly as possible, figuring out how Deep Racer works, getting a handle on the strategies teams have used worldwide to shave milliseconds off their times, and wrapping my head around the various models out there for improving results.

And of course, part of my research looked at how our own team had attacked the challenge. In the many successful strategies and approaches they had employed, one thing they hadn’t considered was an ideal racing line for training their winning models— something I thought could have a big impact.

To briefly explain what I mean, let’s look at the re:Invent 2019 championship track. The total length following the centreline is 23.12 meters, but our current best approximation to the ideal line (in red) is only 20.94 meters long. That means if we taught DeepRacer to drive the red path instead of the centreline, we should be completing laps 10% faster. In addition, our ideal racing line requires less steering than the original track, which means faster driving. In theory, 10% is not the most, but the least we could improve.

If my hypothesis was correct, teaching DeepRacer to aim for the ideal racing line could give us the edge we needed to win the 2019 championship.

# The Ideal Racing Line

In simple terms, the ideal racing line is the fastest possible path through a circuit. There are many well-known guidelines for defining an ideal line, but in general, they aim to balance four (often conflicting) goals:

1. Use the full width of the track, to achieve the widest possible turning radius around a curve, because the wider the turn, the faster you can go through it.
2. Keep braking to a minimum (in terms of both deceleration and time) and try to get it all done before you start turning.
3. Accelerate throughout the curve and, if at all possible, exit the curve at full throttle.
4. Drive in a straight line as much as possible, because that maximizes the time you will be driving at top speed.

Beyond these broad guidelines, defining an ideal line is more art than science; and racing it effectively, depends on everything from the load balancing of the car, to the nature of the circuit, to the reaction time and state of mind of the driver. There are entire books dedicated to the topic, and it takes professional drivers years of relentless practise to master all the techniques.

Knowing the ins and outs of an ideal racing line is great, but teaching a self-driving car to race it is a whole other issue. There are some formulaic solutions to fitting an ideal racing line through a circuit (Euler spirals are perhaps the most popular) and, as this Engineering Master’s thesis suggests, the problem is even deserving of its own separate Machine Learning approach. But is there an easier way? Can we (pardon the pun) cut any corners?

# Finding DeepRacer’s Ideal Line

As the Technical Lead of Myplanet Ventures, I often have to define the boundaries of technical feasibility, given our time constraints, and look for shortcuts or alternatives that can help us reach our goals quickly. But training DeepRacer takes a long time and even after finding the ideal racing line, we still need to translate it into a sensible reward function, so trying to come up with a formula, may not be the best use of our time. Besides, tracks are provided in the form of waypoints, not functions. So I hypothesized that, if cleverly massaged and without too much extra work, these waypoints could well be turned into a sufficient approximation of an ideal racing line. Let’s get to it.

## Low-Pass Filtering

One of the easiest things to notice about ideal racing lines is that they tend to minimize steering. This makes sense since steering typically requires braking, which costs time. In other words, translating the path of a circuit (as represented by its centreline) into an ideal racing line, requires the smoothing out of turns, which, in the case of slight turns, can even result in a straight path right through a curve.

There is a similar context involving the filtering out of sharp changes elsewhere in Engineering: signal processing. Filtering is heavily used in signal processing to remove unwanted noise. For example, one of the most common uses of filtering in audio processing is to remove high frequencies from an audio stream to prevent damaging larger speakers (e.g., sub-woofers), which cannot mechanically sustain the loads involved in displacing air at high speeds. This low-pass filtering is so common that there are some simple and well-known algorithm implementations of it, which can be easily applied to data series. The most common of these implementations is perhaps the first-order RC filter, which only needs the stream and a constant parameter (the RC constant) to do its magic: