Sets

Gives a definition of sets, a list of the operations allowed on sets, and a few words on their initialization.

Definition

Sets are non-indexed collections of elements without duplicates.

OPL supports sets of arbitrary types to model data in applications. If T is a type, then {T}, or alternatively setof(T), denotes the type “set of T”. For example, the declaration:


{int} setInt = ...; 
setof(Precedence) precedences = ...; 

declares a set of integers and a set of precedences.

Sets may be ordered, sorted, or reversed. By default, sets are ordered, which means that:

  • Their elements are considered in the order in which they have been created.

  • Functions and operations applied to ordered sets preserve the order.

See Sorted and ordered sets for details.

Operations on sets

The following operations are allowed on sets. See OPL functions in Language Quick Reference for more information about functions. For the functions on sets, the index starts at 0.

Table 1. Operations allowed on sets
Operations Syntax
union, inter, diff, symdiff set = set1 function set2
first, last elt = function(set)
next, prev elt = function(set,elt,int)
nextc, prevc elt = function(set,elt,int)
item elt = function(set,int)
ord int = function(set,elt)

Initializing sets

A set can be initialized in various ways. The simplest way is by listing its values explicitly. For example:


tuple Precedence {
      int before; 
      int after; 
}
{Precedence} precedences = {<1,2>, <1,3>, <3,4>}; 

See Initializing sets for details.

Methods for sets

For advanced users:

The methods for sets can be found in the class IloDiscreteDataCollection, in the ILOG Script Reference Manual.