# Constraints

### Constraints

Numberjack supports an ever growing catalog of constraints. Not all constraints are implemented in every solver though, for example an element constraint is pretty tricky to implement in a linear solver. We are constantly working on adding support for constraints in solvers.

### Binary Operators

All binary operator constraints are implemented by overriding the associated function of the expression class. These operators then create an expression that can be used in expression trees.

• Multiplication
expr1 * expr2

Creates an expression that had the value of expr1 * expr2, expr2 can be either a variable or a constant.
• Division
expr1 / expr2

Creates an expression that had the value of expr1 / expr2, expr2 can be either a variable or a constant.
expr1 + expr2

Creates an expression that had the value of expr1 + expr2, expr2 can be either a variable or a constant.
• Subtract
expr1 - expr2

Creates an expression that had the value of expr1 - expr2, expr2 can be either a variable or a constant.
• Modulo
expr1 % expr2

Creates an expression that had the value of expr1 \% expr2, expr2 can be either a variable or a constant.
• Equal
expr1 == expr2

Creates an expression that had the value of 1 if expr1 $=$ expr2 and 0 otherwise, expr2 can be either a variable or a constant.
• Not Equal
expr1 != expr2

Creates an expression that had the value of 1 if expr1$\neq$ expr2 and 0 otherwise, expr2 can be either a variable or a constant.
• Less than or Equal
expr1 <= expr2

Creates an expression that had the value of 1 if expr1$\leq$ expr2 and 0 otherwise, expr2 can be either a variable or a constant.
• Less than
expr1 < expr2

Creates an expression that had the value of 1 if expr1$<$ expr2 and 0 otherwise, expr2 can be either a variable or a constant.
• Greater than or Equal
expr1 >= expr2

Creates an expression that had the value of 1 if expr1 $\geq$ expr2 and 0 otherwise, expr2 can be either a variable or a constant.
• Greater than
expr1 > expr2

Creates an expression that had the value of 1 if expr1 $>$ expr2 and 0 otherwise, expr2 can be either a variable or a constant.
• Logical And
expr1 & expr2

Creates an expression that had the value of 1 if expr1 \& expr2 and 0 otherwise. Both expr1 and expr2 are expected to binary variables. The constraint is undefined if this is not the case.
• Logical Or
expr1 | expr2

Creates an expression that had the value of 1 if expr1 $|$ expr2 and 0 otherwise. Both expr1 and expr2 are expected to binary variables. The constraint is undefined if this is not the case.

### Other constraints

There are many other constraints available in Numberjack. These though are specified as function calls not as operators on Variables.

• Sum
Sum(VarArray)
Sum([var1, var2, var4])
Sum([var1, var2, var3], [coef1, coef2, coef3])

There are many ways of specifying the Sum constraint. It is an expression representing the sum of the variables, or the sum of the variables times the coefficients if there are coefficients provided. Something worth noting is that the Sum constraint is intelligent, in that it parses the expression trees below it to see if there are + or * operators or other Sum constraints under it that could be integrated in itself. This allows for some elegant specification of scalar products that is still efficient.
• AllDIff
AllDiff(VarArray)
AllDiff([var1, var2, var3])

The classic global constraint, this constraints all the variables specified as a list argument to take on different values.
• Element

We have already covered the element constraint in the VarArray and Matrix sections.

### Reification

Since all boolean constraints are also expressions reification is possible in Numberjack. All you have to do is treat these constraints as expressions. For example to count the number of times 5 appears in a VarArray all you need to do is

Sum(var == 5 for var in VarArray)

### Optimisation

Optimisation is possible in Numberjack. To specify an objective function to be maximised or minimised just use the following functions