lavagap.py 3.7 KB

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