redbluedoors.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. from gym_minigrid.minigrid import *
  2. from gym_minigrid.register import register
  3. class RedBlueDoorEnv(MiniGridEnv):
  4. """
  5. Single room with red and blue doors on opposite sides.
  6. The red door must be opened before the blue door to
  7. obtain a reward.
  8. """
  9. def __init__(self, size=8):
  10. self.size = size
  11. super().__init__(
  12. width=2*size,
  13. height=size,
  14. max_steps=20*size*size
  15. )
  16. def _gen_grid(self, width, height):
  17. # Create an empty grid
  18. self.grid = Grid(width, height)
  19. # Generate the grid walls
  20. self.grid.wall_rect(0, 0, 2*self.size, self.size)
  21. self.grid.wall_rect(self.size//2, 0, self.size, self.size)
  22. # Place the agent in the top-left corner
  23. self.place_agent(top=(self.size//2, 0), size=(self.size, self.size))
  24. # Add a red door at a random position in the left wall
  25. pos = self._rand_int(1, self.size - 1)
  26. self.red_door = Door("red")
  27. self.grid.set(self.size//2, pos, self.red_door)
  28. # Add a blue door at a random position in the right wall
  29. pos = self._rand_int(1, self.size - 1)
  30. self.blue_door = Door("blue")
  31. self.grid.set(self.size//2 + self.size - 1, pos, self.blue_door)
  32. # Generate the mission string
  33. self.mission = "open the red door then the blue door"
  34. def step(self, action):
  35. red_door_opened_before = self.red_door.is_open
  36. blue_door_opened_before = self.blue_door.is_open
  37. obs, reward, done, info = MiniGridEnv.step(self, action)
  38. red_door_opened_after = self.red_door.is_open
  39. blue_door_opened_after = self.blue_door.is_open
  40. if blue_door_opened_after:
  41. if red_door_opened_before:
  42. reward = self._reward()
  43. done = True
  44. else:
  45. reward = 0
  46. done = True
  47. elif red_door_opened_after:
  48. if blue_door_opened_before:
  49. reward = 0
  50. done = True
  51. return obs, reward, done, info
  52. class RedBlueDoorEnv6x6(RedBlueDoorEnv):
  53. def __init__(self):
  54. super().__init__(size=6)
  55. register(
  56. id='MiniGrid-RedBlueDoors-6x6-v0',
  57. entry_point='gym_minigrid.envs:RedBlueDoorEnv6x6'
  58. )
  59. register(
  60. id='MiniGrid-RedBlueDoors-8x8-v0',
  61. entry_point='gym_minigrid.envs:RedBlueDoorEnv'
  62. )