empty.py 3.1 KB

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