Making an RL-AI to break the Mario speed-run world record.

So, one of the oldest memory of me gaming I have is me playing Mario on a console that my parents bought for me. As a kid I was fascinated…

Making an RL-AI to break the Mario speed-run world record.

Image credits

So, one of the oldest memory of me gaming I have is me playing Mario on a console that my parents bought for me. As a kid I was fascinated by it. Trying to get through the levels, getting the perfect jumps was like achievements for me, Trying and trying to grind the final boss these are all the good memories that Mario have given me. So the other day I saw that someone broke the record and finished Mario in 41.022 seconds. Then I thought to myself “I wonder if I can make an AI to finish it faster” and then I realised I am a noob in reinforcement learning 🙂.

So, I got to some reasearch and found out OpenAI had something called gym-retro.

https://openai.com/blog/gym-retro/

So, they basically provide environments for developers to train there reinforcement learning AIs. It's basically like a game emulator in python.

So, now we have the enviorment. I started learning how to make RL models and found an amazing YouTube channel named: Nicholas Renotte. He had some amazing videos on reinforcement learning and after some yoinking of code from him and a-lot of research I was ready to code this.

So, first lets setup Mario:

Lets download the installs:

Lets get the Imports:

Now lets install some dependencies:

I am on Google Colab so I need to make a virtual screen to display the game lets go ahead and do that:

Lets now actually setup the game:

Now lets start the game and try out random functions:

For now its doing random actions

now setting up Mario is complete.

Lets now Preprocess the environment to learn RL on it:

Lets install Pytorch First:

Lets install baseline RL things:

Lets import the various wrappers we will need:

lets add the wrappers to the enviorment:

Lets get the states:

Lets see the pre-processing:

The Output

Now lets get to training our RL model:

Import and install models:

Lets make a callback function to save trained models:

Lets save the model every 10000 steps:

Now the model needs to be started:

The learning rate is very important too fast learning rate or too slow learing rate can cause the AI to not learn anything.

Now lets start the training of our AI:

So I have set the steps to 500,000 which is not enough to train the AI completely but if you have time and GPU I would suggest it to be set to atleast 1M->5M.

This process took about 2 hrs:

This is the final model I got. I would say pretty good for only 500,000 steps.

Now lets use this model and run our game:

Finally lets see the output:

model_500,000 (the video was slowed down by me willingly its at 0.5x speed)

Honestly for a only 500,000 step model it performed amazing and if it is trained more I believe it can break the record.

Now lets see the AI at its different stages:

At stage 100,000:

model_100,000

At stage 250,000:

At stage 500,000:

Now you can see how the AI improved itself in different stages.

Ahhhh AI is awesome and so fun. I want to make more RL stuff so comment below on what should I make next.

If you liked the article follow me on medium. It honestly helps a lot.

For the Colab Notebook go here:

For more stuff check out my GitHub:

For my day to day AIML research updates follow me on twitter:

Thanks for reading 😁, see ya guys next week 👋🏼

Don’t forget to comment down below what should I make next.