# Agents in space

Neighbours, distances and patches of an agent on a grid or in a plane.

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 when agents live in space and react to what is around them: flocking birds, a segregation model, grazing animals, cars on a ring road. Neighbour counts and sums read the cells around an agent on a grid. The `NEAR` family reads agents within a distance. `PATCH` reads the value of the grid cell the agent stands on.

## Behaviour and rules

The grid functions `NEIGHBOURS`, `NEIGHBOUR_SUM`, `NEIGHBOUR_MEAN`, `CELL_COUNT` and `PATCH` need a grid space, or the check reports an error. `NEIGHBOURS` counts the agents in the cells around self and leaves out self's own cell. `CELL_COUNT` counts self's own cell and includes self. `PATCH` and `CELL_COUNT` give 0 off a bounded grid.

`NEAR` counts the agents of a population within a distance `r` of self, and the distance itself is included. It leaves self out when the population is self's own. Distances are in the space's own units. `NEAREST` is the distance to the closest other agent and is infinite if there is none. `KNN_SUM` and `KNN_MEAN` take the k nearest agents, with ties broken by the lower index.

An optional last argument filters by state first. Sums, means and counts give 0 when no agent qualifies. The maximum, the minimum and the heading give NaN. `NEAR_HEADING` is the circular mean heading and needs a continuous space whose agents have a heading.

## CELL_COUNT

`CELL_COUNT("pop"[, dx, dy])`: 1 to 3 arguments.

pop's agents in self's grid cell (or an offset from it), self included; 0 off a bounded grid.

Example: `CELL_COUNT("Pop")` gives 1.

Used in the bundled models Nagel-Schreckenberg traffic (ring), Wolf-Sheep predation with grass (grid).

## KNN_MEAN

`KNN_MEAN("pop", "attr", k[, "state"])`: 3 to 4 arguments.

attr's mean over the k nearest agents of pop to self (state-filtered first, ties by ascending index); 0 if none.

Example: `KNN_MEAN("Pop", "w", 2)` gives 2.5.

## KNN_SUM

`KNN_SUM("pop", "attr", k[, "state"])`: 3 to 4 arguments.

attr summed over the k nearest agents of pop to self (state-filtered first, ties by ascending index); 0 if none.

Example: `KNN_SUM("Pop", "w", 2)` gives 5.

## NEAR

`NEAR("pop", r[, "state"])`: 2 to 3 arguments.

how many of pop's agents are within r of self (inclusive), self excluded when pop is self's own population.

Example: `NEAR("Pop", 1.5)` gives 2.

Used in the bundled models Boids flock (continuous, heading).

## NEAR_HEADING

`NEAR_HEADING("pop", r[, "state"])`: 2 to 3 arguments.

the circular mean heading of the agents NEAR counts; NaN if none.

Example: `NEAR_HEADING("Pop", 1.5)`. No value is shown: it needs a continuous space whose agents have headings.

Used in the bundled models Boids flock (continuous, heading).

## NEAR_MAX

`NEAR_MAX("pop", "attr", r[, "state"])`: 3 to 4 arguments.

attr's maximum over the agents NEAR counts; NaN if none.

Example: `NEAR_MAX("Pop", "w", 1.5)` gives 3.

## NEAR_MEAN

`NEAR_MEAN("pop", "attr", r[, "state"])`: 3 to 4 arguments.

attr's mean over the agents NEAR counts; 0 if none.

Example: `NEAR_MEAN("Pop", "w", 1.5)` gives 2.5.

## NEAR_MIN

`NEAR_MIN("pop", "attr", r[, "state"])`: 3 to 4 arguments.

attr's minimum over the agents NEAR counts; NaN if none.

Example: `NEAR_MIN("Pop", "w", 1.5)` gives 2.

## NEAR_SUM

`NEAR_SUM("pop", "attr", r[, "state"])`: 3 to 4 arguments.

attr summed over the agents NEAR counts; 0 if none.

Example: `NEAR_SUM("Pop", "w", 1.5)` gives 5.

## NEAREST

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

distance to the closest OTHER agent of pop (inf if there is none).

Example: `NEAREST("Pop")` gives 1.

## NEIGHBOUR_MEAN

`NEIGHBOUR_MEAN("pop", "attr"[, "state"])`: 2 to 3 arguments.

attr's mean over the agents NEIGHBOURS counts; 0 if none.

Example: `NEIGHBOUR_MEAN("Pop", "w")` gives 2.5.

## NEIGHBOUR_SUM

`NEIGHBOUR_SUM("pop", "attr"[, "state"])`: 2 to 3 arguments.

attr summed over the agents NEIGHBOURS counts; 0 if none.

Example: `NEIGHBOUR_SUM("Pop", "w")` gives 5.

## NEIGHBOURS

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

pop's agents in the Moore/von Neumann cells around self's (own cell excluded).

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

Used in the bundled models Schelling segregation (grid).

## PATCH

`PATCH("name"[, dx, dy])`: 1 to 3 arguments.

the patch value at self's grid cell (or an offset from it); 0 off a bounded grid.

Example: `PATCH("food")` gives 0.

Used in the bundled models Sugarscape with regrowth (grid, patches), Wolf-Sheep predation with grass (grid).

## 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), [How do I let an agent read the agents in the cells around it?](../../how-to/recipes/neighbour-grid.md)
