crossing.py 5.8 KB

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