Would it be possible to use a Neural Network / AI to 'optimise' the time taken for the race?

106 views Asked by At

The program when completed will aim to use AI to get the quickest possible time. The car can accelerate, brake or move at constant speed. There will be sections throughout the code (which represent corners) where the speed will have to be = to or under a certain value (depending on how tight the corner is) and I want the program to be able to decide when the best moments would be to accelerate, brake and move at constant speed would be.

Is this even possible with python? Could you create a neural network which would progressively get a better time? If so how would I go about doing something like this?

Thanks !

import time

x = 0

def TrackSimulation(distance, speed, acceleration, loopbreak, time1):

while loopbreak == 1:

    if x == 1:

        acceleration = 9
    elif x == 2:

        acceleration = -9

    elif x == 0:

        acceleration = 0
    else:

        print("Error")

    if distance >= 0 and distance < 80:

        speed = (speed) + ((acceleration) * 0.1)
        distance = (distance) + ((speed) * 0.1)
        time1 = time1 + 0.1

        print((speed), " M/s")
        print((distance), "M")

        time.sleep(0.1)

    elif distance >= 80 and distance <= 110:

        if speed >= 30:

            print("Too fast!")
            loopbreak = 2
            break

        else:
            print("You are in the speed checker")

            speed = (speed) + ((acceleration) * 0.1)
            distance = (distance) + ((speed) * 0.1)
            time1 = time1 + 0.1

            print((speed), " M/s")
            print((distance), "M")

            time.sleep(0.1)

    elif distance >= 110 and distance < 200:

        speed = (speed) + ((acceleration) * 0.1)
        distance = (distance) + ((speed) * 0.1)
        time1 = time1 + 0.1

        print((speed), " M/s")
        print((distance), "M")

        time.sleep(0.1)

    elif distance >= 200:

        print("race over")
        finaltime = round((time1), 3)
        print("This was your time,", (finaltime))
        loopbreak = 2
        break
1

There are 1 answers

2
Akshay Sehgal On

I would recommend checking out how reinforcement learning works. The core idea is this -

Reinforcement learning is about taking suitable action to maximize reward in a particular situation.

So in your case, for example, you have this track and you need to build an algorithm that allows the car to reach the goal in minimum time. This means you need to train a reinforcement learning model which minimizes the time taken to reach the goal. The model will have a few inputs parameters such as velocity, acceleration, left steer, right steer, break etc. It will start by taking random actions in this input space and trying to reach the end goal while staying on track and minimizing the time taken.

Open AI Gym provides an excellent set of tools in python to practice and learn reinforcement algorithms such as q-learning. It contains various games implemented in python that allow you to build your own models and try training your actors against a reward. Check this car racing game implemented there.

Here is a video on reinforcement learning to train Mario in Mario-kart to win the race.