Skip to content

Overview

Agent Interactions

Interactions from params.classes.bond_types.acts_allowed are implemented in standalone files that implement the interface from interactions.BaseInteraction. This allows for the logic related to an interaction type to be consolidated into one place and make incorporating a new interaction type as simple as possible.

To add a new interaction:

  • Add the param to param.classes.bond_types.acts_allowed
  • Add a file to interactions/ which creates a class that is a sub-class of BaseInteraction
    • Implement the methods of BaseInteraction which are needed for this interaction
    • Not all methods are needed for all interactions (see below for details on the methods)
  • Re-export the feature from interactions/__init__.py
  • Add tests in tests/interactions/
  • Add it to the docs in docs/api/interactions/ and to the nav in mkdocs.yml

The TITAN class uses sub-classes of BaseInteraction to initialize the object/call methods as appropriate.

BaseInteraction

Source code in titan/interactions/base_interaction.py
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class BaseInteraction:
    name: str = ""
    """Name of interaction in the params file."""

    @classmethod
    def interact(cls, model: "model.TITAN", rel: "agent.Relationship"):
        """
        Given a model and a relation, have the agents in the relationship interact for a time step.

        args:
            model: The running model
            rel: The relationship where interaction is happening
        """
        num_acts = cls.get_num_acts(model, rel)

        if num_acts < 1:
            return

        for exposure in model.exposures:
            if model.time >= model.params[exposure.name].start_time:
                exposure.expose(model, cls.name, rel, num_acts)

    @classmethod
    def get_num_acts(cls, model: "model.TITAN", rel: "agent.Relationship") -> int:
        return 0

name: str = '' class-attribute instance-attribute

Name of interaction in the params file.

interact(model, rel) classmethod

Given a model and a relation, have the agents in the relationship interact for a time step.

Parameters:

Name Type Description Default
model TITAN

The running model

required
rel Relationship

The relationship where interaction is happening

required
Source code in titan/interactions/base_interaction.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@classmethod
def interact(cls, model: "model.TITAN", rel: "agent.Relationship"):
    """
    Given a model and a relation, have the agents in the relationship interact for a time step.

    args:
        model: The running model
        rel: The relationship where interaction is happening
    """
    num_acts = cls.get_num_acts(model, rel)

    if num_acts < 1:
        return

    for exposure in model.exposures:
        if model.time >= model.params[exposure.name].start_time:
            exposure.expose(model, cls.name, rel, num_acts)