tuple

OPL keyword to cluster data

Purpose

OPL keyword to cluster together closely related data.

context
Model files (.mod)

Syntax

TypeDeclaration: . "tuple" Id '{' TupleComponents '}'

Description

Data structures in OPL can be constructed using tuples that cluster together closely related data.

Example

tuple Point {
  int x; 
  int y; 
}

Point p = <2,3>; 

// This direct initialization is currently not supported in .mod
//Point point[i in 1..3] = [<1,2>, <2,3>, <3,4>];
Point point[i in 1..3] = <i,i+1>;

{Point} points = {<1,2>, <2,3>}; 

tuple Rectangle {
   Point ll; 
   Point ur; 
}

Rectangle aRectangle= <p,<1,2>>;

assert p.x==2;
assert card(points)==2;
assert sum(i in 1..3) point[i].x==6;
assert aRectangle.ll==p;

This extract declares a tuple Point consisting of two fields x and y of type integer. Once a tuple type T has been declared, tuples, arrays of tuples, sets of tuples of type T, and tuples of tuples can be declared, as in:


Point p = <2,3>; 

Point point[1..3] = [<1,2>, <2,3>, <3,4>];

{Point} points = {<1,2>, <2,3>}; 

tuple Rectangle {
   Point ll; 
   Point ur; 
}

These declarations respectively declare a point, an array of three points, a set of two points, and a tuple type where the fields are points.