putnear.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. from gym_minigrid.minigrid import *
  2. from gym_minigrid.register import register
  3. class PutNearEnv(MiniGridEnv):
  4. """
  5. Environment in which the agent is instructed to place an object near
  6. another object through a natural language 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.reward_range = (0, 1)
  16. def _genGrid(self, width, height):
  17. self.grid = Grid(width, height)
  18. # Generate the surrounding walls
  19. self.grid.horzWall(0, 0)
  20. self.grid.horzWall(0, height-1)
  21. self.grid.vertWall(0, 0)
  22. self.grid.vertWall(width-1, 0)
  23. # Types and colors of objects we can generate
  24. types = ['key', 'ball']
  25. colors = list(COLORS.keys())
  26. objs = []
  27. objPos = []
  28. def nearObj(p1):
  29. for p2 in objPos:
  30. dx = p1[0] - p2[0]
  31. dy = p1[1] - p2[1]
  32. if abs(dx) <= 1 and abs(dy) <= 1:
  33. return True
  34. return False
  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, width - 1),
  51. self._randInt(1, height - 1)
  52. )
  53. if nearObj(pos):
  54. continue
  55. if pos == self.startPos:
  56. continue
  57. self.grid.set(*pos, obj)
  58. break
  59. objs.append((objType, objColor))
  60. objPos.append(pos)
  61. # Choose a random object to be moved
  62. objIdx = self._randInt(0, len(objs))
  63. self.moveType, self.moveColor = objs[objIdx]
  64. self.movePos = objPos[objIdx]
  65. # Choose a target object (to put the first object next to)
  66. while True:
  67. targetIdx = self._randInt(0, len(objs))
  68. if targetIdx != objIdx:
  69. break
  70. self.targetType, self.targetColor = objs[targetIdx]
  71. self.targetPos = objPos[targetIdx]
  72. self.mission = 'put the %s %s near the %s %s' % (
  73. self.moveColor,
  74. self.moveType,
  75. self.targetColor,
  76. self.targetType
  77. )
  78. def step(self, action):
  79. preCarrying = self.carrying
  80. obs, reward, done, info = MiniGridEnv.step(self, action)
  81. u, v = self.getDirVec()
  82. ox, oy = (self.agentPos[0] + u, self.agentPos[1] + v)
  83. tx, ty = self.targetPos
  84. # Pickup/drop action
  85. if action == self.actions.toggle:
  86. # If we picked up the wrong object, terminate the episode
  87. if self.carrying:
  88. if self.carrying.type != self.moveType or self.carrying.color != self.moveColor:
  89. done = True
  90. # If successfully dropping an object near the target
  91. if preCarrying:
  92. if self.grid.get(ox, oy) is preCarrying:
  93. if abs(ox - tx) <= 1 and abs(oy - ty) <= 1:
  94. reward = 1
  95. done = True
  96. return obs, reward, done, info
  97. class PutNear8x8N3(PutNearEnv):
  98. def __init__(self):
  99. super().__init__(size=8, numObjs=3)
  100. register(
  101. id='MiniGrid-PutNear-6x6-N2-v0',
  102. entry_point='gym_minigrid.envs:PutNearEnv'
  103. )
  104. register(
  105. id='MiniGrid-PutNear-8x8-N3-v0',
  106. entry_point='gym_minigrid.envs:PutNear8x8N3'
  107. )