Skip to content

Types

Define the types of the core, such as targets of laws and agent protocols.

AgentViewProtocol

Bases: Protocol

A protocol of an agent as the agent rules of an interaction see it.

An agent rule is a Python function rule(agent, system) that runs for each member of a group at each step, and the agent it receives gives its id, its group, its place, its neighbours, and its values, and lets the rule move it, remove it, or create new agents, such as agent.move_to(cell). Values are plain attributes, such as agent.x, and a primed value is read with agent.get_value("x'"). It describes a trader in a market, an animal in a flock, and a voter in a network.

Read more in the user guide, under Agents.

See Also

SystemViewProtocol: The system as agent rules see it. UpdateLaw: Runs an agent rule.

Examples:

>>> from skcomplex.core.components.types import AgentViewProtocol
>>> hasattr(AgentViewProtocol, 'move_to'), hasattr(AgentViewProtocol, 'get_neighbors')
(True, True)

alive property

Whether the agent has not been removed.

group property

Path of the group of the agent.

group_label property

Label of the agent's group, the last part of its path.

id property

Unique id of the agent.

location property

Grid coordinates, network node, or position of the agent.

location_key property

Location of the agent as a key.

It equals the coordinates neighbor_cells gives for its cell or node.

create(group=None, location=None, **values)

Create an agent of a group.

By default it is the agent's own group at the agent's location.

Source code in src/skcomplex/core/components/types.py
126
127
128
129
130
def create(self, group: str | None = None, location: object = None, **values: object) -> AgentViewProtocol:
    """Create an agent of a group.

    By default it is the agent's own group at the agent's location.
    """

get_agents_here(group=None)

Return the other agents of the agent's cell or node.

They are of every group or of the group of a path.

Source code in src/skcomplex/core/components/types.py
84
85
86
87
88
def get_agents_here(self, group: str | None = None) -> Sequence[AgentViewProtocol]:
    """Return the other agents of the agent's cell or node.

    They are of every group or of the group of a path.
    """

get_displacement_to(other)

Return the vector from the agent to another agent or a location.

On a torus it is the shortest one.

Source code in src/skcomplex/core/components/types.py
90
91
92
93
94
def get_displacement_to(self, other: AgentViewProtocol | object) -> npt.NDArray[np.float64]:
    """Return the vector from the agent to another agent or a location.

    On a torus it is the shortest one.
    """

get_distance_to(other)

Return the distance to another agent or a location.

On a network it is the number of hops.

Source code in src/skcomplex/core/components/types.py
 96
 97
 98
 99
100
def get_distance_to(self, other: AgentViewProtocol | object) -> float:
    """Return the distance to another agent or a location.

    On a network it is the number of hops.
    """

get_lags(state, window=None)

Return the past values of a state of the agent at the ends of the last steps.

The last one comes last.

Source code in src/skcomplex/core/components/types.py
138
139
140
141
142
def get_lags(self, state: str, window: int | None = None) -> npt.NDArray[np.float64]:
    """Return the past values of a state of the agent at the ends of the last steps.

    The last one comes last.
    """

get_neighbor_cells()

Return the coordinates of the cells or nodes around the agent's cell or node.

They are hashable as location keys.

Source code in src/skcomplex/core/components/types.py
78
79
80
81
82
def get_neighbor_cells(self) -> list[object]:
    """Return the coordinates of the cells or nodes around the agent's cell or node.

    They are hashable as location keys.
    """

get_neighbors(radius=None, layer=None, group=None, over=None)

Return the other agents in the neighbouring cells or nodes, or nearby in space.

They are of every group or of the group of a path.

Source code in src/skcomplex/core/components/types.py
102
103
104
105
106
107
108
109
110
111
112
def get_neighbors(
    self,
    radius: float | None = None,
    layer: str | None = None,
    group: str | None = None,
    over: DelaunayNeighborhood | None = None,
) -> Sequence[AgentViewProtocol]:
    """Return the other agents in the neighbouring cells or nodes, or nearby in space.

    They are of every group or of the group of a path.
    """

get_referenced(state)

Return the member of another group that a reference state of the agent names.

The result is None when there is none.

Source code in src/skcomplex/core/components/types.py
132
133
134
135
136
def get_referenced(self, state: str) -> AgentViewProtocol | None:
    """Return the member of another group that a reference state of the agent names.

    The result is `None` when there is none.
    """

get_regions_here(layer)

Return the indices of the regions of a continuous space layer with the agent.

Source code in src/skcomplex/core/components/types.py
114
115
def get_regions_here(self, layer: str) -> list[int]:
    """Return the indices of the regions of a continuous space layer with the agent."""

get_value(name)

Return a value of the agent by the name users read, such as x'.

Source code in src/skcomplex/core/components/types.py
144
145
def get_value(self, name: str) -> object:
    """Return a value of the agent by the name users read, such as `x'`."""

move_to(location)

Move the agent to a cell, a node, or a position.

It can also move to a uniform point inside a polygon.

Source code in src/skcomplex/core/components/types.py
117
118
119
120
121
def move_to(self, location: object) -> None:
    """Move the agent to a cell, a node, or a position.

    It can also move to a uniform point inside a polygon.
    """

remove()

Remove the agent from the system.

Source code in src/skcomplex/core/components/types.py
123
124
def remove(self) -> None:
    """Remove the agent from the system."""

set_value(name, value)

Set a state of the agent by the name users read, such as x'.

Source code in src/skcomplex/core/components/types.py
147
148
def set_value(self, name: str, value: object) -> None:
    """Set a state of the agent by the name users read, such as `x'`."""

SystemViewProtocol

Bases: Protocol

A protocol of a system as the agent rules of an interaction see it.

An agent rule is a Python function rule(agent, system), and the system it receives gives the agents of every group, the random numbers of the run, a storage that lasts the whole run, the parameters, the time, and the fields of a space, such as system.get_agents('sheep') or system.rng.random(). It describes the market of the traders, the pasture of a flock, and the network of the voters.

Read more in the user guide, under Agents.

See Also

AgentViewProtocol: An agent as agent rules see it. UpdateLaw: Runs an agent rule.

Examples:

>>> from skcomplex.core.components.types import SystemViewProtocol
>>> hasattr(SystemViewProtocol, 'get_agents'), hasattr(SystemViewProtocol, 'rng')
(True, True)

dimensions property

Extent of the agent space per axis.

It counts cells of a lattice or lengths of a continuous space, and is None otherwise.

inputs property

Values of the states of other partitions at the last synchronisation point.

They are keyed by dotted name.

parameters property

Current values of every parameter by dotted name.

rng property

Random number generator of the system.

round property

Index of the current round of a repeated phase, 0 outside repeated phases.

storage property

Mutable mapping private to the owner of the rule.

It persists across the steps of one run.

time property

Time of the system.

It includes the fraction of the step that the visits of a phase reached.

torus property

Whether the agent space wraps around at its edges.

count(name)

Return the number of agents of a group, a role of the owner or a path.

Source code in src/skcomplex/core/components/types.py
237
238
def count(self, name: str) -> int:
    """Return the number of agents of a group, a role of the owner or a path."""

create(group, location=None, **values)

Create an agent of a group, a role of the owner or a path.

It is created at a location, or by default at a random place.

Source code in src/skcomplex/core/components/types.py
261
262
263
264
265
def create(self, group: str, location: object = None, **values: object) -> AgentViewProtocol:
    """Create an agent of a group, a role of the owner or a path.

    It is created at a location, or by default at a random place.
    """

deposit(name, cell, amount)

Add an amount to a field state at a cell or node.

The state is given by path or as <role>.<state>.

Source code in src/skcomplex/core/components/types.py
255
256
257
258
259
def deposit(self, name: str, cell: object, amount: float) -> None:
    """Add an amount to a field state at a cell or node.

    The state is given by path or as `<role>.<state>`.
    """

draw(name, distribution, **arguments)

Return a value drawn by name from a distribution, which runs record and replay.

The distribution is 'uniform' with low and high, 'normal' with mean and deviation, 'bernoulli' with p, 'categorical' with weights, or a scipy.stats distribution with its keywords.

Source code in src/skcomplex/core/components/types.py
176
177
178
179
180
181
def draw(self, name: str, distribution: str, **arguments: npt.ArrayLike) -> float:
    """Return a value drawn by name from a distribution, which runs record and replay.

    The distribution is `'uniform'` with `low` and `high`, `'normal'` with `mean` and `deviation`, `'bernoulli'`
    with `p`, `'categorical'` with `weights`, or a `scipy.stats` distribution with its keywords.
    """

get_agents(name)

Return the agents of a group bound to a role of the owner or of a group by path.

Source code in src/skcomplex/core/components/types.py
190
191
def get_agents(self, name: str) -> Sequence[AgentViewProtocol]:
    """Return the agents of a group bound to a role of the owner or of a group by path."""

get_composition(name, state)

Return the number of agents by value of a state.

The agents are those of a group, a role of the owner or a path.

Source code in src/skcomplex/core/components/types.py
280
281
282
283
284
def get_composition(self, name: str, state: str) -> dict[Hashable, int]:
    """Return the number of agents by value of a state.

    The agents are those of a group, a role of the owner or a path.
    """

get_field(name, cell)

Return the value of a field state at a cell or node.

The state is given by path or as <role>.<state>.

Source code in src/skcomplex/core/components/types.py
249
250
251
252
253
def get_field(self, name: str, cell: object) -> float:
    """Return the value of a field state at a cell or node.

    The state is given by path or as `<role>.<state>`.
    """

get_groups(name)

Return the paths of the groups a role of the owner binds, or of a group by path.

Source code in src/skcomplex/core/components/types.py
204
205
def get_groups(self, name: str) -> list[str]:
    """Return the paths of the groups a role of the owner binds, or of a group by path."""

get_history(name, window=None)

Return the past values of a reporter or of an observable.

The values of an observable are those at the end of every earlier step.

Source code in src/skcomplex/core/components/types.py
274
275
276
277
278
def get_history(self, name: str, window: int | None = None) -> npt.NDArray[np.float64]:
    """Return the past values of a reporter or of an observable.

    The values of an observable are those at the end of every earlier step.
    """

get_observable(name)

Return the current value of an observable of the system.

An example is an aggregate of a group.

Source code in src/skcomplex/core/components/types.py
243
244
245
246
247
def get_observable(self, name: str) -> object:
    """Return the current value of an observable of the system.

    An example is an aggregate of a group.
    """

get_occupancy(name)

Return the number of agents by the key of their location.

The agents are those of a group, a role of the owner or a path.

Source code in src/skcomplex/core/components/types.py
286
287
288
289
290
def get_occupancy(self, name: str) -> dict[Hashable, int]:
    """Return the number of agents by the key of their location.

    The agents are those of a group, a role of the owner or a path.
    """

get_parameter(name)

Return the current value of a parameter of the owner.

Source code in src/skcomplex/core/components/types.py
213
214
def get_parameter(self, name: str) -> float:
    """Return the current value of a parameter of the owner."""

get_random_empty_cell()

Return the coordinates of a random empty cell of the grid or network.

Source code in src/skcomplex/core/components/types.py
240
241
def get_random_empty_cell(self) -> object:
    """Return the coordinates of a random empty cell of the grid or network."""

get_region(layer, index)

Return the polygon of a region of a layer of the continuous space.

Source code in src/skcomplex/core/components/types.py
210
211
def get_region(self, layer: str, index: int) -> Polygon:
    """Return the polygon of a region of a layer of the continuous space."""

get_regions(layer)

Return the number of regions of a layer of the continuous space.

Source code in src/skcomplex/core/components/types.py
207
208
def get_regions(self, layer: str) -> int:
    """Return the number of regions of a layer of the continuous space."""

report(name, value)

Publish a value of the owner as the reporter <owner>.<name>.

Source code in src/skcomplex/core/components/types.py
292
293
def report(self, name: str, value: float) -> None:
    """Publish a value of the owner as the reporter `<owner>.<name>`."""

set_parameter(name, value)

Set a parameter of any group or interaction during the run.

The change is recorded in the trajectory.

Source code in src/skcomplex/core/components/types.py
220
221
222
223
224
def set_parameter(self, name: str, value: float) -> None:
    """Set a parameter of any group or interaction during the run.

    The change is recorded in the trajectory.
    """