lavagap.py 3.8 KB

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