AnyLogic
Expand
Font size

Event scheduling

Multiple events may take place at the same time during simulation. In this case AnyLogic arranges the events according to the Selection mode for simultaneous events that is set for the current experiment:

  • FIFO - the first scheduled event is the first to run.
  • LIFO - the last scheduled event is the first to run.
  • Random - events occur in random order.

While the Random selection mode is self-explanatory, i.e. the execution order is not regulated, the FIFO and LIFO modes may require clarification, since the order of the events depends on the way AnyLogic initializes the model and processes the schedules of the events.

To better understand FIFO and LIFO selection modes, keep the following in mind:

  • When a cyclic event is triggered, it schedules its next single occurrence only, not a series of them. Scheduling of the next event occurrence is executed when the current event is finished.
  • The order of initializing elements in an agent corresponds to the order, in which they we added to the agent at design-time.
  • Event elements differ from all other design elements in the order of their initialization only. Events contained in an outer (embedding) agent instance are initialized before the events contained in an inner (embedded) agent instance. The initialization order of all other design elements, as opposed to the order of the events, starts from the contained in the embedded agent instances.
  • If your model includes automatically updated data set, its update will be put at the end of the event initialization queue when starting the model. Later on the automatic update of the data set will be performed according to its position in the queue of events.

Let us consider two examples to illustrate the ordering principles. In these examples, each model starts at the T = 0 moment of time and progresses through a series of T = 1, T = 2, etc. moments. Every such moment represents the time (measured in the specified model time units) that has passed since the model launch.

Example A

We will assume that a model has the following conditions:

  • There are two cyclic events: A and B
  • Each event runs every time unit starting with T = 1.

FIFO mode

In the FIFO mode events run in the following order: A, B, A, B, A, B, A, B.

Simulation time Actions Events Schedule
T = 0 A schedules itself for T = 1 T = 1: A

B schedules itself for T = 1

T = 1: A, B
T = 1 A (first queue element) runs and schedules itself for T = 2 T = 1: B
T = 2: A
B runs and schedules itself for T = 2 T = 2: A, B
T = 2 A (first queue element) runs and schedules itself for T = 3 T = 2: B
T = 3: A
B runs and schedules itself for T = 3 T = 3: A, B

LIFO mode

In the LIFO mode events run in the alternating order: B, A, A, B, B, A, A, B.

Simulation time Actions Events Schedule
T = 0 A schedules itself for T = 1 T = 1: A
B schedules itself for T = 1 T = 1: A, B
T = 1 B (last queue element) runs and schedules itself for T = 2 T = 1: A
T = 2: B
A runs and schedules itself for T = 2 T = 2: B, A
T = 2 A (last queue element) runs and schedules itself for T = 3 T = 2: B
T = 3: A
B runs and schedules itself for T = 3 T = 3: A, B

Example B

We will assume that our model has the following conditions:

  • The model has a set of agents: A, B, and C, with A being the Main agent.
  • The Main agent contains two cyclic events (E1 and E2), which run every time period starting with T = 1.
  • An instance of B is embedded inside the A. Agent B contains a single cyclic event E3, which runs every time period starting with T = 2.
  • When the E1 event is executed for the first time, it dynamically creates an instance of the agent C inside the agent A. Agent C contains two cyclic events (E4 and E5), which run every time period starting with T = 2 and T = 3, respectively.
  • The model includes automatically updated data set (AU).

FIFO mode

The events end up running in this order: E5, E3, E4, E1, E2.

Simulation time Actions Events Schedule
T = 0 E1 schedules itself for T = 1 T = 1: E1
E2 schedules itself for T = 1 T = 1: E1, E2
E3 schedules itself for T = 2
T = 1: E1, E2
T = 2: E3
The data set is updated (AU) and schedules its next update for T = 1 T = 1: E1, E2, AU
T = 2: E3
T = 1 E1 (first queue element) runs. It creates the C agent instance, which initializes E4 and E5:
  • E4 schedules itself for T = 2.
  • E5 schedules itself for T = 3.
E1 has finished processing, so it schedules itself for T = 2.
T = 1: E2, AU
T = 2: E3, E4, E1
T = 3: E5
E2 runs and schedules itself for T = 2 T = 1: AU
T = 2: E3, E4, E1, E2
T = 3: E5
The data set is updated (AU) and schedules its next update for the T = 2 T = 2: E3, E4, E1, E2, AU
T = 3: E5
T = 2 E3 runs (first queue element) and schedules itself for T = 3 T = 2: E4, E1, E2, AU
T = 3: E5, E3
E4 runs and schedules itself for T = 3 T = 2: E1, E2, AU
T = 3: E5, E3, E4
E1 runs and schedules itself for T = 3 T = 2: E2, AU
T = 3: E5, E3, E4, E1
E2 runs and schedules itself for T = 3 T = 2: AU
T = 3: E5, E3, E4, E1, E2
The data set is updated (AU) and schedules its next update for the T = 3 T = 3: E5, E3, E4, E1, E2, AU

LIFO Mode

The events end up alternating running in these orders:

  • E3, E2, E4, E1, E5
  • E5, E1, E4, E2, E3
Simulation time Actions Events Schedule
T = 0 E1 schedules itself for T = 1. T = 1: E1
E2 schedules itself for T = 1. T = 1: E1, E2
E3 schedules itself for T = 2. T = 1: E1, E2
T = 2: E3
The data set is updated (AU) and schedules its next update for the T = 1. T = 1: E1, E2, AU
T = 2: E3
T = 1 The data set is updated (AU - last in queue), and schedules its next update for the T = 2. T = 1: E1, E2
T = 2: E3, AU
E2 runs and schedules itself for T = 2. T = 1: E1
T = 2: E3, AU, E2
E1 runs. It creates the C agent instance, which initializes E4 and E5:
  • E4 schedules itself for T = 2.
  • E5 schedules itself for T = 3.
E1 has finished processing, so it schedules itself for T = 2.
T = 2: E3, AU, E2, E4, E1
T = 3: E5
T = 2 E1 runs (last queue element) and schedules itself for T = 3. T = 2: E3, AU, E2, E4
T = 3: E5, E1
E4 runs and schedules itself for T = 3. T = 2: E3, AU, E2
T = 3: E5, E1, E4
E2 runs and schedules itself for T = 3. T = 2: E3, AU
T = 3: E5, E1, E4, E2
The data set is updated, and schedules its next update for T = 3. T = 2: E3
T = 3: E5, E1, E4, E2, AU
E3 runs and schedules itself for T = 3. T = 3: E5, E1, E4, E2, AU, E3
How can we improve this article?