empty.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. from gym_minigrid.minigrid import Goal, Grid, MiniGridEnv, MissionSpace
  2. class EmptyEnv(MiniGridEnv):
  3. """
  4. ### Description
  5. This environment is an empty room, and the goal of the agent is to reach the
  6. green goal square, which provides a sparse reward. A small penalty is
  7. subtracted for the number of steps to reach the goal. This environment is
  8. useful, with small rooms, to validate that your RL algorithm works
  9. correctly, and with large rooms to experiment with sparse rewards and
  10. exploration. The random variants of the environment have the agent starting
  11. at a random position for each episode, while the regular variants have the
  12. agent always starting in the corner opposite to the goal.
  13. ### Mission Space
  14. "get to the green goal square"
  15. ### Action Space
  16. | Num | Name | Action |
  17. |-----|--------------|--------------|
  18. | 0 | left | Turn left |
  19. | 1 | right | Turn right |
  20. | 2 | forward | Move forward |
  21. | 3 | pickup | Unused |
  22. | 4 | drop | Unused |
  23. | 5 | toggle | Unused |
  24. | 6 | done | Unused |
  25. ### Observation Encoding
  26. - Each tile is encoded as a 3 dimensional tuple:
  27. `(OBJECT_IDX, COLOR_IDX, STATE)`
  28. - `OBJECT_TO_IDX` and `COLOR_TO_IDX` mapping can be found in
  29. [gym_minigrid/minigrid.py](gym_minigrid/minigrid.py)
  30. - `STATE` refers to the door state with 0=open, 1=closed and 2=locked
  31. ### Rewards
  32. A reward of '1' is given for success, and '0' for failure.
  33. ### Termination
  34. The episode ends if any one of the following conditions is met:
  35. 1. The agent reaches the goal.
  36. 2. Timeout (see `max_steps`).
  37. ### Registered Configurations
  38. - `MiniGrid-Empty-5x5-v0`
  39. - `MiniGrid-Empty-Random-5x5-v0`
  40. - `MiniGrid-Empty-6x6-v0`
  41. - `MiniGrid-Empty-Random-6x6-v0`
  42. - `MiniGrid-Empty-8x8-v0`
  43. - `MiniGrid-Empty-16x16-v0`
  44. """
  45. def __init__(self, size=8, agent_start_pos=(1, 1), agent_start_dir=0, **kwargs):
  46. self.agent_start_pos = agent_start_pos
  47. self.agent_start_dir = agent_start_dir
  48. mission_space = MissionSpace(
  49. mission_func=lambda: "get to the green goal square"
  50. )
  51. super().__init__(
  52. mission_space=mission_space,
  53. grid_size=size,
  54. max_steps=4 * size * size,
  55. # Set this to True for maximum speed
  56. see_through_walls=True,
  57. **kwargs
  58. )
  59. def _gen_grid(self, width, height):
  60. # Create an empty grid
  61. self.grid = Grid(width, height)
  62. # Generate the surrounding walls
  63. self.grid.wall_rect(0, 0, width, height)
  64. # Place a goal square in the bottom-right corner
  65. self.put_obj(Goal(), width - 2, height - 2)
  66. # Place the agent
  67. if self.agent_start_pos is not None:
  68. self.agent_pos = self.agent_start_pos
  69. self.agent_dir = self.agent_start_dir
  70. else:
  71. self.place_agent()
  72. self.mission = "get to the green goal square"