gotoobject.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. from gym_minigrid.minigrid import COLOR_NAMES, Ball, Box, Grid, Key, MiniGridEnv
  2. from gym_minigrid.register import register
  3. class GoToObjectEnv(MiniGridEnv):
  4. """
  5. Environment in which the agent is instructed to go to a given object
  6. named using an English text 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**2,
  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.wall_rect(0, 0, width, height)
  21. # Types and colors of objects we can generate
  22. types = ["key", "ball", "box"]
  23. objs = []
  24. objPos = []
  25. # Until we have generated all the objects
  26. while len(objs) < self.numObjs:
  27. objType = self._rand_elem(types)
  28. objColor = self._rand_elem(COLOR_NAMES)
  29. # If this object already exists, try again
  30. if (objType, objColor) in objs:
  31. continue
  32. if objType == "key":
  33. obj = Key(objColor)
  34. elif objType == "ball":
  35. obj = Ball(objColor)
  36. elif objType == "box":
  37. obj = Box(objColor)
  38. else:
  39. raise ValueError(
  40. "{} object type given. Object type can only be of values key, ball and box.".format(
  41. objType
  42. )
  43. )
  44. pos = self.place_obj(obj)
  45. objs.append((objType, objColor))
  46. objPos.append(pos)
  47. # Randomize the agent start position and orientation
  48. self.place_agent()
  49. # Choose a random object to be picked up
  50. objIdx = self._rand_int(0, len(objs))
  51. self.targetType, self.target_color = objs[objIdx]
  52. self.target_pos = objPos[objIdx]
  53. descStr = f"{self.target_color} {self.targetType}"
  54. self.mission = "go to the %s" % descStr
  55. # print(self.mission)
  56. def step(self, action):
  57. obs, reward, done, info = super().step(action)
  58. ax, ay = self.agent_pos
  59. tx, ty = self.target_pos
  60. # Toggle/pickup action terminates the episode
  61. if action == self.actions.toggle:
  62. done = True
  63. # Reward performing the done action next to the target object
  64. if action == self.actions.done:
  65. if abs(ax - tx) <= 1 and abs(ay - ty) <= 1:
  66. reward = self._reward()
  67. done = True
  68. return obs, reward, done, info
  69. register(
  70. id="MiniGrid-GoToObject-6x6-N2-v0",
  71. entry_point="gym_minigrid.envs.gotoobject:GoToObjectEnv",
  72. )
  73. register(
  74. id="MiniGrid-GoToObject-8x8-N2-v0",
  75. entry_point="gym_minigrid.envs.gotoobject:GoToObjectEnv",
  76. size=8,
  77. numObjs=2,
  78. )