gotodoor.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. from gym import spaces
  2. from gym_minigrid.minigrid import *
  3. from gym_minigrid.register import register
  4. class GoToDoorEnv(MiniGridEnv):
  5. """
  6. Environment in which the agent is instructed to go to a given object
  7. named using an English text string
  8. """
  9. def __init__(
  10. self,
  11. size=5
  12. ):
  13. assert size >= 5
  14. super().__init__(gridSize=size, maxSteps=10*size)
  15. self.reward_range = (0, 1)
  16. def _genGrid(self, width, height):
  17. # Create the grid
  18. grid = Grid(width, height)
  19. # Randomly vary the room width and height
  20. width = self._randInt(5, width+1)
  21. height = self._randInt(5, height+1)
  22. # Generate the surrounding walls
  23. for i in range(0, width):
  24. grid.set(i, 0, Wall())
  25. grid.set(i, height-1, Wall())
  26. for j in range(0, height):
  27. grid.set(0, j, Wall())
  28. grid.set(width-1, j, Wall())
  29. # Randomize the player start position and orientation
  30. self.startPos = self._randPos(
  31. 1, width-1,
  32. 1, height-1
  33. )
  34. self.startDir = self._randInt(0, 4)
  35. # Generate the 4 doors at random positions
  36. doorPos = []
  37. doorPos.append((self._randInt(2, width-2), 0))
  38. doorPos.append((self._randInt(2, width-2), height-1))
  39. doorPos.append((0, self._randInt(2, height-2)))
  40. doorPos.append((width-1, self._randInt(2, height-2)))
  41. # Generate the door colors
  42. doorColors = []
  43. while len(doorColors) < len(doorPos):
  44. color = self._randElem(COLOR_NAMES)
  45. if color in doorColors:
  46. continue
  47. doorColors.append(color)
  48. # Place the doors in the grid
  49. for idx, pos in enumerate(doorPos):
  50. color = doorColors[idx]
  51. grid.set(*pos, Door(color))
  52. # Select a random target door
  53. doorIdx = self._randInt(0, len(doorPos))
  54. self.targetPos = doorPos[doorIdx]
  55. self.targetColor = doorColors[doorIdx]
  56. # Generate the mission string
  57. self.mission = 'go to the %s door' % self.targetColor
  58. #print(self.mission)
  59. return grid
  60. def step(self, action):
  61. obs, reward, done, info = MiniGridEnv.step(self, action)
  62. ax, ay = self.agentPos
  63. tx, ty = self.targetPos
  64. # Don't let the agent open any of the doors
  65. if action == self.actions.toggle:
  66. done = True
  67. # Reward waiting in front of the target door
  68. if action == self.actions.wait:
  69. if (ax == tx and abs(ay - ty) == 1) or (ay == ty and abs(ax - tx) == 1):
  70. reward = 1
  71. done = True
  72. return obs, reward, done, info
  73. class GoToDoor8x8Env(GoToDoorEnv):
  74. def __init__(self):
  75. super().__init__(size=8)
  76. class GoToDoor6x6Env(GoToDoorEnv):
  77. def __init__(self):
  78. super().__init__(size=6)
  79. register(
  80. id='MiniGrid-GoToDoor-5x5-v0',
  81. entry_point='gym_minigrid.envs:GoToDoorEnv'
  82. )
  83. register(
  84. id='MiniGrid-GoToDoor-6x6-v0',
  85. entry_point='gym_minigrid.envs:GoToDoor6x6Env'
  86. )
  87. register(
  88. id='MiniGrid-GoToDoor-8x8-v0',
  89. entry_point='gym_minigrid.envs:GoToDoor8x8Env'
  90. )