gotodoor.py 2.9 KB

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