dynamicobstacles.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. obs, reward, done, info = MiniGridEnv.step(self, action)
  58. # If the agent tries to walk over an obstacle
  59. if action == self.actions.forward and not_clear:
  60. reward = -1
  61. done = True
  62. return obs, reward, done, info
  63. # Update obstacle positions
  64. for i_obst in range(len(self.obstacles)):
  65. old_pos = self.obstacles[i_obst].cur_pos
  66. top = tuple(map(add, old_pos, (-1, -1)))
  67. try:
  68. self.place_obj(self.obstacles[i_obst], top=top, size=(3,3), max_tries=100)
  69. self.grid.set(*old_pos, None)
  70. except:
  71. pass
  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. )