lavagap.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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):
  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. register(
  44. id='MiniGrid-LavaGapS5-v0',
  45. entry_point='gym_minigrid.envs.lavagap:LavaGapEnv',
  46. size=5
  47. )
  48. register(
  49. id='MiniGrid-LavaGapS6-v0',
  50. entry_point='gym_minigrid.envs.lavagap:LavaGapEnv',
  51. size=6
  52. )
  53. register(
  54. id='MiniGrid-LavaGapS7-v0',
  55. entry_point='gym_minigrid.envs.lavagap:LavaGapEnv',
  56. size=7
  57. )