gotodoor.py 3.6 KB

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