crossing.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import itertools as itt
  2. import numpy as np
  3. from gym_minigrid.minigrid import Goal, Grid, Lava, MiniGridEnv, MissionSpace
  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. if obstacle_type == Lava:
  12. mission_space = MissionSpace(
  13. mission_func=lambda: "avoid the lava and get to the green goal square"
  14. )
  15. else:
  16. mission_space = MissionSpace(
  17. mission_func=lambda: "find the opening and get to the green goal square"
  18. )
  19. super().__init__(
  20. mission_space=mission_space,
  21. grid_size=size,
  22. max_steps=4 * size * size,
  23. # Set this to True for maximum speed
  24. see_through_walls=False,
  25. **kwargs
  26. )
  27. def _gen_grid(self, width, height):
  28. assert width % 2 == 1 and height % 2 == 1 # odd size
  29. # Create an empty grid
  30. self.grid = Grid(width, height)
  31. # Generate the surrounding walls
  32. self.grid.wall_rect(0, 0, width, height)
  33. # Place the agent in the top-left corner
  34. self.agent_pos = np.array((1, 1))
  35. self.agent_dir = 0
  36. # Place a goal square in the bottom-right corner
  37. self.put_obj(Goal(), width - 2, height - 2)
  38. # Place obstacles (lava or walls)
  39. v, h = object(), object() # singleton `vertical` and `horizontal` objects
  40. # Lava rivers or walls specified by direction and position in grid
  41. rivers = [(v, i) for i in range(2, height - 2, 2)]
  42. rivers += [(h, j) for j in range(2, width - 2, 2)]
  43. self.np_random.shuffle(rivers)
  44. rivers = rivers[: self.num_crossings] # sample random rivers
  45. rivers_v = sorted(pos for direction, pos in rivers if direction is v)
  46. rivers_h = sorted(pos for direction, pos in rivers if direction is h)
  47. obstacle_pos = itt.chain(
  48. itt.product(range(1, width - 1), rivers_h),
  49. itt.product(rivers_v, range(1, height - 1)),
  50. )
  51. for i, j in obstacle_pos:
  52. self.put_obj(self.obstacle_type(), i, j)
  53. # Sample path to goal
  54. path = [h] * len(rivers_v) + [v] * len(rivers_h)
  55. self.np_random.shuffle(path)
  56. # Create openings
  57. limits_v = [0] + rivers_v + [height - 1]
  58. limits_h = [0] + rivers_h + [width - 1]
  59. room_i, room_j = 0, 0
  60. for direction in path:
  61. if direction is h:
  62. i = limits_v[room_i + 1]
  63. j = self.np_random.choice(
  64. range(limits_h[room_j] + 1, limits_h[room_j + 1])
  65. )
  66. room_i += 1
  67. elif direction is v:
  68. i = self.np_random.choice(
  69. range(limits_v[room_i] + 1, limits_v[room_i + 1])
  70. )
  71. j = limits_h[room_j + 1]
  72. room_j += 1
  73. else:
  74. assert False
  75. self.grid.set(i, j, None)
  76. self.mission = (
  77. "avoid the lava and get to the green goal square"
  78. if self.obstacle_type == Lava
  79. else "find the opening and get to the green goal square"
  80. )