gotodoor.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. from gym_minigrid.minigrid import *
  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__(
  9. self,
  10. size=5
  11. ):
  12. assert size >= 5
  13. super().__init__(
  14. grid_size=size,
  15. max_steps=5*size,
  16. # Set this to True for maximum speed
  17. see_through_walls=True
  18. )
  19. self.reward_range = (0, 1)
  20. def _gen_grid(self, width, height):
  21. # Create the grid
  22. self.grid = Grid(width, height)
  23. # Randomly vary the room width and height
  24. width = self._rand_int(5, width+1)
  25. height = self._rand_int(5, height+1)
  26. # Generate the surrounding walls
  27. self.grid.wall_rect(0, 0, width, height)
  28. # Generate the 4 doors at random positions
  29. doorPos = []
  30. doorPos.append((self._rand_int(2, width-2), 0))
  31. doorPos.append((self._rand_int(2, width-2), height-1))
  32. doorPos.append((0, self._rand_int(2, height-2)))
  33. doorPos.append((width-1, self._rand_int(2, height-2)))
  34. # Generate the door colors
  35. doorColors = []
  36. while len(doorColors) < len(doorPos):
  37. color = self._rand_elem(COLOR_NAMES)
  38. if color in doorColors:
  39. continue
  40. doorColors.append(color)
  41. # Place the doors in the grid
  42. for idx, pos in enumerate(doorPos):
  43. color = doorColors[idx]
  44. self.grid.set(*pos, Door(color))
  45. # Randomize the agent start position and orientation
  46. self.place_agent(size=(width, height))
  47. # Select a random target door
  48. doorIdx = self._rand_int(0, len(doorPos))
  49. self.target_pos = doorPos[doorIdx]
  50. self.target_color = doorColors[doorIdx]
  51. # Generate the mission string
  52. self.mission = 'go to the %s door' % self.target_color
  53. def step(self, action):
  54. obs, reward, done, info = super().step(action)
  55. ax, ay = self.agent_pos
  56. tx, ty = self.target_pos
  57. # Don't let the agent open any of the doors
  58. if action == self.actions.toggle:
  59. done = True
  60. # Reward waiting in front of the target door
  61. if action == self.actions.wait:
  62. if (ax == tx and abs(ay - ty) == 1) or (ay == ty and abs(ax - tx) == 1):
  63. reward = 1
  64. done = True
  65. return obs, reward, done, info
  66. class GoToDoor8x8Env(GoToDoorEnv):
  67. def __init__(self):
  68. super().__init__(size=8)
  69. class GoToDoor6x6Env(GoToDoorEnv):
  70. def __init__(self):
  71. super().__init__(size=6)
  72. register(
  73. id='MiniGrid-GoToDoor-5x5-v0',
  74. entry_point='gym_minigrid.envs:GoToDoorEnv'
  75. )
  76. register(
  77. id='MiniGrid-GoToDoor-6x6-v0',
  78. entry_point='gym_minigrid.envs:GoToDoor6x6Env'
  79. )
  80. register(
  81. id='MiniGrid-GoToDoor-8x8-v0',
  82. entry_point='gym_minigrid.envs:GoToDoor8x8Env'
  83. )