Unleash your AI to rule the StarCraft2 (on macOS)

screenshot◎ AI is learning how to play SC2

This a bit of pretentious title is about the setup of the great PySC2 library which makes it possible to “rule” the StarCraft II with your own AI.

“Scientifically” speaking, the PySC2 allows for Reinforcement Learning agents to interact with the StarCraft II game. The library does it by exposing the StarCraft II Machine Learning API as a Python RL Environment.

You can learn more about the Reinforcement Learning by reading A Beginner’s Guide to Deep Reinforcement Learning article. And also, here Wikipedia

The below are described the steps necessary to set up the working environment for it, so let’s make it happen.

The Homebrew is the preferred way to install packages for macOS, so make sure that it is installed.

In case it is not, here are the necessary steps.

First, install the Command Line Tools for Xcode

xcode-select --install

Then accept the XCode license

sudo xcodebuild -license accept

Now we are ready to install the Homebrew

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

Let’s make the installed brew command of the Homebrew project available in your terminal console. Add the following line

export PATH=/usr/local/bin:$PATH

to your preferred shell’s profile. E.g. it could be .bash_profile, or .zshrc.

If you are unsure you can take the “compromise” way and use the .profile.

If you have no preferred text editor, you can use nano

nano ~/.profile

Insert that line export PATH=/usr/local/bin:$PATH into the nano, then press Ctrl + o, then enter, and finally Ctrl + x.

Now type

source ~/.profile

(or whatever your preferred shell’s profile) for the added line to take effect, and then

brew update

This way we update the brew package manager’s database.

And here goes the tricky part.

At the moment of writing this article, the TensorFlow does not support yet Python 3.7 that is being installed by default with the Homebrew. We need to install the Python 3.6 for things to go smoothly later.

Luckily there is a simple way for it.

We will use the python: update 3.6.5_1 bottle commit.

In case, you already have installed the Python 3.7 unlink it first

brew unlink python

Note: this command won’t remove the 3.7 version from your system.

Then install the Python 3.6

brew install --ignore-dependencies https://raw.githubusercontent.com/Homebrew/homebrew-core/f2a764ef944b1080be64bd88dca9a1d80130c558/Formula/python.rb

Now check that we have the right version

python3 -V

It should be Python 3.6.5.

Congratulations 🎉.

The next step is not necessary but recommended.

It is useful for cases when you screw something up and would like to go “from the scratch”.

Let’s install and use the virtualenv

mkdir ~/.virtualenvs
pip3 install virtualenv virtualenvwrapper

And to make it available in your terminal console, add these lines to your shell’s profile

# virtualenv and virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3
source /usr/local/bin/virtualenvwrapper.sh

and then load it for those lines to take effect

source ~/.profile

(or whatever your shell’s preferred profile is).

Pay attention to the terminal output so there are no errors.

Now we are ready to create a dedicated virtual environment which we’ll use for our further setup.

Let’s call it sc2.

mkvirtualenv sc2 -p python3

After this command, your prompt should be prefixed with (sc2).

If you would like to switch to this virtual environment in another terminal tab, or get back to it after you’ve closed your previous one, just type

workon sc2

Now we finally are ready to install the PySC2.

pip install pysc2

Now go to the https://www.blizzard.com and get the StarCraft II installed. The starter edition will suffice for our experiments.

For the PySC2 library to work we need to download and extract some additional materials.

The Ladder maps and Mini-games.

They go to the StarcraftII/Maps/ directory.

This folder does not exist by default so we should create it first.

cd “/Applications/StarCraft II”
mkdir Maps
open Maps

Now download those archives and extract them into the newly created Maps folder.

The map packs are licensed.

Follow the instructions here https://github.com/Blizzard/s2client-proto.

Basically, you just need to enter the iagreetotheeula in the password field while extracting those. By typing this in you agree to be bound by the terms of the AI and Machine Learning License. That’s it.

Now, after you’ve installed the StarCraft II game client and downloaded all the additional materials, you can start experimenting

https://github.com/deepmind/pysc2#run-an-agent

Run this command to check out that everything works fine

python -m pysc2.bin.agent -map Simple64

If you’ve got the StarCraft II opened in a mini window with PySC2 doing it’s RL job, then congratulations! ✨🎉

screenshot◎ AI is 'thinking'