crossing.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. from gym_minigrid.minigrid import *
  2. from gym_minigrid.register import register
  3. import itertools as itt
  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, seed=None):
  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. seed=None
  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. room_i += 1
  57. elif direction is v:
  58. i = self.np_random.choice(
  59. range(limits_v[room_i] + 1, limits_v[room_i + 1]))
  60. j = limits_h[room_j + 1]
  61. room_j += 1
  62. else:
  63. assert False
  64. self.grid.set(i, j, None)
  65. self.mission = (
  66. "avoid the lava and get to the green goal square"
  67. if self.obstacle_type == Lava
  68. else "find the opening and get to the green goal square"
  69. )
  70. class LavaCrossingEnv(CrossingEnv):
  71. def __init__(self):
  72. super().__init__(size=9, num_crossings=1)
  73. class LavaCrossingS9N2Env(CrossingEnv):
  74. def __init__(self):
  75. super().__init__(size=9, num_crossings=2)
  76. class LavaCrossingS9N3Env(CrossingEnv):
  77. def __init__(self):
  78. super().__init__(size=9, num_crossings=3)
  79. class LavaCrossingS11N5Env(CrossingEnv):
  80. def __init__(self):
  81. super().__init__(size=11, num_crossings=5)
  82. register(
  83. id='MiniGrid-LavaCrossingS9N1-v0',
  84. entry_point='gym_minigrid.envs:LavaCrossingEnv'
  85. )
  86. register(
  87. id='MiniGrid-LavaCrossingS9N2-v0',
  88. entry_point='gym_minigrid.envs:LavaCrossingS9N2Env'
  89. )
  90. register(
  91. id='MiniGrid-LavaCrossingS9N3-v0',
  92. entry_point='gym_minigrid.envs:LavaCrossingS9N3Env'
  93. )
  94. register(
  95. id='MiniGrid-LavaCrossingS11N5-v0',
  96. entry_point='gym_minigrid.envs:LavaCrossingS11N5Env'
  97. )
  98. class SimpleCrossingEnv(CrossingEnv):
  99. def __init__(self):
  100. super().__init__(size=9, num_crossings=1, obstacle_type=Wall)
  101. class SimpleCrossingS9N2Env(CrossingEnv):
  102. def __init__(self):
  103. super().__init__(size=9, num_crossings=2, obstacle_type=Wall)
  104. class SimpleCrossingS9N3Env(CrossingEnv):
  105. def __init__(self):
  106. super().__init__(size=9, num_crossings=3, obstacle_type=Wall)
  107. class SimpleCrossingS11N5Env(CrossingEnv):
  108. def __init__(self):
  109. super().__init__(size=11, num_crossings=5, obstacle_type=Wall)
  110. register(
  111. id='MiniGrid-SimpleCrossingS9N1-v0',
  112. entry_point='gym_minigrid.envs:SimpleCrossingEnv'
  113. )
  114. register(
  115. id='MiniGrid-SimpleCrossingS9N2-v0',
  116. entry_point='gym_minigrid.envs:SimpleCrossingS9N2Env'
  117. )
  118. register(
  119. id='MiniGrid-SimpleCrossingS9N3-v0',
  120. entry_point='gym_minigrid.envs:SimpleCrossingS9N3Env'
  121. )
  122. register(
  123. id='MiniGrid-SimpleCrossingS11N5-v0',
  124. entry_point='gym_minigrid.envs:SimpleCrossingS11N5Env'
  125. )