doorkey.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. from gym_minigrid.minigrid import Door, Goal, Grid, Key, MiniGridEnv
  2. class DoorKeyEnv(MiniGridEnv):
  3. """
  4. Environment with a door and key, sparse reward
  5. """
  6. def __init__(self, size=8, **kwargs):
  7. if "max_steps" not in kwargs:
  8. kwargs["max_steps"] = 10 * size * size
  9. super().__init__(grid_size=size, **kwargs)
  10. def _gen_grid(self, width, height):
  11. # Create an empty grid
  12. self.grid = Grid(width, height)
  13. # Generate the surrounding walls
  14. self.grid.wall_rect(0, 0, width, height)
  15. # Place a goal in the bottom-right corner
  16. self.put_obj(Goal(), width - 2, height - 2)
  17. # Create a vertical splitting wall
  18. splitIdx = self._rand_int(2, width - 2)
  19. self.grid.vert_wall(splitIdx, 0)
  20. # Place the agent at a random position and orientation
  21. # on the left side of the splitting wall
  22. self.place_agent(size=(splitIdx, height))
  23. # Place a door in the wall
  24. doorIdx = self._rand_int(1, width - 2)
  25. self.put_obj(Door("yellow", is_locked=True), splitIdx, doorIdx)
  26. # Place a yellow key on the left side
  27. self.place_obj(obj=Key("yellow"), top=(0, 0), size=(splitIdx, height))
  28. self.mission = "use the key to open the door and then get to the goal"