gotodoor.py 2.8 KB

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