# Agents and links

Count and reference agents, and read an agent's links.

Each value is the expression read by agent 0 at t = 1 in a population `Pop` of four agents on a 4 x 4 grid at cells (0, 0), (1, 0), (1, 1) and (3, 3), linked in a ring; attribute `w` is the agent's number plus one, and the grid's patch `food` is the cell's x plus y.

## Typical uses

Use these functions in an agent type's rates, conditions and actions, or in a model expression that reports on a population. `COUNT` is the one most models need: it counts the live agents of a population, or those in a state, such as the infected in an epidemic. `AGENT` and `ALIVE` handle references to single agents. The `LINK` functions read the agents that self is linked to.

## Behaviour and rules

`COUNT("pop", "state")` is a direct lookup and costs the same however many agents there are. With two more arguments it counts the agents whose categorical attribute holds that value. `AGENT("pop", i)` is a reference to agent number i. It is what `entity.agent` holds, and a send takes it. A reference reads an attribute with `AGENT("pop", i).attr`.

`ALIVE(ref)` tells whether the referenced agent still exists. Using a reference to an agent that was removed stops the run with an error, so test `ALIVE` first when agents can die.

The link functions are directed: they follow the links that go out from self. `LINKED` counts them. `LINK_SUM` and `LINK_MEAN` aggregate an attribute over them, and give 0 when there is none. `IS_LINKED` tests one agent. The link functions work only inside an agent's own expressions, where self is defined.

An unknown population name is reported by a check.

## AGENT

`AGENT("pop", i)`: 2 arguments.

a reference to agent i (what entity.agent holds and a send takes); AGENT("pop", i).attr reads its attribute.

Example: `AGENT("Pop", 2).w` gives 3.

Used in the bundled models Random walker (continuous space).

## ALIVE

`ALIVE(ref)`: 1 argument.

whether a stored agent reference (AGENT(...), entity.agent) still names its agent; using one whose agent was removed raises.

Example: `ALIVE(AGENT("Pop", 2))` gives 1.

## COUNT

`COUNT("pop"[, "state"]\[, "attr", "value"])`: 1 to 4 arguments.

a population's live agents, those in a state (O(1)), or those whose categorical attribute holds that category.

Example: `COUNT("Pop", "Red")` gives 4.

Used in the bundled models Nagel-Schreckenberg traffic (ring), Bass diffusion (agents), Machine agent with repair, SIR epidemic (agents) and 1 more.

## IS_LINKED

`IS_LINKED(ref)`: 1 argument.

whether self is linked to that agent (directed: self -> it).

Example: `IS_LINKED(AGENT("Pop", 1))` gives 1.

## LINK_MEAN

`LINK_MEAN("attr"[, "state"])`: 1 to 2 arguments.

attr's mean over self's linked neighbours; 0 if none.

Example: `LINK_MEAN("w")` gives 3.

## LINK_SUM

`LINK_SUM("attr"[, "state"])`: 1 to 2 arguments.

attr summed over self's linked neighbours; 0 if none.

Example: `LINK_SUM("w")` gives 6.

## LINKED

`LINKED("pop"[, "state"])`: 1 to 2 arguments.

how many agents self is linked to (pop is self's own population; directed: its out-links).

Example: `LINKED("Pop")` gives 2.

## See also

- [All function groups and the grammar](index.md), [Distributions](../distributions/index.md)
- Guides that use them: [Agent-based modelling](../../agent-based-modelling.md), [Coming from NetLogo](../../coming-from/netlogo.md), [Coming from AnyLogic](../../coming-from/anylogic.md), [Coming from Mesa](../../coming-from/mesa.md), [How do I combine event flow, system dynamics and agents?](../../goals/hybrid-model.md)
