Set expressions

Describes the use of set expressions in OPL.

Set data can be initialized by set expressions, as mentioned in Data types. This section describes how these expressions are constructed and what functions are defined over sets.

Construction of set expressions

Set expressions are constructed from previously defined sets and the set operations union, inter, diff, and symdiff. For instance:


{int} s1 = {1,2,3}; 
{int} s2 = {1,4,5}; 
{int} i = s1 inter s2; 
{int} u = s1 union s2; 
{int} d = s1 diff s2; 
{int} sd = s1 symdiff s2; 

initializes i to {1}, u to {1,2,3,4,5}, d to {2,3}, and sd to {2,3,4,5}. In addition, set expressions can be constructed from ranges. For instance, the excerpt


{int} s = asSet(1..10);

initializes s to the finite set {1,2,...,10}

Important:

The range 1..10 takes constant space, while the set s takes space proportional to the number of elements in the range.

Functions for sets

Table 1 shows the functions available over sets. These methods apply to all sets, including sets of tuples and other complex sets. In this section, we assume that:
  • S is the set 3, 6, 7, 9

  • item is an element of S

  • n is an integer number

Table 1. Functions over sets 
Function Description
card card(S) returns the size of S, that is, the number of items.
ord

ord(S,item) returns the position of item in S. Positions start at 0 and ord(S,item) produces an execution error if item is not in S.

Example: ord(S,6) evaluates to 1 and ord(S,9) to 3.

The order of items in an explicit set is by order of appearance in the initialization and is implementation-dependent when the sets are the results of a set operation.

first first(S) returns the first item in S, 3 in this example.
item

item(S,n) returns the n-th item in set S. Counting starts from 0. This is equivalent to next(first(S),n)

Example: item(S,1) = 6

last last(S) returns the last item in S, 9 in this example.
next

next(S,item) returns the item in S that comes after item and produces an execution error if item is the last item.

Example: next(S,3) = 6

next(S,item,n) returns the n-th next item. next(S,item) is equivalent to next(S,item,1).

nextc

A circular version of next. nextC(S,item) returns the first item in S if item is the last item.

Example: nextc(S,9) = 3

nextc(S,item,n) returns the n-th circular next item. nextc(S,item) is equivalent to nextc(S,item,1).

prev

prev(S,item) returns the item in S that comes before item and produces an execution error if item is the first item.

Example: prev(S,6) = 3

prev(S,item,n) returns the n-th previous item. prev(S,item) is equivalent to prev(S,item,1).

prevc

A circular version of prev. prevc(S,item) returns the last item in S if item is the first item.

Example: prev(S,3) = 9

prevc(S,item,n) returns the n-th circular previous item. prevc(S,item) is equivalent to prevc(S,item,1).