AnyLogic
Expand
Font size

Parameters or variables: what to use?

You can define agent data using parameters and variables.

Parameters

Parameters are usually used for defining some characteristics of the modeled object. They are helpful when agents have the same behavior described in agent type, but differ in some parameter values. Generally, numeric parameters are used, while you can define a parameter of any Java class.

Variables

Agent can contain variables. Variables are generally used to store the results of model simulation or to model some characteristics of agents, changing over time. AnyLogic supports two types of variables – variables and collections.

A variable is a simple variable of an arbitrary scalar type or Java class.

However, you cannot set any formula for a variable. If you need a variable that changes its value according to specified differential equation, please use Stock. Otherwise, if you need a variable that changes its value according to some algebraic formula, please use dynamic variable. You can find both these variables on the System Dynamics palette.

Collections are used for defining data objects that group multiple elements into a single unit. AnyLogic supports the following collections defined in the Java Collections Framework:ArrayList and LinkedList.

What to use: parameters or variables?

There is a clear difference between variables and parameters. A variable represents a model state, and may change during simulation. A parameter is commonly used to describe agents statically. A parameter is normally a constant in a single simulation, and is changed only when you need to adjust your model behavior.

Use a variable instead of a parameter if you need to model some data unit continuously changing over time. Use a parameter instead of a variable if you just need to model some parameter of an agent changed only at particular moments of time.

All parameters and variables are visible and changeable throughout the model execution, so you can simply adjust your model by changing them at runtime.

Examples

To make it easier for us to figure out when to use parameters, let's look at the most illustrative examples of models in which both parameters and variables are found.

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

In the first example model, Field Service, we will examine the EquipmentUnit agent type. Here several agent attributes are defined with parameters: MaintenancePeriod, NormalFailureRate, and others. These parameters define static characteristics, they are initialized once and keep the same values throughout the simulation.

On contrary, TimeLastReplacement and TimeLastMaintenance are service variables. They store timestamps of the last performed equipment replacement and maintenance correspondingly, and change their values each time the replacement or maintenance finishes (this is done in actions of FinishReplacement and FinishMaintenance statechart transitions, see the figure below). Since these data items change their values frequently, it is more convenient to define them as variables.

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

In the Influenza example model, we will examine the Main agent type. Here you can see four parameters and one variable, nSick. While parameters define constant characteristics that we define once on model startup, nSick is the counter of sick people and its value is constantly updated during the simulation.

Another important point is that this model has a Monte Carlo experiment. If you have any complex experiments that vary some data (optimization, parameter variation, calibration, sensitivity analysis, Monte Carlo experiment), you have to define such data items as parameters. Experiments can access and vary only parameters. Parameters appear on experiment interface, while variables not (see the figure below).

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

In the Airline Fleet example model, we will examine the Plane agent type. Here you can see several parameters defining the plane attributes: aircraftNumber, airplaneType, seatsNumber, and others.

There is one more reason why parameters are used here. In this model airplanes are created and initialized by using the data stored in the fleet table of the model's database (it is done in the properties of the fleet agent population, see the figure below). If you have data stored in a database, use parameters since AnyLogic provides extremely easy way of initializing agents within a population with parameters read from a database. This approach is thoroughly described here.

How can we improve this article?