20.7 The Global( ) Function

These advanced functions can be very powerful tools for accomplishing complex calculations and reporting tasks. It is recommended that they be tested in simple models before being used in complex models.

We recommend using the Global/GlobalN functions that are specifically designed to store, retrieve, increment, initialize and export data rather than the generic Global/GlobalN functions.

Function syntax Description

GlobalSet(row; col; value

GlobalNSet(matrix; row; col; value)

Global(row; col; value)

GlobalN(matrix; row; col; value)

To store and retrieve values globally, use Global() and GlobalN() to calculate "value" and set a cell in a matrix equal to the result (also returns the calculated value). Anywhere "value" is currently used in an expression, it can instead be "wrapped" in the Global function in order to save the calculated value to the matrix (for reporting purposes, for example). A value saved to a global matrix can be referenced using the second form of the Global() function; see below.

The entire content of the global matrix can be dynamically saved to a text file using the third form of the function. To output to Excel, a simple Command("Excel";...) syntax allows you to output a matrix to a spreadsheet.

GlobalN() has an extra parameter matrix (integer > 0) which picks a matrix to use/create, while Global() simply uses a single, default matrix (i.e., matrix 0, not matrix 1 from GlobalN). Usually a few matrices at most will be required, but thousands can be created.

The first cell in a matrix is at row=1, column=1. Matrix size increases dynamically as cells are assigned values. Attempting to lookup a cell that is out of range, however, will result in an error. Currently, the maximum total number of cells for all matrices in the tree is 100 million (e.g., 10000x10000, 50x2000000, or 4x5000x5000). Take note of memory usage with very large numbers of matrix cells. If you are exceeding this limit, try saving/dumping a matrix to a file periodically.

If both row and col are specified as 0, the calculated value is instead applied to the entire existing matrix, allowing it to be dynamically reset during calculations. If an argument (including row or col) is an expression, it is evaluated at the node being calculated just as if the expression were not inside the Global() function.

Global matrices are deleted when the tree is closed, and emptied automatically at the start of each analysis.

GlobalGet(row; col)

GlobalNGet(matrix; row; col)

Global(row; col)

GlobalN(matrix; row; col)

Retrieves the value at a specified cell in a global matrix for use in a tree calculation. The first cell in the global matrix is at row=1, column=1. See information on GlobalN syntax, above.

GlobalIncr(row; col; incr)

GlobalNIncr(matrix; row; col; incr)

This syntax is equivalent to: GlobalN(n;row;col; incr + GlobalN(n;row;col)). If "; incr" is omitted, the increment = 1.

Returns the incremented value (fourth argument).

GlobalExport(suffixNo)

GlobalNExpport(matrix; suffixNo)

Global(value)

GlobalN(matrix; value)

 

 

 

 

 

 

 

 

 

GlobalExport(path + "custom_file_name.txt")

GlobalNExport(matrix; path + "custom_file_name.txt")

If suffixNo evaluates to a non-zero number, the matrix is silently saved to a text file (even if a simulation is running). If suffixNo evaluates to zero, no file is created. If suffixNo is less than zero, the matrix is also reported in the Debug pane (if Text-only analysis debugging setting is on). Always returns a value of 1.

Text files are saved in the same directory as the tree, using the tree filename followed by the word "_globals_" and the integer part of suffixNo. A calculation in "c:\trees\mytree.tre" of the function "GlobalN(3; 103)" would save the current matrix #3 to a text file called: "c:\trees\mytree_globals_103.txt"

Command("ExportGlobalMatrixN";n;"ColumnLabels"; "Label 1"; ...) can be used to pre-set column labels.

Excel Module users: The Command("Excel";"ExportGlobalMatrixN";n) function can be used to export a matrix to a spreadsheet instead. See the previous section on the Command() function syntax.

GlobalExport has the option of saving the default matrix to a text file with a custom name in an alternative folder to the current model.

path can be any valid location and would use the folloiwng syntax, replacing the usual "\" with "\\", for example: "X:\\Testing\\DocModels\\Output_files\\"

"custom_file_name.txt" is the file name you want the output to be saved as.

GlobalNExport has the option of saving any matrix, defined by the matrix number, to a text file with a custom name in an alternative folder to the current model. The syntax is the same as the function GlobalExport with the addition of the matrix number.

GlobalInit()

GlobalNInit()

GlobalInitN()

Global()

GlobalN(n)

GlobalN()

Global matrix is emptied; size becomes 0. (Occurs automatically at the start of an analysis, or when the tree is closed. After a simulation/analysis finishes, however, matrices are still available via the above syntax.)

Global() and GlobalN(n) empty only one matrix while GlobalN() and GlobalInitN() empties all created matrices.

It is important to consider where to place Global matrix commands within the model.

If you are running Microsimulation, you can use a tracker modification to execute the global matrix command at any node.

If you are running EV calculations, you can add the global matrix call to an existing expression in a probability expression or a payoff expression. However, be sure to multiply the Global matrix call by zero to prevent the call from changing the value of the expression.

Most of the Global/GlobalN syntax options are used in Special Feature tutorial example models called Demo Global Functions.trex (action-specific Global functions) and Demo Global Function.trex (generic Global functions).

Important Note: During multiprocessor simulations, Global/GlobalN refers to a set of matrices in the main thread. Separate, simultaneously processing threads evaluating a particular expression which modifies a particular matrix n will take turns updating matrix n; each thread does NOT have its own copy (except in the unusual case of a distributed simulation which divides batches among different computers).