gotodoor.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. from gym_minigrid.minigrid import COLOR_NAMES, Door, Grid, MiniGridEnv, MissionSpace
  2. class GoToDoorEnv(MiniGridEnv):
  3. """
  4. Environment in which the agent is instructed to go to a given object
  5. named using an English text string
  6. """
  7. def __init__(self, size=5, **kwargs):
  8. assert size >= 5
  9. self.size = size
  10. mission_space = MissionSpace(
  11. mission_func=lambda color: f"go to the {color} door",
  12. ordered_placeholders=[COLOR_NAMES],
  13. )
  14. super().__init__(
  15. mission_space=mission_space,
  16. width=size,
  17. height=size,
  18. max_steps=5 * size**2,
  19. # Set this to True for maximum speed
  20. see_through_walls=True,
  21. **kwargs,
  22. )
  23. def _gen_grid(self, width, height):
  24. # Create the grid
  25. self.grid = Grid(width, height)
  26. # Randomly vary the room width and height
  27. width = self._rand_int(5, width + 1)
  28. height = self._rand_int(5, height + 1)
  29. # Generate the surrounding walls
  30. self.grid.wall_rect(0, 0, width, height)
  31. # Generate the 4 doors at random positions
  32. doorPos = []
  33. doorPos.append((self._rand_int(2, width - 2), 0))
  34. doorPos.append((self._rand_int(2, width - 2), height - 1))
  35. doorPos.append((0, self._rand_int(2, height - 2)))
  36. doorPos.append((width - 1, self._rand_int(2, height - 2)))
  37. # Generate the door colors
  38. doorColors = []
  39. while len(doorColors) < len(doorPos):
  40. color = self._rand_elem(COLOR_NAMES)
  41. if color in doorColors:
  42. continue
  43. doorColors.append(color)
  44. # Place the doors in the grid
  45. for idx, pos in enumerate(doorPos):
  46. color = doorColors[idx]
  47. self.grid.set(*pos, Door(color))
  48. # Randomize the agent start position and orientation
  49. self.place_agent(size=(width, height))
  50. # Select a random target door
  51. doorIdx = self._rand_int(0, len(doorPos))
  52. self.target_pos = doorPos[doorIdx]
  53. self.target_color = doorColors[doorIdx]
  54. # Generate the mission string
  55. self.mission = "go to the %s door" % self.target_color
  56. def step(self, action):
  57. obs, reward, terminated, truncated, info = super().step(action)
  58. ax, ay = self.agent_pos
  59. tx, ty = self.target_pos
  60. # Don't let the agent open any of the doors
  61. if action == self.actions.toggle:
  62. terminated = True
  63. # Reward performing done action in front of the target door
  64. if action == self.actions.done:
  65. if (ax == tx and abs(ay - ty) == 1) or (ay == ty and abs(ax - tx) == 1):
  66. reward = self._reward()
  67. terminated = True
  68. return obs, reward, terminated, truncated, info