Skip to content

How do I let an agent react to the agents near it?

A vehicle slows down when others are close, or a sensor reads the load of its neighbours. The NEAR functions reduce an attribute over the agents within a radius. The script places three sensors 2 m apart and reads the sum, smallest, largest and mean load within 5 m.

from nagasim import build as nb

# Three sensors sit 2 m apart and each carries a load equal to its x position (10, 12 and 14).
# Every tick each one reads the loads of the others within 5 m: their sum, smallest, largest and mean.
# `self` is left out of the neighbourhood. With nobody in range MIN and MAX are NaN, so guard them
# with an IF on NEAR("Field", 5) > 0 when agents can be alone.
m = nb.Model(time_limit=3)
m.add_agent_type(
    name="Sensor", initial_state="On", states=[{"name": "On"}],
    attributes=[{"name": "load", "init": "self.x"}, {"name": "crowd", "init": "0"},
                {"name": "lightest", "init": "0"}, {"name": "heaviest", "init": "0"},
                {"name": "average", "init": "0"}],
    space={"kind": "continuous", "bounds": [0, 0, 100, 100]},
    tick={"dt": 1},
    on_tick=[{"target": "self.crowd", "expr": 'NEAR_SUM("Field", "load", 5)'},
             {"target": "self.lightest", "expr": 'NEAR_MIN("Field", "load", 5)'},
             {"target": "self.heaviest", "expr": 'NEAR_MAX("Field", "load", 5)'},
             {"target": "self.average", "expr": 'NEAR_MEAN("Field", "load", 5)'}])
m.add_population(name="Field", type="Sensor", count=3,
                 placement={"kind": "table", "points": [[10, 10], [12, 10], [14, 10]]})

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-near-agents.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
     load     10.0     14.0
    crowd     22.0     26.0
 lightest     10.0     12.0
 heaviest     12.0     14.0
  average     11.0     13.0

How it works

  1. NEAR_SUM, NEAR_MIN, NEAR_MAX and NEAR_MEAN take the population, the attribute and the radius, and leave the agent itself out.
  2. The sensors carry loads of 10, 12 and 14, so the sums run from 22 to 26 and the means from 11 to 13.
  3. With nobody in range, MIN and MAX are NaN, so guard them with an IF on NEAR when agents can be alone.

Elements used

Back to all recipes.