dynamicobstacles.py 4.4 KB

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