gotoobject.py 3.5 KB

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