Elementary modifiers for variables
Elementary modifiers for decision variables are used to remove inconsistent values from domains.
A propagator reduces the domains of the variables involved in the
constraint propagator. In the C++ API, the modifiers for the domains
of decision variables are available as member functions of IloPropagatorI.
In the Java™ API, the modifiers
for the domains of integer variables are available as member functions
of IloCustomConstraint. In both APIs, these functions
are removeValue, setMax, setMin, setRange and setValue.
You can use these elementary modifiers to implement the inference
algorithm for your constraint propagator.
For example, consider how setValue, one of these
predefined modifiers, behaves when called on a decision variable x with
current domain [0..1]. In the C++ API, the code fragment:
setValue(x,1);
reduces the domain of x to a singleton (a single element) with the value 1. This behavior literally involves a domain reduction, and this behavior is consistent throughout CP Optimizer.
For example, consider the following constraint propagator written using the C++ API:
void ModConstraintI::execute () {
setValue(_x,1);
}
Adding this constraint propagator to the model and propagating:
IloIntVar x(env, 1, 3, "x");
model.add(ModConstraint(x));
IloCP cp(model);
if (cp.propagate())
cp.out() << cp.domain(x) << std::endl;
else
cp.out() << "No solution." << std::endl;
cp.end();
produces output that contains:
x[1]
If you attempt to set the value of a constrained variable to a value that is not in the current domain, this action leads to a failure in the search. Assume that the decision variable x has the domain of [2..3] and you set the value to 1. This leads to a failure, and the optimizer will no longer investigate the subtree of the current node. The output produced contains
No solution.
In the C++ API, the elementary modifiers used in a propagator are
member functions of the class IloPropagatorI. In
the Java API, the elementary
modifiers are member functions of the class IloCustomConstraint which
implements the interface IloPropagator.
In both APIs, the modifiers are:
setValue(IloIntVar x, IloInt value)tries to makexequal tovalue;setMin(IloIntVar x, IloInt min)tries to makexgreater than or equal tomin;setMax(IloIntVar x, IloInt max)tries to makexless than or equal tomax;setRange(IloIntVar x, IloInt min, IloInt max)tries to makexstay betweenminandmax, inclusive;removeValue(IloIntVar x, IlcInt value)tries to make the current domain ofxnot containvalue.
Elementary modifiers only reduce domains: they do not enlarge domains. For that reason, the following code does not modify the domain [0..1] of the decision variable and, in particular, it does not make the variable’s upper bound equal to 2:
setMax(x,2);
The next section shows how to take an implementation of an invariant and write a constraint propagator.