putnear.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. from gym_minigrid.minigrid import COLOR_NAMES, Ball, Box, Grid, Key, MiniGridEnv
  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__(self, size=6, numObjs=2, **kwargs):
  9. self.numObjs = numObjs
  10. super().__init__(
  11. grid_size=size,
  12. max_steps=5 * size,
  13. # Set this to True for maximum speed
  14. see_through_walls=True,
  15. **kwargs
  16. )
  17. def _gen_grid(self, width, height):
  18. self.grid = Grid(width, height)
  19. # Generate the surrounding walls
  20. self.grid.horz_wall(0, 0)
  21. self.grid.horz_wall(0, height - 1)
  22. self.grid.vert_wall(0, 0)
  23. self.grid.vert_wall(width - 1, 0)
  24. # Types and colors of objects we can generate
  25. types = ["key", "ball", "box"]
  26. objs = []
  27. objPos = []
  28. def near_obj(env, 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._rand_elem(types)
  38. objColor = self._rand_elem(COLOR_NAMES)
  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. pos = self.place_obj(obj, reject_fn=near_obj)
  49. objs.append((objType, objColor))
  50. objPos.append(pos)
  51. # Randomize the agent start position and orientation
  52. self.place_agent()
  53. # Choose a random object to be moved
  54. objIdx = self._rand_int(0, len(objs))
  55. self.move_type, self.moveColor = objs[objIdx]
  56. self.move_pos = objPos[objIdx]
  57. # Choose a target object (to put the first object next to)
  58. while True:
  59. targetIdx = self._rand_int(0, len(objs))
  60. if targetIdx != objIdx:
  61. break
  62. self.target_type, self.target_color = objs[targetIdx]
  63. self.target_pos = objPos[targetIdx]
  64. self.mission = "put the {} {} near the {} {}".format(
  65. self.moveColor,
  66. self.move_type,
  67. self.target_color,
  68. self.target_type,
  69. )
  70. def step(self, action):
  71. preCarrying = self.carrying
  72. obs, reward, done, info = super().step(action)
  73. u, v = self.dir_vec
  74. ox, oy = (self.agent_pos[0] + u, self.agent_pos[1] + v)
  75. tx, ty = self.target_pos
  76. # If we picked up the wrong object, terminate the episode
  77. if action == self.actions.pickup and self.carrying:
  78. if (
  79. self.carrying.type != self.move_type
  80. or self.carrying.color != self.moveColor
  81. ):
  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. register(
  91. id='MiniGrid-PutNear-6x6-N2-v0',
  92. entry_point='gym_minigrid.envs.putnear:PutNearEnv'
  93. )
  94. register(
  95. id='MiniGrid-PutNear-8x8-N3-v0',
  96. entry_point='gym_minigrid.envs.putnear:PutNearEnv',
  97. size=8, numObjs=3
  98. )