dynamicobstacles.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. from gym_minigrid.minigrid import *
  2. from gym_minigrid.register import register
  3. from operator import add
  4. class DynamicObstaclesEnv(MiniGridEnv):
  5. """
  6. Empty 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. show_obstacles=True
  15. ):
  16. self.agent_start_pos = agent_start_pos
  17. self.agent_start_dir = agent_start_dir
  18. self.show_obstacles = show_obstacles
  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. )
  30. # Allow only 3 actions permitted: left, right, forward
  31. self.action_space = spaces.Discrete(self.actions.forward + 1)
  32. self.reward_range = (-1, 1)
  33. def _gen_grid(self, width, height):
  34. # Create an empty grid
  35. self.grid = Grid(width, height)
  36. # Generate the surrounding walls
  37. self.grid.wall_rect(0, 0, width, height)
  38. # Place a goal square in the bottom-right corner
  39. self.grid.set(width - 2, height - 2, Goal())
  40. # Place the agent
  41. if self.agent_start_pos is not None:
  42. self.start_pos = self.agent_start_pos
  43. self.start_dir = self.agent_start_dir
  44. else:
  45. self.place_agent()
  46. # Place obstacles
  47. if self.show_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. obs, reward, done, info = MiniGridEnv.step(self, action)
  55. # Update obstacle positions
  56. if self.show_obstacles:
  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. self.place_obj(self.obstacles[i_obst], top=top, size=(3,3), max_tries=100)
  61. self.grid.set(old_pos[0], old_pos[1], None)
  62. if np.array_equal(self.obstacles[i_obst].cur_pos, self.agent_pos):
  63. reward = -1
  64. done = True
  65. return obs, reward, done, info
  66. class DynamicObstaclesEnv5x5(DynamicObstaclesEnv):
  67. def __init__(self):
  68. super().__init__(size=5, n_obstacles=2)
  69. class DynamicObstaclesRandomEnv5x5(DynamicObstaclesEnv):
  70. def __init__(self):
  71. super().__init__(size=5, agent_start_pos=None, n_obstacles=2)
  72. class DynamicObstaclesEnv6x6(DynamicObstaclesEnv):
  73. def __init__(self):
  74. super().__init__(size=6, n_obstacles=3)
  75. class DynamicObstaclesRandomEnv6x6(DynamicObstaclesEnv):
  76. def __init__(self):
  77. super().__init__(size=6, agent_start_pos=None, n_obstacles=3)
  78. class DynamicObstaclesEnv16x16(DynamicObstaclesEnv):
  79. def __init__(self):
  80. super().__init__(size=16, n_obstacles=8)
  81. register(
  82. id='MiniGrid-Dynamic-Obstacles-5x5-v0',
  83. entry_point='gym_minigrid.envs:DynamicObstaclesEnv5x5'
  84. )
  85. register(
  86. id='MiniGrid-Dynamic-Obstacles-Random-5x5-v0',
  87. entry_point='gym_minigrid.envs:DynamicObstaclesRandomEnv5x5'
  88. )
  89. register(
  90. id='MiniGrid-Dynamic-Obstacles-6x6-v0',
  91. entry_point='gym_minigrid.envs:DynamicObstaclesEnv6x6'
  92. )
  93. register(
  94. id='MiniGrid-Dynamic-Obstacles-Random-6x6-v0',
  95. entry_point='gym_minigrid.envs:DynamicObstaclesRandomEnv6x6'
  96. )
  97. register(
  98. id='MiniGrid-Dynamic-Obstacles-8x8-v0',
  99. entry_point='gym_minigrid.envs:DynamicObstaclesEnv'
  100. )
  101. register(
  102. id='MiniGrid-Dynamic-Obstacles-16x16-v0',
  103. entry_point='gym_minigrid.envs:DynamicObstaclesEnv16x16'
  104. )