lavagap.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import numpy as np
  2. from gym_minigrid.minigrid import Goal, Grid, Lava, MiniGridEnv, MissionSpace
  3. class LavaGapEnv(MiniGridEnv):
  4. """
  5. Environment with one wall of lava with a small gap to cross through
  6. This environment is similar to LavaCrossing but simpler in structure.
  7. """
  8. def __init__(self, size, obstacle_type=Lava, **kwargs):
  9. self.obstacle_type = obstacle_type
  10. self.size = size
  11. if obstacle_type == Lava:
  12. mission_space = MissionSpace(
  13. mission_func=lambda: "avoid the lava and get to the green goal square"
  14. )
  15. else:
  16. mission_space = MissionSpace(
  17. mission_func=lambda: "find the opening and get to the green goal square"
  18. )
  19. super().__init__(
  20. mission_space=mission_space,
  21. width=size,
  22. height=size,
  23. max_steps=4 * size * size,
  24. # Set this to True for maximum speed
  25. see_through_walls=False,
  26. )
  27. def _gen_grid(self, width, height):
  28. assert width >= 5 and height >= 5
  29. # Create an empty grid
  30. self.grid = Grid(width, height)
  31. # Generate the surrounding walls
  32. self.grid.wall_rect(0, 0, width, height)
  33. # Place the agent in the top-left corner
  34. self.agent_pos = np.array((1, 1))
  35. self.agent_dir = 0
  36. # Place a goal square in the bottom-right corner
  37. self.goal_pos = np.array((width - 2, height - 2))
  38. self.put_obj(Goal(), *self.goal_pos)
  39. # Generate and store random gap position
  40. self.gap_pos = np.array(
  41. (
  42. self._rand_int(2, width - 2),
  43. self._rand_int(1, height - 1),
  44. )
  45. )
  46. # Place the obstacle wall
  47. self.grid.vert_wall(self.gap_pos[0], 1, height - 2, self.obstacle_type)
  48. # Put a hole in the wall
  49. self.grid.set(*self.gap_pos, None)
  50. self.mission = (
  51. "avoid the lava and get to the green goal square"
  52. if self.obstacle_type == Lava
  53. else "find the opening and get to the green goal square"
  54. )