dynamicobstacles.py 4.3 KB

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