The element expression
The constraint propagation algorithm for element expressions reduces domains efficiently.
The element expression indexes an array of values with a decision variable. This expression is used to associate a cost or a distance to the value of a variable, for example.
Suppose there is a decision variable x that chooses a delivery customer and suppose that the distances to the customers are:
7 for customer 1,
12 for customer 2,
5 for customer 3 and
21 for customer 4.
If the variable y is to be equal to the distance of the chosen customer, you can write:
IloIntArray distance(env, 4, 7, 12, 5, 21);
model.add(y == IloElement(distance, x));
This constraint states that y is equal to the x-th element of the distance array. In the C++ API, another way to state the same constraint is to write:
model.add(y == distance[x]);
This constraint achieves full domain reduction. In other words, after domain reduction, for each value of x there is a value for y that satisfies the constraint and vice-versa.
Here is an example of a model with one element expression:
IloIntVar x(env, 0, 3);
IloIntVar y(env, 0, 20);
IloModel model(env);
model.add(y == IloElement(IloIntArray(env, 4, 7, 12, 5, 21), x));
IloCP cp(model);
if ( cp.propagate() ) {
cp.out() << " Domains reduced: " << std::endl;
cp.out() << " Domain of x is " << cp.domain(x) << std::endl;
cp.out() << " Domain of y is " << cp.domain(y) << std::endl;
} else
cp.out() << " Model has no solution." << std::endl;
Running this code produces the output:
Domains reduced:
Domain of x is [0..2]
Domain of y is [5 7 12]
The value 3 is removed from the domain of x because the fourth element in the array is 21, which does not belong to the domain of y. The domain of y is reduced to the set of values that are indexed by values of x, that is 5, 7 and 12.