lavagap.py 2.2 KB

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