The source code and dockerfile for the GSW2024 AI Lab.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

139 lines
4.0 KiB

3 months ago
  1. from __future__ import annotations
  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. ## 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 - 0.9 * (step_count / max_steps)' 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__(
  48. self, size, obstacle_type=Lava, max_steps: int | None = None, **kwargs
  49. ):
  50. self.obstacle_type = obstacle_type
  51. self.size = size
  52. if obstacle_type == Lava:
  53. mission_space = MissionSpace(mission_func=self._gen_mission_lava)
  54. else:
  55. mission_space = MissionSpace(mission_func=self._gen_mission)
  56. if max_steps is None:
  57. max_steps = 4 * size**2
  58. super().__init__(
  59. mission_space=mission_space,
  60. width=size,
  61. height=size,
  62. # Set this to True for maximum speed
  63. see_through_walls=False,
  64. max_steps=max_steps,
  65. **kwargs,
  66. )
  67. @staticmethod
  68. def _gen_mission_lava():
  69. return "avoid the lava and get to the green goal square"
  70. @staticmethod
  71. def _gen_mission():
  72. return "find the opening and get to the green goal square"
  73. def _gen_grid(self, width, height):
  74. assert width >= 5 and height >= 5
  75. # Create an empty grid
  76. self.grid = Grid(width, height)
  77. # Generate the surrounding walls
  78. self.grid.wall_rect(0, 0, width, height)
  79. # Place the agent in the top-left corner
  80. self.agent_pos = np.array((1, 1))
  81. self.agent_dir = 0
  82. # Place a goal square in the bottom-right corner
  83. self.goal_pos = np.array((width - 2, height - 2))
  84. self.put_obj(Goal(), *self.goal_pos)
  85. # Generate and store random gap position
  86. self.gap_pos = np.array(
  87. (
  88. #self._rand_int(2, width - 2),
  89. #self._rand_int(1, height - 1),
  90. self._rand_int(2,3),
  91. self._rand_int(2,3),
  92. )
  93. )
  94. # Place the obstacle wall
  95. self.grid.vert_wall(self.gap_pos[0], 1, height - 2, self.obstacle_type)
  96. # Put a hole in the wall
  97. self.grid.set(*self.gap_pos, None)
  98. self.mission = (
  99. "avoid the lava and get to the green goal square"
  100. if self.obstacle_type == Lava
  101. else "find the opening and get to the green goal square"
  102. )