Bases: AgentSet, Generic[A]
Extended agent set specifically designed for managing Actor collections.
ActorsList extends Mesa's AgentSet with ABSESpy-specific functionality, providing enhanced batch operations on actor collections. It focuses on returning numpy arrays for efficient numerical operations and maintaining compatibility with other ABSESpy components.
The class provides methods for filtering, grouping, updating attributes in batch, and performing vectorized operations on all actors in the collection. It serves as the primary return type for queries that retrieve multiple actors, such as container selections and breed-based lookups.
Key features: - Numpy array returns for numerical operations - Batch attribute updates with validation - Grouping by breed or custom attributes - Integration with ABSESpy's random number generation - Type-safe operations through generic typing
Attributes:
| Name | Type | Description |
|---|---|---|
_model |
The ABSESpy model this list belongs to. |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
MainModelProtocol
|
The ABSESpy model this list belongs to. |
required |
objs
|
Iterable[A]
|
Iterable of actors to include in the list. Defaults to empty. |
()
|
Source code in abses/agents/sequences.py
linked_agents
property
¶
Get the agents from the list.
This property returns agents based on the list content: - If the list contains all cells, returns all agents located on those cells - If the list contains all actors, returns the cells where the actors are located - If the list is mixed, raises an error
Returns:
| Type | Description |
|---|---|
ActorsList[A]
|
ActorsList containing the relevant agents (from cells) or cells (from actors). |
Raises:
| Type | Description |
|---|---|
ABSESpyError
|
If the list contains both cells and actors (mixed). |
Example
# From cells -> agents on those cells
cells = model.nature.grid.cells_lst.select(lambda c: c.wealth > 100)
agents_on_rich_cells = cells.linked_agents # All agents on these cells
# From actors -> cells where these actors are
agents = model.agents.select(lambda a: a.wealth > 100)
cells_where_agents_are = agents.linked_agents # Cells where these agents are located
random
property
writable
¶
返回一个 ListRandom 实例,用于随机操作。
Returns:
| Name | Type | Description |
|---|---|---|
ListRandom |
ListRandom
|
用于随机操作的实例,使用与 AgentSet 相同的随机数生成器。 |
is_cells
property
¶
is_actors
property
¶
is_mixed
property
¶
Check if this list contains both cells and actors.
Returns:
| Type | Description |
|---|---|
bool
|
True if the list contains both PatchCell and Actor instances. |
to_dict ¶
Convert all actors to a dictionary grouped by breed.
This method groups actors by their breed attribute and returns a dictionary where keys are breed names and values are ActorsList instances containing actors of that breed. This is useful for operations that need to process different actor types separately.
Returns:
| Type | Description |
|---|---|
Dict[str, ActorsList[A]]
|
Dictionary mapping breed names (str) to ActorsList containing actors |
Dict[str, ActorsList[A]]
|
of that breed. |
Example
Source code in abses/agents/sequences.py
select ¶
Select actors from the list based on filter criteria.
This method provides flexible filtering with support for callable functions, dictionaries of attribute-value pairs, or attribute names. It extends Mesa's select method to return ActorsList instances and support additional filter formats.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filter_func
|
Callable[[A], bool] | None
|
Filter criteria. Can be: - A callable taking an agent and returning bool - A dictionary of {attribute: value} pairs for matching - A string attribute name (selects where attribute is truthy) - None to select all agents |
None
|
at_most
|
int | float
|
Maximum number of agents to select. Can be an integer or a fraction (0-1) of the current list size. |
float('inf')
|
inplace
|
bool
|
If True, modifies current list; otherwise returns new list. Defaults to False. |
False
|
agent_type
|
Agent | None
|
Optional agent type to filter by. |
None
|
Returns:
| Type | Description |
|---|---|
ActorsList[A]
|
ActorsList containing the selected actors. |
Example
Source code in abses/agents/sequences.py
better ¶
Select actors with a metric value better than a threshold.
This method filters actors based on a numerical metric, selecting those with values greater than the specified threshold. If no threshold is provided, returns actors with the maximum metric value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metric
|
str
|
Name of the attribute to compare. |
required |
than
|
Optional[Union[Number, A]]
|
Threshold value. Can be a number, an actor (in which case the actor's metric value is used), or None (returns actors with maximum metric value). |
None
|
Returns:
| Type | Description |
|---|---|
ActorsList[A]
|
ActorsList containing actors with metric values greater than the threshold, |
ActorsList[A]
|
or actors with the maximum metric value if than is None. |
Example
Source code in abses/agents/sequences.py
update ¶
Update the specified attribute of each agent in the sequence with the corresponding value in the given iterable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attr
|
str
|
The name of the attribute to update. |
required |
values
|
Iterable[Any]
|
An iterable of values to update the attribute with. Must be the same length as the sequence. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the length of the values iterable does not match the length of the sequence. |
Source code in abses/agents/sequences.py
split ¶
Split actors into N+1 groups at specified positions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
where
|
NDArray[Any]
|
Array of indices where splits should occur. |
required |
Returns:
| Type | Description |
|---|---|
List[ActorsList[A]]
|
List of ActorsList instances, one for each split group. |
Example
Source code in abses/agents/sequences.py
array ¶
将所有 actor 的指定属性转换为 numpy 数组。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attr
|
str
|
要转换为 numpy 数组的属性名称。 |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
包含所有 actor 指定属性的 numpy 数组。 |
apply ¶
trigger ¶
item ¶
Get a single actor from the list.
This method provides convenient access to a single actor from the list using different selection strategies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
how
|
HOW_TO_SELECT
|
Selection method. Options: - 'item': Get actor at specified index (default) - 'only': Get the only actor, raise error if list doesn't contain exactly one |
'item'
|
index
|
int
|
Index of actor to retrieve when how='item'. Defaults to 0. |
0
|
Returns:
| Type | Description |
|---|---|
Optional[A]
|
The selected actor, or None if index is out of range (for 'item' method). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If how is not a valid selection method or if 'only' method is used but list doesn't contain exactly one actor. |