crossing.py 6.6 KB

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