empty.py 3.4 KB

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