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__(gridSize=size, maxSteps=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.horzWall(0, 0, width)
  23. self.grid.horzWall(0, height-1, width)
  24. self.grid.vertWall(0, 0, height)
  25. self.grid.vertWall(width-1, 0, height)
  26. # Generate the 4 doors at random positions
  27. doorPos = []
  28. doorPos.append((self._randInt(2, width-2), 0))
  29. doorPos.append((self._randInt(2, width-2), height-1))
  30. doorPos.append((0, self._randInt(2, height-2)))
  31. doorPos.append((width-1, self._randInt(2, height-2)))
  32. # Generate the door colors
  33. doorColors = []
  34. while len(doorColors) < len(doorPos):
  35. color = self._randElem(COLOR_NAMES)
  36. if color in doorColors:
  37. continue
  38. doorColors.append(color)
  39. # Place the doors in the grid
  40. for idx, pos in enumerate(doorPos):
  41. color = doorColors[idx]
  42. self.grid.set(*pos, Door(color))
  43. # Select a random target door
  44. doorIdx = self._randInt(0, len(doorPos))
  45. self.targetPos = doorPos[doorIdx]
  46. self.targetColor = doorColors[doorIdx]
  47. # Generate the mission string
  48. self.mission = 'go to the %s door' % self.targetColor
  49. def step(self, action):
  50. obs, reward, done, info = MiniGridEnv.step(self, action)
  51. ax, ay = self.agentPos
  52. tx, ty = self.targetPos
  53. # Don't let the agent open any of the doors
  54. if action == self.actions.toggle:
  55. done = True
  56. # Reward waiting in front of the target door
  57. if action == self.actions.wait:
  58. if (ax == tx and abs(ay - ty) == 1) or (ay == ty and abs(ax - tx) == 1):
  59. reward = 1
  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(
  69. id='MiniGrid-GoToDoor-5x5-v0',
  70. entry_point='gym_minigrid.envs:GoToDoorEnv'
  71. )
  72. register(
  73. id='MiniGrid-GoToDoor-6x6-v0',
  74. entry_point='gym_minigrid.envs:GoToDoor6x6Env'
  75. )
  76. register(
  77. id='MiniGrid-GoToDoor-8x8-v0',
  78. entry_point='gym_minigrid.envs:GoToDoor8x8Env'
  79. )