AnyLogic
Expand
Font size

Modifying variables and parameters at runtime

You can not only view but also modify values of variables and parameters at model runtime, interactively affecting the model behavior. The changes are applied instantly after they were made.

AnyLogic provides several ways of changing values of parameters and variables:

There are some common rules for all these ways:

  • When you modify a parameter that is linked to a parameter of some embedded object, the value of embedded object’s parameter also changes (with the exception of the case when you modify the parameter from code using not the special function set_parameter(10), but simple assignment operator parameter = 10).
  • When you modify a variable that is connected to another variable declared as external (i.e. dependent), the value of that variable also changes.

Modifying parameters and variables using inspect windows

The simplest way of modifying values of parameters and variables at model runtime is to use inspect windows. Inspect windows allow users to modify values of:

  • Parameters of numeric types (double, int, etc.), boolean, String, if they are not dynamic.
  • Variables of numeric types (double, int, etc.), boolean, String, if they are not made constants.
  • Stocks, if they are not arrays.

To modify value of a variable or parameter using inspect window

  1. While running the model, click the parameter/variable icon in the presentation window. The light brown pop-up box — inspect window — will be shown. The window has two modes: one for displaying the current value, another for modifying the value. Stocks also have the third mode for displaying the time plot where you can see how the value of the stock changes over time.

  2. By default the inspect window displays the current value of a parameter/variable. To switch to the edit mode, double click the value itself. The background of the model window will change color and an edit box will appear.

  3. Enter new value in the opened edit box. If it does not correspond to the type of this parameter/variable (e.g. something different from true and false for the boolean parameter), the value will remain unchanged.
  4. When you are finished, press Enter. Your change will be applied instantly.

  5. Finally, you can close the inspect window by clicking the “cross” button in the upper right corner of the window. The button will become visible if you hover the mouse over the inspect window.

Modifying parameters and variables using controls

You can also modify a variable/parameter by associating it with a control and changing the control at model runtime.

The table below lists controls that can be linked to variables and parameters:

Control Can be associated with the variable or parameter of type Comments
Checkbox

boolean When you select the check box, the associated variable/parameter becomes true, when you clear it — false.
Edit box

String, double, int In some cases it may be more convenient to modify variables and parameters of the int type with sliders.
Slider

double, int In some cases it may be more convenient to modify variables and parameters of the double type with edit boxes.
Radio buttons

int The first choice corresponds to value 0, the second — to 1, and so on.
Combo box

String Can be editable or fixed — in the latter case, the choice is limited to a defined set.
List box

String If you choose the Multiple selection option, the list box cannot be linked to variables and parameters.

To modify value of a variable/parameter using a control

  1. Drag the required control from the  Controls palette on the diagram of your agent or experiment.
  2. Go to the control’s properties, select there the checkbox Link to and enter the name of the variable or parameter that you want to modify using this control, in the field to the right.
  3. If you are linking the control to the numeric variable or parameter, you may define the range of valid values in the fields Minimum value and Maximum value.
  4. When you have finished, you can run the model and play with the control, changing the value of the associated variable/parameter.

You can take a look at the simple demo model where the value of a parameter is modified with the slider.

Modifying parameters and variables from code

Either parameters and variables can be accessed from code simply by their names, e.g.: parameter, plainVar.

You can modify their values from code by typing the corresponding lines of code in actions of events and statechart transitions, functions, action parameters of your flowchart objects, etc.

Modifying variables from code

To modify the value of a scalar variable, use the simple assignment operator, e.g. plainVar = 10;

In the case of, say, variable — array of int values (variable plainVar with type int[] and initial value new int[5]), to assign 10 to the first element of this array, you should call the code line plainVar[0]=10;.

You cannot modify values of variables that are declared as constants.

Modifying parameters from code

You can change a parameter from code in the same way as a variable, using the simple assignment operator, e.g. parameter = 10;.

If, however, you want this modification to be propagated down along the parameter dependencies, you need to call the set_parameterName() function (in our case, set_Parameter()) automatically generated by AnyLogic, passing the value you want to assign as a function parameter.

If you need to perform some action on parameter change, you can define a handler function using the On change property of the parameter in the Advanced section. Please note that this code will be called only on set_parameterName() function calls. And the handler is also not executed in the case set_parameterName() function assigns the value equal to the actual parameter value. Here in the code you can use the oldValue variable that stores the old value of the parameter (the value this parameter had before this particular change of the value).

How can we improve this article?