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 agent, the value of embedded agent’s parameter also changes.
- When you modify a variable that is connected to another variable declared as external (i.e. dependent), the value of that variable also changes.
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
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.
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.
- 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.
When you are finished, press Enter. Your change will be applied instantly.
- 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.
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|
|boolean||When you select the check box, the associated variable/parameter becomes true, when you clear it — false.|
|String, double, int||In some cases it may be more convenient to modify variables and parameters of the int type with sliders.|
|double, int||In some cases it may be more convenient to modify variables and parameters of the double type with edit boxes.|
|int||The first choice corresponds to value 0, the second — to 1, and so on.|
|String||Can be editable or fixed — in the latter case, the choice is limited to a defined set.|
|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
- Drag the required control from the Controls palette on the diagram of your agent or experiment.
- 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.
- 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.
- 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 Open the model page in AnyLogic Cloud. There, you can run the model or download it (by clicking Model source files).
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.
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), to assign 10 to the first element of this array, you should call the code line plainVar=10;.
You can change a parameter from code using the set_parameterName() function. The modified value will be propagated down along the parameter dependencies. For example, if your parameter is named density, and you want to set its value to 20, call set_density(20). The function is automatically generated by AnyLogic. You have to pass 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?