putnear.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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__(grid_size=size, max_steps=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', 'box']
  25. objs = []
  26. objPos = []
  27. def near_obj(env, p1):
  28. for p2 in objPos:
  29. dx = p1[0] - p2[0]
  30. dy = p1[1] - p2[1]
  31. if abs(dx) <= 1 and abs(dy) <= 1:
  32. return True
  33. return False
  34. # Until we have generated all the objects
  35. while len(objs) < self.numObjs:
  36. objType = self._randElem(types)
  37. objColor = self._randElem(COLOR_NAMES)
  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. pos = self.placeObj(obj, reject_fn=near_obj)
  48. objs.append((objType, objColor))
  49. objPos.append(pos)
  50. # Randomize the agent start position and orientation
  51. self.placeAgent()
  52. # Choose a random object to be moved
  53. objIdx = self._randInt(0, len(objs))
  54. self.move_type, self.moveColor = objs[objIdx]
  55. self.move_pos = objPos[objIdx]
  56. # Choose a target object (to put the first object next to)
  57. while True:
  58. targetIdx = self._randInt(0, len(objs))
  59. if targetIdx != objIdx:
  60. break
  61. self.target_type, self.target_color = objs[targetIdx]
  62. self.target_pos = objPos[targetIdx]
  63. self.mission = 'put the %s %s near the %s %s' % (
  64. self.moveColor,
  65. self.move_type,
  66. self.target_color,
  67. self.target_type
  68. )
  69. def step(self, action):
  70. preCarrying = self.carrying
  71. obs, reward, done, info = super().step(action)
  72. u, v = self.getDirVec()
  73. ox, oy = (self.agent_pos[0] + u, self.agent_pos[1] + v)
  74. tx, ty = self.target_pos
  75. # If we picked up the wrong object, terminate the episode
  76. if action == self.actions.pickup and self.carrying:
  77. if self.carrying.type != self.move_type or self.carrying.color != self.moveColor:
  78. done = True
  79. # If successfully dropping an object near the target
  80. if action == self.actions.drop and preCarrying:
  81. if self.grid.get(ox, oy) is preCarrying:
  82. if abs(ox - tx) <= 1 and abs(oy - ty) <= 1:
  83. reward = 1
  84. done = True
  85. return obs, reward, done, info
  86. class PutNear8x8N3(PutNearEnv):
  87. def __init__(self):
  88. super().__init__(size=8, numObjs=3)
  89. register(
  90. id='MiniGrid-PutNear-6x6-N2-v0',
  91. entry_point='gym_minigrid.envs:PutNearEnv'
  92. )
  93. register(
  94. id='MiniGrid-PutNear-8x8-N3-v0',
  95. entry_point='gym_minigrid.envs:PutNear8x8N3'
  96. )