redbluedoors.py 2.2 KB

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