dynamicobstacles.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. from operator import add
  2. import gym
  3. from gym_minigrid.minigrid import Ball, Goal, Grid, MiniGridEnv
  4. from gym_minigrid.register import register
  5. class DynamicObstaclesEnv(MiniGridEnv):
  6. """
  7. Single-room square grid environment with moving obstacles
  8. """
  9. def __init__(
  10. self, size=8, agent_start_pos=(1, 1), agent_start_dir=0, n_obstacles=4
  11. ):
  12. self.agent_start_pos = agent_start_pos
  13. self.agent_start_dir = agent_start_dir
  14. # Reduce obstacles if there are too many
  15. if n_obstacles <= size / 2 + 1:
  16. self.n_obstacles = int(n_obstacles)
  17. else:
  18. self.n_obstacles = int(size / 2)
  19. super().__init__(
  20. grid_size=size,
  21. max_steps=4 * size * size,
  22. # Set this to True for maximum speed
  23. see_through_walls=True,
  24. )
  25. # Allow only 3 actions permitted: left, right, forward
  26. self.action_space = gym.spaces.Discrete(self.actions.forward + 1)
  27. self.reward_range = (-1, 1)
  28. def _gen_grid(self, width, height):
  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 a goal square in the bottom-right corner
  34. self.grid.set(width - 2, height - 2, Goal())
  35. # Place the agent
  36. if self.agent_start_pos is not None:
  37. self.agent_pos = self.agent_start_pos
  38. self.agent_dir = self.agent_start_dir
  39. else:
  40. self.place_agent()
  41. # Place obstacles
  42. self.obstacles = []
  43. for i_obst in range(self.n_obstacles):
  44. self.obstacles.append(Ball())
  45. self.place_obj(self.obstacles[i_obst], max_tries=100)
  46. self.mission = "get to the green goal square"
  47. def step(self, action):
  48. # Invalid action
  49. if action >= self.action_space.n:
  50. action = 0
  51. # Check if there is an obstacle in front of the agent
  52. front_cell = self.grid.get(*self.front_pos)
  53. not_clear = front_cell and front_cell.type != "goal"
  54. # Update obstacle positions
  55. for i_obst in range(len(self.obstacles)):
  56. old_pos = self.obstacles[i_obst].cur_pos
  57. top = tuple(map(add, old_pos, (-1, -1)))
  58. try:
  59. self.place_obj(
  60. self.obstacles[i_obst], top=top, size=(3, 3), max_tries=100
  61. )
  62. self.grid.set(*old_pos, None)
  63. except Exception:
  64. pass
  65. # Update the agent's position/direction
  66. obs, reward, done, info = super().step(action)
  67. # If the agent tried to walk over an obstacle or wall
  68. if action == self.actions.forward and not_clear:
  69. reward = -1
  70. done = True
  71. return obs, reward, done, info
  72. return obs, reward, done, info
  73. class DynamicObstaclesEnv5x5(DynamicObstaclesEnv):
  74. def __init__(self):
  75. super().__init__(size=5, n_obstacles=2)
  76. class DynamicObstaclesRandomEnv5x5(DynamicObstaclesEnv):
  77. def __init__(self):
  78. super().__init__(size=5, agent_start_pos=None, n_obstacles=2)
  79. class DynamicObstaclesEnv6x6(DynamicObstaclesEnv):
  80. def __init__(self):
  81. super().__init__(size=6, n_obstacles=3)
  82. class DynamicObstaclesRandomEnv6x6(DynamicObstaclesEnv):
  83. def __init__(self):
  84. super().__init__(size=6, agent_start_pos=None, n_obstacles=3)
  85. class DynamicObstaclesEnv16x16(DynamicObstaclesEnv):
  86. def __init__(self):
  87. super().__init__(size=16, n_obstacles=8)
  88. register(
  89. id="MiniGrid-Dynamic-Obstacles-5x5-v0",
  90. entry_point="gym_minigrid.envs:DynamicObstaclesEnv5x5",
  91. )
  92. register(
  93. id="MiniGrid-Dynamic-Obstacles-Random-5x5-v0",
  94. entry_point="gym_minigrid.envs:DynamicObstaclesRandomEnv5x5",
  95. )
  96. register(
  97. id="MiniGrid-Dynamic-Obstacles-6x6-v0",
  98. entry_point="gym_minigrid.envs:DynamicObstaclesEnv6x6",
  99. )
  100. register(
  101. id="MiniGrid-Dynamic-Obstacles-Random-6x6-v0",
  102. entry_point="gym_minigrid.envs:DynamicObstaclesRandomEnv6x6",
  103. )
  104. register(
  105. id="MiniGrid-Dynamic-Obstacles-8x8-v0",
  106. entry_point="gym_minigrid.envs:DynamicObstaclesEnv",
  107. )
  108. register(
  109. id="MiniGrid-Dynamic-Obstacles-16x16-v0",
  110. entry_point="gym_minigrid.envs:DynamicObstaclesEnv16x16",
  111. )