NeurIPS 2021: MineRL Diamond Competition
Testing MineRL environment
Test the environment by running a fixed sequence of actions in a fixed world
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:
Setup¶
%%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
%%capture
# ^ hides output
!pip3 install --upgrade minerl
!pip3 install pyvirtualdisplay
!pip3 install -U colabgymrender
Import libraries¶
import gym
import minerl
from tqdm.notebook import tqdm
from colabgymrender.recorder import Recorder
from pyvirtualdisplay import Display
Start of the agent code¶
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:
- Pitch (up/down), where up is negative, down is positive.
- 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.
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¶
display = Display(visible=0, size=(400, 300))
display.start();
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.
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}')
Content
Comments
You must login before you can post a comment.