forked from Farama-Foundation/Minigrid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoorkey.py
94 lines (74 loc) · 2.39 KB
/
doorkey.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from gym_minigrid.minigrid import *
from gym_minigrid.register import register
class DoorKeyEnv(MiniGridEnv):
"""
Environment with a door and key, sparse reward
"""
def __init__(self, size=8, second=False):
self.second = second
super().__init__(
grid_size=size,
max_steps=10*size*size
)
def _gen_grid(self, width, height):
# Create an empty grid
self.grid = Grid(width, height)
# Generate the surrounding walls
self.grid.wall_rect(0, 0, width, height)
# Place a goal in the bottom-right corner
self.put_obj(Goal(), width - 2, height - 2)
# Create a vertical splitting wall
splitIdx = self._rand_int(2, width-2)
self.grid.vert_wall(splitIdx, 0)
# Place the agent at a random position and orientation
# on the left side of the splitting wall
self.place_agent(size=(splitIdx, height))
# Place a door in the wall
doorIdx = self._rand_int(1, width-2)
self.put_obj(Door('yellow', is_locked=True), splitIdx, doorIdx)
# Place a yellow key on the left side
self.place_obj(
obj=Key('yellow'),
top=(0, 0),
size=(splitIdx, height)
)
if self.second:
# Place a blue key on the left side
self.place_obj(
obj=Key('blue'),
top=(0, 0),
size=(splitIdx, height)
)
self.mission = "use the key to open the door and then get to the goal"
class DoorKeyEnv5x5(DoorKeyEnv):
def __init__(self):
super().__init__(size=5)
class DoorKeyEnv6x6(DoorKeyEnv):
def __init__(self):
super().__init__(size=6)
class DoorKeyEnv16x16(DoorKeyEnv):
def __init__(self):
super().__init__(size=16)
class Door2KeyEnv16x16(DoorKeyEnv):
def __init__(self):
super().__init__(size=16, second=True)
register(
id='MiniGrid-DoorKey-5x5-v0',
entry_point='gym_minigrid.envs:DoorKeyEnv5x5'
)
register(
id='MiniGrid-DoorKey-6x6-v0',
entry_point='gym_minigrid.envs:DoorKeyEnv6x6'
)
register(
id='MiniGrid-DoorKey-8x8-v0',
entry_point='gym_minigrid.envs:DoorKeyEnv'
)
register(
id='MiniGrid-DoorKey-16x16-v0',
entry_point='gym_minigrid.envs:DoorKeyEnv16x16'
)
register(
id='MiniGrid-Door2Key-16x16-v0',
entry_point='gym_minigrid.envs:Door2KeyEnv16x16'
)