Initializing Arrays

Some system dynamics variables of type array (stock variables, parameters and constant flow and dynamic variables) need to be initialized. 

AnyLogic provides easy and flexible tool for defining initial values of array elements. This is done because initialization of multidimensional arrays is nontrivial task, first of all as there is no way of intuitive visual representation of multidimensional data.

As arrays can have unlimited number of dimensions, each one possibly containing a great amount of elements, the sole convenient way of defining initial values of an array is layering this array (i.e. partitioning this array to layers) and successive initialization of each resulting layer.

You define a layer by fixing values in all but two dimensions of an array. When we say fix we mean that you select one particular element in the dimension. Thereby each fixation narrows the list of array dimensions by one dimension. Having finished narrowing dimensions to two ones, you get an ability to represent data of the resulting two-dimensional layer in tabular form. To define initial values for all array elements you need to go over all combinations of elements of non-fixed dimensions.

 To open the editor of array initial values
  1. Select the array in a graphical editor or in the Projects view.
  2. Click the Edit... button in the Properties view.
  3. The editor will be opened. The editor consists of two panels. In the left panel you define a layer, whose elements you want to initialize. The right panel contains a table where you, in fact, define values of elements of the selected array's layer.

To make clear how to initialize array elements, let us illustrate it by a pair of simple examples. 

Initializing all array elements with one value

 To initialize all array elements with one value

  1. Open the editor of array values.
  2. Make sure that in the left table the value [ALL] is selected in all cells of the Elements column.
  3. Remove marks from either  and  columns of this table. 
  4. In the right table, enter the value you want to assign to all elements of this array. 

  1. Click OK when finished.

Initializing array elements with different values

Assume you are creating a model studying population dynamics in LA and NY. Let's conditionally categorize people by three characteristics: gender, age and region. These characteristics are comfortably described using dimensions - enumerations: Gender(male, female), Age(child, teenager, adult, aged) and Region(LA, NY)

Let's study how to initialize three-dimensional array Population[Region, Age, Gender] using our editor of array values:

 To initialize all array elements with one value

  1. Open the editor of array values.
  2. You will see that two dimensions have marks in the left table of the editor: one in the  column and one in the  column. Marking this or that dimension you can choose dimensions, whose elements you will define in the right table. Elements of the dimension, selected in the  column, will be displayed there as rows, while elements of the dimension selected in the  column will be displayed as columns:
  1. If you will start entering values now, they will be assigned to the corresponding elements of all non-fixed dimensions of the array, i.e. if you enter, say, 1000000 in the cell adult - LA, thereby you will initialize both number of adult men and adult women in LA with this value.
  2. If you want to define different initial values for different elements of Gender dimension, you should fix this dimension as we mentioned above.
  3. Let's first initialize the number of men. Thereto select male in the cell Gender - Elements in the left table. Now you can define the number of men in your model in the right table - each cell of the table defines the value of the corresponding element of the population array, e.g. in the cell aged - NY the number of aged men living in NY can be defined.
    Define values of all elements of this array's layer [Region, Age, male]:

  1. Now we should define the number of women in the similar way. This time you should select another element of the fixed dimension, whose elements are not fully initialized yet. Choose female in the cell Gender - Elements and define the number of women of each category in the right table:

  1. Now we have finished initialization of array's layer [Region, Age, female] and therefore the initialization of the whole array (as in this simplest case our array has only two layers since it is three-dimensional and the only dimension we are fixing has only two elements).
  2. Click OK to finish the initialization.

You can check whether you have defined initial values correctly by inspecting variable value at the model runtime:

The resulting array's initialization string is displayed in the text form in the Initial value field (or Default value, in the case of a parameter-array). If you need to initialize some another array with similar values, you can simply copy this string to the analogous property of that array and then modify the required values using the editor of array values.

Related topics

 Stock variables



 Defining an array variable

 Defining equations for array variables

           API reference: HyperArray class