lavagap.py 3.6 KB

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