empty.py 3.2 KB

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