7. Modeling convenience: MiniModel¶
This chapter provides an overview of modeling convenience implemented by MiniModel. MiniModel (see Direct modeling support) provides some little helpers to the constraint modeler. However, it does not offer any new constraints or branchers.
Overview. Expressions and relations surveys how constraints represented by integer, Boolean, set, and float expressions and relations can be posted. How matrix interfaces for arrays can be defined and used is discussed in Matrix interface for arrays. Support for defining cost functions for cost-based optimization is presented in Support for cost-based optimization. Regular expressions for expressing extensional constraints are discussed in Regular expressions for extensional constraints. Channeling functions surveys channeling functions, whereas Aliases for integer constraints and Aliases for set constraints discuss aliases for some commonly used constraints.
Important
Do not forget to add
#include <gecode/minimodel.hh>
to your program when you want to use MiniModel. Note that the same conventions hold as in Integer and Boolean variables and constraints.
7.1. Expressions and relations¶
The main part of MiniModel consists of overloaded operators and functions that provide a more natural syntax for posting constraints. These operators can be used in two slightly different ways. You can post a relation, or create a new variable from an expression.
For example, the following code creates a fresh integer variable z that is constrained to be equal to the given expression 3*x-4*y+2, where both x and y are integer variables:
IntVar z=expr(home, 3*x-4*y+2);
An important aspect of posting an expression is that the returned variable is initialized with a reasonably small variable domain, see Small variable domains are beautiful.
A relation can be posted using the rel function, which posts the corresponding constraints and consequently returns void. Assume that z is an integer variable, then
rel(home, z == 3*x-4*y+2);
posts the same constraint as in the previous example.
MiniModel provides syntax for expressions and relations over integer, Boolean, set, and float variables, which can be freely mixed. For example, the following code snippet returns a Boolean variable that is true if and only if \(\{\mathtt x\}\subseteq \mathtt s\) and \(|\mathtt s|=\mathtt y\), where \(\mathtt x\) and \(\mathtt y\) are integer variables, and \(\mathtt s\) is a set variable:
BoolVar b = expr(home, (singleton(x) <= s) && (cardinality(s) == y));
The rest of this section presents the different ways to construct expressions and relations, grouped by the type of the expressions.
7.1.1. Integer expressions and relations¶
\(\langle\mathit{IntExpr}\rangle\) |
\(::=\) |
\(\langle n\rangle\) |
integer value |
\(|\) |
\(\langle x\rangle\) |
integer or Boolean variable |
|
\(|\) |
\(\mathtt{-}\langle\mathit{IntExpr}\rangle\) |
unary minus |
|
\(|\) |
\(\langle\mathit{IntExpr}\rangle\mathbin{\texttt{+}} \langle\mathit{IntExpr}\rangle\) |
addition |
|
\(|\) |
\(\langle\mathit{IntExpr}\rangle\mathbin{\texttt{-}} \langle\mathit{IntExpr}\rangle\) |
subtraction |
|
\(|\) |
\(\langle\mathit{IntExpr}\rangle\mathbin{\texttt{*}} \langle\mathit{IntExpr}\rangle\) |
multiplication |
|
\(|\) |
\(\langle\mathit{IntExpr}\rangle\mathbin{\texttt{/}} \langle\mathit{IntExpr}\rangle\) |
integer division |
|
\(|\) |
\(\langle\mathit{IntExpr}\rangle\mathbin{\texttt{\%}} \langle\mathit{IntExpr}\rangle\) |
modulo |
|
\(|\) |
\(\texttt{sum(}\langle\overline x\rangle\texttt{)}\) |
sum of integer or Boolean variables |
|
\(|\) |
\(\texttt{sum(}\langle\overline n\rangle\texttt{,}\langle\overline x\rangle\texttt{)}\) |
sum of integer or Boolean variables |
|
with integer coefficients |
|||
\(|\) |
\(\operatorname{\texttt{min}}(\langle\mathit{IntExpr}\rangle,\langle\mathit{IntExpr}\rangle)\) |
minimum |
|
\(|\) |
\(\operatorname{\texttt{min}}(\langle\overline x\rangle)\) |
minimum of integer variables |
|
\(|\) |
\(\operatorname{\texttt{max}}(\langle\mathit{IntExpr}\rangle,\langle\mathit{IntExpr}\rangle)\) |
maximum |
|
\(|\) |
\(\operatorname{\texttt{max}}(\langle\overline x\rangle)\) |
maximum of integer variables |
|
\(|\) |
\(\operatorname{\texttt{abs}}(\langle\mathit{IntExpr}\rangle)\) |
absolute value |
|
\(|\) |
\(\operatorname{\texttt{sqr}}(\langle\mathit{IntExpr}\rangle)\) |
square |
|
\(|\) |
\(\operatorname{\texttt{sqrt}}(\langle\mathit{IntExpr}\rangle)\) |
square root |
|
\(|\) |
\(\operatorname{\texttt{pow}}(\langle\mathit{IntExpr}\rangle,\langle n\rangle)\) |
power |
|
\(|\) |
\(\operatorname{\texttt{nroot}}(\langle\mathit{IntExpr}\rangle,\langle n\rangle)\) |
\(n\)-th root |
|
\(|\) |
\(\operatorname{\texttt{element}}(\langle\overline x\rangle,\langle\mathit{IntExpr}\rangle)\) |
array element of integer variables |
|
\(|\) |
\(\operatorname{\texttt{element}}(\langle\overline n\rangle,\langle\mathit{IntExpr}\rangle)\) |
array element of integers |
|
\(|\) |
\(\operatorname{\texttt{ite}}(\langle\mathit{BoolExpr}\rangle,\langle\mathit{IntExpr}\rangle,\langle\mathit{IntExpr}\rangle)\) |
if-then-else |
|
\(|\) |
\(\operatorname{\texttt{min}}(\langle\mathit{SetExpr}\rangle)\) |
minimum of a set expression |
|
\(|\) |
\(\operatorname{\texttt{max}}(\langle\mathit{SetExpr}\rangle)\) |
maximum of a set expression |
|
\(|\) |
\(\operatorname{\texttt{cardinality}}(\langle\mathit{SetExpr}\rangle)\) |
cardinality of a set expression |
|
\(\langle\overline x\rangle\) |
\(::=\) |
array of integer or Boolean variables |
|
\(\langle\overline n\rangle\) |
\(::=\) |
array of integers |
Figure 7.1 Integer expressions¶
\(\langle\mathit{IntRel}\rangle\) |
\(::=\) |
\(\langle\mathit{IntExpr}\rangle\mathrel{\langle r\rangle} \langle\mathit{IntExpr}\rangle\) |
integer relation |
\(|\) |
\(\operatorname{\texttt{dom}}(\langle x\rangle,\langle n\rangle)\) |
domain relation |
|
\(|\) |
\(\operatorname{\texttt{dom}}(\langle x\rangle,\langle n\rangle,\langle n\rangle)\) |
domain relation |
|
\(|\) |
\(\operatorname{\texttt{dom}}(\langle x\rangle,\langle s\rangle)\) |
domain relation |
|
\(\langle n\rangle\) |
\(::=\) |
integer value |
|
\(\langle s\rangle\) |
\(::=\) |
set constant ( |
|
\(\langle x\rangle\) |
\(::=\) |
integer or Boolean variable |
|
\(\langle r\rangle\) |
\(::=\) |
|
integer relation symbol |
Figure 7.2 Integer relations¶
Integer expressions (that is, expressions that evaluate to an integer) are constructed according to the structure sketched in Figure 7.1, whereas integer relations are constructed according to the structure sketched in Figure 7.2. We use the standard C++ operators (for an example, see Posting linear constraints de-mystified), as well as several functions with intuitive names such as min or max. Integer expressions and relations can be constructed over integer, Boolean, and set variables. In Gecode, integer expressions are of type LinIntExpr, which are constructed using Linear expressions and relations, Arithmetic functions, and some Set expressions and relations.
Even arrays of variables (possibly with integer argument arrays as coefficients) can be used for posting some expressions and relations. For example, if x and y are integer variables and z is an array of integer variables, then
rel(home, x+2*sum(z) < 4*y);
posts a single linear constraint that involves all variables from the array z.
As long as an expression is linear (i.e., it can be represented as \(\sum a_i\cdot x_i\) where the \(a_i\) are integers and \(x_i\) are integer or Boolean variables), the constraint posted for the expression will be as few linear constraints as possible (see Linear constraints) to ensure maximal constraint propagation. [1]
Non-linear expressions, such as a multiplication of two variables, are handled by MiniModel using decomposition. For example, posting the constraint
rel(home, a+b*(c+d) == 0);
for integer variables a, b, c, and d is equivalent to the decomposition
IntVar tmp0 = expr(home, c+d);
IntVar tmp1 = expr(home, b*tmp0);
rel(home, a+tmp1 == 0);
Like the post functions for integer and Boolean constraints presented in Constraint overview, posting integer expressions and relations supports an optional argument of type IntPropLevel to select the propagation level. For more information, see Posting of expressions and relations and Posting constraints.
Using the expr() function, you can enforce a particular decomposition, and you can specify the propagation level for each subexpression. For example,
rel(home, x+expr(home,y*z,IPL_DOM) == 0);
will perform domain propagation for the multiplication, but bounds propagation (the default) for the sum.
An element expression such as element(x,e), where x is an array of integers or integer variables, and e is an integer expression, corresponds to an array access x[e], implemented using an element constraint (see Element constraints).
MiniModel provides three integer expressions whose arguments are set expressions: the minimum of a set, the maximum of a set, and a set’s cardinality. We will see later how set expressions are constructed.
For examples of integer expressions, see Alpha puzzle, SEND+MORE=MONEY puzzle, Grocery puzzle, Golomb rulers, Locating warehouses, and Posting linear constraints de-mystified.
Integer propagation levels. When posting integer expressions and relations it can be controlled which integer propagation level is used for each constraint. The integer propagation levels for all relevant constraints are specified by an object of class IntPropLevels. The expr() and rel() functions for posting expressions take an object of this class as last argument.
Declaring an object of class IntPropLevels by default initializes all propagation levels to the default integer propagation level IPL_DEF. All integer propagation levels can be initialized to, for example, IPL_DOM by
IntPropLevels ipls(IPL_DOM);
and then used as last argument of rel() and expr() for posting relations and expressions.
However, this also uses domain propagation for linear constraints as well as minimum and maximum with an arbitrary number of variables where domain propagation can be very slow. To use default propagation for these constraints, ipls can be modified by
ipls.linear(IPL_DEF).min(IPL_DEF).max(IPL_DEF);
The list of constraints for which the propagation level can be specified can be seen from the class definition IntPropLevels.
7.1.2. Boolean expressions and relations¶
\(\langle\mathit{BoolExpr}\rangle\) |
\(::=\) |
\(\langle x\rangle\) |
Boolean variable |
\(|\) |
\(\operatorname{\texttt{!}}\langle\mathit{BoolExpr}\rangle\) |
negation |
|
\(|\) |
\(\langle\mathit{BoolExpr}\rangle\mathbin{\texttt{\&\&}} \langle\mathit{BoolExpr}\rangle\) |
conjunction |
|
\(|\) |
\(\langle\mathit{BoolExpr}\rangle\mathbin{\texttt{||}} \langle\mathit{BoolExpr}\rangle\) |
disjunction |
|
\(|\) |
\(\langle\mathit{BoolExpr}\rangle\mathbin{\texttt{==}} \langle\mathit{BoolExpr}\rangle\) |
equivalence |
|
\(|\) |
\(\langle\mathit{BoolExpr}\rangle\mathbin{\texttt{!=}} \langle\mathit{BoolExpr}\rangle\) |
non-equivalence |
|
\(|\) |
\(\langle\mathit{BoolExpr}\rangle\mathbin{\texttt{>{}>{}}} \langle\mathit{BoolExpr}\rangle\) |
implication |
|
\(|\) |
\(\langle\mathit{BoolExpr}\rangle\mathbin{\texttt{<{}<{}}} \langle\mathit{BoolExpr}\rangle\) |
reverse implication |
|
\(|\) |
\(\langle\mathit{BoolExpr}\rangle\mathbin{\texttt{\^{}}} \langle\mathit{BoolExpr}\rangle\) |
exclusive or |
|
\(|\) |
\(\operatorname{\texttt{element}}(\langle\overline x\rangle,\langle\mathit{IntExpr}\rangle)\) |
array element of Boolean variables |
|
\(|\) |
|
Boolean constant |
|
\(|\) |
\(\langle\mathit{IntRel}\rangle\) |
reified integer relation |
|
\(|\) |
\(\langle\mathit{SetRel}\rangle\) |
reified set relation |
|
\(|\) |
\(\langle\mathit{FloatRel}\rangle\) |
reified float relation |
Figure 7.3 Boolean expressions¶
Boolean expressions are constructed using standard C++ operators according to the structure sketched in Figure 7.3.
C++ Boolean constants can initialize a BoolExpr or occur in an
expression with Boolean variables and relations. For example,
BoolExpr e = true starts with a true expression, and e && x then
gives the same condition as x. Among C++ scalar constants, only bool
values convert to Boolean expressions; integer constants keep their integer
meaning. A default-constructed BoolExpr is not a Boolean constant. It is
an empty accumulator for conjunctions or disjunctions.
To post a constant Boolean expression, use expr(home, BoolExpr(true)).
This returns a Boolean variable fixed to one. The call expr(home, true)
instead selects the integer-expression overload and returns an integer
variable fixed to one.
Again, the purpose of a Boolean expression or relation is to post a corresponding constraint for it (see Posting of expressions and relations). Posting a Boolean expression returns a new Boolean variable that is constrained to the value of the expression. Several constraints might be posted for a single expression, however as few constraints as possible are posted. For example, all negation constraints are eliminated by rewriting the Boolean expression into NNF (negation normal form) and conjunction and disjunction constraints are combined whenever possible.
For example, the Boolean expression x && (y >> z) (to be read as \(\mathtt{x}\wedge(\mathtt{y}\to\mathtt{z})\)) for Boolean variables x, y, and z is posted by
BoolVar b=expr(home, x && (y >> z));
Any Boolean expression \(e\) corresponds to the Boolean relation stating that \(e\) is true. Posting a Boolean relation posts the corresponding Boolean constraint. Using the Boolean expression from above,
rel(home, x && (y >> z));
posts that \(\mathtt{x}\wedge(\mathtt{y}\to\mathtt{z})\) must be true, whereas
rel(home, !(x && (y >> z)));
posts that \(\mathtt{x}\wedge(\mathtt{y}\to\mathtt{z})\) must be false.
A Boolean element expression such as element(x,e), where x is an array of Boolean variables, and e is an integer expression, corresponds to an array access x[e], implemented using an element constraint (see Element constraints).
Boolean expressions include reified integer relations. As an example consider the placement of two squares \(s_1\) and \(s_2\) such that the squares do not overlap. A well known model for this constraint is
The meaning of the integer variables \(\mathtt{x}_i\) and \(\mathtt{y}_i\), and the integer values \(\mathtt{d}_i\) is sketched to the right. The squares do not overlap, if the relative position of \(s_1\) with respect to \(s_2\) is either left, right, above, or below. As soon as one of the relationships is established, the squares do not overlap. Please also consult Geometrical packing constraints for geometrical packing constraints.
With Boolean relations using reified integer relations, the constraint that the squares \(s_1\) and \(s_2\) do not overlap can be posted as follows:
rel(home, (x1+d1 <= x2) || (x2+d2 <= x1) ||
(y1+d1 <= y2) || (y2+d2 <= y1));
Like the post functions for integer and Boolean variables presented above, posting Boolean expressions and relations supports an optional argument of type IntPropLevel to select the propagation level. For more information, see Posting constraints.
Boolean expressions also include reified set relations, which will be covered below.
For more examples using Boolean expressions and Boolean relations including reification, see Photo alignment.
7.1.3. Set expressions and relations¶
\(\langle\mathit{SetExpr}\rangle\) |
\(::=\) |
\(\langle y\rangle\) |
set variable |
\(|\) |
\(\langle s\rangle\) |
set constant ( |
|
\(|\) |
\(\mathtt{-}\langle\mathit{SetExpr}\rangle\) |
complement |
|
\(|\) |
\(\langle\mathit{SetExpr}\rangle\mathbin{\texttt{\&}} \langle\mathit{SetExpr}\rangle\) |
intersection |
|
\(|\) |
\(\langle\mathit{SetExpr}\rangle\mathbin{\texttt{|}} \langle\mathit{SetExpr}\rangle\) |
union |
|
\(|\) |
\(\langle\mathit{SetExpr}\rangle\mathbin{\texttt{+}} \langle\mathit{SetExpr}\rangle\) |
disjoint union |
|
\(|\) |
\(\langle\mathit{SetExpr}\rangle\mathbin{\texttt{-}} \langle\mathit{SetExpr}\rangle\) |
set difference |
|
\(|\) |
\(\operatorname{\texttt{inter}}(\langle\overline y\rangle)\) |
intersection of variables |
|
\(|\) |
\(\operatorname{\texttt{setunion}}(\langle\overline y\rangle)\) |
union of variables |
|
\(|\) |
\(\operatorname{\texttt{setdunion}}(\langle\overline y\rangle)\) |
disjoint union of variables |
|
\(|\) |
\(\operatorname{\texttt{singleton}}(\langle\mathit{IntExpr}\rangle)\) |
singleton given by integer expression |
|
\(\langle\mathit{SetRel}\rangle\) |
\(::=\) |
\(\langle\mathit{SetExpr}\rangle\mathbin{\texttt{==}} \langle\mathit{SetExpr}\rangle\) |
expressions are equal |
\(|\) |
\(\langle\mathit{SetExpr}\rangle\mathbin{\texttt{!=}} \langle\mathit{SetExpr}\rangle\) |
expressions are not equal |
|
\(|\) |
\(\langle\mathit{SetExpr}\rangle\mathbin{\texttt{<=}} \langle\mathit{SetExpr}\rangle\) |
first is subset of second expression |
|
\(|\) |
\(\langle\mathit{SetExpr}\rangle\mathbin{\texttt{>=}} \langle\mathit{SetExpr}\rangle\) |
first is superset of second expression |
|
\(|\) |
\(\langle\mathit{SetExpr}\rangle\mathbin{\texttt{||}} \langle\mathit{SetExpr}\rangle\) |
expressions are disjoint |
|
\(|\) |
\(\langle\mathit{SetExpr}\rangle\mathrel{\langle r\rangle} \langle\mathit{IntExpr}\rangle\) |
set-integer relation |
|
\(|\) |
\(\langle\mathit{IntExpr}\rangle\mathrel{\langle r\rangle} \langle\mathit{SetExpr}\rangle\) |
integer-set relation |
|
\(|\) |
\(\operatorname{\texttt{dom}}(\langle y\rangle,\langle r_s\rangle,\langle n\rangle)\) |
domain relation |
|
\(|\) |
\(\operatorname{\texttt{dom}}(\langle y\rangle,\langle r_s\rangle,\langle n\rangle,\langle n\rangle)\) |
domain relation |
|
\(|\) |
\(\operatorname{\texttt{dom}}(\langle y\rangle,\langle r_s\rangle,\langle s\rangle)\) |
domain relation |
|
\(\langle n\rangle\) |
\(::=\) |
integer value |
|
\(\langle\overline y\rangle\) |
\(::=\) |
array of set variables |
|
\(\langle r\rangle\) |
\(::=\) |
|
integer relation symbol |
\(\langle r_s\rangle\) |
\(::=\) |
|
set relation symbol |
Figure 7.4 Set expressions and relations¶
Set expressions and relations are constructed using the standard C++ operators and the functions listed in Figure 7.4. Just like for integer and Boolean expressions, posting of a set expression returns a new set variable that is constrained to the value of the expression.
For example, the set expression x & (y | z) (to be read as \(\mathtt{x}\cap(\mathtt{y}\cup\mathtt{z})\)) for set variables x, y, and z is posted by
SetVar s = expr(home, x & (y | z));
Posting a set relation posts the corresponding constraint. Given an existing set variable s, the previous code fragment could therefore be written as
rel(home, s == (x & (y | z)));
As noted above, set relations can be reified, turning them into Boolean expressions. The following code posts the constraint that b is true if and only if x is the complement of y:
BoolVar b = expr(home, (x == -y));
Instead of a set variable, you can always use a constant IntSet, for example for reifying the fact that x is empty:
BoolVar b = expr(home, (x == IntSet::empty));
The subset relations can also be posted two-sided, such as
rel(home, IntSet(0,10) <= x <= IntSet(0,20));
7.1.4. Float expressions and relations¶
\(\langle\mathit{FloatExpr}\rangle\) |
\(::=\) |
\(\langle z\rangle\) |
float variable |
\(|\) |
\(\langle f\rangle\) |
float value |
|
\(|\) |
\(\mathtt{-}\langle\mathit{FloatExpr}\rangle\) |
unary minus |
|
\(|\) |
\(\langle\mathit{FloatExpr}\rangle\mathbin{\texttt{+}} \langle\mathit{FloatExpr}\rangle\) |
addition |
|
\(|\) |
\(\langle\mathit{FloatExpr}\rangle\mathbin{\texttt{-}} \langle\mathit{FloatExpr}\rangle\) |
subtraction |
|
\(|\) |
\(\langle\mathit{FloatExpr}\rangle\mathbin{\texttt{*}} \langle\mathit{FloatExpr}\rangle\) |
multiplication |
|
\(|\) |
\(\langle\mathit{FloatExpr}\rangle\mathbin{\texttt{/}} \langle\mathit{FloatExpr}\rangle\) |
division |
|
\(|\) |
\(\texttt{sum(}\langle\overline z\rangle\texttt{)}\) |
sum of float variables |
|
\(|\) |
\(\texttt{sum(}\langle\overline f\rangle\texttt{,}\langle\overline z\rangle\texttt{)}\) |
sum of float with coefficients |
|
\(|\) |
\(\operatorname{\texttt{min}}(\langle\mathit{FloatExpr}\rangle,\langle\mathit{FloatExpr}\rangle)\) |
minimum |
|
\(|\) |
\(\operatorname{\texttt{min}}(\langle\overline z\rangle)\) |
minimum of float variables |
|
\(|\) |
\(\operatorname{\texttt{max}}(\langle\mathit{FloatExpr}\rangle,\langle\mathit{FloatExpr}\rangle)\) |
maximum |
|
\(|\) |
\(\operatorname{\texttt{max}}(\langle\overline z\rangle)\) |
maximum of float variables |
|
\(|\) |
\(\operatorname{\texttt{abs}}(\langle\mathit{FloatExpr}\rangle)\) |
absolute value |
|
\(|\) |
\(\operatorname{\texttt{sqr}}(\langle\mathit{FloatExpr}\rangle)\) |
square |
|
\(|\) |
\(\operatorname{\texttt{sqrt}}(\langle\mathit{FloatExpr}\rangle)\) |
square root |
|
\(|\) |
\(\operatorname{\texttt{pow}}(\langle\mathit{FloatExpr}\rangle,\langle n\rangle)\) |
power |
|
\(|\) |
\(\operatorname{\texttt{nroot}}(\langle\mathit{FloatExpr}\rangle,\langle n\rangle)\) |
\(n\)-th root |
|
\(|\) |
\(\operatorname{\texttt{exp}}(\langle\mathit{FloatExpr}\rangle)\) |
exponentialMPFR |
|
\(|\) |
\(\operatorname{\texttt{log}}(\langle\mathit{FloatExpr}\rangle)\) |
logarithmMPFR |
|
\(|\) |
\(\operatorname{\texttt{sin}}(\langle\mathit{FloatExpr}\rangle)\) |
sineMPFR |
|
\(|\) |
\(\operatorname{\texttt{cos}}(\langle\mathit{FloatExpr}\rangle)\) |
cosineMPFR |
|
\(|\) |
\(\operatorname{\texttt{tan}}(\langle\mathit{FloatExpr}\rangle)\) |
tangentMPFR |
|
\(|\) |
\(\operatorname{\texttt{asin}}(\langle\mathit{FloatExpr}\rangle)\) |
arcsineMPFR |
|
\(|\) |
\(\operatorname{\texttt{acos}}(\langle\mathit{FloatExpr}\rangle)\) |
arccosineMPFR |
|
\(|\) |
\(\operatorname{\texttt{atan}}(\langle\mathit{FloatExpr}\rangle)\) |
arctangentMPFR |
|
\(\langle\overline z\rangle\) |
\(::=\) |
array of float variables |
|
\(\langle\overline f\rangle\) |
\(::=\) |
array of float values |
Figure 7.5 Float expressions¶
Linear float expressions and relations are constructed using the standard C++ operators and the functions listed in Figure 7.5 and Figure 7.6 (see also Arithmetic functions, Transcendental functions, and Trigonometric functions). Posting a float expression returns a new float variable that is constrained to the value of the expression.
\(\langle\mathit{FloatRel}\rangle\) |
\(::=\) |
\(\langle\mathit{FloatExpr}\rangle\mathrel{\langle r\rangle} \langle\mathit{FloatExpr}\rangle\) |
float relation |
\(|\) |
\(\operatorname{\texttt{dom}}(\langle z\rangle,\langle f\rangle)\) |
domain relation |
|
\(|\) |
\(\operatorname{\texttt{dom}}(\langle z\rangle,\langle m\rangle,\langle m\rangle)\) |
domain relation |
|
\(\langle r\rangle\) |
\(::=\) |
|
float relation symbol |
\(\langle f\rangle\) |
\(::=\) |
float value |
|
\(\langle m\rangle\) |
\(::=\) |
float number |
|
\(\langle z\rangle\) |
\(::=\) |
float variable |
Figure 7.6 Float relations¶
Instead of a float variable, you can always use a constant of type FloatVal.
7.1.5. Extending Boolean expressions and relations¶
Boolean expressions and relations can easily be extended. A typical case for extension is when a new variable type is added (see also Programming variables) and that there are also reified constraints using the new variable type that should be included in Boolean expressions and relations.
As an example, we assume that we would like to extend Boolean expressions and relations by a domain expression dom() (as a more convenient form of domain constraints as described in Domain constraints), so for example
IntVar x(home,-10,10);
rel(home, dom(x,1,5));
constrains the domain of x to \(\{1,2,3,4,5\}\) whereas
IntVar x(home,-10,10);
rel(home, !dom(x,1,5));
constrains the domain of x to \(\{-10,-9,\ldots,0,6,7,\ldots,10\}\). The domain expression can be used together with other Boolean expressions. For example, if both x and y are integer variables, then the following is possible:
rel(home, dom(x,3,4) && !(y > 4));
class BoolDomExpr : public BoolExpr::Misc {
protected:
IntVar x; int l, u;
public:
BoolDomExpr(IntVar x0, int l0, int u0)
: x(x0), l(l0), u(u0) {}
// [Boolean domain expression:post member function]
virtual ~BoolDomExpr(void) {}
};
// [Boolean domain expression:create Boolean domain expression]
In order to extend Boolean expressions one must implement the following:
A function
dom(IntVar x, int l, int u)that creates a Boolean expression wherexis the variable andlanduare the lower and upper bound for the domain.A class
BoolDomExprthat inherits from the class BoolExpr::Misc. An object of the class is created by ourdom()function and the modeling layer uses a virtual member functionpost()to post a constraint when therel()orexpr()functions require this.
The dom() function. The definition of the dom() function is straightforward and as follows:
BoolExpr dom(IntVar x, int l, int u) {
return BoolExpr(new BoolDomExpr(x,l,u));
}
It returns a new Boolean expression that contains an object of class BoolDomExpr that can be used by the rel() and expr() functions.
The Boolean domain expression class. The class is shown in Program 7.1. An object of class BoolDomExpr stores the information needed for the actual post function: the variable x and lower and upper bounds l and u. Note that our class does not need a destructor, it is only shown as a reminder for classes that actually need a destructor!
The post() member function is defined as follows:
virtual void post(Home home, BoolVar b, bool neg,
const IntPropLevels& ipls) {
(void) ipls;
if (neg) {
const int nlu[2][2] = { { Int::Limits::min, l-1 },
{ u+1, Int::Limits::max } };
dom(home, x, IntSet(nlu,2), b);
} else {
dom(home, x, l, u, b);
}
}
where the posted expression must constrain the Boolean variable b with integer propagation level ipl. If neg is true, a negated constraint must be posted.
7.2. Matrix interface for arrays¶
MiniModel provides a Matrix support class for accessing an array as a two dimensional matrix. The following
IntVarArgs x(n*m);
...
Matrix<IntVarArgs> mat(x, n, m);
declares an array of integer variables x and superimposes a matrix interface to x called mat with width n and height m. Note that the first argument specifies the number of columns, and the second argument specifies the number of rows.
The elements of the array can now be accessed at positions \(\langle i,j\rangle\) in the matrix mat (that is, the element in column \(i\) and row \(j\)) using
IntVar mij = mat(i,j);
Furthermore, the rows and columns of the matrix can be accessed using mat.row(i) and mat.col(j). If a rectangular slice is required, the slice() member function can be used.
A matrix interface can be declared for any standard array or argument array used in Gecode, such as IntVarArray or IntSetArgs.
As an example of how the Matrix class can be used, consider the Sudoku problem (see Solving Sudoku puzzles using integer constraints). Given that there is a member IntVarArray x that contains \(9\cdot 9\) integer variables with domain \(\{1,\ldots,9\}\), the following code posts constraints that implement the basic rules for a Sudoku.
Matrix<IntVarArray> m(x, 9, 9);
for (int i=0; i<9; i++)
distinct(home, m.row(i));
for (int i=0; i<9; i++)
distinct(home, m.col(i));
for (int i=0; i<9; i+=3)
for (int j=0; j<9; j+=3)
distinct(home, m.slice(i, i+3, j, j+3));
For more examples that use the Matrix class, see Crossword puzzle, Social golfers, Kakuro, Nonogram, Magic squares, and Nonogram.
Element constraints. A matrix can also be used with an element constraint that propagates information about the row and column of matrix entries.
For example, the following code assumes that x is an integer array of type IntArgs with 12 elements.
Matrix<IntArgs> m(x, 3, 4);
IntVar r(home,0,2), c(home,0,3), v(home,0,1024);
element(home, m, r, c, v);
constrains the variable v to the value at position \(\langle \mathtt{r},\mathtt{c}\rangle\) of the matrix m (see also GCCat: element_matrix).
7.3. Support for cost-based optimization¶
Support for cost-based optimization provides several subclasses of Space for cost-based optimization. IntMinimizeSpace and IntMaximizeSpace support search for a solution of minimal and maximal, respectively, integer cost. FloatMinimizeSpace and FloatMaximizeSpace support search for a solution of minimal and maximal, respectively, float cost, possibly with an improvement step (see below). IntLexMinimizeSpace and IntLexMaximizeSpace support search for the lexicographically smallest and largest solution where the cost is defined as an array of integer variables.
Optimizing integer cost. The classes IntMinimizeSpace and IntMaximizeSpace support searching a solution of minimal and maximal, respectively, integer cost.
In order to use these abstract classes, a class inheriting from IntMinimizeSpace and IntMaximizeSpace must implement a virtual cost function of type
virtual IntVar cost(void) const { ... }
The function must return an integer variable for the cost. For an example, see Using a cost function.
Optimizing float cost with improvement step. The classes FloatMinimizeSpace and FloatMaximizeSpace support searching a solution of minimal and maximal, respectively, float cost.
Note that the constructor of these classes take an optional argument of type FloatNum that defines the improvement step: a better solution is found only if it is better than the previous solution and the improvement step. For example, suppose
class WithStep : public FloatMinimizeSpace {
public:
WithStep(void) : FloatMinimizeSpace(0.25), ... {
...
}
};
that searching for a best solution of WithStep finds a solution s with cost value c=s.cost().val(). Then, the next solution must have a cost that is strictly smaller than \(\mathtt{c}-\mathtt{s}\). For FloatMaximizeSpace, the next solution must have a cost that is strictly larger than \(\mathtt{c}+\mathtt{s}\).
Lexicographically optimizing for integer costs. The classes IntLexMinimizeSpace and IntLexMaximizeSpace support searching for a solution with lexicographically smallest and largest cost. The cost is defined by an array of integer variables.
In order to use these abstract classes, a class inheriting from IntLexMinimizeSpace and IntLexMaximizeSpace must implement a virtual cost function of type
virtual IntVarArgs cost(void) const { ... }
The function must return an array of integer variable as cost. For an example, see Locating warehouses.
7.4. Regular expressions for extensional constraints¶
operation |
meaning |
|---|---|
|
initialize |
|
initialize |
|
initialize |
|
|
|
|
|
efficient shortcut for |
|
efficient shortcut for |
|
repeat |
|
repeat |
|
repeat |
|
repeat |
Figure 7.7 Constructing regular expressions (r and s are regular expressions, n and m are unsigned integers)¶
Regular expressions are implemented as instances of the class REG and provide an alternative, typically more convenient, interface for the specification of extensional constraints than DFAs do. The construction of regular expressions is summarized in Figure 7.7.
Let us reconsider the Swedish drinking protocol from Extensional constraints. The protocol can be described by a regular expression r constructed by
REG r = *REG(0) + *(REG(1) + +REG(0));
A sequence of activities x (an integer or Boolean variable array) can be constrained by
DFA d(r);
extensional(home, x, d);
after a DFA for the regular expression has been computed.
For examples on using regular expressions for extensional constraints, see the nonogram case study in Nonogram or the examples Solitaire domino, Nonogram, and Pentominoes. The models are based on ideas described in [26], where regular expressions for extensional constraints nicely demonstrate their usefulness.
7.5. Channeling functions¶
Channel functions are functions to channel a Boolean variable to an integer variable and vice versa, to channel a float variable to an integer variable, and to channel between integer variables and a set variable.
For an integer variable x,
channel(home, x);
returns a new Boolean variable that is equal to x. Likewise, for a Boolean variable x an equal integer variable is returned.
For a float variable x, channel(home, x) returns an integer variable equal to x.
For an array of integer variables x, channel(home, x) returns a set variable equal to all the integers in x.
7.6. Aliases for integer constraints¶
alias |
constraint posted |
GCCat |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Figure 7.8 Aliases for integer constraints (x and y are integer variable arrays, u and v are integers or integer variables, r is an integer relation type, s is an integer set)¶
Aliases for integer constraints provide some popular aliases. Figure 7.8 lists the aliases and their corresponding definitions.
7.7. Aliases for set constraints¶
Aliases for set constraints provide aliases and convenience post functions for useful set constraints.
channel(home, x, y) is an alias for rel(home, SOT_UNION, x, y), posting the constraint that y is exactly the set of integers \(\{\mathtt{x}_0,\dots,\mathtt{x}_{|\mathtt{x}|-1}\}\). In addition to the union constraint, it posts an nvalues constraint for stronger propagation (see Number of values constraints).
range(home, x, y, z), where x is an array of integer variables and y and z are set variables, is an alias for element(home, SOT_UNION, x, y, z). This constraints treats x as defining a function, and constrains z to be the range of the function restricted to y:
Conversely, roots(home, x, y, z) constrains y to be the roots of the elements in z, i.e., those indices mapping to elements in z:
(see also GCCat: roots).