multigrid.core.agent module#

class multigrid.core.agent.Agent[source]#

Bases: object

Class representing an agent in the environment.

Observation Space:

Observations are dictionaries with the following entries:

  • imagendarray[int] of shape (view_size, view_size, WorldObj.dim)

    Encoding of the agent’s view of the environment

  • directionint

    Agent’s direction (0: right, 1: down, 2: left, 3: up)

  • missionMission

    Task string corresponding to the current environment configuration

Action Space:

Actions are discrete integers, as enumerated in Action.

Attributes:
indexint

Index of the agent in the environment

stateAgentState

State of the agent

missionMission

Current mission string for the agent

action_spacegym.spaces.Discrete

Action space for the agent

observation_spacegym.spaces.Dict

Observation space for the agent

__init__(index: int, mission_space: MissionSpace = MissionSpace('maximize reward'), view_size: int = 7, see_through_walls: bool = False)[source]#
Parameters:
indexint

Index of the agent in the environment

mission_spaceMissionSpace

The mission space for the agent

view_sizeint

The size of the agent’s view (must be odd)

see_through_wallsbool

Whether the agent can see through walls

property color#

Alias for AgentState.color.

property dir#

Alias for AgentState.dir.

property pos#

Alias for AgentState.pos.

property terminated#

Alias for AgentState.terminated.

property carrying#

Alias for AgentState.carrying.

property front_pos: tuple[int, int]#

Get the position of the cell that is directly in front of the agent.

reset(mission: Mission = Mission('maximize reward'))[source]#

Reset the agent to an initial state.

Parameters:
missionMission

Mission string to use for the new episode

encode() tuple[int, int, int][source]#

Encode a description of this agent as a 3-tuple of integers.

Returns:
type_idxint

The index of the agent type

color_idxint

The index of the agent color

agent_dirint

The direction of the agent (0: right, 1: down, 2: left, 3: up)

render(img: ndarray[Any, dtype[uint8]])[source]#

Draw the agent.

Parameters:
imgndarray[int] of shape (width, height, 3)

RGB image array to render agent on

class multigrid.core.agent.AgentState[source]#

Bases: ndarray

State for an Agent object.

AgentState objects also support vectorized operations, in which case the AgentState object represents the states of multiple agents.

Examples

Create a vectorized agent state for 3 agents:

>>> agent_state = AgentState(3)
>>> agent_state
AgentState(3)

Access and set state attributes for one agent at a time:

>>> a = agent_state[0]
>>> a
AgentState()
>>> a.color
'red'
>>> a.color = 'yellow'

The underlying vectorized state is automatically updated as well:

>>> agent_state.color
array(['yellow', 'green', 'blue'])

Access and set state attributes all at once:

>>> agent_state.dir
array([-1, -1, -1])
>>> agent_state.dir = np.random.randint(4, size=(len(agent_state)))
>>> agent_state.dir
array([2, 3, 0])
>>> a.dir
2
Attributes:
colorColor or ndarray[str]

Return the agent color.

dirDirection or ndarray[int]

Return the agent direction.

postuple[int, int] or ndarray[int]

Return the agent’s (x, y) position.

terminatedbool or ndarray[bool]

Return whether the agent has terminated.

carryingWorldObj or None or ndarray[object]

Return the object the agent is carrying.

TYPE = 0#
COLOR = 1#
DIR = 2#
ENCODING = slice(0, 3, None)#
POS = slice(3, 5, None)#
TERMINATED = 5#
CARRYING = slice(6, 9, None)#
dim = 9#
static __new__(cls, *dims: int)[source]#
Parameters:
dimsint, optional

Shape of vectorized agent state

property color: Color | ndarray[np.str]#

Return the agent color.

property dir: Direction | ndarray[np.int]#

Return the agent direction.

property pos: tuple[int, int] | ndarray[np.int]#

Return the agent’s (x, y) position.

property terminated: bool | ndarray[np.bool]#

Return whether the agent has terminated.

property carrying: WorldObj | None | ndarray[np.object]#

Return the object the agent is carrying.