putnear.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 = (-1, 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 _observation(self, obs):
  82. """
  83. Encode observations
  84. """
  85. obs = {
  86. 'image': obs,
  87. 'mission': self.mission
  88. }
  89. return obs
  90. def _reset(self):
  91. obs = MiniGridEnv._reset(self)
  92. return self._observation(obs)
  93. def _step(self, action):
  94. preCarrying = self.carrying
  95. obs, reward, done, info = MiniGridEnv._step(self, action)
  96. u, v = self.getDirVec()
  97. ox, oy = (self.agentPos[0] + u, self.agentPos[1] + v)
  98. tx, ty = self.targetPos
  99. # Pickup/drop action
  100. if action == self.actions.toggle:
  101. # If we picked up the wrong object, terminate the episode
  102. if self.carrying:
  103. if self.carrying.type != self.moveType or self.carrying.color != self.moveColor:
  104. done = True
  105. # If successfully dropping an object near the target
  106. if preCarrying:
  107. if self.grid.get(ox, oy) is preCarrying:
  108. if abs(ox - tx) <= 1 and abs(oy - ty) <= 1:
  109. reward = 1
  110. done = True
  111. obs = self._observation(obs)
  112. return obs, reward, done, info
  113. class PutNear8x8N3(PutNearEnv):
  114. def __init__(self):
  115. super().__init__(size=8, numObjs=3)
  116. register(
  117. id='MiniGrid-PutNear-6x6-N2-v0',
  118. entry_point='gym_minigrid.envs:PutNearEnv'
  119. )
  120. register(
  121. id='MiniGrid-PutNear-8x8-N3-v0',
  122. entry_point='gym_minigrid.envs:PutNear8x8N3'
  123. )