Loading

NeurIPS 2021: MineRL Diamond Competition

Testing MineRL environment

Test the environment by running a fixed sequence of actions in a fixed world

karolisram

Introduction

This notebook is part one of the Intro track baselines for the MineRL 2021 competition. It provides an easy way to try out the MineRL environment without having to install anything on your machine. The notebook should run in 5-10 minutes if you click Runtime -> Run All in the menu above.

If you want to edit the notebook, you will have to save a copy to your Google Drive. Simply click File -> Save a copy in Drive.

Below you will find a fully scripted agent that spawns in a fixed world and executes a sequence of actions to acquire 6 pieces of wood, craft a wooden pickaxe and dig some cobblestone.

You can try adjusting the scripted part to progress further in the task and acquire more rewards. Please note that in the competition setting your agent will spawn in a random world - this is explored in the second notebook:

MineRL fully scripted

Setup

In [ ]:
%%capture
# ^ hides output
!sudo add-apt-repository -y ppa:openjdk-r/ppa
!sudo apt-get purge openjdk-*
!sudo apt-get install openjdk-8-jdk
!sudo apt-get install xvfb xserver-xephyr vnc4server python-opengl ffmpeg
In [ ]:
%%capture
# ^ hides output
!pip3 install --upgrade minerl
!pip3 install pyvirtualdisplay
!pip3 install -U colabgymrender

Import libraries

In [ ]:
import gym
import minerl
from tqdm.notebook import tqdm
from colabgymrender.recorder import Recorder
from pyvirtualdisplay import Display
/usr/local/lib/python3.7/dist-packages/gym/logger.py:30: UserWarning: WARN: Box bound precision lowered by casting to float32
  warnings.warn(colorize('%s: %s'%('WARN', msg % args), 'yellow'))
Imageio: 'ffmpeg-linux64-v3.3.1' was not found on your computer; downloading it now.
Try 1. Download from https://github.com/imageio/imageio-binaries/raw/master/ffmpeg/ffmpeg-linux64-v3.3.1 (43.8 MB)
Downloading: 8192/45929032 bytes (0.0%)1400832/45929032 bytes (3.0%)4153344/45929032 bytes (9.0%)6963200/45929032 bytes (15.2%)9773056/45929032 bytes (21.3%)12771328/45929032 bytes (27.8%)15753216/45929032 bytes (34.3%)18497536/45929032 bytes (40.3%)21135360/45929032 bytes (46.0%)24141824/45929032 bytes (52.6%)26787840/45929032 bytes (58.3%)29556736/45929032 bytes (64.4%)32309248/45929032 bytes (70.3%)35078144/45929032 bytes (76.4%)37568512/45929032 bytes (81.8%)40361984/45929032 bytes (87.9%)43016192/45929032 bytes (93.7%)45858816/45929032 bytes (99.8%)45929032/45929032 bytes (100.0%)
  Done
File saved as /root/.imageio/ffmpeg/ffmpeg-linux64-v3.3.1.

Start of the agent code

In [ ]:
def str_to_act(env, actions):
    """
    Simplifies specifying actions for the scripted part of the agent.
    Some examples for a string with a single action:
        'craft:planks'
        'camera:[10,0]'
        'attack'
        'jump'
        ''
    There should be no spaces in single actions, as we use spaces to separate actions with multiple "buttons" pressed:
        'attack sprint forward'
        'forward camera:[0,10]'

    :param env: base MineRL environment.
    :param actions: string of actions.
    :return: dict action, compatible with the base MineRL environment.
    """
    act = env.action_space.noop()
    for action in actions.split():
        if ":" in action:
            k, v = action.split(':')
            if k == 'camera':
                act[k] = eval(v)
            else:
                act[k] = v
        else:
            act[action] = 1
    return act

Actions

Here's a list of all possible actions:

Dict(attack:Discrete(2),
     back:Discrete(2),
     camera:Box(low=-180.0, high=180.0, shape=(2,)),
     craft:Enum(crafting_table,none,planks,stick,torch),
     equip:Enum(air,iron_axe,iron_pickaxe,none,stone_axe,stone_pickaxe,wooden_axe,wooden_pickaxe),
     forward:Discrete(2),
     jump:Discrete(2),
     left:Discrete(2),
     nearbyCraft:Enum(furnace,iron_axe,iron_pickaxe,none,stone_axe,stone_pickaxe,wooden_axe,wooden_pickaxe),
     nearbySmelt:Enum(coal,iron_ingot,none),
     place:Enum(cobblestone,crafting_table,dirt,furnace,none,stone,torch),
     right:Discrete(2),
     sneak:Discrete(2),
     sprint:Discrete(2))

Camera

Camera actions contain two values:

  1. Pitch (up/down), where up is negative, down is positive.
  2. Yaw (left/right), where left is negative, right is positive.

For example, moving the camera up by 10 degrees would be 'camera:[-10,0]'.

Change agent behaviour here

To change the sequence of actions that the agent performs, change the code inside the get_action_sequence() function below. One action is done every tick and there are 20 ticks per second in a regular Minecraft game.

In [ ]:
def get_action_sequence():
    """
    Specify the action sequence for the agent to execute.
    """
    # get 6 logs:
    action_sequence = []
    action_sequence += [''] * 100  # wait 5 sec
    action_sequence += ['forward'] * 8
    action_sequence += ['attack'] * 61
    action_sequence += ['camera:[-10,0]'] * 7  # look up
    action_sequence += ['attack'] * 61
    action_sequence += ['attack'] * 61
    action_sequence += ['attack'] * 61
    action_sequence += ['attack'] * 61
    action_sequence += [''] * 50
    action_sequence += ['jump']
    action_sequence += ['forward'] * 10
    action_sequence += ['camera:[-10,0]'] * 2
    action_sequence += ['attack'] * 61
    action_sequence += ['attack'] * 61
    action_sequence += ['attack'] * 61
    action_sequence += ['camera:[10,0]'] * 9  # look down
    action_sequence += [''] * 50

    # make planks, sticks, crafting table and wooden pickaxe:
    action_sequence += ['back'] * 2
    action_sequence += ['craft:planks'] * 4
    action_sequence += ['craft:stick'] * 2
    action_sequence += ['craft:crafting_table']
    action_sequence += ['camera:[10,0]'] * 9
    action_sequence += ['jump']
    action_sequence += [''] * 5
    action_sequence += ['place:crafting_table']
    action_sequence += [''] * 10

    # bug: looking straight down at a crafting table doesn't let you craft. So we look up a bit before crafting:
    action_sequence += ['camera:[-1,0]']
    action_sequence += ['nearbyCraft:wooden_pickaxe']
    action_sequence += ['camera:[1,0]']
    action_sequence += [''] * 10
    action_sequence += ['equip:wooden_pickaxe']
    action_sequence += [''] * 10

    # dig down:
    action_sequence += ['attack'] * 600
    action_sequence += [''] * 10

    return action_sequence

Start Minecraft

In [ ]:
display = Display(visible=0, size=(400, 300))
display.start();
In [ ]:
env = gym.make('MineRLObtainDiamond-v0')
env = Recorder(env, './video', fps=60)

Run your agent

After the code below finishes you should see a video of the agent and a line saying it received 35.0 reward.

In [ ]:
action_sequence = get_action_sequence()

env.seed(21)
obs = env.reset();
 
total_reward = 0
for i, action in enumerate(tqdm(action_sequence)):
    obs, reward, done, _ = env.step(str_to_act(env, action))
    total_reward += reward
    if done:
        break

env.release()
env.play()

print(f'\nTotal reward = {total_reward}')
  0%|          | 0/1396 [00:00<?, ?it/s]

100%|█████████▉| 1395/1396 [00:00<00:00, 1536.28it/s]
Out[ ]:
Total reward = 35.0

Comments

You must login before you can post a comment.

Execute