doorkey.py 2.1 KB

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