crossing.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import itertools as itt
  2. import numpy as np
  3. from minigrid.core.grid import Grid
  4. from minigrid.core.mission import MissionSpace
  5. from minigrid.core.world_object import Goal, Lava
  6. from minigrid.minigrid_env import MiniGridEnv
  7. class CrossingEnv(MiniGridEnv):
  8. """
  9. ![LavaCrossingS9N1](../_static/figures/LavaCrossingS9N1.png)<br />
  10. ![LavaCrossingS9N2](../_static/figures/LavaCrossingS9N2.png)<br />
  11. ![LavaCrossingS9N3](../_static/figures/LavaCrossingS9N3.png)<br />
  12. ![LavaCrossingS11N5](../_static/figures/LavaCrossingS11N5.png)<br />
  13. ![SimpleCrossingS9N1](../_static/figures/SimpleCrossingS9N1.png)<br />
  14. ![SimpleCrossingS9N2](../_static/figures/SimpleCrossingS9N2.png)<br />
  15. ![SimpleCrossingS9N3](../_static/figures/SimpleCrossingS9N3.png)<br />
  16. ![SimpleCrossingS11N5](../_static/figures/SimpleCrossingS11N5.png)<br />
  17. ### Description
  18. Depending on the `obstacle_type` parameter:
  19. - `Lava` - The agent has to reach the green goal square on the other corner
  20. of the room while avoiding rivers of deadly lava which terminate the
  21. episode in failure. Each lava stream runs across the room either
  22. horizontally or vertically, and has a single crossing point which can be
  23. safely used; Luckily, a path to the goal is guaranteed to exist. This
  24. environment is useful for studying safety and safe exploration.
  25. - otherwise - Similar to the `LavaCrossing` environment, the agent has to
  26. reach the green goal square on the other corner of the room, however
  27. lava is replaced by walls. This MDP is therefore much easier and maybe
  28. useful for quickly testing your algorithms.
  29. ### Mission Space
  30. Depending on the `obstacle_type` parameter:
  31. - `Lava` - "avoid the lava and get to the green goal square"
  32. - otherwise - "find the opening and get to the green goal square"
  33. ### Action Space
  34. | Num | Name | Action |
  35. |-----|--------------|--------------|
  36. | 0 | left | Turn left |
  37. | 1 | right | Turn right |
  38. | 2 | forward | Move forward |
  39. | 3 | pickup | Unused |
  40. | 4 | drop | Unused |
  41. | 5 | toggle | Unused |
  42. | 6 | done | Unused |
  43. ### Observation Encoding
  44. - Each tile is encoded as a 3 dimensional tuple:
  45. `(OBJECT_IDX, COLOR_IDX, STATE)`
  46. - `OBJECT_TO_IDX` and `COLOR_TO_IDX` mapping can be found in
  47. [minigrid/minigrid.py](minigrid/minigrid.py)
  48. - `STATE` refers to the door state with 0=open, 1=closed and 2=locked
  49. ### Rewards
  50. A reward of '1' is given for success, and '0' for failure.
  51. ### Termination
  52. The episode ends if any one of the following conditions is met:
  53. 1. The agent reaches the goal.
  54. 2. The agent falls into lava.
  55. 3. Timeout (see `max_steps`).
  56. ### Registered Configurations
  57. S: size of the map SxS.
  58. N: number of valid crossings across lava or walls from the starting position
  59. to the goal
  60. - `Lava` :
  61. - `MiniGrid-LavaCrossingS9N1-v0`
  62. - `MiniGrid-LavaCrossingS9N2-v0`
  63. - `MiniGrid-LavaCrossingS9N3-v0`
  64. - `MiniGrid-LavaCrossingS11N5-v0`
  65. - otherwise :
  66. - `MiniGrid-SimpleCrossingS9N1-v0`
  67. - `MiniGrid-SimpleCrossingS9N2-v0`
  68. - `MiniGrid-SimpleCrossingS9N3-v0`
  69. - `MiniGrid-SimpleCrossingS11N5-v0`
  70. """
  71. def __init__(self, size=9, num_crossings=1, obstacle_type=Lava, **kwargs):
  72. self.num_crossings = num_crossings
  73. self.obstacle_type = obstacle_type
  74. if obstacle_type == Lava:
  75. mission_space = MissionSpace(
  76. mission_func=lambda: "avoid the lava and get to the green goal square"
  77. )
  78. else:
  79. mission_space = MissionSpace(
  80. mission_func=lambda: "find the opening and get to the green goal square"
  81. )
  82. super().__init__(
  83. mission_space=mission_space,
  84. grid_size=size,
  85. max_steps=4 * size * size,
  86. # Set this to True for maximum speed
  87. see_through_walls=False,
  88. **kwargs
  89. )
  90. def _gen_grid(self, width, height):
  91. assert width % 2 == 1 and height % 2 == 1 # odd size
  92. # Create an empty grid
  93. self.grid = Grid(width, height)
  94. # Generate the surrounding walls
  95. self.grid.wall_rect(0, 0, width, height)
  96. # Place the agent in the top-left corner
  97. self.agent_pos = np.array((1, 1))
  98. self.agent_dir = 0
  99. # Place a goal square in the bottom-right corner
  100. self.put_obj(Goal(), width - 2, height - 2)
  101. # Place obstacles (lava or walls)
  102. v, h = object(), object() # singleton `vertical` and `horizontal` objects
  103. # Lava rivers or walls specified by direction and position in grid
  104. rivers = [(v, i) for i in range(2, height - 2, 2)]
  105. rivers += [(h, j) for j in range(2, width - 2, 2)]
  106. self.np_random.shuffle(rivers)
  107. rivers = rivers[: self.num_crossings] # sample random rivers
  108. rivers_v = sorted(pos for direction, pos in rivers if direction is v)
  109. rivers_h = sorted(pos for direction, pos in rivers if direction is h)
  110. obstacle_pos = itt.chain(
  111. itt.product(range(1, width - 1), rivers_h),
  112. itt.product(rivers_v, range(1, height - 1)),
  113. )
  114. for i, j in obstacle_pos:
  115. self.put_obj(self.obstacle_type(), i, j)
  116. # Sample path to goal
  117. path = [h] * len(rivers_v) + [v] * len(rivers_h)
  118. self.np_random.shuffle(path)
  119. # Create openings
  120. limits_v = [0] + rivers_v + [height - 1]
  121. limits_h = [0] + rivers_h + [width - 1]
  122. room_i, room_j = 0, 0
  123. for direction in path:
  124. if direction is h:
  125. i = limits_v[room_i + 1]
  126. j = self.np_random.choice(
  127. range(limits_h[room_j] + 1, limits_h[room_j + 1])
  128. )
  129. room_i += 1
  130. elif direction is v:
  131. i = self.np_random.choice(
  132. range(limits_v[room_i] + 1, limits_v[room_i + 1])
  133. )
  134. j = limits_h[room_j + 1]
  135. room_j += 1
  136. else:
  137. assert False
  138. self.grid.set(i, j, None)
  139. self.mission = (
  140. "avoid the lava and get to the green goal square"
  141. if self.obstacle_type == Lava
  142. else "find the opening and get to the green goal square"
  143. )