Introduction to the Minecraft environment

The original OpenAI Gym does not contain the Minecraft environment. We need to install a Minecraft environment bundle, available at https://github.com/tambetm/gym-minecraft. This bundle is built based on Microsoft's Malmö, which is a platform for AI experimentation and research built on top of Minecraft.

Before installing the gym-minecraft package, Malmö should first be downloaded from https://github.com/Microsoft/malmo. We can download the latest pre-built version from https://github.com/Microsoft/malmo/releases. After unzipping the package, go to the Minecraft folder and run launchClient.bat on Windows, or launchClient.sh on Linux/MacOS, to launch a Minecraft environment. If it is successfully launched, we can now install gym-minecraft via the following scripts:

python3 -m pip install gym
python3 -m pip install pygame

git clone https://github.com/tambetm/minecraft-py.git
cd minecraft-py
python setup.py install

git clone https://github.com/tambetm/gym-minecraft.git
cd gym-minecraft
python setup.py install

Then, we can run the following code to test whether gym-minecraft has been successfully installed or not:

import logging
import minecraft_py
logging.basicConfig(level=logging.DEBUG)

proc, _ = minecraft_py.start()
minecraft_py.stop(proc)

The gym-minecraft package provides 15 different missions, including MinecraftDefaultWorld1-v0 and MinecraftBasic-v0. For example, in MinecraftBasic-v0, the agent can move around in a small chamber with a box placed in the corner, and the goal is to reach the position of this box. The following screenshots show several missions available in gym-minecraft:

The gym-minecraft package has the same interface as other Gym environments, such as Atari and classic control tasks. You can run the following code to test different Minecraft missions and try to get a high-level understanding of their properties, for example, goal, reward, and observation:

import gym
import gym_minecraft
import minecraft_py

def start_game():
env = gym.make('MinecraftBasic-v0')
env.init(start_minecraft=True)
env.reset()

done = False
while not done:
env.render(mode='human')
action = env.action_space.sample()
obs, reward, done, info = env.step(action)
env.close()

if __name__ == "__main__":
start_game()

At each step, an action is randomly drawn from the action space by calling env.action_space.sample(), and then this action is submitted to the system by calling the env.step(action) function, which returns the observation and the reward corresponding to this action. You can also try other missions by replacing MinecraftBasic-v0 with other names, for example, MinecraftMaze1-v0 and MinecraftObstacles-v0.