lavagap.py 2.3 KB

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