Internal and external initialization

Defines these two kinds of data initialization.

In OPL, you can initialize data internally or externally. Your choice affects memory allocation. See Initialization and memory allocation for details.

Internally

This initialization mode consists in initializing the data in the model file at the same time as it is declared. Inline initializations can contain expressions to initialize data items, such as


int a[1..5] = [b+1, b+2, b+3, b+4, b+5];

Note:

If you choose to initialize data within a model file, you will get an error message if you try to access it later by means of a scripting statement such as:

myData.myArray_inMod[1] = 2;

Externally

This initialization mode consists in specifying initialization subsequently as an OPL statement in a separate .dat file (see OPL Syntax in the Language Quick Reference). This includes reading from a spreadsheet as explained in Spreadsheet Input/Output.

You declare external data using the ellipsis syntax. However, data initialization instructions cannot contain expressions, since they are intended to specify data. Data initialization instructions make it possible to specify sets of tuples in very compact ways. Consider these types in a .mod file:

{string} Product ={"flour", "wheat", "sugar"};
{string} City ={"Providence", "Boston", "Mansfield"};
tuple Ship {
string orig;
string dest;
string p;
}
{Ship} shipData = ...;

and assume that the set of shipments is initialized externally in a separate .dat file like this:

shipData =
{
<"Providence", "Boston", "wheat">
<"Providence", "Boston", "flour">
<"Providence", "Boston", "sugar">
<"Providence", "Boston", "wheat">
<"Providence", "Mansfield", "wheat">
<"Providence", "Mansfield", "flour">
<"Boston", "Providence", "sugar">
<"Boston", "Providence", "flour">
};
Note:

In .dat files, the separating comma is optional. For strings without any special characters, even the enclosing quotes are optional.