Initializing tuples

Describes the two ways of initializing tuples.

You initialize tuples either by giving the list of the values of the various fields (see Tuples) or by listing the fields and values. For example:

In the .mod file, you write:

tuple point
{
int x;
int y;
}
point p1=...;
point p2=...;

In the .dat file, you write:

p1=#<y:1,x:2>#;
p2=<2,1>;

As with arrays, the delimiters < and > are replaced by #< and ># and the ordering of the pairs is not important. OPL checks whether all fields are initialized exactly once.

The type of the fields can be arbitrary and the fields can contain arrays and sets.

Example 1: tuple Rectangle

For example, the following code lines declare a tuple with three fields: the first is an integer and the other two are arrays of two points.

tuple Rectangle {
       int id; 
       int x[1..2];
       int y[1..2];
}

Rectangle r = ...; 

execute
{
 writeln(r);   
}

A specific “rectangle” can be declared in the data file as:

r=<1, [0,10], [0,10]>;

Example 2: tuple Precedence

The declaration

tuple Precedence {
      string name; 
     {string} after; 
}

defines a tuple in which the first field is a set item and the second field is a set of values. A possible precedence can be declared as follows:


Precedence p = <a1, {a2, a3, a4, a5}>; 

assuming that a1,..,a5 are strings.

You can also initialize tuples internally within the .mod file. If you choose to do so, you cannot use the named tuple component syntax #<, >#, which is supported in .dat files but not in .mod files. Components may be expressions and will be evaluated during initialization.