Arithmetic expressions

Arithmetic expressions consist of arithmetic terms that are combined by arithmetic operations.

An arithmetic expression consists of arithmetic terms, such as x, x2, xy, or 3xy2, combined by arithmetic operations, such as addition, subtraction, multiplication, and division.

Arithmetic expressions can appear in arithmetic relations, such as equality and inequality. Arithmetic expressions can be linear (such as x < z + 3y) or nonlinear (such as x < z2 + y*z + 3y). CP Optimizer supports arithmetic constraints over integer decision variables. It also supports integer or numeric (floating-point) expressions.

Note:

CP Optimizer supports integer decision variables. Moreover, it is possible to constrain floating-point expressions. It is also possible to use a floating-point expression as a term in an objective function. For example, a floating-point cost function can be calculated from expressions by using integer decision variables, like this: cost = x/1000 + y/3.

There are no inherent restrictions on the magnitude of arithmetic operations that you can perform with CP Optimizer expressions and decision variables. The only limitation that you must bear in mind in your CP Optimizer application is any possibility of overflow due to the size and configuration of your platform (that is, limits on hardware and operating system). For example, if you multiply the largest possible integer available on your platform by the largest possible integer, you risk overflow because of limitations of your platform.

In the C++ API of CP Optimizer, there are overloaded operators for building arithmetic expressions and stating constraints over them. For many arithmetic operations, such as addition, exponentiation, or modular arithmetic, there are global functions that offer a variety of signatures that accommodate many combinations of decision variables and integer or numeric values in expressions.

In the Java™ API of CP Optimizer, there are methods to enable you to build arithmetic expressions to state constraints. Many such methods are defined in the interface IloModeler, implemented in the class IloCP.

Likewise, in the C# API of CP Optimizer, there are similar methods for building arithmetic expressions and stating constraints over them. The interface IModeler defines these methods, and the class CP inherits them.

An arithmetic constraint involves one or more decision variables in an arithmetic expression.

Table 1 summarizes the methods that support arithmetic operations to create expressions to use in constraints. Table 2 summarizes the methods and functions that return expressions for use in constraints. Table 3 summarizes the methods that implement arithmetic constraints. The reference manuals of the application programming interfaces (APIs) document each method, operator or global function more fully.

In those tables, the names of applications where the expressions or constraints are used appear in parentheses, like this (sports.cpp). The applications are part of the product, available in the appropriate subdirectory of the examples directory of your installation.

Table 1. Arithmetic operations for use in constraints
Arithmetic operation C++ API Java API C# API
addition

operator+

IloSum

(sports.cpp)

IloModeler.sum

(Facility.java)

IModeler.Sum

(Steelmill.cs)

subtraction

operator-

IloDiff

IloModeler.diff

(Sports.java)

IModeler.Diff

(Sports.cs)

multiplication operator*

IloModeler.prod

(Facility.java)

IModeler.Prod

(Facility.cs)

scalar product

IloScalProd

(facility.cpp)

IloModeler.scalProd IModeler.ScalProd
integer division

IloDiv

(sports.cpp)

IloCP.div

(Sports.java)

CP.Div

(Sports.cs)

floating-point division operator/ IloCP.quot CP.Quot
modular arithmetic

operator%

(sports.cpp)

IloCP.modulo

(Sports.java)

CP.Modulo

(Sports.cs)

Table 2. Arithmetic expressions for use in constraints
Expression C++ API Java API C# API
standard deviation IloStandardDeviation IloCP.standardDeviation CP.StandardDeviation
minimum

IloMin

(talent.cpp)

IloModeler.min

(Sports.java)

IModeler.Min

(Sports.cs)

maximum IloMax IloModeler.max IModeler.Max
counting

IloCount

(teambuilding.cpp)

IloCountDifferent

(alloc.cpp)

IloCP.count

(Facility.java)

IloCP.countDifferent

(Alloc.java)

CP.Count

(Sports.cs)

CP.CountDifferent

(Alloc.cs)

absolute value

IloAbs

(sports.cpp)

IloModeler.abs

(Sports.java)

IModeler.Abs

(Sports.cs)

element or index IloElement

IloCP.element

(Facility.java)

CP.Element

(Steelmill.cs)

Table 3. Arithmetic constraints
Arithmetic constraint C++ API Java API C# API
equal to operator== IloModeler.eq IModeler.Eq
not equal to operator!= IloCP.neq CP.Neq
strictly less than operator< IloCP.lt CP.Lt
strictly greater than operator> IloCP.gt CP.Gt
less than or equal to operator<= IloModeler.le IModeler.Le
greater than or equal to operator>= IloModeler.ge IModeler.Ge