Overview | Group | Tree | Graph | Index | Concepts |
This function can be used to define simple constraints that are not predefined in Solver
for use during a Solver search, for example, inside a goal or constraint. (For similar
functionality to use in an IBM® ILOG® Concert Technology model, see IloTableConstraint
.)
This function creates and returns a constraint. That constraint is defined
vars
;x
and y
.The semantics of that generic constraint can be indicated in either one of several ways:
predicate
, of course, indicates that predicate;
set
indicates the combinations of values that satisfy the constraint, and the argument
compatible
must be IlcTrue
;
set
indicates the unsatisfactory combinations of values, and the argument
compatible
must be
IlcFalse
;y
equal to the element of
the array a
at the index indicated by x
. In other
words, y=a[x];
. This kind of constraint is sometimes known as an
element constraint.The order of the constrained variables in the array vars
is important because
the same order is respected in the predicate
or the set
. That is,
IlcTableConstraint
passes an array of values to the member function isTrue
for a predicate or to the member function isIn
for a set, where the first such value is a
value of vars[0]
, the second is a value of vars[1]
, and in general, the
ith value is a value of the constrained variable vars[i]
.
This function will throw an exception (an instance of IloSolver::SolverErrorException
)
if any of the following conditions occur:
predicate
as an argument but the size of the array of constrained variables is greater than three;
vars
is different from the size of the
set
.This function reduces domains efficiently, but it may take some time to do so. The time it needs
for domain reduction depends on the size of the domains of the constrained variables in vars
.
Programming Hint
Solver does not copy the array a
when a
is an instance of
IlcConstAnyArray
, IlcConstFloatArray
,
or IlcConstIntArray.
When a is an instance of one of those constant array
classes, then Solver will share the array among constraints instead of copying it. In fact, if you want to
build a table constraint of the form y=a[x]
, we strongly recommend that you should use only
the function that takes an instance of IlcConstFloatArray
or
IlcConstIntArray
as an argument. In that spirit, you can build an
instance of IlcConstIntArray
from an instance of
IlcIntArray
, or an instance of IlcConstFloatArray
from an instance of IlcFloatArray.
Examples:
The following code defines a constraint of arity four such that only these combinations of
values are allowed: (0, 1, 1, 2)
, (1, 0, 2, 3)
, and (0, 0, 2, 1)
.
IlcIntTupleSet set(s,4); set.add(IlcIntArray(s,4,0,1,1,2)); set.add(IlcIntArray(s,4,1,0,2,3)); set.add(IlcIntArray(s,4,0,0,2,1)); set.close(); IlcIntVar x(s,0,1),y(s,0,1),z(s,0,3),t(s,0,2); IlcIntVarArray vars(s,4,x,y,z,t);
Inside a goal or constraint now, you can post that constraint by adding it to an instance of
IloSolver
, like this:
s.add(IlcTableConstraint(vars,set,IlcTrue));
The following code defines a constraint of arity three. It uses a predicate that is true if the three variables are pairwise different or the sum of the first two constrained variables is equal to the third variable.
IlcBool IlcIntPredicateI::isTrue(IlcIntArray val) { return((val[0] != val[1] && val[1] != val[2] && val[0] != val[2]) || (val[0] + val[1] == val[2])); } IlcIntVar x(s, 0, 3), y(s, 0, 3), z(s, 0, 3); IlcIntVarArray vars(s, 3, x, y, z);
Inside a goal or constraint now, you can post this constraint by adding it to an instance
of IloSolver
, like this:
s.add(IlcTableConstraint(vars, Predicate(s));
See Also:
ILCANYPREDICATE0, IlcAnyTupleSet, IlcConstFloatArray, IlcConstIntArray, IlcConstraint, ILCINTPREDICATE0, IlcIntPredicate, IlcIntTupleSet, IloTableConstraint