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