lavagap.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. from typing import Optional
  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 LavaGapEnv(MiniGridEnv):
  8. """
  9. <p>
  10. <img src="https://raw.githubusercontent.com/Farama-Foundation/Minigrid/master/figures/LavaGapS6.png" alt="LavaGapS6" width="200px"/>
  11. </p>
  12. ### Description
  13. The agent has to reach the green goal square at the opposite corner of the
  14. room, and must pass through a narrow gap in a vertical strip of deadly lava.
  15. Touching the lava terminate the episode with a zero reward. This environment
  16. is useful for studying safety and safe exploration.
  17. ### Mission Space
  18. Depending on the `obstacle_type` parameter:
  19. - `Lava`: "avoid the lava and get to the green goal square"
  20. - otherwise: "find the opening and get to the green goal square"
  21. ### Action Space
  22. | Num | Name | Action |
  23. |-----|--------------|--------------|
  24. | 0 | left | Turn left |
  25. | 1 | right | Turn right |
  26. | 2 | forward | Move forward |
  27. | 3 | pickup | Unused |
  28. | 4 | drop | Unused |
  29. | 5 | toggle | Unused |
  30. | 6 | done | Unused |
  31. ### Observation Encoding
  32. - Each tile is encoded as a 3 dimensional tuple:
  33. `(OBJECT_IDX, COLOR_IDX, STATE)`
  34. - `OBJECT_TO_IDX` and `COLOR_TO_IDX` mapping can be found in
  35. [minigrid/minigrid.py](minigrid/minigrid.py)
  36. - `STATE` refers to the door state with 0=open, 1=closed and 2=locked
  37. ### Rewards
  38. A reward of '1' is given for success, and '0' for failure.
  39. ### Termination
  40. The episode ends if any one of the following conditions is met:
  41. 1. The agent reaches the goal.
  42. 2. The agent falls into lava.
  43. 3. Timeout (see `max_steps`).
  44. ### Registered Configurations
  45. S: size of map SxS.
  46. - `MiniGrid-LavaGapS5-v0`
  47. - `MiniGrid-LavaGapS6-v0`
  48. - `MiniGrid-LavaGapS7-v0`
  49. """
  50. def __init__(
  51. self, size, obstacle_type=Lava, max_steps: Optional[int] = None, **kwargs
  52. ):
  53. self.obstacle_type = obstacle_type
  54. self.size = size
  55. if obstacle_type == Lava:
  56. mission_space = MissionSpace(mission_func=self._gen_mission_lava)
  57. else:
  58. mission_space = MissionSpace(mission_func=self._gen_mission)
  59. if max_steps is None:
  60. max_steps = 4 * size**2
  61. super().__init__(
  62. mission_space=mission_space,
  63. width=size,
  64. height=size,
  65. # Set this to True for maximum speed
  66. see_through_walls=False,
  67. max_steps=max_steps,
  68. **kwargs
  69. )
  70. @staticmethod
  71. def _gen_mission_lava():
  72. return "avoid the lava and get to the green goal square"
  73. @staticmethod
  74. def _gen_mission():
  75. return "find the opening and get to the green goal square"
  76. def _gen_grid(self, width, height):
  77. assert width >= 5 and height >= 5
  78. # Create an empty grid
  79. self.grid = Grid(width, height)
  80. # Generate the surrounding walls
  81. self.grid.wall_rect(0, 0, width, height)
  82. # Place the agent in the top-left corner
  83. self.agent_pos = np.array((1, 1))
  84. self.agent_dir = 0
  85. # Place a goal square in the bottom-right corner
  86. self.goal_pos = np.array((width - 2, height - 2))
  87. self.put_obj(Goal(), *self.goal_pos)
  88. # Generate and store random gap position
  89. self.gap_pos = np.array(
  90. (
  91. self._rand_int(2, width - 2),
  92. self._rand_int(1, height - 1),
  93. )
  94. )
  95. # Place the obstacle wall
  96. self.grid.vert_wall(self.gap_pos[0], 1, height - 2, self.obstacle_type)
  97. # Put a hole in the wall
  98. self.grid.set(*self.gap_pos, None)
  99. self.mission = (
  100. "avoid the lava and get to the green goal square"
  101. if self.obstacle_type == Lava
  102. else "find the opening and get to the green goal square"
  103. )