Defining Equations for Array Variables

There is a number of ways you can define the equation (or equations) for an array stock, flow or dynamic variable

Defining the same equation for all array elements

In the simplest case when the equation is the same for all elements of the array, you need only one equation field, where you type the expression, possibly referencing the dimensions of the array variable. For example, if the stock Population is array with dimensions:

Region = {NORTH, SOUTH} and Gender = { MALE, FEMALE } the equation may look like:

d(Population[Gender, Region])/dt = Births[Gender, Region]-Deaths[Gender, Region]

 To set the same equation for all array elements

  1. Select the array variable in the graphical editor and go to the Properties view.
  2. By default the stock's formula is generated automatically according to flows flowing and out of this stock, it is so called classic equation mode, for more details please refer here. The value of inflows i.e. flows that increase stock value, are added and the value of outflows, i.e. flows that decrease the stock are subtracted from the current value of the stock. In the figure below you can see the auto-generated equation formed by AnyLogic for the stock Population from our example, see the disabled field d(Population [Region,Gender])/dt=
  1. If anyhow you need to overcome the limitations of classic system dynamics formula of the stock and want to edit it by yourself, select the Custom option from the Equation mode group of buttons and specify the formula in the d(stock_name)/dt field. 


  1. If in our example Births are the same for MALE and FEMALE and differ only by region (so that variable Births is the array with one dimension Region), you may write:


The equation above is mapped to a very simple loop construct looking like:

for( r : Region )
    for( g : Gender )
        Population[ r, g ] += Births[ r ] – Deaths[ r, g ]

If birth rate is the same even in different regions, you can use a scalar variable Births in the same equation:


Defining different equations for different element sets of an array

There are cases however when equations are different for different elements of the array or different element sets. Suppose in the population example above people do intensively migrate out of NORTH region, but migration out of SOUTH region is negligible. Then there will be two equations for the stock Population:

d(Population[Gender, SOUTH])/dt = Births[SOUTH] - Deaths[Gender, SOUTH]

d(Population[Gender, NORTH])/dt = Births[NORTH] - Deaths[Gender, NORTH] – OutMigration

 To define several equations for different sub-arrays of an array variable

  1. Select the variable in a graphical editor or in the Projects view.
  2. Go to the Properties view.
  3. Specify the list of subscripts identifying element(s) of the array that should get the value calculated by the equation you specify. By default, all array dimensions are selected. You should modify this dimension list by specifying some particular elements for those enumerations that are presented in the sub-array by a subdimension or (in case of enumerations) an individual element. For such dimensions, click on the dimension name in the {...} to the right of the Array check box and choose the subdimension or individual element from the drop-down list.
  4. To define the first formula of the example described above, you should do the following: 

  1. Enter the formula calculating the initial value for the defined sub-array in the d(<array_name>)/dt = edit box:

  1. If you need to define one more formula initializing another sub-array, click the Add formula button and define new formula in the same way.
  2. For the described example, you need to define the second equation describing the population of north region habitants:

  1. If needed, you can remove formulas any time you like. To remove a formula, click the  button to the right of the formula.
In the equation sets like above you can also refer to element subsets by using subdimensions. For example, if there is dimension Income = { POOR, MIDDLECLASS, WEALTHY } and its subdimension AllButPoor = { MIDDLECLASS, WEALTHY } you can write for a three-dimensional array Population:

d( Population[ Region, Gender, POOR ] )/dt =
    Births[ Region ] - Deaths[ Region, Gender, POOR ] – OutMigration

d( Population[ Region, Gender, AllButPoor ] )/dt =
    Births[ Region ] - Deaths[ Region, Gender, AllButPoor ]

Referring to next, previous or arbitrary other indexes of elements

In some cases you need to refer to a different index of an array in the equation. Suppose you are modeling an ageing chain and your stock Population is an array with one dimension Age = { 0 .. 99 }. The inflow for an element of the array is the outflow of the element with the previous index for all element but 0, and for the element 0 the inflow is Births. To implement this you may define two subdimensions of Age: Age0 = { 0 } and AgesAllBut0 = { 1 .. 99 } and write:

d( Population[ Age0 ] )/dt =
    Births - Deaths[ Age0 ] – Ageing[ Age0 ]

d( Population[ AgesAllBut0 ] )/dt =
    Ageing[ AgesAllBut0 – 1 ] - Deaths[ AgesAllBut0 ] – Ageing[ AgesAllBut0 ]

To understand how it works it makes sense to map this into loop construct:

for( a : 0 }
    Population[ a ] += Births - Deaths[ a ] – Ageing[ a ]

for( a : 1..99 }
    Population[ a ] += Ageing[ a-1 ] - Deaths[ a ] – Ageing[ a ]

As you can see, the dimension names are simply substituted with loop indexes in the equations. This gives you a high degree of flexibility when you define equations. You can define the dependency of element with a certain index on the element of same of another array having arbitrary other index. 


Related topics

 Arrays

 Dimensions

 Defining an array variable

 Initializing an array variable

           API Reference: HyperArray Class