lavagap.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. from gym_minigrid.minigrid import *
  2. from gym_minigrid.register import register
  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, seed=None):
  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. seed=None
  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 = (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. self._rand_int(2, width - 2),
  32. self._rand_int(1, height - 1),
  33. ))
  34. # Place the obstacle wall
  35. self.grid.vert_wall(self.gap_pos[0], 1, height - 2, self.obstacle_type)
  36. # Put a hole in the wall
  37. self.grid.set(*self.gap_pos, None)
  38. self.mission = (
  39. "avoid the lava and get to the green goal square"
  40. if self.obstacle_type == Lava
  41. else "find the opening and get to the green goal square"
  42. )
  43. class LavaGapS5Env(LavaGapEnv):
  44. def __init__(self):
  45. super().__init__(size=5)
  46. class LavaGapS6Env(LavaGapEnv):
  47. def __init__(self):
  48. super().__init__(size=6)
  49. class LavaGapS7Env(LavaGapEnv):
  50. def __init__(self):
  51. super().__init__(size=7)
  52. register(
  53. id='MiniGrid-LavaGapS5-v0',
  54. entry_point='gym_minigrid.envs:LavaGapS5Env'
  55. )
  56. register(
  57. id='MiniGrid-LavaGapS6-v0',
  58. entry_point='gym_minigrid.envs:LavaGapS6Env'
  59. )
  60. register(
  61. id='MiniGrid-LavaGapS7-v0',
  62. entry_point='gym_minigrid.envs:LavaGapS7Env'
  63. )