lavagap.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import numpy as np
  2. from gym_minigrid.minigrid import Goal, Grid, Lava, MiniGridEnv
  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. super().__init__(
  11. grid_size=size,
  12. max_steps=4 * size * size,
  13. # Set this to True for maximum speed
  14. see_through_walls=False,
  15. **kwargs
  16. )
  17. def _gen_grid(self, width, height):
  18. assert width >= 5 and height >= 5
  19. # Create an empty grid
  20. self.grid = Grid(width, height)
  21. # Generate the surrounding walls
  22. self.grid.wall_rect(0, 0, width, height)
  23. # Place the agent in the top-left corner
  24. self.agent_pos = np.array((1, 1))
  25. self.agent_dir = 0
  26. # Place a goal square in the bottom-right corner
  27. self.goal_pos = np.array((width - 2, height - 2))
  28. self.put_obj(Goal(), *self.goal_pos)
  29. # Generate and store random gap position
  30. self.gap_pos = np.array(
  31. (
  32. self._rand_int(2, width - 2),
  33. self._rand_int(1, height - 1),
  34. )
  35. )
  36. # Place the obstacle wall
  37. self.grid.vert_wall(self.gap_pos[0], 1, height - 2, self.obstacle_type)
  38. # Put a hole in the wall
  39. self.grid.set(*self.gap_pos, None)
  40. self.mission = (
  41. "avoid the lava and get to the green goal square"
  42. if self.obstacle_type == Lava
  43. else "find the opening and get to the green goal square"
  44. )