gotodoor.py 3.4 KB

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