Skip to content

How do I let an agent read the agents in the cells around it?

On a grid, neighbours are the cells around you and not the agents within a distance. NEIGHBOUR_SUM and NEIGHBOUR_MEAN reduce an attribute over them. The script puts three houses in a row and reads the heat of the houses next to each.

from nagasim import build as nb

# Three houses stand side by side in one row of a 6 x 6 grid. A house's heat is its x position at the
# cell centre (1.5, 2.5, 3.5). Each tick it adds up and averages the heat of the houses in the eight
# cells around it. Neighbours are cells, not distances, and the agent's own cell is never counted.
m = nb.Model(time_limit=3)
m.add_agent_type(
    name="House", initial_state="On", states=[{"name": "On"}],
    attributes=[{"name": "heat", "init": "self.x"}, {"name": "around", "init": "0"},
                {"name": "mean_around", "init": "0"}],
    space={"kind": "grid", "size": [6, 6], "torus": False, "neighbourhood": "moore"},
    tick={"dt": 1},
    on_tick=[{"target": "self.around", "expr": 'NEIGHBOUR_SUM("Street", "heat")'},
             {"target": "self.mean_around", "expr": 'NEIGHBOUR_MEAN("Street", "heat")'}])
m.add_population(name="Street", type="House", count=3,
                 placement={"kind": "table", "points": [[1, 2], [2, 2], [3, 2]]})

attributes = m.run(seed=1)["agent_attributes"]
everyone = attributes[attributes["State"] == "All"]
print(everyone[["Attribute", "End Min", "End Max"]].to_string(index=False))

Save it as recipe-neighbour-grid.py and run it with python. The script seeds itself, so the numbers below are the same on your machine.

  Attribute  End Min  End Max
       heat      1.5      3.5
     around      2.5      5.0
mean_around      2.5      2.5

How it works

  1. NEIGHBOUR_SUM(pop, attr) and NEIGHBOUR_MEAN(pop, attr) use the eight Moore cells, or the four von Neumann cells if you ask for them.
  2. The middle house has two neighbours, 1.5 and 3.5, so its sum is 5.0; each end house has one, so their sums are 2.5.
  3. The agent's own cell is never counted, and an empty neighbourhood gives 0.

Elements used

Back to all recipes.