forked from Farama-Foundation/Minigrid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlavagap.py
80 lines (63 loc) · 2.18 KB
/
lavagap.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
from gym_minigrid.minigrid import *
from gym_minigrid.register import register
class LavaGapEnv(MiniGridEnv):
"""
Environment with one wall of lava with a small gap to cross through
This environment is similar to LavaCrossing but simpler in structure.
"""
def __init__(self, size, obstacle_type=Lava, seed=None):
self.obstacle_type = obstacle_type
super().__init__(
grid_size=size,
max_steps=4*size*size,
# Set this to True for maximum speed
see_through_walls=False,
seed=None
)
def _gen_grid(self, width, height):
assert width >= 5 and height >= 5
# Create an empty grid
self.grid = Grid(width, height)
# Generate the surrounding walls
self.grid.wall_rect(0, 0, width, height)
# Place the agent in the top-left corner
self.agent_pos = (1, 1)
self.agent_dir = 0
# Place a goal square in the bottom-right corner
self.goal_pos = np.array((width - 2, height - 2))
self.put_obj(Goal(), *self.goal_pos)
# Generate and store random gap position
self.gap_pos = np.array((
self._rand_int(2, width - 2),
self._rand_int(1, height - 1),
))
# Place the obstacle wall
self.grid.vert_wall(self.gap_pos[0], 1, height - 2, self.obstacle_type)
# Put a hole in the wall
self.grid.set(*self.gap_pos, None)
self.mission = (
"avoid the lava and get to the green goal square"
if self.obstacle_type == Lava
else "find the opening and get to the green goal square"
)
class LavaGapS5Env(LavaGapEnv):
def __init__(self):
super().__init__(size=5)
class LavaGapS6Env(LavaGapEnv):
def __init__(self):
super().__init__(size=6)
class LavaGapS7Env(LavaGapEnv):
def __init__(self):
super().__init__(size=7)
register(
id='MiniGrid-LavaGapS5-v0',
entry_point='gym_minigrid.envs:LavaGapS5Env'
)
register(
id='MiniGrid-LavaGapS6-v0',
entry_point='gym_minigrid.envs:LavaGapS6Env'
)
register(
id='MiniGrid-LavaGapS7-v0',
entry_point='gym_minigrid.envs:LavaGapS7Env'
)