ILOG CPLEX 11.0 User's Manual > Discrete Optimization > Using Logical Constraints: Food Manufacture 2 > Formulating Logical Constraints

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 ILOG CPLEX.

// Logical constraints
         // The food cannot use more than 3 oils
         // (or at least two oils must not be used)
         model.add((use[i][v1] == 0) + (use[i][v2] == 0) + (use[i][o1] == 0) +
                   (use[i][o2] == 0) + (use[i][o3] == 0) >= 2);
         // When an oil is used, the quantity must be at least 20 tons
         for (p = 0; p < nbProducts; p++)
            model.add((use[i][p] == 0) || (use[i][p] >= 20));
         // If products v1 or v2 are used, then product o3 is also used
         model.add(IloIfThen(env, (use[i][v1] >= 20) || (use[i][v2] >= 20),
           use[i][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) in C++:

model.add ( (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 in Optimization introduced overloaded logical operators that you can use to combine linear, semi-continuous, or piecewise linear constraints in ILOG CPLEX. In this example, notice the overloaded logical operators ==, >=, || that appear in these logical constraints.