putnear.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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__(
  15. grid_size=size,
  16. max_steps=5*size,
  17. # Set this to True for maximum speed
  18. see_through_walls=True
  19. )
  20. def _gen_grid(self, width, height):
  21. self.grid = Grid(width, height)
  22. # Generate the surrounding walls
  23. self.grid.horz_wall(0, 0)
  24. self.grid.horz_wall(0, height-1)
  25. self.grid.vert_wall(0, 0)
  26. self.grid.vert_wall(width-1, 0)
  27. # Types and colors of objects we can generate
  28. types = ['key', 'ball', 'box']
  29. objs = []
  30. objPos = []
  31. def near_obj(env, p1):
  32. for p2 in objPos:
  33. dx = p1[0] - p2[0]
  34. dy = p1[1] - p2[1]
  35. if abs(dx) <= 1 and abs(dy) <= 1:
  36. return True
  37. return False
  38. # Until we have generated all the objects
  39. while len(objs) < self.numObjs:
  40. objType = self._rand_elem(types)
  41. objColor = self._rand_elem(COLOR_NAMES)
  42. # If this object already exists, try again
  43. if (objType, objColor) in objs:
  44. continue
  45. if objType == 'key':
  46. obj = Key(objColor)
  47. elif objType == 'ball':
  48. obj = Ball(objColor)
  49. elif objType == 'box':
  50. obj = Box(objColor)
  51. pos = self.place_obj(obj, reject_fn=near_obj)
  52. objs.append((objType, objColor))
  53. objPos.append(pos)
  54. # Randomize the agent start position and orientation
  55. self.place_agent()
  56. # Choose a random object to be moved
  57. objIdx = self._rand_int(0, len(objs))
  58. self.move_type, self.moveColor = objs[objIdx]
  59. self.move_pos = objPos[objIdx]
  60. # Choose a target object (to put the first object next to)
  61. while True:
  62. targetIdx = self._rand_int(0, len(objs))
  63. if targetIdx != objIdx:
  64. break
  65. self.target_type, self.target_color = objs[targetIdx]
  66. self.target_pos = objPos[targetIdx]
  67. self.mission = 'put the %s %s near the %s %s' % (
  68. self.moveColor,
  69. self.move_type,
  70. self.target_color,
  71. self.target_type
  72. )
  73. def step(self, action):
  74. preCarrying = self.carrying
  75. obs, reward, done, info = super().step(action)
  76. u, v = self.dir_vec
  77. ox, oy = (self.agent_pos[0] + u, self.agent_pos[1] + v)
  78. tx, ty = self.target_pos
  79. # If we picked up the wrong object, terminate the episode
  80. if action == self.actions.pickup and self.carrying:
  81. if self.carrying.type != self.move_type or self.carrying.color != self.moveColor:
  82. done = True
  83. # If successfully dropping an object near the target
  84. if action == self.actions.drop and preCarrying:
  85. if self.grid.get(ox, oy) is preCarrying:
  86. if abs(ox - tx) <= 1 and abs(oy - ty) <= 1:
  87. reward = self._reward()
  88. done = True
  89. return obs, reward, done, info
  90. class PutNear8x8N3(PutNearEnv):
  91. def __init__(self):
  92. super().__init__(size=8, numObjs=3)
  93. register(
  94. id='MiniGrid-PutNear-6x6-N2-v0',
  95. entry_point='gym_minigrid.envs:PutNearEnv'
  96. )
  97. register(
  98. id='MiniGrid-PutNear-8x8-N3-v0',
  99. entry_point='gym_minigrid.envs:PutNear8x8N3'
  100. )