memory.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import numpy as np
  2. from gym_minigrid.minigrid import Ball, Grid, Key, MiniGridEnv, Wall
  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 = np.array((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