# 2.2.4: Interactions

We have defined agents and the environment they live in, but on their own these are just isolated pieces. The question this page answers is how the agents actually affect one another and the world around them, because it is interactions that turn a set of agents into a system and drive the emergence we care about. Interactions come in two broad kinds: agent-agent interactions (one agent affecting another, by talking, trading, or competing) and agent-environment interactions (an agent sensing, moving through, or leaving a mark in the world). We look at each kind in turn, first the concept and then a worked example, before bringing the two together at the end.

We begin with the first kind: how one agent affects another.

## 2.2.4.1: Agent-agent interactions

When agents affect one another, whether by talking, trading, competing, or simply bumping into each other, they interact, and it is these interactions that turn a collection of individuals into a system. This is where emergence comes from, meaning that patterns at the level of the whole are built out of many small exchanges between agents, so the interactions deserve as much care as the agents themselves. Interactions differ not only in their form, such as talking or trading, but in their pattern, meaning who reaches whom, and that pattern strongly shapes what the group as a whole can produce. In the panel below, first look at the forms a direct interaction can take, then click through the patterns and watch how the shape of the connections, from a single pair up to a whole network, changes how far one exchange can reach.

```{raw} html
:file: ../learning_activities_and_widgets/widgets/widget_2_2_4a_how_agents_interact.html
```

The lesson to take away is that interactions are what link agents into a system, and both their form and their pattern, who reaches whom, help decide what emerges at the level of the whole.

## 2.2.4.2: Example: agent-agent interaction

The following pseudocode gives a basic example of how two agents might interact in a simple negotiation:

```
FUNCTION agent_interaction(agent1, agent2):
    // Agent 1 offers a high-five
    IF agent1 offers high-five to agent2 THEN
        // Agent 2 decides whether to high-five back
        IF agent2 considers agent1 a friend THEN
            agent2 gives high-five back to agent1
            SET agent1.mood to "happy"
            SET agent2.mood to "happy"
            "High-five exchanged!"
        ELSE
            "High-five ignored"
    
    ELSE
     "No interaction"
END FUNCTION
```

This simple interaction demonstrates:
- Basic social interaction between agents
- Conditional responses based on relationships
- Simple state changes (mood updates)


One rule between two agents is easy to read on its own, but the point of ABM is what happens when the whole room runs it. The pseudocode above is one agent-agent rule; the activity below sets many agents following it at once, together with a simple agent-environment rule (the weather), which is the second kind of interaction we cover in the next subsection. No one sets the mood of the room, yet a collective mood emerges.

```{raw} html
:file: ../learning_activities_and_widgets/learning_activities/activity_2_2_4b_high_five_hall.html
```

The lesson to carry away is that the mood of the room is set by no one in particular; it emerges from many small interactions, agent to agent and agent to environment, each following a simple rule.

## 2.2.4.3: Example: one-to-many interaction, guiding a crowd

The high-five hall showed many pairwise interactions adding up into a mood no one set on purpose. Interactions can also be *one-to-many*, where a single agent influences many others at once, the "leader signalling to a crowd" broadcast pattern from the interaction types above. The activity below makes that pattern concrete. A crowd is caught in an unfamiliar building where nobody knows the way out, so at first each person can only wander. Then you step in as a guide who knows where the exit is and shares that knowledge with whoever is nearby. You never carry anyone out yourself; you only broadcast what you know, and the crowd does the rest.

```{raw} html
:file: ../learning_activities_and_widgets/learning_activities/activity_2_2_4c_guide_them_out.html
```

Notice what changed between the two rounds. The building, the crowd, and the exit were identical; the only new ingredient was one informed agent passing what it knew to its neighbours. That single one-to-many interaction reorganised the whole evacuation, which is why an informed agent, a marshal, a clear sign, or a knowledgeable neighbour, can matter so much in a real crowd.

## 2.2.4.4: Agent-environment interactions

Agents interact with their environment whenever they read information from it, move through it, or change something that later actions can observe. This creates a feedback loop: environmental conditions shape an agent's decision, the decision alters the environment, and the changed environment then affects the same or other agents. A path can become crowded, a resource can be depleted, or a signal can be left behind even when agents never communicate directly.

The distinction matters because an environment is not merely where the model is drawn. It can store state and mediate interaction across space and time. To specify an agent-environment rule clearly, state what the agent can sense, over what range, what action it may take, and whether that action changes a shared condition immediately or only at the end of the step. These possibilities fall into four recurring forms:

::::{tab-set}

:::{tab-item} Perception
- Reading environmental state and conditions
- Sensing local information (temperature, resources, obstacles)
- Detecting other agents within sensing range
:::

:::{tab-item} Movement
- Changing spatial position within the environment
- Navigation and pathfinding
- Response to environmental gradients or signals
:::

:::{tab-item} Modification
- Altering environmental properties or resources
- Depositing or consuming materials
- Creating or destroying environmental features
:::

:::{tab-item} Information exchange
- Leaving traces or signals for other agents
- Reading information left by previous agents
- Communication through environmental markers
:::

::::

```{figure} ../figures/fig_2_9_model_interactions.png
:name: fig-model-interactions
:align: center

**Figure 2.7** Example of a model with interactions to both agents and the environment itself.
```

Across all four, the shared idea is that the agent treats the environment as something it can read from and write to, rather than as a fixed backdrop.


## 2.2.4.5: Example: agent-environment interaction

The following pseudocode revisits the mood rule already at work in the High-five hall. Reading the weather is the agent-environment part of that rule, but the weather is only a small nudge: an agent that keeps missing its friends can grow glum even while the sun shines. The environment shapes an agent's mood; it does not dictate it.

```
CLASS Agent:
    PROPERTIES:
        mood: 0.5                      // 0 = glum, 1 = happy
        steps_since_friend: 0          // ticks since we last met a friend
    
    FUNCTION update_mood(environment, nearby_friends):
        // 1. The weather is only a small nudge, not the whole story
        
        IF weather is "sunny" THEN
            mood = mood + small_amount
        ELSE
            mood = mood - small_amount

        // 2. Meeting a friend is what really lifts the mood
        IF a friend is nearby THEN
            mood = mood + large_amount
            steps_since_friend = 0
        ELSE
            steps_since_friend = steps_since_friend + 1

        // 3. Going too long without company drags the mood down,
        //    so mood can fall even on a sunny day
        IF steps_since_friend > threshold THEN
            mood = mood - loneliness_amount
    END FUNCTION
END CLASS
```

Reacting to the weather is a direct, one-way reading of the environment. Agents also interact with the environment in a second, indirect way: by leaving traces in it and reading traces left by others. Here each agent drops a scent trail as it moves and steers towards nearby scent. No agent plans a route, yet trails and paths appear on their own.

```{raw} html
:file: ../learning_activities_and_widgets/learning_activities/activity_2_2_4d_trail_maker.html
```

The lesson to take away is that agents can coordinate without any of them planning a route, by writing to and reading from the environment. This indirect coordination through shared traces is called stigmergy, and it is how real ants build their trails.

## 2.2.4.6: Putting the two kinds together

Interactions in a model fall into two broad kinds: those between one agent and another, such as talking, trading, or competing, and those between an agent and its environment, such as sensing it, moving through it, or leaving a mark in it. Having now seen both at work, we can name the distinction directly. Telling them apart is worth doing because the two kinds are built differently and often work together, and both feed into the patterns a model produces. In the activity below, label each interaction as agent to agent or agent to environment, and notice that the deciding question is simply whether the other party is another decision-maker or the world itself.

```{raw} html
:file: ../learning_activities_and_widgets/learning_activities/activity_2_2_4e_agent_agent_or_agent_environment.html
```

The lesson to remember is that agents interact both with each other and with the world around them, and a full model usually needs both kinds, because together they shape what emerges.

```{important}
**Key points about interactions:**
- **Direct vs indirect**: Agents can interact face-to-face or through environmental mediation
- **Bidirectional**: Interactions typically affect both/all participating agents
- **Range-limited**: Most interactions occur within a limited spatial or social range
- **Rule-based**: Interaction outcomes follow programmed rules and conditions
- **Emergent patterns**: Simple interaction rules can lead to complex system behaviours
- **Environment as mediator**: Environment often facilitates or constrains interactions
```

With interactions in place, we have now assembled all of the elements an agent-based model needs. The next section brings them together into a complete model and shows how emergence arises from the whole.

## Key takeaways

```{admonition} Key takeaways
:class: important

- Interactions are what turn a collection of agents into a system, and they occur both between agents and between agents and their environment.
- Agent-agent interactions transmit information, influence, or resources between individuals, while agent-environment interactions allow agents to sense and modify their surroundings.
- Most models combine both kinds of interaction, and it is their combination that gives rise to emergent, system-level behaviour.
```
