Runtime functions with arguments

On the one hand AIMMS functions and procedures use local identifiers to hold the values of the arguments.

On the other hand the model editor functions that form the basis of runtime libraries operate on elements of AllIdentifiers; these function create, modify, and delete corresponding global identifiers in the model. In other words, these functions do not operate on local identifiers.

This short how-to presents a way to add local identifiers to AIMMS procedures and functions that are created at runtime. To create local identifiers, including procedure and function arguments, first create a global declaration section, and, when finished, move the declaration section below the procedure or function being created.

To show what happens:

For instance, to create a variant of Sqrt, do the following steps:

  1. Create the function, including specifications for the attributes body and arguments.

  2. Create a declaration section with a parameter a. The parameter a gets property input.

    The model explorer for the runtime library now looks as follows.

  3. A call to me::move to move the declaration section below the function looks as follows:

    1 ! Now the trick: move the declaration section local to the function.
    2 if not me::Move(
    3         runtimeId :  ep_sqrtDeclLocalFunction,
    4         parentId  :  ep_sqrtFunction,
    5         pos       :  0) then
    6     raise error "Unable to move declaration section local to function: " + CurrentErrorMessage ;
    7 endif ;

    The runtime library in the model explorer then looks as follows:



    The elements in AllIdentifiers that used to reference the declaration section and the arguments, are no longer valid and should not be used after this statement.

  4. Finish the creation by calling the AIMMS compiler using me::compile, the runtime library in model explorer looks as follows:


    Note that the model explorer now shows the argument of the function.

  5. Subsequently, we can use the APPLY operator to call the function created.

    p_res := apply( ep_sqrtFunction, p_inp );

You can download AIMMS 4.79 example project here

See also:

  1. Runtime Libraries and the Model Edit Functions

  2. Instead of using functions, use macros: Use Formulas as Data

    • A call to a macro inside a constraint may involve variables.

    • With functions, assignment statements, if-then-else, while loops, and for loops can be used.