Skip to main content
BaseAgent is the base class for all agent implementations in Foundation. Agent instances are stateful objects that capture location, inventory, escrow, endogenous variables, and any extra state fields added by components. They also provide a simple API for getting and setting actions across registered action subspaces.

Constructor

int | str
default:"0"
Index that uniquely identifies this agent within its environment. Integer indices are used for mobile agents; the planner always uses "p".
bool
default:"False"
If True, the agent takes one action per registered action subspace each timestep. If False, the agent takes a single action from the combined action space each timestep.

Class attribute

str
required
Unique string identifier for the agent class. Must be set on each concrete subclass and registered in agent_registry.

Properties

idx

The unique index for this agent within its environment. Mobile agents use integer indices (0, 1, 2, …); the planner uses the string "p".

state

The agent’s full state dictionary. Initialized with the following keys:
list[int, int]
[row, col] position in the 2D world grid. Present on BasicMobileAgent; removed on BasicPlanner.
dict
Resource quantities held by the agent, keyed by resource name. E.g. {"Wood": 3, "Stone": 20, "Coin": 1002.83}.
dict
Resource quantities reserved for pending obligations, keyed by resource name. Shares the same keys as inventory.
dict
Internal state quantities (e.g. {"Labor": 30.25}). Only observable by the agent itself.
Components extend agent.state with additional fields via BaseComponent.get_additional_state_fields.

loc

Shorthand for agent.state["loc"]. The agent’s [row, col] position in the world.
BasicPlanner.loc raises AttributeError because planner agents have no physical location.

inventory

Shorthand for agent.state["inventory"]. A dictionary mapping resource names to quantities.

escrow

Shorthand for agent.state["escrow"]. Resources placed into escrow are reserved and cannot be used for other purposes until released. Shares keys with inventory. See the ContinuousDoubleAuction component for an example of escrow usage: when an agent lists a sell order, the resource moves from inventory to escrow; on a match it transfers to the buyer.

endogenous

Shorthand for agent.state["endogenous"]. Quantities that represent the agent’s internal state, such as accumulated labor effort.

action_spaces

  • Single-action mode (multi_action_mode=False): returns a single integer equal to the total number of available actions (including the universal NO-OP).
  • Multi-action mode (multi_action_mode=True): returns an integer array whose i-th element is the size of the i-th action subspace (each subspace includes its own NO-OP).

Inventory management

Agents track resources across two pools — inventory and escrow — which share the same set of resource keys.

inventory_to_escrow

Moves up to amount of resource from inventory into escrow. The transfer is capped at the available inventory quantity.
str
required
Resource name, e.g. "Wood" or "Coin".
float
required
Amount to move. Must be non-negative.
float
Actual amount transferred: min(inventory[resource], amount).

escrow_to_inventory

Moves up to amount of resource from escrow back into inventory. Capped at the available escrow quantity.
str
required
Resource name.
float
required
Amount to move. Must be non-negative.
float
Actual amount transferred: min(escrow[resource], amount).

total_endowment

Returns the combined inventory[resource] + escrow[resource].
str
required
Resource name.

Action API

get_component_action

Returns the action taken for the named component, or None if the agent does not use that component.
str
required
The component name, e.g. "Build" or "ContinuousDoubleAuction".
str
For components with multiple action subspaces, the subspace name.

set_component_action

Sets the action buffer for component_name to action.
str
required
Component name. Must be registered as a subaction for this agent.
int | np.ndarray
required
Action index or array of action indices.

reset_actions

Resets all action buffers to NO-OP (index 0). If component is given, resets only that component’s action(s).

parse_actions

Parses an actions array or dict and fills each component’s action buffer. Used by the environment to apply actions each step.
  • Multi-action mode: actions is an array of length n_unique_actions.
  • Single-action mode: actions is an integer index into the combined action space, or a {component_name: action} dict.

has_component

Returns True if component_name is registered as an action subspace for this agent.

flatten_masks

Converts a {component_name: mask_array} dictionary into a single flat mask vector compatible with action_spaces.

get_random_action

Selects a random component and returns a random non-NO-OP action for it. Useful for testing.

populate_random_actions

Fills all action buffers with random values. Useful for testing.

Registration methods

These methods are called by the environment during construction and should not be called directly.

Concrete subclasses

BasicMobileAgent

Represents an individual actor in the economic simulation. Mobile agents can move around the 2D world grid. Their state includes loc, inventory, escrow, and endogenous. Indexed with integers starting at 0.

BasicPlanner

Represents a social planner that sets macroeconomic policy. Differs from BasicMobileAgent in two ways:
  1. No location: state["loc"] is deleted on construction. Accessing planner.loc raises AttributeError.
  2. Fixed index: Always indexed as "p" regardless of the idx argument passed to the constructor.
There is expected to be exactly one planner per environment. To support multiple planners, create a separate subclass.

Defining a custom agent

An agent class registered with @agent_registry.add is only visible in foundation.agents if the file defining it is imported in ai_economist/foundation/agents/__init__.py.