doorkey.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. from gym_minigrid.minigrid import *
  2. from gym_minigrid.register import register
  3. class DoorKeyEnv(MiniGridEnv):
  4. """
  5. Environment with a door and key, sparse reward
  6. """
  7. def __init__(self, size=8, **kwargs):
  8. super().__init__(
  9. grid_size=size,
  10. max_steps=10*size*size,
  11. **kwargs
  12. )
  13. def _gen_grid(self, width, height):
  14. # Create an empty grid
  15. self.grid = Grid(width, height)
  16. # Generate the surrounding walls
  17. self.grid.wall_rect(0, 0, width, height)
  18. # Place a goal in the bottom-right corner
  19. self.put_obj(Goal(), width - 2, height - 2)
  20. # Create a vertical splitting wall
  21. splitIdx = self._rand_int(2, width-2)
  22. self.grid.vert_wall(splitIdx, 0)
  23. # Place the agent at a random position and orientation
  24. # on the left side of the splitting wall
  25. self.place_agent(size=(splitIdx, height))
  26. # Place a door in the wall
  27. doorIdx = self._rand_int(1, width-2)
  28. self.put_obj(Door('yellow', is_locked=True), splitIdx, doorIdx)
  29. # Place a yellow key on the left side
  30. self.place_obj(
  31. obj=Key('yellow'),
  32. top=(0, 0),
  33. size=(splitIdx, height)
  34. )
  35. self.mission = "use the key to open the door and then get to the goal"
  36. class DoorKeyEnv5x5(DoorKeyEnv):
  37. def __init__(self, **kwargs):
  38. super().__init__(size=5, **kwargs)
  39. class DoorKeyEnv6x6(DoorKeyEnv):
  40. def __init__(self, **kwargs):
  41. super().__init__(size=6, **kwargs)
  42. class DoorKeyEnv16x16(DoorKeyEnv):
  43. def __init__(self, **kwargs):
  44. super().__init__(size=16, **kwargs)
  45. register(
  46. id='MiniGrid-DoorKey-5x5-v0',
  47. entry_point='gym_minigrid.envs:DoorKeyEnv5x5'
  48. )
  49. register(
  50. id='MiniGrid-DoorKey-6x6-v0',
  51. entry_point='gym_minigrid.envs:DoorKeyEnv6x6'
  52. )
  53. register(
  54. id='MiniGrid-DoorKey-8x8-v0',
  55. entry_point='gym_minigrid.envs:DoorKeyEnv'
  56. )
  57. register(
  58. id='MiniGrid-DoorKey-16x16-v0',
  59. entry_point='gym_minigrid.envs:DoorKeyEnv16x16'
  60. )