# Logical constraints for CPLEX

Describes the use of logical constraints in OPL.

Logical constraints are one particular kind of discrete or numerical constraints. OPL and CPLEX can translate logical constraints automatically into their transformed equivalent that the discrete (MIP) or continuous (LP) optimizers of IBM ILOG CPLEX can process efficiently. This section describes all the available logical constraints, as well as the logical expressions that can be used in logical constraints. Logical constraints are available in constraint programming models without linearization.

For an example of how OPL uses logical constraints, see Tutorial: Using CPLEX logical constraints in the Language User’s Manual.

In this section, you will learn:

## What are logical constraints?

For IBM ILOG CPLEX, a logical constraint combines linear constraints by means of logical operators, such as logical-and, logical-or, implication, negation (not), conditional statements (if ... then ...) to express complex relations between linear constraints. IBM ILOG CPLEX can also handle certain logical expressions appearing within a linear constraint. One such logical expression is the minimum of a set of variables. Another such logical expression is the absolute value of a variable. There’s more about logical expressions in Which nonlinear expressions can be extracted?.

## What can be extracted from a model with logical constraints?

The following table lists the logical constraints that CPLEX can extract.

Symbol Meaning
&& Logical AND
| | Logical OR
! Logical NOT
=> Imply
!= Different from
== Equivalence

All those constructs accept as their arguments other linear constraints or logical constraints, so you can combine linear constraints with logical constraints in complicated expressions in your application.

Note:

With the C++ API you should use the symbol <= to express implication.

## Which nonlinear expressions can be extracted?

Some expressions are easily recognized as nonlinear, for example, a function such as

``x^2 + y^2 = 1``

However, other nonlinearities are less obvious, such as absolute value as a function. In a very real sense, MIP is a class of nonlinearly constrained problems because the integrality restriction destroys the property of convexity which any linear constraints otherwise might possess. Because of that characteristic, certain (although not all) nonlinearities are capable of being converted to a MIP formulation, and thus can be solved by IBM ILOG CPLEX. The following nonlinear expressions are accepted in an OPL model:

• min and minl : the minimum of several numeric expressions

• max and maxl : the maximum of several numeric expressions

• abs : the absolute value of a numeric expression

• piecewise : the piecewise linear combination of a numeric expression

• A linear constraint can appear as a term in a logical constraint.

In fact, ranges containing logical expressions can, in turn, appear in logical constraints. It is important to note here that only linear constraints can appear as arguments of logical constraints extracted by IBM ILOG CPLEX. That is, quadratic constraints are not handled in logical constraints. Similarly, quadratic terms can not appear as arguments of logical expressions such as min, max, abs, and piecewise.

## Logical constraints for counting

In many cases it is even unnecessary to allocate binary variables explicitly in order to gain the benefit of linear constraints within logical expressions. For example, optimizing how many items appear in a solution is often an issue in practical problems. Questions of counting (how many?) can be represented formally as cardinality constraints. Suppose that your application includes three variables, each representing a quantity of one of three products, and assume further that a good solution to the problem means that the quantity of at least two of the three products must be greater than 20. Then you can represent that idea in your application, like this:

``(x[0] >= 20) + (x[1] >= 20) + (x[2] >= 20) >= 2;``

## How are logical constraints extracted?

Logical constraints are transformed automatically into equivalent linear formulations when they are extracted by an IBM ILOG CPLEX algorithm. This transformation involves automatic creation by IBM ILOG CPLEX of new variables and constraints. For more details on this transformation, refer to the IBM ILOG CPLEX documentation.