Variable | AnyLogic Help (2024)

  • Types of variables
    • Creating a variable
  • Properties

Viewing variables at runtimeModifying variables at runtime

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

Collections are used for defining data objects that group multiple elements into a single unit.

Variable is a simple variable of an arbitrary scalar type or Java class. It always has some value assigned. You specify the variable’s initial value in the Initial value property of the variable. If an initial value is not specified, Java rules apply, for example a variable of type double is set to 0.

The variable’s value can be changed during the model simulation using the assignment operator, e.g. you can set integer variable myVariable to be equal to 5 by the following code:

myVariable = 5;

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 variables. You can find both these variables on the System Dynamics palette.

You can declare Java variables in the Additional class code field in the Advanced Java properties section of the agent type. Variables declared in the code can also be accessed within this object, but defining them visually using variables is much more efficient.

Types of variables

Alike other simulation tools AnyLogic supports variables of primitive types: double, integer, boolean. But only AnyLogic gives you infinite possibilities in defining data units by supporting variables of any Java classes.

You can define variables of common Java classes—e.g., a variable of String class to store character strings, as well as of your own classes, defined elsewhere in your model.

To create a variable

  1. Drag the Variable | AnyLogic Help (1)Variable element from the Variable | AnyLogic Help (2)Agent palette to the agent diagram.
  2. In the Properties view, type the name of the variable in the Name edit box. The name is used to identify and access the variable from code.
  3. Specify the type of the variable. You can choose one of the most-used types (int, double, boolean, String) using the corresponding option from the Type buttons group. However, if you need to define a variable of some other Java class, choose Other option and type the required class name in the edit box to the right.
  4. Specify the variable’s initial value in the Initial value edit box. The initial value of the variable can be changed afterwards during the model simulation. If an initial value is not specified, Java rules apply, for example a variable of type double is set to 0.
  5. Finally, if you want to make this variable a constant and prevent it from modifying at model runtime, open the Advanced properties section and select the Constant checkbox. If you set this variable as Constant, you cannot use parameters, stocks, flows, and dynamic variables in the expression specified in the Initial value field.

Properties

General

Name—The name of the variable. The name is used to identify and access the variable from code.

Show name—If selected, the name of the variable is displayed on a presentation diagram.

Ignore—If selected, the variable is excluded from the model.

Visible—If selected, the variable is visible on a presentation at runtime.

Type—The type of the variable. Choose, whether you want variable of some most-used type (int, double, boolean, String), or of some Other Java class using the controls to the right.

Initial value—You can define the initial value for the variable here. The initial value of the variable can be changed afterwards during the model simulation. If an initial value is not specified, Java rules apply, for example a variable of type double is set to 0.
If you set this variable as Constant, you cannot use parameters, stocks, flows, and dynamic variables in the expression specified in the Initial value field. In this case you can use only values and other constants here.

Advanced

Access—The access type of the variable. There are four access types:

  • public—the variable can be accessed from anywhere.
  • private—the variable can be accessed from this agent class only.
  • protected—the variable can be accessed from this agent class and its subclasses.
  • default—the variable can be accessed anywhere within this model.

Static—[Only visible if Library developer mode is enabled] If selected, the variable is static, i.e. it will have the same value for all instances of this class in the model (e.g. for all agents inside the population).

Do not use static variables, if you plan to run iterations of complex experiments (optimization, parameter variation, etc.) in parallel on different processor cores (this is set by the experiment’s advanced option Allow parallel evaluations).
Static variables are not saved into a snapshot file when you save/restore a state of a running model. If you do not alter the value of a static variable in your model, the state of the restored model will be exactly the same as you saved it into the snapshot. However, if you somehow change the value of a static variable (e.g. in agent’s On startup code), the modified value won’t be saved into a snapshot, and the restored state of the model will differ from the state you have previously saved.

Constant—If selected, the variable is made constant, i.e. it has the same value at all times and cannot be changed during the simulation. Making variable a constant, you prevent it from unwanted modifying.

Save in snapshot—[Enabled if Static checkbox is not selected] If selected, the variable will be saved in the model snapshot.

System dynamics units—If selected, you will be able to specify units of measurements for this variable in the edit box below. Having specified units for elements of your model, you may perform unit checking to find out dimension inconsistencies in the model.

How can we improve this article?

Variable | AnyLogic Help (2024)

FAQs

What do you mean by variables? ›

Definition. A variable is any characteristic, number, or quantity that can be measured or counted. A variable may also be called a data item. Age, sex, business income and expenses, country of birth, capital expenditure, class grades, eye colour and vehicle type are examples of variables.

What is the purpose of a variable? ›

A variable is a way to store values. To use a variable, we must both declare it—to let the program know about the variable—and then assign it—to let the program know what value we are storing in the variable.

How does a variable work? ›

Variables are used to store information to be referenced and manipulated in a computer program. They also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. It is helpful to think of variables as containers that hold information.

What is the difference between variable and argument? ›

The argument of a function is a crucial piece of info that may need to be different every time you call the function, whereas the function's local variables will behave (i.e. change or remain unchanged) the same way every time the function is called.

What are the 3 types of variables meaning? ›

Independent Variable. (What I change) Dependent Variables. (What I observe) Controlled Variables.

What's another word for variables? ›

irregular. versatile. protean. hit-or-miss. adaptable.

What is an example of a variable? ›

A variable is a letter, either lowercase or uppercase. For example, in the equation x + 2 = 5, the x is being used as a variable.

What are two reasons for using a variable? ›

Storing a value in a variable allows you to use that value multiple times, easily change what value is used where that variable is used and help explain to other developers, what you are using it for.

What is variable in basic? ›

Visual Basic, just like most programming languages, uses variables for storing values. A variable has a name (the word that you use to refer to the value that the variable contains). A variable also has a data type (which determines the kind of data that the variable can store).

What is the rule of a variable? ›

Rules to Declare a Variable
  • A variable name can consist of Capital letters A-Z, lowercase letters a-z digits 0-9, and two special characters such as _ underscore and $ dollar sign.
  • The first character must not be a digit.
  • Blank spaces cannot be used in variable names.
  • Java keywords cannot be used as variable names.
Jun 15, 2022

What are the two rules of a variable? ›

A variable name cannot start with a digit. A variable name can only contain alpha-numeric characters and underscores ( a-z, A-Z , 0-9 , and _ ) Variable names are case-sensitive (age, Age and AGE are three different variables)

What is variable in psychology? ›

Variables in psychology are things that can be changed or altered, such as a characteristic or value. Variables are generally used in psychology experiments to determine if changes to one thing result in changes to another. Variables in psychology play a critical role in the research process.

Are there 3 types of variables? ›

An experimental inquiry typically has three main types of variables: an independent variable, a dependent variable and controlled variables. We will look at each of these three types of variables and how they are related to experimental inquiries involving plants.

Is A variable an argument? ›

In mathematics, an argument of a function is a value provided to obtain the function's result. It is also called an independent variable.

What is an example of a case vs variable? ›

Example: Study Time & Grades

A teacher wants to know if third grade students who spend more time reading at home get higher homework and exam grades. The students are the cases. There are three variables: amount of time spent reading at home, homework grades, and exam grades.

How do you define variables in a study? ›

A variable in research simply refers to a person, place, thing, or phenomenon that you are trying to measure in some way. The best way to understand the difference between a dependent and independent variable is that the meaning of each is implied by what the words tell us about the variable you are using.

What are the 5 variables? ›

Independent & dependent variables, Active and attribute variables, Continuous, discrete and categorical variable, Extraneous variables and Demographic variables.

What is variables meaning definition and types? ›

A variable is defined as anything that has a quantity or quality that varies. The dependent variable is the variable a researcher is interested in. An independent variable is a variable believed to affect the dependent variable. Confounding variables are defined as interference caused by another variable.

Top Articles
Latest Posts
Article information

Author: Frankie Dare

Last Updated:

Views: 5704

Rating: 4.2 / 5 (73 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Frankie Dare

Birthday: 2000-01-27

Address: Suite 313 45115 Caridad Freeway, Port Barabaraville, MS 66713

Phone: +3769542039359

Job: Sales Manager

Hobby: Baton twirling, Stand-up comedy, Leather crafting, Rugby, tabletop games, Jigsaw puzzles, Air sports

Introduction: My name is Frankie Dare, I am a funny, beautiful, proud, fair, pleasant, cheerful, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.