gotodoor.py 3.5 KB

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