crossing.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import itertools as itt
  2. from gym_minigrid.minigrid import Goal, Grid, Lava, MiniGridEnv, Wall
  3. from gym_minigrid.register import register
  4. class CrossingEnv(MiniGridEnv):
  5. """
  6. Environment with wall or lava obstacles, sparse reward.
  7. """
  8. def __init__(self, size=9, num_crossings=1, obstacle_type=Lava, **kwargs):
  9. self.num_crossings = num_crossings
  10. self.obstacle_type = obstacle_type
  11. super().__init__(
  12. grid_size=size,
  13. max_steps=4 * size * size,
  14. # Set this to True for maximum speed
  15. see_through_walls=False,
  16. **kwargs
  17. )
  18. def _gen_grid(self, width, height):
  19. assert width % 2 == 1 and height % 2 == 1 # odd size
  20. # Create an empty grid
  21. self.grid = Grid(width, height)
  22. # Generate the surrounding walls
  23. self.grid.wall_rect(0, 0, width, height)
  24. # Place the agent in the top-left corner
  25. self.agent_pos = (1, 1)
  26. self.agent_dir = 0
  27. # Place a goal square in the bottom-right corner
  28. self.put_obj(Goal(), width - 2, height - 2)
  29. # Place obstacles (lava or walls)
  30. v, h = object(), object() # singleton `vertical` and `horizontal` objects
  31. # Lava rivers or walls specified by direction and position in grid
  32. rivers = [(v, i) for i in range(2, height - 2, 2)]
  33. rivers += [(h, j) for j in range(2, width - 2, 2)]
  34. self.np_random.shuffle(rivers)
  35. rivers = rivers[: self.num_crossings] # sample random rivers
  36. rivers_v = sorted(pos for direction, pos in rivers if direction is v)
  37. rivers_h = sorted(pos for direction, pos in rivers if direction is h)
  38. obstacle_pos = itt.chain(
  39. itt.product(range(1, width - 1), rivers_h),
  40. itt.product(rivers_v, range(1, height - 1)),
  41. )
  42. for i, j in obstacle_pos:
  43. self.put_obj(self.obstacle_type(), i, j)
  44. # Sample path to goal
  45. path = [h] * len(rivers_v) + [v] * len(rivers_h)
  46. self.np_random.shuffle(path)
  47. # Create openings
  48. limits_v = [0] + rivers_v + [height - 1]
  49. limits_h = [0] + rivers_h + [width - 1]
  50. room_i, room_j = 0, 0
  51. for direction in path:
  52. if direction is h:
  53. i = limits_v[room_i + 1]
  54. j = self.np_random.choice(
  55. range(limits_h[room_j] + 1, limits_h[room_j + 1])
  56. )
  57. room_i += 1
  58. elif direction is v:
  59. i = self.np_random.choice(
  60. range(limits_v[room_i] + 1, limits_v[room_i + 1])
  61. )
  62. j = limits_h[room_j + 1]
  63. room_j += 1
  64. else:
  65. assert False
  66. self.grid.set(i, j, None)
  67. self.mission = (
  68. "avoid the lava and get to the green goal square"
  69. if self.obstacle_type == Lava
  70. else "find the opening and get to the green goal square"
  71. )
  72. register(
  73. id="MiniGrid-LavaCrossingS9N1-v0",
  74. entry_point="gym_minigrid.envs.crossing:CrossingEnv",
  75. size=9,
  76. num_crossings=1,
  77. )
  78. register(
  79. id="MiniGrid-LavaCrossingS9N2-v0",
  80. entry_point="gym_minigrid.envs.crossing:CrossingEnv",
  81. size=9,
  82. num_crossings=2,
  83. )
  84. register(
  85. id="MiniGrid-LavaCrossingS9N3-v0",
  86. entry_point="gym_minigrid.envs.crossing:CrossingEnv",
  87. size=9,
  88. num_crossings=3,
  89. )
  90. register(
  91. id="MiniGrid-LavaCrossingS11N5-v0",
  92. entry_point="gym_minigrid.envs.crossing:CrossingEnv",
  93. size=11,
  94. num_crossings=5,
  95. )
  96. register(
  97. id="MiniGrid-SimpleCrossingS9N1-v0",
  98. entry_point="gym_minigrid.envs.crossing:CrossingEnv",
  99. size=9,
  100. num_crossings=1,
  101. obstacle_type=Wall,
  102. )
  103. register(
  104. id="MiniGrid-SimpleCrossingS9N2-v0",
  105. entry_point="gym_minigrid.envs.crossing:CrossingEnv",
  106. size=9,
  107. num_crossings=2,
  108. obstacle_type=Wall,
  109. )
  110. register(
  111. id="MiniGrid-SimpleCrossingS9N3-v0",
  112. entry_point="gym_minigrid.envs.crossing:CrossingEnv",
  113. size=9,
  114. num_crossings=3,
  115. obstacle_type=Wall,
  116. )
  117. register(
  118. id="MiniGrid-SimpleCrossingS11N5-v0",
  119. entry_point="gym_minigrid.envs.crossing:CrossingEnv",
  120. size=11,
  121. num_crossings=5,
  122. obstacle_type=Wall,
  123. )