Arithmetic constraints

Arithmetic constraints are created by using arithmetic operators between expressions and decision variables.

Arithmetic constraints can be created in a variety of ways.

In the C++ API, you can express constraints between expressions using the following operators:

  • equality (==),

  • less than or equal to (<=),

  • less than (<),

  • greater than or equal to (>=),

  • greater than (>) and

  • not equal to (!=).

For example, you can write a constraint that one expression is not equal to another in the C++ API:


      x[1] + 2*x[2] + 3*x[3] != 4*x[1]*x[2]

Using the Java™ API, the constraint could be written as:


        cp.neq(cp.sum(x[1], 
                      cp.sum(cp.prod(2,x[2]), cp.prod(3,x[3]))) , 
                             cp.prod(4,cp.prod(x[1],x[2])))

Likewise, using the C# API, the constraint could be written as:


        cp.Neq(cp.Sum(x[1], 
                      cp.Sum(cp.Prod(2,x[2]), cp.Prod(3,x[3]))) , 
                             cp.Prod(4,cp.Prod(x[1],x[2])))

Explicitly, this constraint enforces that the values the CP Optimizer search assigns to the decision variables x[1], x[2] and x[3] must be such that the expression x[1] + 2*x[2] + 3*x[3] does not equal the expression 4*x[1]*x[2].

After you create a constraint, you must explicitly add it to the model in order for it to be taken into account by the optimizer.

For more details on expressing constraints, refer to the section Arithmetic constraints and expressions.