gotoobject.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. from gym_minigrid.minigrid import *
  2. from gym_minigrid.register import register
  3. class GoToObjectEnv(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=6,
  11. numObjs=2,
  12. waitEnds=True
  13. ):
  14. self.numObjs = numObjs
  15. super().__init__(gridSize=size, maxSteps=5*size)
  16. self.reward_range = (-1, 1)
  17. self.waitEnds = waitEnds
  18. def _genGrid(self, width, height):
  19. assert width == height
  20. gridSz = width
  21. # Create a grid surrounded by walls
  22. grid = Grid(width, height)
  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. # Types and colors of objects we can generate
  30. types = ['key', 'ball', 'box']
  31. colors = list(COLORS.keys())
  32. objs = []
  33. objPos = []
  34. # Until we have generated all the objects
  35. while len(objs) < self.numObjs:
  36. objType = self._randElem(types)
  37. objColor = self._randElem(colors)
  38. # If this object already exists, try again
  39. if (objType, objColor) in objs:
  40. continue
  41. if objType == 'key':
  42. obj = Key(objColor)
  43. elif objType == 'ball':
  44. obj = Ball(objColor)
  45. elif objType == 'box':
  46. obj = Box(objColor)
  47. while True:
  48. pos = (
  49. self._randInt(1, gridSz - 1),
  50. self._randInt(1, gridSz - 1)
  51. )
  52. if grid.get(*pos) != None:
  53. continue
  54. if pos == self.startPos:
  55. continue
  56. grid.set(*pos, obj)
  57. break
  58. objs.append((objType, objColor))
  59. objPos.append(pos)
  60. # Choose a random object to be picked up
  61. objIdx = self._randInt(0, len(objs))
  62. self.targetType, self.targetColor = objs[objIdx]
  63. self.targetPos = objPos[objIdx]
  64. descStr = '%s %s' % (self.targetColor, self.targetType)
  65. self.mission = 'go to the %s' % descStr
  66. #print(self.mission)
  67. return grid
  68. def _observation(self, obs):
  69. """
  70. Encode observations
  71. """
  72. obs = {
  73. 'image': obs,
  74. 'mission': self.mission
  75. }
  76. return obs
  77. def _reset(self):
  78. obs = MiniGridEnv._reset(self)
  79. return self._observation(obs)
  80. def _step(self, action):
  81. obs, reward, done, info = MiniGridEnv._step(self, action)
  82. ax, ay = self.agentPos
  83. tx, ty = self.targetPos
  84. # Toggle/pickup action terminates the episode
  85. if action == self.actions.toggle:
  86. done = True
  87. # Reward performing the wait action next to the target object
  88. if action == self.actions.wait:
  89. if abs(ax - tx) <= 1 and abs(ay - ty) <= 1:
  90. reward = 1
  91. done = self.waitEnds
  92. obs = self._observation(obs)
  93. return obs, reward, done, info
  94. class GotoEnv8x8N2(GoToObjectEnv):
  95. def __init__(self):
  96. super().__init__(size=8, numObjs=2)
  97. register(
  98. id='MiniGrid-GoToObject-6x6-N2-v0',
  99. entry_point='gym_minigrid.envs:GoToObjectEnv'
  100. )
  101. register(
  102. id='MiniGrid-GoToObject-8x8-N2-v0',
  103. entry_point='gym_minigrid.envs:GotoEnv8x8N2'
  104. )