-
Notifications
You must be signed in to change notification settings - Fork 0
/
blockedunlockpickup.py
52 lines (42 loc) · 1.51 KB
/
blockedunlockpickup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from gym_minigrid.minigrid import Ball
from gym_minigrid.roomgrid import RoomGrid
from gym_minigrid.register import register
class BlockedUnlockPickup(RoomGrid):
"""
Unlock a door blocked by a ball, then pick up a box
in another room
"""
def __init__(self, seed=None):
room_size = 6
super().__init__(
num_rows=1,
num_cols=2,
room_size=room_size,
max_steps=16*room_size**2,
seed=seed
)
def _gen_grid(self, width, height):
super()._gen_grid(width, height)
# Add a box to the room on the right
obj, _ = self.add_object(1, 0, kind="box")
# Make sure the two rooms are directly connected by a locked door
door, pos = self.add_door(0, 0, 0, locked=True)
# Block the door with a ball
color = self._rand_color()
self.grid.set(pos[0]-1, pos[1], Ball(color))
# Add a key to unlock the door
self.add_object(0, 0, 'key', door.color)
self.place_agent(0, 0)
self.obj = obj
self.mission = "pick up the %s %s" % (obj.color, obj.type)
def step(self, action):
obs, reward, done, info = super().step(action)
if action == self.actions.pickup:
if self.carrying and self.carrying == self.obj:
reward = self._reward()
done = True
return obs, reward, done, info
register(
id='MiniGrid-BlockedUnlockPickup-v0',
entry_point='gym_minigrid.envs:BlockedUnlockPickup'
)