putnear.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. # Create a grid surrounded by walls
  18. grid = Grid(width, height)
  19. for i in range(0, width):
  20. grid.set(i, 0, Wall())
  21. grid.set(i, height-1, Wall())
  22. for j in range(0, height):
  23. grid.set(0, j, Wall())
  24. grid.set(width-1, j, Wall())
  25. # Types and colors of objects we can generate
  26. types = ['key', 'ball']
  27. colors = list(COLORS.keys())
  28. objs = []
  29. objPos = []
  30. def nearObj(p1):
  31. for p2 in objPos:
  32. dx = p1[0] - p2[0]
  33. dy = p1[1] - p2[1]
  34. if abs(dx) <= 1 and abs(dy) <= 1:
  35. return True
  36. return False
  37. # Until we have generated all the objects
  38. while len(objs) < self.numObjs:
  39. objType = self._randElem(types)
  40. objColor = self._randElem(colors)
  41. # If this object already exists, try again
  42. if (objType, objColor) in objs:
  43. continue
  44. if objType == 'key':
  45. obj = Key(objColor)
  46. elif objType == 'ball':
  47. obj = Ball(objColor)
  48. elif objType == 'box':
  49. obj = Box(objColor)
  50. while True:
  51. pos = (
  52. self._randInt(1, width - 1),
  53. self._randInt(1, height - 1)
  54. )
  55. if nearObj(pos):
  56. continue
  57. if pos == self.startPos:
  58. continue
  59. grid.set(*pos, obj)
  60. break
  61. objs.append((objType, objColor))
  62. objPos.append(pos)
  63. # Choose a random object to be moved
  64. objIdx = self._randInt(0, len(objs))
  65. self.moveType, self.moveColor = objs[objIdx]
  66. self.movePos = objPos[objIdx]
  67. # Choose a target object (to put the first object next to)
  68. while True:
  69. targetIdx = self._randInt(0, len(objs))
  70. if targetIdx != objIdx:
  71. break
  72. self.targetType, self.targetColor = objs[targetIdx]
  73. self.targetPos = objPos[targetIdx]
  74. self.mission = 'put the %s %s near the %s %s' % (
  75. self.moveColor,
  76. self.moveType,
  77. self.targetColor,
  78. self.targetType
  79. )
  80. return grid
  81. def step(self, action):
  82. preCarrying = self.carrying
  83. obs, reward, done, info = MiniGridEnv.step(self, action)
  84. u, v = self.getDirVec()
  85. ox, oy = (self.agentPos[0] + u, self.agentPos[1] + v)
  86. tx, ty = self.targetPos
  87. # Pickup/drop action
  88. if action == self.actions.toggle:
  89. # If we picked up the wrong object, terminate the episode
  90. if self.carrying:
  91. if self.carrying.type != self.moveType or self.carrying.color != self.moveColor:
  92. done = True
  93. # If successfully dropping an object near the target
  94. if preCarrying:
  95. if self.grid.get(ox, oy) is preCarrying:
  96. if abs(ox - tx) <= 1 and abs(oy - ty) <= 1:
  97. reward = 1
  98. done = True
  99. return obs, reward, done, info
  100. class PutNear8x8N3(PutNearEnv):
  101. def __init__(self):
  102. super().__init__(size=8, numObjs=3)
  103. register(
  104. id='MiniGrid-PutNear-6x6-N2-v0',
  105. entry_point='gym_minigrid.envs:PutNearEnv'
  106. )
  107. register(
  108. id='MiniGrid-PutNear-8x8-N3-v0',
  109. entry_point='gym_minigrid.envs:PutNear8x8N3'
  110. )