1

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
sadGreenRL
  • 23
  • 2
  • I think as stated this question is far too broad for stackoverflow. I suggest looking at https://gym.openai.com/ and if you need foundational knowledge on machine learning techniques, including neural networks, then I would recommend https://www.coursera.org/learn/machine-learning as an excellent introduction to the broader topic. – Ian Ash Jul 23 '20 at 14:06
  • Ah okay, Thank you for the links. – sadGreenRL Jul 23 '20 at 14:10

1 Answers1

1

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.

Akshay Sehgal
  • 18,741
  • 3
  • 21
  • 51
  • Thank you for the response! I've had a look at Gym and I wanna ask, Do you know if you can create your own 'environments' using it? I was reading about gym on their website last night and they have many environments built in but I'm guessing I would need to use my own. Thanks for your help ! – sadGreenRL Jul 25 '20 at 12:29
  • Yes you can! https://medium.com/@apoddar573/making-your-own-custom-environment-in-gym-c3b65ff8cdaa – Akshay Sehgal Jul 25 '20 at 16:28