gotoobject.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. from gym_minigrid.minigrid import (
  2. COLOR_NAMES,
  3. Ball,
  4. Box,
  5. Grid,
  6. Key,
  7. MiniGridEnv,
  8. MissionSpace,
  9. )
  10. class GoToObjectEnv(MiniGridEnv):
  11. """
  12. Environment in which the agent is instructed to go to a given object
  13. named using an English text string
  14. """
  15. def __init__(self, size=6, numObjs=2, **kwargs):
  16. self.numObjs = numObjs
  17. self.size = size
  18. # Types of objects to be generated
  19. self.obj_types = ["key", "ball", "box"]
  20. mission_space = MissionSpace(
  21. mission_func=lambda color, type: f"go to the {color} {type}",
  22. ordered_placeholders=[COLOR_NAMES, self.obj_types],
  23. )
  24. super().__init__(
  25. mission_space=mission_space,
  26. width=size,
  27. height=size,
  28. max_steps=5 * size**2,
  29. # Set this to True for maximum speed
  30. see_through_walls=True,
  31. **kwargs,
  32. )
  33. def _gen_grid(self, width, height):
  34. self.grid = Grid(width, height)
  35. # Generate the surrounding walls
  36. self.grid.wall_rect(0, 0, width, height)
  37. # Types and colors of objects we can generate
  38. types = ["key", "ball", "box"]
  39. objs = []
  40. objPos = []
  41. # Until we have generated all the objects
  42. while len(objs) < self.numObjs:
  43. objType = self._rand_elem(types)
  44. objColor = self._rand_elem(COLOR_NAMES)
  45. # If this object already exists, try again
  46. if (objType, objColor) in objs:
  47. continue
  48. if objType == "key":
  49. obj = Key(objColor)
  50. elif objType == "ball":
  51. obj = Ball(objColor)
  52. elif objType == "box":
  53. obj = Box(objColor)
  54. else:
  55. raise ValueError(
  56. "{} object type given. Object type can only be of values key, ball and box.".format(
  57. objType
  58. )
  59. )
  60. pos = self.place_obj(obj)
  61. objs.append((objType, objColor))
  62. objPos.append(pos)
  63. # Randomize the agent start position and orientation
  64. self.place_agent()
  65. # Choose a random object to be picked up
  66. objIdx = self._rand_int(0, len(objs))
  67. self.targetType, self.target_color = objs[objIdx]
  68. self.target_pos = objPos[objIdx]
  69. descStr = f"{self.target_color} {self.targetType}"
  70. self.mission = "go to the %s" % descStr
  71. # print(self.mission)
  72. def step(self, action):
  73. obs, reward, terminated, truncated, info = super().step(action)
  74. ax, ay = self.agent_pos
  75. tx, ty = self.target_pos
  76. # Toggle/pickup action terminates the episode
  77. if action == self.actions.toggle:
  78. terminated = True
  79. # Reward performing the done action next to the target object
  80. if action == self.actions.done:
  81. if abs(ax - tx) <= 1 and abs(ay - ty) <= 1:
  82. reward = self._reward()
  83. terminated = True
  84. return obs, reward, terminated, truncated, info