5. Set variables and constraints¶
This chapter gives an overview over set variables and set constraints in Gecode and serves as a starting point for using set variables. For the reference documentation, see Using integer set variables and constraints.
Overview. Set variables details how set variables can be used for modeling. The sections Constraint overview and Synchronized execution provide an overview of the constraints that are available for set variables in Gecode.
Important
Do not forget to add
#include <gecode/set.hh>
to your program when you want to use set variables. Note that the same conventions hold as in Integer and Boolean variables and constraints.
5.1. Set variables¶
Set variables in Gecode model sets of integers and are instances of the class SetVar.
Representing set domains as intervals. The domain of a set variable is a set of sets of integers (in contrast to a simple set of integers for an integer variable). For example, assume that the domain of the set variable \(x\) is the set of subsets of \(\{1,2,3\}\):
Set variable domains can become very large – the set of subsets of \(\{1,\dots,n\}\) has \(2^n\) elements. Gecode (like most constraint solvers) therefore approximates set variable domains by a set interval \(\left[l..u\right]\) of a lower bound \(l\) and an upper bound \(u\). The interval \(\left[l..u\right]\) denotes the set of sets \(\{s\mid l\subseteq s\subseteq u\}\). The lower bound \(l\) (commonly referred to as greatest lower bound or glb) contains all elements that are known to be included in the set, whereas the upper bound \(u\) (commonly referred to as least upper bound or lub) contains the elements that may be included in the set. As only the two interval bounds are stored, this representation is space-efficient. The domain of \(x\) from the above example can be represented as \(\left[\{\}..\{1,2,3\}\right]\).
Set intervals can only approximate set variable domains. For example, the domain
cannot be captured exactly by an interval. The closest interval would be \(\left[\{\}..\{1,2,3\}\right]\). In order to get a closer approximation of set variable domains, Gecode additionally stores cardinality bounds. We write \(\#\left[i..j\right]\) to express that the cardinality is at least \(i\) and at most \(j\). The set interval bounds \(\left[\{\}..\{1,2,3\}\right]\) together with cardinality bounds \(\#\left[1..2\right]\) represent the above example domain exactly.
Creating a set variable. New set variables are created using a constructor. A new set variable x is created by
SetVar x(home, IntSet::empty, IntSet(1, 3), 1, 2);
This declares a variable x of type SetVar in the space home, creates a new set variable implementation with domain \(\left[\{\}..\{1,2,3\}\right],\#\left[1..2\right]\), and makes x refer to the newly created set variable implementation.
There are several overloaded versions of the constructor, you can for example omit the cardinality bounds if you do not want to restrict the cardinality. You find the full interface in the reference documentation of the class SetVar. An attempt to create a set variable with an empty domain throws an exception of type Set::VariableEmptyDomain.
As for integer and Boolean variables, the default and copy constructors do not create new variable implementations. Instead, the variable does not refer to any variable implementation (default constructor) or to the same variable implementation (copy constructor). For example in
SetVar x(home, IntSet::empty, IntSet(1, 3), 1, 2);
SetVar y(x);
SetVar z;
z=y;
the variables x, y, and z all refer to the same set variable implementation.
Limits for set elements. All set variable bounds are subsets of the universe, defined as
The universe is symmetric: \(-\mbox{\texttt{Set::Limits::min}}=\mbox{\texttt{Set::Limits::max}}\). Furthermore, the cardinality of a set is limited to the unsigned integer interval
The limits have been chosen such that an integer variable can hold the cardinality. This means that the maximal element of a set variable is \(\mathtt{Int::Limits::max} / 2 - 1\). The limits are defined in the namespace Set::Limits.
Any attempt to create a set variable with values outside the defined limits throws an exception of type Set::OutOfLimits.
Variable access functions. You can access the current domain of a set variable x using member functions such as x.cardMax(), returning the upper bound of the cardinality, or x.glbMin(), returning the smallest element of the lower bound. Furthermore, you can print a set variable’s domain using the standard output operator <<.
Iterating variable domain interval bounds. For access to the interval bounds of a set variable, Gecode provides three value iterators and corresponding range iterators. For example, the following loop
for (SetVarGlbValues i(x); i(); ++i)
std::cout << i.val() << ' ';
uses the value iterator i to print all values of the greatest lower bound of the domain of x in increasing order. If x is assigned, this of course corresponds to the value of x. Similarly, the following loop
for (SetVarLubRanges i(x); i(); ++i)
std::cout << i.min() << ".." << i.max() << ' ';
uses the range iterator i to print all ranges of the least upper bound of the domain of x. The third kind of iterator, SetVarUnknownValues or SetVarUnknownRanges, iterate the values resp. ranges that are still unknown to be part or not part of the set, that is \(u\setminus l\) for the domain \(\left[l..u\right]\).
When to inspect a variable. The same restrictions hold as for integer variables (see When to inspect a variable). The important restriction is that one must not change the domain of a variable (for example, by posting a constraint on that variable) while an iterator for that variable is being used.
Updating variables. Set variables behave exactly like integer variables during cloning of a space. A set variable is updated by
x.update(home, y);
where y is the variable from which x is to be updated. While home is the space x belongs to, y belongs to the space which is being cloned.
Variable and argument arrays. Set variable arrays can be allocated using the class SetVarArray. The constructors of this class take the same arguments as the set variable constructors, preceded by the size of the array. For example,
SetVarArray x(home, 4, IntSet::empty, IntSet(1, 3));
creates an array of four set variables, each with domain \(\left[\{\}..\{1,2,3\}\right]\).
To pass temporary data structures as arguments, you can use the SetVarArgs class (see Argument arrays). Some set constraints are defined in terms of arrays of sets of integers. These can be passed using IntSetArgs (see Argument arrays). Set variable argument arrays support the same operations introduced in Argument arrays.
5.2. Constraint overview¶
This section introduces the different groups of constraints over set variables available in Gecode. The section serves only as an overview. For the details and the full list of available post functions, the section refers to the relevant reference documentation.
Reified constraints. Several set constraints also exist as a reified variant. Whether a reified version exists for a given constraint can be found in the reference documentation. If a reified version does exist, the reification information combining the Boolean control variable and an optional reification mode is passed as the last non-optional argument, see Half reification.
5.2.1. Domain constraints¶
|
equality (\(=\)) |
|
disequality (\(\neq\)) |
|
lex. less than or equal |
|
lex. less than |
|
lex. greater than or equal |
|
lex. greater than |
|
subset (\(\subseteq\)) |
|
superset (\(\supseteq\)) |
|
disjointness (\(\parallel\)) |
|
complement (\(\;\overline{\cdot}\;\)) |
Figure 5.1 Set relation types¶
Domain constraints restrict the domain of a set variable using a set constant (given as a single integer, an interval of two integers or an IntSet), depending on set relation types of type SetRelType (see Using integer set variables and constraints). Figure 5.1 lists the available set relation types and their meaning. The relations SRT_LQ, SRT_LE, SRT_GQ, and SRT_GR establish a total order based on the lexicographic order of the characteristic functions of the two sets.
For example, the constraints
dom(home, x, SRT_SUB, 1, 10);
dom(home, x, SRT_SUP, 1, 3);
dom(home, y, SRT_DISJ, IntSet(4, 6));
result in the set variable x being a subset of \(\{1,\dots,10\}\) and a superset of \(\{1,2,3\}\), while \(4\), \(5\), and \(6\) are not elements of the set \(\mathtt{y}\). The domain constraints for set variables support reification. Both x and y can also be arrays of set variables where each array element is constrained accordingly (but no reification is supported).
In addition to the above constraints,
cardinality(home, x, 3, 5);
restricts the cardinality of the set variable x to be between \(3\) and \(5\). x can also be an array of set variables.
The domain of a set variable x can be constrained according to the domain of another variable set d by
dom(home, x, d);
Here, x and d can also be arrays of set variables.
For examples using domain constraints, see Airline crew allocation, as well as the redundant constraints in Golf tournament.
5.2.2. Relation constraints¶
Relation constraints enforce relations between set variables and between set and integer variables, depending on the set relation types introduced above.
For set variables x and y, the following constrains x to be a subset of y:
rel(home, x, SRT_SUB, y);
If x is a set variable and y is an integer variable, then
rel(home, x, SRT_SUP, y);
constrains x to be a superset of the singleton set \(\{\mathtt{y}\}\), which means that y must be an element of x.
The last form of set relation constraint uses an integer relation type (see Figure 4.1) instead of a set relation type. This constraint restricts all elements of a set variable to be in the given relation to the value of an integer variable. For example,
rel(home, x, IRT_GR, y);
constrains all elements of the set variable x to be strictly greater than the value of the integer variable y (see also GCCat: eq_set, in, in_set, not_in).
Gecode provides reified versions of all set relation constraints. For an example, see Golf tournament and Social golfers.
If-then-else constraint. An if-then-else constraint can be posted by
ite(home, b, x, y, z);
where b is a Boolean variable and x, y, and z are set variables. In case b is one, then \(\mathtt{x}=\mathtt{z}\) must hold, otherwise \(\mathtt{y}=\mathtt{z}\) must hold.
5.2.3. Set operations¶
|
union (\(\cup\)) |
|
intersection (\(\cap\)) |
|
disjoint union (\(\uplus\)) |
|
set minus (\(\setminus\)) |
Figure 5.2 Set operation types¶
Set operation/relation constraints perform set operations according to the type shown in Figure 5.2 and relate the result to a set variable. For example,
rel(home, x, SOT_UNION, y, SRT_EQ, z);
enforces the relation \(\mathtt{x}\cup \mathtt{y}=\mathtt{z}\) for set variables x, y, and z. For an array of set variables x,
rel(home, SOT_UNION, x, y);
enforces the relation
Instead of set variables, the relation constraints also accept IntSet arguments as set constants. There are no reified versions of the set operation constraints (you can decompose using reified relation constraints on the result, see Tip 5.3).
Set operation constraints are used in most examples that contain set variables, such as Airline crew allocation or Generating Hamming codes.
5.2.4. Element constraints¶
Element constraints (element()) generalize array access to set variables. The simplest version of element for set variables is stated as
element(home, x, y, z);
for an array of set variables or constants x, an integer variable y, and a set variable z. It constrains z to be the element of array x at index y (where the index starts at 0).
A further generalization uses a set variable as the index, thus selecting several sets at once. The result variable is constrained to be the union, disjoint union, or intersection of the selected set variables, depending on the set operation type argument. For example,
element(home, SOT_UNION, x, y, z);
for set variables y and z and an array of set variables x enforces the following relation:
Note that generalized element constraints follow the usual semantics of set operations if the index variable is the empty set: an empty union is the empty set, whereas an empty intersection is the full universe. Because of this semantics, the element constraint has an optional set constant argument so that you can specify the universe (i.e., usually the full set of elements your problem deals with) explicitly. For an example of a set element constraint, see Golf tournament and Social golfers.
5.2.5. Constraints connecting set and integer variables¶
Most models that involve set variables also involve integer variables. In addition to the set relation constraints that accept integer variables (interpreting them as singleton sets), Cardinality constraints (cardinality()) and the operations below provide the necessary interface for models that use both set variables and integer or Boolean variables.
The most obvious constraint connecting integer and set variables is the cardinality constraint:
cardinality(home, x, y);
It states that the integer variable y is equal to the cardinality of the set variable x.
Gecode provides constraints for the minimal and maximal elements of a set. The following code
min(home, x, y);
constrains the integer variable y to be the minimum of the set x.
For an example of constraints connecting integer and set variables, see Steiner triples.
Weighted sets. The weights constraint assigns a weight to each possible element of a set variable x, and then constrains an integer variable y to be the sum of the weights of the elements of x. The mapping is given using two integer arrays, e and w. For example,
IntArgs e({ 1, 3, 4, 5, 7, 9});
IntArgs w({-1, 4, 1, 1, 3, 3});
weights(home, e, w, x, y);
enforces that x is a subset of \(\{1,3,4,5,7,9\}\) (the set of elements), and that y is the sum of the weights of the elements in x, where the weight of the element 1 would be -1, the weight of 3 would be 4 and so on. Assigning x to the set \(\{3,7,9\}\) would therefore result in y being assigned to \(4+3+3=10\) (see also GCCat: sum_set).
5.2.6. Set channeling constraints¶
Channel constraints (channel()) link arrays of set variables, as well as set variables with integer and Boolean variables.
For an two arrays of set variables x and y,
channel(home, x, y);
posts the constraint
For an array of integer variables x and an array of set variables y,
channel(home, x, y);
posts the constraint
The channel between a set variable y and an array of Boolean variables x,
channel(home, x, y);
enforces the constraint
An array of integer variables x can be channeled to a set variable y using
rel(home, SOT_UNION, x, y);
which constrains y to be the set \(\{\mathtt{x}_0,\dots,\mathtt{x}_{|\mathtt{x}|-1}\}\). An alias for this constraint is defined in the modeling convenience library, see Channeling functions and Aliases for set constraints.
A specialized version of the previous constraint is
channelSorted(home, x, y);
which constrains y to be the set \(\{\mathtt{x}_0,\dots,\mathtt{x}_{|\mathtt{x}|-1}\}\), and the integer variables in x are sorted in increasing order (\(\mathtt{x}_i<\mathtt{x}_{i+1}\) for \(0\leq i<|\mathtt{x}|\)) (see also GCCat: link_set_to_booleans).
5.2.7. Convexity constraints¶
Convexity constraints enforce that set variables are convex, which means that the elements form an integer interval. For example, the set \(\{1,2,3,4,5\}\) is convex, while \(\{1,3,4,5\}\) is not, as it contains a hole. The convex hull of a set \(s\) is the smallest convex set containing \(s\) (\(\{1,2,3,4,5\}\) is the convex hull of \(\{1,3,4,5\}\)).
The constraint
convex(home, x);
states that the set variable x must be convex, and
convex(home, x, y);
enforces that the set variable y is the convex hull of the set variable x.
5.2.8. Sequence constraints¶
Sequence constraints enforce an order among an array of set variables x. Posting the constraint
sequence(home, x);
results in the sets x being pairwise disjoint, and furthermore \(\max(\mathtt{x}_i)<\min(\mathtt{x}_{i+1})\) for all \(0\leq i<|\mathtt{x}|-1\). Posting
sequence(home, x, y);
additionally constrains the set variable y to be the union of the x.
For an example of sequence constraints, see Steiner triples.
5.2.9. Value precedence constraints¶
Value precedence constraints (precede()) enforce that a value precedes another value in an array of set variables. By
precede(home, x, s, t);
where x is an array of set variables and both s and t are integers, the following is enforced: if there exists \(j\) (\(0\leq j<|\mathtt{x}|\)) such that \(s\notin\mathtt{x}_j\) and \(t\in\mathtt{x}_j\), then there must exist \(i\) with \(i<j\) such that \(s\in\mathtt{x}_i\) and \(t\notin\mathtt{x}_i\).
A generalization is available for precedences between several integer values. By
precede(home, x, c);
where x is an array of set variables and c is an array of integers, it is enforced that \(\mathtt{c}_k\) precedes \(\mathtt{c}_{k+1}\) in x for \(0\leq k<|\mathtt c|-1\).
The constraint is implemented by the propagator introduced in [29] (see also GCCat: set_value_precede), the paper also explains how to use the precede constraint for breaking value symmetries. For an example, see Golf tournament and Social golfers.
5.3. Synchronized execution¶
Gecode offers support in Synchronized execution (wait()) for executing a function when set variables become assigned.
The code
wait(home, x, [] (Space &home) { ...; });
posts a propagator that waits until the set variable x (or, if x is an array of set variables: all variables in x) is assigned. If x becomes assigned, the function passed as argument is executed with the current home space passed as argument. The type of the function must be
std::function<void(Space& home)>