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:

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:

 To modify value of a variable/parameter using inspect window
  1. While running the model, click the parameter/variable icon in the presentation window. The yellow popup box - inspect window - will be shown. The window has three modes: one for displaying the current value, another for modifying the value and the last one for displaying the time plot for this parameter/variable.

  1. By default the inspect window displays the current value of a parameter/variable. To switch to the edit mode, click the "pencil" button in the top part of the inspect window.

  1. Enter new value in the opened edit box. You can see that values are validated and if they do not corrrespond to the type of this parameter/variable (e.g. something different from true and false for the boolean parameter), the warning message is shown in the bottom part of the inspect window.
  1. When you are finished, press Enter. Your change will be applied instantly. 

  1. Finally, you can close the inspect window by clicking the "cross" button () in the upper right corner of the 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 variable/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/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.

Demo model: Slider Linked To Parameter


Modifying parameters and variables programmatically

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

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

Modifying variables programmatically

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 programmatically

You can change a parameter programmatically 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).