lavagap.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. **kwargs
  27. )
  28. def _gen_grid(self, width, height):
  29. assert width >= 5 and height >= 5
  30. # Create an empty grid
  31. self.grid = Grid(width, height)
  32. # Generate the surrounding walls
  33. self.grid.wall_rect(0, 0, width, height)
  34. # Place the agent in the top-left corner
  35. self.agent_pos = np.array((1, 1))
  36. self.agent_dir = 0
  37. # Place a goal square in the bottom-right corner
  38. self.goal_pos = np.array((width - 2, height - 2))
  39. self.put_obj(Goal(), *self.goal_pos)
  40. # Generate and store random gap position
  41. self.gap_pos = np.array(
  42. (
  43. self._rand_int(2, width - 2),
  44. self._rand_int(1, height - 1),
  45. )
  46. )
  47. # Place the obstacle wall
  48. self.grid.vert_wall(self.gap_pos[0], 1, height - 2, self.obstacle_type)
  49. # Put a hole in the wall
  50. self.grid.set(*self.gap_pos, None)
  51. self.mission = (
  52. "avoid the lava and get to the green goal square"
  53. if self.obstacle_type == Lava
  54. else "find the opening and get to the green goal square"
  55. )