gotoobject.py 3.3 KB

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