dynamicobstacles.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. from gym_minigrid.minigrid import *
  2. from gym_minigrid.register import register
  3. from operator import add
  4. class DynamicObstaclesEnv(MiniGridEnv):
  5. """
  6. Single-room square grid environment with moving obstacles
  7. """
  8. def __init__(
  9. self,
  10. size=8,
  11. agent_start_pos=(1, 1),
  12. agent_start_dir=0,
  13. n_obstacles=4
  14. ):
  15. self.agent_start_pos = agent_start_pos
  16. self.agent_start_dir = agent_start_dir
  17. # Reduce obstacles if there are too many
  18. if n_obstacles <= size/2 + 1:
  19. self.n_obstacles = int(n_obstacles)
  20. else:
  21. self.n_obstacles = int(size/2)
  22. super().__init__(
  23. grid_size=size,
  24. max_steps=4 * size * size,
  25. # Set this to True for maximum speed
  26. see_through_walls=True,
  27. )
  28. # Allow only 3 actions permitted: left, right, forward
  29. self.action_space = spaces.Discrete(self.actions.forward + 1)
  30. self.reward_range = (-1, 1)
  31. def _gen_grid(self, width, height):
  32. # Create an empty grid
  33. self.grid = Grid(width, height)
  34. # Generate the surrounding walls
  35. self.grid.wall_rect(0, 0, width, height)
  36. # Place a goal square in the bottom-right corner
  37. self.grid.set(width - 2, height - 2, Goal())
  38. # Place the agent
  39. if self.agent_start_pos is not None:
  40. self.agent_pos = self.agent_start_pos
  41. self.agent_dir = self.agent_start_dir
  42. else:
  43. self.place_agent()
  44. # Place obstacles
  45. self.obstacles = []
  46. for i_obst in range(self.n_obstacles):
  47. self.obstacles.append(Ball())
  48. self.place_obj(self.obstacles[i_obst], max_tries=100)
  49. self.mission = "get to the green goal square"
  50. def step(self, action):
  51. # Invalid action
  52. if action >= self.action_space.n:
  53. action = 0
  54. # Check if there is an obstacle in front of the agent
  55. front_cell = self.grid.get(*self.front_pos)
  56. not_clear = front_cell and front_cell.type != 'goal'
  57. # Update obstacle positions
  58. for i_obst in range(len(self.obstacles)):
  59. old_pos = self.obstacles[i_obst].cur_pos
  60. top = tuple(map(add, old_pos, (-1, -1)))
  61. try:
  62. self.place_obj(self.obstacles[i_obst], top=top, size=(3,3), max_tries=100)
  63. self.grid.set(*old_pos, None)
  64. except:
  65. pass
  66. # Update the agent's position/direction
  67. obs, reward, done, info = MiniGridEnv.step(self, 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):
  76. super().__init__(size=5, n_obstacles=2)
  77. class DynamicObstaclesRandomEnv5x5(DynamicObstaclesEnv):
  78. def __init__(self):
  79. super().__init__(size=5, agent_start_pos=None, n_obstacles=2)
  80. class DynamicObstaclesEnv6x6(DynamicObstaclesEnv):
  81. def __init__(self):
  82. super().__init__(size=6, n_obstacles=3)
  83. class DynamicObstaclesRandomEnv6x6(DynamicObstaclesEnv):
  84. def __init__(self):
  85. super().__init__(size=6, agent_start_pos=None, n_obstacles=3)
  86. class DynamicObstaclesEnv16x16(DynamicObstaclesEnv):
  87. def __init__(self):
  88. super().__init__(size=16, n_obstacles=8)
  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. )