AnyLogic
Expand
Font size

Controlling the model execution

When AnyLogic model is run, you can control the model execution using the control panel, displayed in the bottom of AnyLogic model window.

The control panel contains buttons for controlling the execution of the launched model:

Button Command Description
Run [Visible only if the model is not running currently]
Runs the model from the current state. The model will be run until you will stop it manually by clicking the  Pause or  Stop button (or until something in the model will pause or stop its execution).
You can either run the model until the specified moment of model time, or for a specified period of model time starting from the current moment. Please see Running the model until a certain date or for a certain time interval for details.
Pause [Visible only if the model is running currently]
Pauses the running model. You can resume the paused simulation any time.
You can pause the model at the specified moment of model time, please see Running the model until a certain date or for a certain time interval for details.
Stop Terminates the model run.

Run  control becomes disabled when there is no activity in the model. This indicates that your model has finished its work.

You can enable or disable these buttons and check their state (enabled / disabled) from code during the model runtime. The corresponding functions are described in the Control panel section in Accessing the presentation.

Controlling the model execution from code

You may need to control your model execution programmatically. For example, you may need to pause the model on some event, e.g. on event expiry and resume its execution when some statechart transition will be taken. AnyLogic offers a rich API capable of solving all tasks concerned with controlling the model execution.

The table below lists the related functions of the model engine and analogous functions available at each agent.

To call some engine function, write e.g. getEngine().finish() wherever you need in your model.

Alternatively, you can simply call analogous function finishSimulation() from any agent’s activity.

Engine function Agent’s function Short description Full description
boolean start() N/A Starts the model (if it is idle at the moment) and pauses it. Engine command applicable only in IDLE state (in other states does nothing and returns false).
Does the following:
  1. Sets up the start time of the model execution.
  2. Creates whatever is needed in the root object — calls root.create();.
  3. Starts the model (first events get scheduled) — calls root.start();.
  4. Puts the engine into the PAUSED state.
boolean pause() boolean pauseSimulation() Pauses the running model. Puts the engine into PAUSED state and causes the model to terminate after completing the current event execution. Engine command applicable only in RUNNING state (in other states does nothing and returns false). Puts the engine into PLEASE_WAIT state and then sets a flag that, when tested by the engine, causes it to terminate after completing the current event execution. Further behavior depends on context where this function is called:
  • When this function is called from the model execution thread, from control action code, or from on-click code of a shape, it returns true immediately. Model is paused just after current event execution.
  • When this function is called from other locations (e.g. user-defined concurrent thread), it waits for the model execution thread to terminate and returns true.
By the time this function finishes, the engine may be in PAUSED, FINISHED, or ERROR state.
Returns false, if the engine state does not allow pausing.
boolean run() boolean runSimulation() If the model is paused at the moment, runs the model. Puts the engine into RUNNING state and then starts the model execution. Engine command applicable only in PAUSED state (in other states does nothing and returns false). Puts the engine into RUNNING state and then starts the model execution in a separate thread. The execution may discontinue due to the one of the following reasons:
  • there are no more events to execute (state -> FINISHED)
  • stopTime is reached (state -> FINISHED)
  • pause() was called (state -> PAUSED)
  • top level agent has been destroyed (state -> FINISHED)
  • exception occurred during event execution or agent destruction (state -> ERROR)
This function should never be called from the model execution thread!
By the time this function finishes, then engine may already be in PAUSED, FINISHED, or ERROR state.
Returns false if the engine state does not allow running.
boolean stop() boolean stopSimulation() Terminates the model execution, destroys the model and forgets it. Then puts the engine into IDLE state and returns true. Engine command applicable only in any non-IDLE state (in IDLE state does nothing and returns false). If the state is RUNNING, sets a flag that, when tested by the model execution thread, causes it to terminate. Further behavior depends on context where this function is called:
  • When this function is called from the model execution thread, or from control action code, or from on-click code of a shape, it returns true immediately, leaving the model in PLEASE_WAIT state. Model is stopped and destroyed some time later (After Simulation Run code of an experiment or On Destroy code of the top level agent may be used to handle this moment).
    It is more recommended to use finish() function in such situations.
  • When this function is called from other locations (e.g. user-defined concurrent thread), it waits for the model execution thread to terminate and then destroys the model (calls root.onDestroy()) and forgets it. Then puts the engine into IDLE state and returns true.
boolean finish() boolean finishSimulation() Finishes currently running or paused model. Unlike the stop() function, it does not destroy the model, so you can examine and analyze its state. Engine command applicable only in RUNNING or PAUSED state (in other states does nothing and returns false). Sets a flag that, when tested by the engine, causes it to finish after completing the current event execution. Further behavior depends on context where this function is called:
  • When this function is called from the model execution thread, control action code, or from on-click code of a shape, it returns true immediately. Model is finished just after current event execution (After Simulation Run code of an experiment may be used to handle this moment).
  • When this function is called from other locations (e.g. user-defined concurrent thread), it waits for the model execution thread to terminate and returns true.
By the time this function finishes, the engine may be in FINISHED, or ERROR state.
Returns false if the engine state does not allow pausing.
boolean runFast() N/A Runs the model in the fastest possible way. The run is done in virtual time mode regardless any settings. Runs the model in the fastest possible way in the same (calling) thread. The run is done in virtual time mode regardless any settings. The function finishes in one of the following cases:
  • illegal engine state when it is called (returns false, in all other cases — returns true)
  • there are no more events/equations to execute (state -> FINISHED)
  • stopTime is reached (state -> FINISHED)
  • pause() was called (state -> PAUSED)
  • finish() was called (state -> FINISHED)
  • top level agent has been destroyed (state -> FINISHED)
  • exception occurred during model execution (state -> ERROR)
The model is locked for both write and read during the entire run, so you should not try to display presentation of the agents, or execute any concurrent actions that may access the model.
Returns false if the engine state does not allow running.
boolean step() N/A Makes at most one discrete step of the model (can be done from the PAUSED state only). Makes at most one discrete step of the model (can be done from the PAUSED state only). Finishes in either FINISHED, ERROR or PAUSED state.
This function should never be called from the model execution thread!
Returns false if the engine state does not allow for making a step.
Engine.State getState() N/A Returns the current state of the engine (IDLE, PAUSED, RUNNING, FINISHED, ERROR, or PLEASE_WAIT). Returns the current state of the engine:
  • IDLE — no model is set for execution, doing nothing.
  • PAUSED — model is set and have started, ready to run or make a step.
  • RUNNING — in the loop of model execution invoked by run() or runFast().
  • FINISHED — the model execution is finished OK, but the model is not yet destroyed.
  • ERROR — the model execution is finished with error, the model is not yet destroyed.
  • PLEASE_WAIT — in the process of executing an uninterruptible command like pause(), step() or stop().
The state is however not guaranteed as may be modified concurrently.
boolean getRealTimeMode() N/A Returns the current model execution mode. Returns true if the current execution mode is real time, false if virtual time.
void setRealTimeMode(boolean on) N/A Sets the virtual or real time execution mode. Sets the virtual or real time execution mode. In virtual time mode the model is executed as fast as possible. In real time mode the engine tries to maintain the given scale between model time and real time.

on — true means execution in scale to real time.
double getRealTimeScale() N/A Returns the currently set real time scale of model execution. Returns the currently set real time scale of model execution, i.e. the number of model time units to be simulated per real second. The actual scale will depend on factors such as simulation performance, applications running in parallel, etc.
This scale only has effect in real time execution mode.
void setRealTimeScale(double scale) N/A Sets the desired real time scale of model execution. Sets the desired real time scale of model execution, i.e. the number of model time units to be simulated per real second. The actual scale will depend on factors such as simulation performance, applications running in parallel, etc. This scale only has effect in real time execution mode.

scale — the new real time scale.
How can we improve this article?