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}
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
Sis the set3, 6, 7, 9itemis an element ofSnis an integer number
| Function | Description |
|---|---|
card |
card(S) returns the size of S,
that is, the number of items. |
ord |
Example: 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 |
Example: item(S,1) = 6 |
last |
last(S) returns the last item in S, 9 in
this example. |
next |
Example:
|
nextc |
A circular version of Example:
|
prev |
Example:
|
prevc |
A circular version of Example:
|