Font size

Model execution order

To tailor the model to your needs, you often have to modify it with custom code specified in various locations and user-defined functions. This code must be executed at specific stages of the model run to best effect. You can find out when precisely a certain function is called during the experiment by looking at the model execution order.

The simple execution order of the model is as follows:

  1. Instantiation of the experiment
    Initially, AnyLogic creates an instance of the experiment class, runtime engine, and an instance of the ExperimentHost class.
    During this phase, the Initial experiment setup code is executed as well — check the description of your chosen experiment type to learn more about it.
  2. The engine startup
    Immediately after the experiment starts, the Before each experiment run code is executed. Then, the top-level agent of the model is instantiated, and AnyLogic configures its parameters. Values of the parameters are specified in the order in which the parameters are presented in the Parameters preview section of the agent type properties.
    After the parameters are set up, the Before simulation run code is executed. Then, the agent’s create() function is called, dynamic variables are initialized, and the animation is created. The create() function is executed sequentially for all nested agents of the top-level agent.
    At this point, the creation of the model and the animation are basically completed.
    After the create() function, the start() function for each agent is called. Each agent creates and initializes its statecharts (each statechart enters its initial state) and events. Finally, the On startup code is executed, first on the innermost agents and then up the model tree.
    The On startup code of the top-level agent is executed the last: after all agents throughout the entire model have been constructed, connected, and initialized, and before anything else is done. This is a place for some additional initialization and starting agents’ activities such as events.
  3. The model run
    During the model run, the system dynamics models perform all the calculations required to launch the closest discrete event. The discrete event and agent-based models simply execute their first discrete event. Next, the model runs as specified by its logic, advancing the time when needed, launching events, and executing code as specified in the model’s process flow, diagram, properties, event settings, and so on.
  4. Completion
    After the experiment is completed (either as specified in the experiment settings or after the user manually stops it), the top-level agent is destroyed with all nested agents and library blocks. The On destroy code of agents is executed during this phase.
    Finally, when all the model’s agents are destroyed, the engine executes the After simulation run code of the model.

Execution scheme

The following scheme demonstrates the execution flow of AnyLogic models (click to open the expanded version in your default browser):

How can we improve this article?