dynamicobstacles.py 3.1 KB

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