memory.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. from gym_minigrid.minigrid import Ball, Grid, Key, MiniGridEnv, Wall
  2. from gym_minigrid.register import register
  3. class MemoryEnv(MiniGridEnv):
  4. """
  5. This environment is a memory test. The agent starts in a small room
  6. where it sees an object. It then has to go through a narrow hallway
  7. which ends in a split. At each end of the split there is an object,
  8. one of which is the same as the object in the starting room. The
  9. agent has to remember the initial object, and go to the matching
  10. object at split.
  11. """
  12. def __init__(self, size=8, random_length=False, **kwargs):
  13. self.random_length = random_length
  14. super().__init__(
  15. grid_size=size,
  16. max_steps=5 * size**2,
  17. # Set this to True for maximum speed
  18. see_through_walls=False,
  19. **kwargs
  20. )
  21. def _gen_grid(self, width, height):
  22. self.grid = Grid(width, height)
  23. # Generate the surrounding walls
  24. self.grid.horz_wall(0, 0)
  25. self.grid.horz_wall(0, height - 1)
  26. self.grid.vert_wall(0, 0)
  27. self.grid.vert_wall(width - 1, 0)
  28. assert height % 2 == 1
  29. upper_room_wall = height // 2 - 2
  30. lower_room_wall = height // 2 + 2
  31. if self.random_length:
  32. hallway_end = self._rand_int(4, width - 2)
  33. else:
  34. hallway_end = width - 3
  35. # Start room
  36. for i in range(1, 5):
  37. self.grid.set(i, upper_room_wall, Wall())
  38. self.grid.set(i, lower_room_wall, Wall())
  39. self.grid.set(4, upper_room_wall + 1, Wall())
  40. self.grid.set(4, lower_room_wall - 1, Wall())
  41. # Horizontal hallway
  42. for i in range(5, hallway_end):
  43. self.grid.set(i, upper_room_wall + 1, Wall())
  44. self.grid.set(i, lower_room_wall - 1, Wall())
  45. # Vertical hallway
  46. for j in range(0, height):
  47. if j != height // 2:
  48. self.grid.set(hallway_end, j, Wall())
  49. self.grid.set(hallway_end + 2, j, Wall())
  50. # Fix the player's start position and orientation
  51. self.agent_pos = (self._rand_int(1, hallway_end + 1), height // 2)
  52. self.agent_dir = 0
  53. # Place objects
  54. start_room_obj = self._rand_elem([Key, Ball])
  55. self.grid.set(1, height // 2 - 1, start_room_obj("green"))
  56. other_objs = self._rand_elem([[Ball, Key], [Key, Ball]])
  57. pos0 = (hallway_end + 1, height // 2 - 2)
  58. pos1 = (hallway_end + 1, height // 2 + 2)
  59. self.grid.set(*pos0, other_objs[0]("green"))
  60. self.grid.set(*pos1, other_objs[1]("green"))
  61. # Choose the target objects
  62. if start_room_obj == other_objs[0]:
  63. self.success_pos = (pos0[0], pos0[1] + 1)
  64. self.failure_pos = (pos1[0], pos1[1] - 1)
  65. else:
  66. self.success_pos = (pos1[0], pos1[1] - 1)
  67. self.failure_pos = (pos0[0], pos0[1] + 1)
  68. self.mission = "go to the matching object at the end of the hallway"
  69. def step(self, action):
  70. if action == MiniGridEnv.Actions.pickup:
  71. action = MiniGridEnv.Actions.toggle
  72. obs, reward, done, info = MiniGridEnv.step(self, action)
  73. if tuple(self.agent_pos) == self.success_pos:
  74. reward = self._reward()
  75. done = True
  76. if tuple(self.agent_pos) == self.failure_pos:
  77. reward = 0
  78. done = True
  79. return obs, reward, done, info
  80. class MemoryS17Random(MemoryEnv):
  81. def __init__(self, **kwargs):
  82. super().__init__(size=17, random_length=True, **kwargs)
  83. register(
  84. id="MiniGrid-MemoryS17Random-v0",
  85. entry_point="gym_minigrid.envs:MemoryS17Random",
  86. )
  87. class MemoryS13Random(MemoryEnv):
  88. def __init__(self, **kwargs):
  89. super().__init__(size=13, random_length=True, **kwargs)
  90. register(
  91. id="MiniGrid-MemoryS13Random-v0",
  92. entry_point="gym_minigrid.envs:MemoryS13Random",
  93. )
  94. class MemoryS13(MemoryEnv):
  95. def __init__(self, **kwargs):
  96. super().__init__(size=13, **kwargs)
  97. register(
  98. id="MiniGrid-MemoryS13-v0",
  99. entry_point="gym_minigrid.envs:MemoryS13",
  100. )
  101. class MemoryS11(MemoryEnv):
  102. def __init__(self, **kwargs):
  103. super().__init__(size=11, **kwargs)
  104. register(
  105. id="MiniGrid-MemoryS11-v0",
  106. entry_point="gym_minigrid.envs:MemoryS11",
  107. )
  108. class MemoryS9(MemoryEnv):
  109. def __init__(self, **kwargs):
  110. super().__init__(size=9, **kwargs)
  111. register(
  112. id="MiniGrid-MemoryS9-v0",
  113. entry_point="gym_minigrid.envs:MemoryS9",
  114. )
  115. class MemoryS7(MemoryEnv):
  116. def __init__(self, **kwargs):
  117. super().__init__(size=7, **kwargs)
  118. register(
  119. id="MiniGrid-MemoryS7-v0",
  120. entry_point="gym_minigrid.envs:MemoryS7",
  121. )