crossing.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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, num_crossings=1
  76. )
  77. register(
  78. id='MiniGrid-LavaCrossingS9N2-v0',
  79. entry_point='gym_minigrid.envs.crossing:CrossingEnv',
  80. size=9, num_crossings=2
  81. )
  82. register(
  83. id='MiniGrid-LavaCrossingS9N3-v0',
  84. entry_point='gym_minigrid.envs.crossing:CrossingEnv',
  85. size=9, num_crossings=3
  86. )
  87. register(
  88. id='MiniGrid-LavaCrossingS11N5-v0',
  89. entry_point='gym_minigrid.envs.crossing:CrossingEnv',
  90. size=11, num_crossings=5
  91. )
  92. register(
  93. id='MiniGrid-SimpleCrossingS9N1-v0',
  94. entry_point='gym_minigrid.envs.crossing:CrossingEnv',
  95. size=9, num_crossings=1, obstacle_type=Wall
  96. )
  97. register(
  98. id='MiniGrid-SimpleCrossingS9N2-v0',
  99. entry_point='gym_minigrid.envs.crossing:CrossingEnv',
  100. size=9, num_crossings=2, obstacle_type=Wall
  101. )
  102. register(
  103. id='MiniGrid-SimpleCrossingS9N3-v0',
  104. entry_point='gym_minigrid.envs.crossing:CrossingEnv',
  105. size=9, num_crossings=3, obstacle_type=Wall
  106. )
  107. register(
  108. id='MiniGrid-SimpleCrossingS11N5-v0',
  109. entry_point='gym_minigrid.envs.crossing:CrossingEnv',
  110. size=11, num_crossings=5, obstacle_type=Wall
  111. )