dynamicobstacles.py 3.9 KB

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