fourrooms.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. from gym_minigrid.minigrid import Goal, Grid, MiniGridEnv
  2. class FourRoomsEnv(MiniGridEnv):
  3. """
  4. Classic 4 rooms gridworld environment.
  5. Can specify agent and goal position, if not it set at random.
  6. """
  7. def __init__(self, agent_pos=None, goal_pos=None, **kwargs):
  8. self._agent_default_pos = agent_pos
  9. self._goal_default_pos = goal_pos
  10. super().__init__(grid_size=19, max_steps=100, **kwargs)
  11. def _gen_grid(self, width, height):
  12. # Create the grid
  13. self.grid = Grid(width, height)
  14. # Generate the surrounding walls
  15. self.grid.horz_wall(0, 0)
  16. self.grid.horz_wall(0, height - 1)
  17. self.grid.vert_wall(0, 0)
  18. self.grid.vert_wall(width - 1, 0)
  19. room_w = width // 2
  20. room_h = height // 2
  21. # For each row of rooms
  22. for j in range(0, 2):
  23. # For each column
  24. for i in range(0, 2):
  25. xL = i * room_w
  26. yT = j * room_h
  27. xR = xL + room_w
  28. yB = yT + room_h
  29. # Bottom wall and door
  30. if i + 1 < 2:
  31. self.grid.vert_wall(xR, yT, room_h)
  32. pos = (xR, self._rand_int(yT + 1, yB))
  33. self.grid.set(*pos, None)
  34. # Bottom wall and door
  35. if j + 1 < 2:
  36. self.grid.horz_wall(xL, yB, room_w)
  37. pos = (self._rand_int(xL + 1, xR), yB)
  38. self.grid.set(*pos, None)
  39. # Randomize the player start position and orientation
  40. if self._agent_default_pos is not None:
  41. self.agent_pos = self._agent_default_pos
  42. self.grid.set(*self._agent_default_pos, None)
  43. self.agent_dir = self._rand_int(0, 4) # assuming random start direction
  44. else:
  45. self.place_agent()
  46. if self._goal_default_pos is not None:
  47. goal = Goal()
  48. self.put_obj(goal, *self._goal_default_pos)
  49. goal.init_pos, goal.cur_pos = self._goal_default_pos
  50. else:
  51. self.place_obj(Goal())
  52. self.mission = "reach the goal"
  53. def step(self, action):
  54. obs, reward, done, info = MiniGridEnv.step(self, action)
  55. return obs, reward, done, info