gotodoor.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. from gym_minigrid.minigrid import COLOR_NAMES, Door, Grid, MiniGridEnv
  2. from gym_minigrid.register import register
  3. class GoToDoorEnv(MiniGridEnv):
  4. """
  5. Environment in which the agent is instructed to go to a given object
  6. named using an English text string
  7. """
  8. def __init__(self, size=5):
  9. assert size >= 5
  10. super().__init__(
  11. grid_size=size,
  12. max_steps=5 * size**2,
  13. # Set this to True for maximum speed
  14. see_through_walls=True,
  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
  62. class GoToDoor8x8Env(GoToDoorEnv):
  63. def __init__(self):
  64. super().__init__(size=8)
  65. class GoToDoor6x6Env(GoToDoorEnv):
  66. def __init__(self):
  67. super().__init__(size=6)
  68. register(id="MiniGrid-GoToDoor-5x5-v0", entry_point="gym_minigrid.envs:GoToDoorEnv")
  69. register(id="MiniGrid-GoToDoor-6x6-v0", entry_point="gym_minigrid.envs:GoToDoor6x6Env")
  70. register(id="MiniGrid-GoToDoor-8x8-v0", entry_point="gym_minigrid.envs:GoToDoor8x8Env")