AnyLogic
Expand
Font size

Agent synchronization

AnyLogic supports synchronous, asynchronous, and mixed modeling. Asynchronous modeling means truly continuous time axis and ability to schedule events at arbitrary time moments and execute continuous processes such as System Dynamics ones. Synchronous modeling assumes discrete time steps and agents (and maybe environment) executing their actions synchronously at these time steps. While in most cases asynchronous models are closer to reality and also computationally more efficient (no "empty" steps are done), sometimes the problem is best solved with synchronized model (this is true for many academic models and for those real-world systems where decisions are made regularly at discrete time points, e.g. every day or every month).

You can implement synchronization "manually" or you may use AnyLogic services provided by environment and Agent constructs. The simplest manual implementation would be this: at the Main agent (or other agent container) you create a cyclic timeout event with timeout 1 and the following action code:

for( Person p : people )
    p.step();

where Person is the agent type of your agents, people is its replicated instance in Main, and step() is the function of the Person type.

Alternatively you can utilize AnyLogic built-in support for agent synchronization. Assume you have your agent type Person, defined environment at Main and specified that the agent population people belongs to the environment.

To add synchronization to your agent based model

  1. Open Properties of the agent playing a role of environment for other agents (e.g. Main).
  2. Open the Space and network properties section.
  3. Check the checkbox Enable steps. The default step duration is 1 time unit but may be changed (use the Step duration field)
  4. In the On before step and On after step fields of the same section write actions that you wish be executed before and after all agents perform their individual steps, if any.
  5. In the Agent actions section of the Person agent type properties specify the On before step and On step actions (obviously, you may leave these fields empty).

The following execution order is guaranteed at the time moments 0, 1, 2, 3, ...:

  1. On before step action of the environment is executed
  2. On before step actions of all agents are executed in some deterministic order
  3. On step actions of all agents are executed in same deterministic order
  4. On after step action of the environment is executed

Typically, the two phases of environment action are used to prepare the model for a step and to wrap up a step, e.g. update statistics. The two phases of agent action are used to collect information and to make and apply the decisions. For example, in the well-known Game of Life every cell would count the alive neighbor cells in On before step and change its state in On step. You can find the corresponding code in the given example model, in the Agent actions section of the properties of the Cell agent type.

Demo model: The Game of Life Open the model page in AnyLogic Cloud. There you can run the model or download it (by clicking Model source files).

By adding such synchronization you do not restrict the dynamics of your model with discrete time steps. Your agents as well as other objects are still able to schedule events, execute state transitions and run other processes — this will go in parallel with steps execution.

How can we improve this article?