lavagap.py 3.9 KB

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