Today I will show you how you can create create a game AI in under 10 minutes. All you have to do is bear with me for 10 minutes.
This tutorial assumes that you have only basic knowledge of programming and are familiar with at least one procedural language (because we are going to use Python). If not, no worries. You can learn it later too. Let’s focus on the aim of this tiny endeavor.
Open AI is an Open Source project on Artificial Intelligence providing a workspace for programmers (such as yourself) to experiment with AIs without much hassle on your behalf. Designing a Game AI required you to design a Game first. Well, we don’t have time for that… What we are interested in is the AI and not the game. The OpenAI Gym project provides us a set of utilities to incorporate our AI into any supported Games. At the moment, let’s just use a Retro-style Arcade game. Gym comes prepackaged with a set of environments based on Arcade Games but you can install fancier environments later.
Installing Open AI Gym
On Linux (tested on Ubuntu 17.10)
- Installing Python: We will be using Python 3 for this project. So, even though most Linux environments ship with Python 2.7 installed, we have to do a clean installation of Python 3. Open Terminal and type –
sudo apt-get install python3
- Making sure pip is installed: Pip is the python command line package management tool which should be installed by default but it’s safe to check.
sudo apt-get install python3-pip
- Installing OpenAI Gym: There are several instructions for installing Gym for several supported systems. Head over to Gym’s Github Page to see the specific instruction for your system. If required you might have to install linuxbrew too.
- Installing Python Idle: IDLE is a Python IDE developed by it’s creators. This enables us to run an interactive shell for Python. Let’s install this to check out our cool new playground. Open Terminal (if not already open) and run this code:
sudo apt-get install idle
On Windows (tested on Windows 10)
- Installing Python: This is easy as pie for Windows users. Python now provides GUI Installations for your system. Go to their Releases Page and download the correct executable installer for your System Architecture (x86 if 32-bit and x86-64 if 64-bit) and install. While installing be sure to check the Set PATH variable box.
- Installing Gym: Open a command prompt window and type in
pip install gym
Let’s get started
Now you should be locked and loaded to fire up your first OpenAI environment. Open a new Terminal on Linux and type in “`idle“` and hit Enter. It should open up an IDLE prompt.
On Windows, open up a Command Prompt window and type in “`python“` to open an interactive shell.
Put the following code. (Note: Python IDLE might show a multiline detected error if you copy and paste the code so you have to type it in manually, no shortcuts 😛 ).
import gym env = gym.make('MountainCar-v0') state = env.reset() env.render()
You should see a screen like this:
The goal in this game is to get this car to the top of that mountain with the Yellow Flag on it. The rules of the game are:
- You can only push the car to the left (state 0), not at all (state 1) or to the right (state 2).
- There is no penalty for going left
- Each step you make gives you a -1 point. So, you have to complete it in the minimum number of steps.
The variables for any environment in OpenAI Gym can be categorised into 2 sections:
- Observation Space: The data you get from the state of the environment (e.g., current position and velocity)
- Action Space: The data you put into the environment (e.g., push direction)
For this game the observation space returns you a list of 2 values.
- The first one is a measure of your displacement.
- The second one is a measure of your velocity.
You can perform an action by specifying a single variable having a Discrete value 0,1 or 2. A value of 0 means a push to the left, 1 means no push and 2 means a push to the right. By learning to co-ordinate the pushes, your AI must overcome the hurdle of climbing the hill. First let’s use a dummy method to achieve our goal. Open a new file named test.py and paste the following code into it and save:
<pre>import gym env = gym.make('MountainCar-v0') env.reset() state, reward, done, info = env.step(2) vel = state accl = vel for _ in range(1000): env.render() if(accl >= 0 or state > 0): state, reward, done, info = env.step(2) else: state, reward, done, info = env.step(0) if(state >= 0.5): break accl = state - vel vel = state print('Solved')</pre>
This will do the job. Open a Terminal and type in “`python3 test.py“` and you can see the car in action. But this is just a fix for a particular game. There are several paths you can take from here. For instance, you can apply a Neural Network to simulate more than one episodes and at each state, update the nodes until your NN figures out the problem. Or you can implement a Reinforcement Learning Algorithm like I’ll be showing in the next tutorial to improve this AI. If you are familiar with the basics of AI algorithms, implementing one should not be a problem.
Here is a list of resources for learning AI algorithms:
P.S., if you have not already seen it, now might be a good time to check out my earlier post about the several Optimization Algorithms. You can also see my Python implementation of such algorithms and try to implement it here. Stay tuned for the next Part to learn how to implement a proper algorithm for such games. Good luck exploring !
If you like this post please do share it and subscribe. If you have a query or would like to add to this post, please comment down below. Thanks for reading.