# Constraints Reference¶

## Single operand predicates¶

class Numberjack.Predicate(vars, op)

All constraints in Numberjack extend the this class. It provides accessors to get information about the predicate trees and the variables the constraints constrain.

domain(solver=None)

Returns a string representing the current domain of the predicate.

Parameters: solver (NBJ_STD_Solver) – If specified, the solver from which the state will be sourced, if None then the most recently loaded solver is used. str
get_domain(solver=None)

Creates a new Domain instance representing the current domain of the expression.

Parameters: solver (NBJ_STD_Solver) – If specified, the solver from which the domain will be sourced, if None then the most recently loaded solver is used. The current domain of the expression. Domain
get_max(solver=None)

Current upper bound of variable.

Parameters: solver (NBJ_STD_Solver) – If specified, the solver from which the upper bound will be sourced, if None then the most recently loaded solver is used. The current upper bound of the variable. The same as the original domain, either int, float, or str.
get_min(solver=None)

Current lower bound of the expression.

Parameters: solver (NBJ_STD_Solver) – If specified, the solver from which the lower bound will be sourced, if None then the most recently loaded solver is used. The current lower bound of the expression. The same as the original domain, either int, float, or str.
get_size(solver=None)

The current size of the expression’s domain.

Parameters: solver (NBJ_STD_Solver) – If specified, the solver from which the domain size will be sourced, if None then the most recently loaded solver is used. The size of the expression’s domain. int
get_solver()

Returns the solver with which this expression was last loaded.

Returns: The last loaded solver, or None if it has not been loaded anywhere. NBJ_STD_Solver
get_value(solver=None)

The current value of the expression. Should be used to retrieve the assigned value of a variable and the value of expressions like the objective function.

In the case of variables, it may not be passed in to the solver if it is not involved in a non-trivial constraint. For example, x <= 1, x will not get added (by this constraint alone) if it has a upper bound which is less or equal to 1. The variable’s lower bound will be returned as the value in this case.

Parameters: solver (NBJ_STD_Solver) – If specified, the solver from which the value will be sourced, if None then the most recently loaded solver is used. The current value of the expression. The same as the original domain, either int, float, or str.
initial()

Returns a string representing the initial definition of the content of the predicate and all its children. For example:

var1 = Variable(0, 10)
constraint = var1 < 10
print constraint.initial()
>>> (x0 < 10)

Return type: str
name()

Returns a string that represents the name of the Predicate and the name of all its children.

Return type: str
solution(solver=None)

Deprecated since version 1.1: Instead you should use Expression.get_value() and call str() on that.

Returns a string containing the valuation of the predicate.

Parameters: solver (NBJ_STD_Solver) – If specified, the solver from which the state will be sourced, if None then the most recently loaded solver is used. str
class Numberjack.Neg(vars)

Negate expression, used to negate another expression. It is equivalent to multiplying by -1.

var = Variable(1, 10)

class Numberjack.Abs(vars)

Absolute expression, represents the absolute value of an expression or variable.

var = Variable(-5, 5)


## Binary constraints¶

Each of the following classes take two sub-expressions or variables as parameters and can usually be created by operator overloading. For example x * y will create a Numberjack.Mul instance if either x or y are Numberjack variables.

class Numberjack.BinPredicate(vars, op)

All binary predicates such as And, LessThan and GreaterThan extend this class. They are separated from the base Predicate class to facilitate ease of print representations of the predicates.

class Numberjack.And(vars)

Logical ‘and’ expression. Can be used at both the top-level to specify an ‘and’ constraint, or reified to equal the truth value of the relation.

var1 = Variable()  # Binary variable
var2 = Variable()  # Binary variable

model.add(var1 & var2)  # Post var1 And var2 constraint

var1 = Variable()  # Binary variable
var2 = Variable()  # Binary variable
var3 = Variable()  # Binary variable

model.add( var3 == (var1 & var2) )  # Used as an expression

class Numberjack.Or(vars)

Logical ‘or’ expression. Can be used at both the top-level to specify an ‘or’ constraint, or reified to equal the truth value of the relation.

var1 = Variable()  # Binary variable
var2 = Variable()  # Binary variable

model.add(var1 | var2)  # Post var1 Or var2 constraint

var1 = Variable()  # Binary variable
var2 = Variable()  # Binary variable
var3 = Variable()  # Binary variable

model.add( var3 == (var1 | var2) )  # Used as an expression

class Numberjack.Eq(vars)

Equality expression between two expressions, or an expression and a constant. It can be used as either a top-level hard constraint or reified as a sub-expression.

var1 = Variable(0, 10)
var2 = Variable(0, 100)
var3 = Variable()

model.add( var3 == (var1 == var2) )

class Numberjack.Ne(vars)

Disequality expression between two expressions, or an expression and a constant. It can be used as either a top-level hard constraint or reified as a sub-expression.

var1 = Variable(0, 10)
var2 = Variable(0, 100)
var3 = Variable()

model.add( var3 != (var1 != var2) )

class Numberjack.Lt(vars)

Less-than expression between two expressions, or an expression and a constant. It can be used as either a top-level hard constraint or reified as a sub-expression.

var1 = Variable(0, 10)
var2 = Variable(0, 100)
var3 = Variable()

model.add( var3 < (var1 < var2) )

class Numberjack.Le(vars)

Less than or equal expression between two expressions, or an expression and a constant. It can be used as either a top-level hard constraint or reified as a sub-expression.

var1 = Variable(0, 10)
var2 = Variable(0, 100)
var3 = Variable()

model.add( var3 <= (var1 <= var2) )

class Numberjack.Gt(vars)

Greater-than expression between two expressions, or an expression and a constant. It can be used as either a top-level hard constraint or reified as a sub-expression.

var1 = Variable(0, 10)
var2 = Variable(0, 100)
var3 = Variable()

model.add( var3 > (var1 > var2) )

class Numberjack.Ge(vars)

Greater than or equal expression between two expressions, or an expression and a constant. It can be used as either a top-level hard constraint or reified as a sub-expression.

var1 = Variable(0, 10)
var2 = Variable(0, 100)
var3 = Variable()

model.add( var3 >= (var1 >= var2) )

class Numberjack.Mul(vars)

Multiplication expression to equal the multiplication of the two operands. Cannot be used as a top-level constraint.

var1 = Variable(0, 10)
var2 = Variable(0, 100)

mulxp1 = var2 * var1
mulexp2 = var2 * 10


Warning

Cannot be used with all solvers and ConstraintNotSupportedError will be raised when loading the model if this is the case.

class Numberjack.Div(vars)

Division expression to equal the integral division of the two operands. Cannot be used as a top-level constraint.

var1 = Variable(0, 10)
var2 = Variable(0, 100)

divexp1 = var2 / var1
divexp2 = var2 / 10


Warning

Cannot be used with all solvers and ConstraintNotSupportedError will be raised when loading the model if this is the case.

class Numberjack.Mod(vars)

Modulus expression to equal the modulo two expressions or an expression and a constraint. Cannot be used as a top-level constraint.

var1 = Variable(0, 10)
var2 = Variable(0, 100)

modexp1 = var2 % var1
modexp2 = var2 % 10


Note

For MIP and SAT, the constraint is encoded such that the remainder takes the sign of the numerator, as per the C standard. This differs from Python where the remainder takes the sign of the denominator.

Warning

Cannot be used with all solvers and ConstraintNotSupportedError will be raised when loading the model if this is the case.

## Other constaints¶

class Numberjack.Table(vars, tuples=[], type='support')

Table constraint explicityly specifying the list of allowed or forbidden tuples. Must be used as a top-level constraint, it cannot be used as a sub-expression.

Parameters: vars (list) – the variables to be constrained by the constraint. tuples – list of tuples used for the table constraint. type – type of table constraint, either support of conflict

## Optimisation¶

class Numberjack.Maximise(vars)

Maximisation objective function, sets the goal of search to be the maximisation of its’ arguments.

Parameters: vars – The Variable or Expression to be maximized.
Numberjack.Maximize(var)

Alias for American spelling of Maximise.

class Numberjack.Minimise(vars)

Minimisation objective function, sets the goal of search to be the minimisation of its’ arguments.

Parameters: vars – The Variable or Expression to be minimized.
Numberjack.Minimize(var)

Alias for American spelling of Minimise.