GridWorld#
Overview#
- class GridWorld(origin_shape=None)[source]#
Base class for gridworld environment.
Gridworldenvironment consists of aworld,objects, and oneagent. The world consists of multiple connected rectangle areas and each area is represented by a two-dimensional gird graph, which has each node connected to its four nearest neighbors. Each node represents a state in the world, and has an attributealtitude. When the agent moves from one state toward another state, it will get a reward generated by the altitude change (if there is), i.e.\[R_{move} = A_s - A_{s + 1}\]where \(R_{move}\) is the movement reward and \(A\) represents the altitude of current state \(s\) and next state \(s + 1\).
At each position(state), the agent can choose from 5
actionsto move towards UP, DOWN, LEFT, RIGHT, and STAY in the same state. When the performed movement would make the agent get out of the world, the agent would be forced to stay in the same state.Objects where the agent can get reward are placed at different states and each state can only obtain one object. Each object has its own adjustable probability (
prob) of getting arewardwhen the agent reaches the state with this object, if the agent fails to get a reward, it will get a punishment (punish).\[\begin{split}R_{object} = \left \{ \begin{aligned} & reward, P=p \\ & punish, P=1-p \end{aligned} \right.\end{split}\]Under this situation, the total reward for this step will be the movement reward adding the object reward.
\[R_{total} = R_{move} + R_{object}\]So long as the agent gets to an object (no matter it was reward or punish that it got), this trial is finished and then the agent will be sent back to the start state of each trial.
- Parameters:
origin_shape (tuple of ints (optional, default: None)) – Shape of the world origin. If not provided, the origin will be initialized to be only one state (0, 0, 0), otherwise it will be a rectangular area of shape
origin_shape.
Examples
Initialize a gridworld environment with only an origin state.
>>> W = GridWorld()
W can be grown in several aspects.
Areas:
Add one area of shape (2, 2).
>>> W.add_area((2, 2))
Remove areas.
>>> W.remove_area(1)
Set area altitude.
>>> W.add_area((3, 3)) >>> W.set_altitude(1, altitude_mat=np.random.randn(3, 3))
Paths:
Add inter-area paths.
>>> W = GridWorld() >>> W.add_area((2, 2)) >>> W.add_path((0, 0, 0), (1, 0, 0)) >>> W.add_area((2, 2)) >>> W.add_path((1, 1, 1), (2, 1, 0), register_action=(0, 1))
Remove paths.
>>> W.remove_path((1, 1, 1), (2, 1, 0))
Objects:
Add objects.
>>> W = GridWorld() >>> W.add_area((2, 2)) >>> W.add_object((0, 0, 0), reward=1, prob=0.7) >>> W.add_object((1, 0, 0), reward=1, prob=0.3, punish=-10)
Remove objects.
>>> W.remove_object((1, 0, 0))
Update object attributes.
>>> W.update_object((0, 0, 0), reward=10) >>> W.update_object((0, 0, 0), reward=1, prob=0.8)
Agent:
>>> W = GridWorld() >>> W.init_agent()
One can also manually set the agent initial state.
>>> W = GridWorld() >>> W.add_area((2, 2)) >>> W.add_path((0, 0, 0), (1, 0, 0)) >>> W.init_agent(init_coord=(1, 1, 1))
When the agent is initialized, the agent can move in the world and get rewards.
>>> next_state, reward, done = W.step(action=(0, 1))
Reset:
To reset the environment, first set a reset checkpoint.
>>> W.set_reset_checkpoint()
Then the environment can be reset if needed.
>>> W.reset()
Methods#
Configuring the environment#
|
Initialize a gridworld environment. |
|
Add a new area to the world. |
|
Remove an area from the world. |
|
Set an alias name for an area. |
|
Add a new inter-area connection. |
|
Remove one inter-area connection from the world. |
|
Add one object to the world. |
|
Remove one object from the world. |
|
Reset object attributes. |
|
Set the altitude of each state for one area. |
|
Block one state. |
|
Unblock one state. |
|
Initialize an agent in the world. |
|
Set environment checkpoint for reset. |
Reset the environment to the checkpoint state. |
Get environment information#
A copy of |
|
Gridworld environment time. |
|
Number of areas in the |
|
Action space of the gridworld environment. |
|
Whether there is a reset checkpoint for the gridworld environment. |
|
|
Get the alias name of an area using area index. |
|
Get the index of an area with its alias name. |
|
Get the shape of one area. |
Get the altitude of each state in one area. |
|
|
Get the value of object attribute. |
|
Get state of the agent. |
Moving the agent#
|
Make the agent move toward direction given by |