gotodoor.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. 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. for i in range(0, width):
  23. grid.set(i, 0, Wall())
  24. grid.set(i, height-1, Wall())
  25. for j in range(0, height):
  26. grid.set(0, j, Wall())
  27. grid.set(width-1, j, Wall())
  28. # Randomize the player start position and orientation
  29. self.startPos = self._randPos(
  30. 1, width-1,
  31. 1, height-1
  32. )
  33. self.startDir = self._randInt(0, 4)
  34. # Generate the 4 doors at random positions
  35. doorPos = []
  36. doorPos.append((self._randInt(2, width-2), 0))
  37. doorPos.append((self._randInt(2, width-2), height-1))
  38. doorPos.append((0, self._randInt(2, height-2)))
  39. doorPos.append((width-1, self._randInt(2, height-2)))
  40. # Generate the door colors
  41. doorColors = []
  42. while len(doorColors) < len(doorPos):
  43. color = self._randElem(COLOR_NAMES)
  44. if color in doorColors:
  45. continue
  46. doorColors.append(color)
  47. # Place the doors in the grid
  48. for idx, pos in enumerate(doorPos):
  49. color = doorColors[idx]
  50. grid.set(*pos, Door(color))
  51. # Select a random target door
  52. doorIdx = self._randInt(0, len(doorPos))
  53. self.targetPos = doorPos[doorIdx]
  54. self.targetColor = doorColors[doorIdx]
  55. # Generate the mission string
  56. self.mission = 'go to the %s door' % self.targetColor
  57. #print(self.mission)
  58. return grid
  59. def step(self, action):
  60. obs, reward, done, info = MiniGridEnv.step(self, action)
  61. ax, ay = self.agentPos
  62. tx, ty = self.targetPos
  63. # Don't let the agent open any of the doors
  64. if action == self.actions.toggle:
  65. done = True
  66. # Reward waiting in front of the target door
  67. if action == self.actions.wait:
  68. if (ax == tx and abs(ay - ty) == 1) or (ay == ty and abs(ax - tx) == 1):
  69. reward = 1
  70. done = True
  71. return obs, reward, done, info
  72. class GoToDoor8x8Env(GoToDoorEnv):
  73. def __init__(self):
  74. super().__init__(size=8)
  75. class GoToDoor6x6Env(GoToDoorEnv):
  76. def __init__(self):
  77. super().__init__(size=6)
  78. register(
  79. id='MiniGrid-GoToDoor-5x5-v0',
  80. entry_point='gym_minigrid.envs:GoToDoorEnv'
  81. )
  82. register(
  83. id='MiniGrid-GoToDoor-6x6-v0',
  84. entry_point='gym_minigrid.envs:GoToDoor6x6Env'
  85. )
  86. register(
  87. id='MiniGrid-GoToDoor-8x8-v0',
  88. entry_point='gym_minigrid.envs:GoToDoor8x8Env'
  89. )