crossing.py 4.2 KB

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