Initializing help system before first use

Finalizing sets and dynamic arrays

The declaration of an array in Mosel has one of these two forms

  1. Explicit declaration as sparse array by using one of the keywords dynamic or hashmap.
  2. `Standard' declaration, resulting in a dense array that is either static (all index sets are known) or not fixed (some or all indexing sets are unknown at the point where the declaration takes place).

If an array is used to represent dense data one should avoid defining it as a sparse array as that uses more memory and is slower than the corresponding dense array.

In many optimization models, dense arrays are created as non-fixed arrays because their contents is initially unknown—but there is no real need to treat them as dynamic structures throughout the whole model as they remain unchanged once they have been initialized.

The automatic finalization mechanism of Mosel therefore transforms such initially dynamic sets/non-fixed arrays as to handle them more efficiently. As an additional advantage, set finalization allows Mosel to check for `out of range' errors that cannot be detected if the sets are allowed to grow dynamically.

  • By default, initializations blocks finalize the sets they initialize and also the index sets of initialized dense arrays.
  • Data of non-dynamic arrays is read before finalization of the index sets in order to create the arrays static.
  • Arrays that are not explicitly declared as sparse arrays are only allocated when they are first accessed: this allows these arrays to be static even if their index sets are finalized after the declaration of the arrays.

So, code like the following example

 declarations
  S: set of string
  A,B: array(S) of real
  x: array(S) of mpvar
 end-declarations

 initializations from "mydata.dat"
  A
 end-initializations

 sum(s in S) B(s)*x(s)

where all arrays are declared as dense arrays that are not fixed (their size is not known at their declaration) but only A that is initialized using a data file really needs to be non-fixed, will be treated by Mosel as if you had written the following

 declarations
  S: set of string
  A: array(S) of real
 end-declarations

 initializations from "mydata.dat"
  A
 end-initializations

 finalize(S)

 declarations
  B: array(S) of real
  x: array(S) of mpvar
 end-declarations

That is, B and x are created as static arrays, making the access to the array entries more efficient.

As a general rule, the following sequence of actions gives better results (in terms of memory consumption and efficiency):

  1. Declare data arrays and sets that are to be initialized from external sources.
  2. Perform initializations of data.
  3. Finalize all related sets.
  4. Declare any other arrays indexed by these sets (including decision variable arrays).

Note: there are several possibilities to stop Mosel from applying automatic finalization to model objects:

  • Declare arrays explicitly as dynamic or hashmap arrays. (See examples in Sections A transport example and Conditional variable creation and create.)
  • Declare sets explicitly as dynamic in which case they cannot be finalized.
  • Use control parameter autofinal to enable/disable automatic finalization locally:
     setparam("autofinal", false)
     initializations from "datafile.dat"
       ...
     end-initializations
     setparam("autofinal", true) 
  • Use option noautofinal to disable automatic finalization globally for the whole model:
    model "modelname"
     options noautofinal

© 2001-2019 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.