gotodoor.py 2.5 KB

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