Using logical constraints
Describes how logical constraints are automatically transformed in OPL as based on the CPLEX solving engine.
You have already seen how to represent the logical constraints of this problem in What are the constraints?. However, they deserve a second glance because they illustrate an important point about logical constraints and automatic transformation in OPL as based on the CPLEX solving engine.
forall( m in Months ) {
// Using some constraints as boolean expressions to state that at least
// 2 of the given 5 constraints must be true.
ctUse7:
(Use[m]["v1"] == 0) + (Use[m]["v2"] == 0) + (Use[m]["o1"] == 0) +
(Use[m]["o2"] == 0) + (Use[m]["o3"] == 0) >= 2;
// Using the "or" operator, set each Use variable to be
// zero or greater than 20.
forall( p in Products )
ctUse8:
(Use[m][p] == 0) || (Use[m][p] >= 20);
// Using "or" and "implication" operator, set that if one of 2 given products
// is used more than 20 then a third one must be used more than 20 too.
ctUse9:
(Use[m]["v1"] >= 20) || (Use[m]["v2"] >= 20) => Use[m]["o3"] >= 20;
Consider, for example, the constraint that the blended product cannot use more than three oils in a batch. Given that constraint, many programmers might naturally write the following statement (or something similar):
(use[i][v1] != 0)
+ (use[i][v2] != 0)
+ (use[i][o1] != 0)
+ (use[i][o2] != 0)
+ (use[i][o3] != 0)
<= 3;
That statement expresses the same constraint without changing the set of solutions to the problem. However, the formulations are different and can lead to different running times and different amounts of memory used for the search tree. In other words, given a logical English expression, there may be more than one logical constraint for expressing it, and the different logical constraints may perform differently in terms of computing time and memory.
Logical constraints for
CPLEX in the section Constraints of the Language
Reference Manual introduces overloaded logical operators
that you can use to combine linear, or piecewise linear constraints
in OPL. In this example, notice the logical operators ==, >=, || that
appear in these logical constraints.