# 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

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

- Division

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

- Add

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

- Subtract

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

- Modulo

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

- Equal

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

- Not Equal

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.expr1 != expr2

- Less than or Equal

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.expr1 <= expr2

- Less than

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

- Greater than or Equal

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.expr1 >= expr2

- Greater than

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

- Logical And

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.expr1 & expr2

- Logical Or

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.expr1 | expr2

### Other constraints

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

- Sum

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.Sum(VarArray)

Sum([var1, var2, var4])

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

- AllDIff

The classic global constraint, this constraints all the variables specified as a list argument to take on different values.AllDiff(VarArray)

AllDiff([var1, var2, var3])

- 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

### Optimisation

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

model.add(Minimise(obj)) # For minimisation

where obj is an expression representing what needs to be optimised.