Numberjack Variables

Variables

Variables in Numberjack are specified by the constructors

var1 = Variable() # Creates a binary variable
var2 = Variable(lower, upper) # Creates a variable with domain [lower ... upper]
var3 = Variable([3,4,6]) # Creates a variable with a domain of the specified list

Variables are by default integer variables, special cases of continuous variables are possible but solver specific. They will be covered later on.

Expressions

Variable objects are expressions. Pretty much everything in Numberjack is an expression. Expressions have the following available methods

  •         get_value()
           
    If the problem has been solved returns the value of the expression. Return value is undefined if the problem is infeasable. Return type is also undefined if the problem has yet to be solved.
  •         __str__()
           
    Returns a string representation of the expression. If the expression has yet to be solved the string will be a representation of the model in string formet. For example a Sum expression might have the string representation of "Sum(x1 in [0,5], x2 in [1,3], x3 in [4,8])" where as a variable would have the string represention of "xN in [domain]" before solving and it's value after solving. This method allows printing of the objects with meaningful output.

There are many more methods to the Expression class, these are just the basics. We will cover more later.

VarArray

It is possible to create lists of variables with the same bounds all at once with the VarArray object.

vars1 = VarArray(5) # Creates a list of five binary variables
vars2 = VarArray(5, lower, upper) # Creates a list of five variables with domains
                                  # [lower, upper]

The VarArray object has some useful operators overridden:

  •                 vars1 < vars2
                   
    This creates a Less Lex constraint. Normally used for symmetry breaking.
  •                 vars1 <= vars2
                   
    Creates the Leq Lex constraint.
  •                 vars1 > vars2
                   
    Creates the Gt Lex constraint.
  •                 vars1 >= vars2
                   
    Creates the Geq Lex constraint.
  •                 vars1[variable1]
                   
    Creates the Element expression. The value of this expression will be the value of the $n^{th}$ element of vars1 where n is equal to the value of variable1.
  •                 vars1[n]
                   
    On the other hand returns the $n^{th}$ element of the array, if n is an integer.
  •                 __str__()
                   
    This returns a string representation of the VarArray, it is the concatenation of the string representations of all the variables in the array. This methods allows printing of the object with sensible output.

Matrix

The Matrix construct can be seen to be an extension of the VarArray construct. It provides a method of creating and manipulating two dimensional matrices of variables.

square = Matrix(n, m) # Creates a n by m matrix of binary variables
square = Matrix(n, m, l, u) # Creates a n by m matrix if variables in domain [l,u]

The matrix construct, like the VarArray has many useful operators overridden.

  •                 __str__()
                   
    Returns the string representation of the matrix, it is the concatenation of the string representations of the Matrix's VarArray objects and the newline character. This methods allows printing of the object with sensible output.
  •                 matrix.flat
                   
    This is a list representing the flattened matrix.
  •                 matrix.row
                   
    This is a list representing the rows of the matrix
  •                 matrix.col
                   
    This is a list representing the columns of the matrix
  •                 matrix[n]
                   
    This returns the $n^{th}$ VarArray of the matrix
  •                 matrix[(expr1, expr2)]
                   
    This creates an element expression. If both expr1 and expr2 are indeed expressions then
    the expression will be
                    matrix.flat[(expr1 * len(matrix.col)) + expr2]
                   

    If one of expr1 or expr2 are integers then the element construct is on the rows or columns of the integer index with the
    expression being variable. i.e
                    matrix.row[expr1][expr2] # If expr1 is an integer
                    matrix.col[expr2][expr1] # If expr2 is an integer