AnyLogic
Expand
Font size

Function

AnyLogic enables defining your own functions. Function will return the value of an expression each time the user calls it from the model. Functions are helpful when you need to re-use the same function in multiple places in your model. Since you write functions in Java, you have all the advantages of this language, such as conditional and cyclic operators, branches, and more.

AnyLogic also supports a special type of functions — table functions. A table function is a function defined in the tabular form. You can make it continuous by interpolating and/or extrapolating. You may need table functions to define a complex non-linear relationship which cannot be described as a composition of standard functions, or to bring experimental data defined as a table function to a continuous mode.

To define a function

  1. Drag the  Function element from the  Agent palette onto the graphical diagram of agent (or experiment).
  2. In the Properties view, type the name of the function in the Name box.
  3. If your function only performs some actions but does not return any result of the calculations, specify that this function returns nothing by choosing Just action (returns nothing) from the group of buttons.
  4. Otherwise, if you need to obtain the result of calculations, select Returns value. Unlike many other simulation tools, in AnyLogic user-defined function can return a value of absolutely any Java class.
  5. You can choose one of the most-used types (boolean, int, double, String) using the corresponding choice from the Type drop-down list. However, if you need to define a function returning the value of some other Java class, choose Other option and type the required class name in the edit box to the right.
  6. Open the Function body section of the function properties and type the function expression in the field. The final line of the expression should be the return expression:
    return <expression that returns the value of specified return type>;

Properties

General

Name — The name of the function. The name is used to identify and access the function.

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

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

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

Just action (returns nothing) — If selected, the function just performs some actions and returns nothing.

Returns value — If selected, the function returns the result of calculations. You can specify the type of the function’s return value in the Type field below.

Type — [Visible if the option Returns value is selected] Here you can specify the type of the function’s return value. A function can return a value of some most-used types (boolean, int, double, String), or a value of some Other Java class (in this case specify the Java class name in the field to the right).

Arguments

Here you can define some optional arguments for your function to enable passing some additional input data necessary for function calculations.

Each row of the table specifies one particular argument. To remove an argument, select the corresponding row in the table and click the “cross” button. Rearrange arguments in the table using the “arrow” buttons.

Function body

Type here the function body. If the function returns some value, the final line should be the return expression.

Advanced

Static — [Only visible if Library developer mode is enabled] If selected, the function is static. A static function does not need creating any instances of the agent type, in whose code it is defined. Static function defined in the MyClass agent type can be accessed from any place of the model as MyClass.function().

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

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

System dynamics units — If selected, you will be able to specify measurement units for the value returned by this function in the edit box to the right. Having specified units for elements of your model, you may perform unit checking to find out dimension inconsistencies in the model.

Function arguments

You may define some optional arguments for your function to enable passing some additional input data necessary for function calculations.

To define arguments of the function

  1. Select the function in the graphical editor.
  2. On the Arguments section of the Properties view, define parameters in the function arguments table.
  3. Each row of the table specifies one particular parameter.
  4. In the Name cell of the table, type the parameter name.
  5. In the Type cell of the table, specify the type of the parameter. Click in the cell and choose the required type from the drop-down list or type in the cell.

If you have defined any parameters for the function, you must pass parameter values any time you call the function. The values must be provided in the order they are defined in the function arguments table.

Since the order of parameters in the table is significant, AnyLogic enables changing the parameter order just moving them up or down using the buttons and . To remove a parameter, select the parameter row in the table and click the button.

Calling functions

To call a function, write the function name followed by parentheses. If the function has arguments, you need to pass the argument values separated by commas inside the parentheses. The values must be provided in the order they are defined in the function arguments table in the Arguments section of the function properties.

Examples:

stopObject();

moveTo(15, 27);

Functions access scope

By default, a function can be called from any place of your model. You can restrict the access scope for the function by changing the function’s access type. Choose the required access type using the Access drop-down list in the function’s properties Advanced section.

Adjusting the agent creation process with functions

AnyLogic enables you to modify the model execution flow with custom code during the creation phase of each agent (see the model execution scheme for reference).

To provide a code override, you need to define a custom function as described above and give it one of the following names:

  • onBeforeCreate() function of an agent will be executed before the start of the creation phase of this agent
  • onCreate() function of an agent will be executed after the completion of the creation phase of this agent
Any code you supply within these functions will overwrite the default code executed by the AnyLogic engine, so use it with caution.
How can we improve this article?