gotoobject.py 3.2 KB

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