crossing.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import itertools as itt
  2. import numpy as np
  3. from gym_minigrid.minigrid import Goal, Grid, Lava, MiniGridEnv
  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 = np.array((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. )