8

A couple of days ago, this discussion about the difficulty of getting an AI to play Mario Kart started up on a post I made. I decided to start doing some research into gaming/speedrunning AI and it has ultimately led to me staring at a screen watching an AI try to complete 1-1 in Super Mario Bros for the past two days.

The AI is a simple Lua script that runs in an emulator, but it was designed specifically for Super Mario World. There are some issues I would like to straighten out with it, as it can not complete 1-1 yet. Basically, the AI presses random buttons until it starts to move to the right. Moving to the right will give it a progressively higher score, and if it stops or dies, it will reset and try again. It then takes the best attempts and expands on them.

I've been observing some of the intricacies of how the AI functions, and I think that with some tweaks, it could actually learn to complete quite a few of the levels in Mario.

The source code is here. It runs in the FCEUX emulator and is very easy to set up. It'd be cool to turn this into a little project and expand on it, because it really is fascinating to watch and I don't think it will be that difficult to do.

A couple of days ago, [this discussion about the difficulty of getting an AI to play Mario Kart](https://phuks.co/s/Speedrun/31965/5d278ffe-8fc0-47d5-aa1b-135e2571863f) started up on a post I made. I decided to start doing some research into gaming/speedrunning AI and it has ultimately led to me staring at a screen watching an AI try to complete 1-1 in Super Mario Bros for the past two days. The AI is a simple Lua script that runs in an emulator, but it was designed specifically for Super Mario World. There are some issues I would like to straighten out with it, as it can not complete 1-1 yet. Basically, the AI presses random buttons until it starts to move to the right. Moving to the right will give it a progressively higher score, and if it stops or dies, it will reset and try again. It then takes the best attempts and expands on them. I've been observing some of the intricacies of how the AI functions, and I think that with some tweaks, it could actually learn to complete quite a few of the levels in Mario. [The source code is here.](https://github.com/juvester/mari-o-fceux) It runs in the FCEUX emulator and is very easy to set up. It'd be cool to turn this into a little project and expand on it, because it really is fascinating to watch and I don't think it will be that difficult to do.

13 comments

[–] Sarcastaway 1 points (+1|-0)

Are you familiar with a youtuber called SethBling? He's done neural network projects on SMK and SMW.

I know nothing about actually implementing machine learning, but conceptually its some really interesting stuff. The implications are scary, but its all very cool.

[–] PMYA [OP] 1 points (+1|-0)

That is exactly what this is, it's the SMW AI ported to SMB1 and using the FCEUX emulator rather than Bizhawk.

This thing has massive potential for a few different platformers, it just needs tweaking in the right places.

[–] Sarcastaway 2 points (+2|-0)

Ah! There I go displaying my ignorance.

After watching the SMK vid, I realized the SMK program can only drive (give or take) as well as the human it learns from, which makes setting new records an impossibility.

So what happens if the trial/error system used in SMW is allowed to "mutate" a fully trained SMK program? Perhaps through some sort of shared control, where the SMK program only keeps memories from high-fitness species of the trial/error program? It seems to me that if the lap times were used as a measure of "fitness," a hybrid system could achieve some interesting results.

I know nothing about actually implementing any of that, and for all I know the idea is either redundant or impossible, but I figured I'd share regardless.

[–] PMYA [OP] 1 points (+1|-0)

The criteria for the SMW AI improving is extremely basic. All it really does is try to move right as much as possible, with a small incentive to go fast. It also has no starting point to work from, it just does stuff randomly until something good happens, and then it builds from that.

In SMK, the criteria for measuring progress is infinitely more complex. It needs a starting point to go from, because letting it hit random buttons is not going to produce the same level of results as it does in a 2d platformer. There are also issues with developing long term strategies. Imagine a square track, just 4 corners. Let us assume that the AI's starting point is a path that drives all the way around the track in the middle of the road. It mutates a lap attempt where it takes Corner 1 sharply, and saves time overall, so it memorises this action for going around Corner 1. But imagine that the line it takes around Corner 1 makes it impossible to hit an optimal angle on Corner 2, and the real best way to get around the first two corners is to not take the "best" line around Corner 1 in order to get a better average time around both corners.

This example only scratches the surface at how insanely complex a racing AI would be in comparison.

This is lovely! So I'm taking away from this that the script has the AI keep going right while pressing every combination of buttons infinitely until it finds the one combination that is the perfect playthrough?

[–] PMYA [OP] 1 points (+1|-0)

No, there is no kind of input specified in the script. When it starts, it will reset around 50-100 times without moving right. When it moves right, it will start getting score, until it either stops for too long or dies. It will then begin to build on all of its recorded strategies, slightly prioritising the ones that gave it a better score. The score also seems to be partially based on speed, which is causing some issues as it will sometimes recognise an attempt that is shorter but faster than another attempt as being better.

I do not actually know how it works specifically, as I haven't looked too much at the code, but it is only around 800 lines of lua. It is doing some stuff like reading ram values to "see" the level. But basically, it learns everything on its own with no outside input.

[–] smallpond 1 points (+1|-0)

It is doing some stuff like reading ram values to "see" the level.

I'm too lazy to look into this further at the moment, but I would be interested in any elaboration of the above you could provide. I have some had some exposure to people who have trained neural nets for image processing. That sort of thing could be the basis of an AI that plays computer games. Seems like this approach is much more basic..

[–] PMYA [OP] 1 points (+1|-0)

trained neural nets for image processing

That's kind of what this thing is, just a very basic version of it. Some emulators have tools for reading all of the RAM values in a game in real time. The AI looks at this and puts all of the objects in the level into two categories - enemies and platforms. It will learn to jump over enemies and either walk along or jump over blocks depending on their formation (I think, I'm basing this on what I've seen it do).

This does cause some issues and is the reason it has ended up getting itself stuck twice now. It got to a part where there are some steps it needs to jump up, but it recognises the top of each block as being a platform it needs to run along. The result is that it just lands somewhere on the steps and then runs into the block in front of it.