Variables Reference

class Numberjack.Variable(argopt1=None, argopt2=None, argopt3=None)

Creates a new variable. The following tables shows example calls to the constructor which results in different kinds of variables.

Variable()  # Binary variable
Variable(N)  # Variable in the domain of {0, N-1}
Variable('x')  # Binary variable called 'x'
Variable(N, 'x')  # Variable in the domain of {0, N-1} called 'x'
Variable(l,u)  # Variable in the domain of {l, u}
Variable(l,u, 'x')  # Variable in the domain of {l, u} called 'x'
Variable(list)  # Variable with domain specified as a list
Variable(list, 'x')  # Variable with domain specified as a list called x

To create a continuous variable just use float values instead of integer values when specifying the domain of the variable.

Note

Typically, just the Mixed Integer Programming solvers support continuous valued variables, other solvers use finite domains.

The variable’s domain may also be specified as a list of strings, and the interface between the solver’s representation will be handled by Numberjack. For example in a map-colouring problem, we may have something like the following:

v = Variable(['red', 'green', 'blue'])
# ... load and solve the model
v.get_value()  # Returns 'red'
domain(solver=None)

Returns a string representing the current domain of the expression.

Parameters:solver (NBJ_STD_Solver) – If specified, the solver for which this expression has been loaded. If not specified, the solver that has most recenlty loaded the expression will be used.
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.
Returns:The current domain of the expression.
Return type: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.
Returns:The current upper bound of the variable.
Return type: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.
Returns:The current lower bound of the expression.
Return type: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.
Returns:The size of the expression’s domain.
Return type: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.
Return type: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.
Returns:The current value of the expression.
Return type:The same as the original domain, either int, float, or str.
initial()

Returns a string representing the initial domain of the expression. For example:

var1 = Variable(0, 10)
print var1.initial()
>>> x0 in {0..10}
Returns:A String representation of original expression definition
Return type:str
name()

The name of the variable or the operator if this is an expression.

solution(solver=None)

Deprecated since version 1.1: Instead you should use get_value(), this function is equivalent to calling str() on that.

Returns a string containing the solution value of the expression. For a native representation of the solution value, use get_value()

Parameters:solver (NBJ_STD_Solver) – If specified, the solver from which the solution will be sourced, if None then the most recently loaded solver is used.
Returns:String representation of the expressions solution.
Return type:str
class Numberjack.VarArray(n, optarg1=None, optarg2=None, optarg3=None)

A VarArray is a list of Expression objects. Various methods are overloaded to allow easy declaration, formatted printing, and syntactic sugars for modelling. The following tables shows example calls to the constructor which results in different kinds of variable arrays.

VarArray(l)  # creates an array from a list l
VarArray(n)  # creates an array of n Boolean variables
VarArray(n, 'x')  # creates an array of n Boolean variables with
                  # names 'x0..xn-1'
VarArray(n, m)  # creates an array of n variables with domains [0..m-1]
VarArray(n, m, 'x')  # creates an array of n variables with domains
                     # [0..m-1] and names 'x0..xn-1'
VarArray(n, d)  # creates an array of n variables with domains specified
                # in the list 'd'
VarArray(n, d, 'x')  # creates an array of n variables with domains
                     # specified in the list 'd' and names 'x0..xn-1'
VarArray(n, l, u, 'x')  # creates an array of n variables with domains
                        # [l..u] and names 'x0..xn-1'
VarArray(n, l, u)  # creates an array of n variables with domains [l..u]

VarArray’s allow you to state Element and lexicographic ordering constraints over a sequence of variables using, respectively the operator ‘[]’ and ‘<’, ‘>’, ‘<=’, ‘>=’. For instance, given two VarArray X and Y, and an Expression x:

X[x]  # returns an Element expression, that is, a variable equal to the
      # xth element of the array X
X <= Y  # returns a LeqLex constraint between X and Y
__eq__(other)

Syntactic sugar for the equality constraint X == Y.

Parameters:other (VarArray) – Another VarArray of the same length.
Return type:A list of equality (Eq) expressions.
__ge__(other)

Syntactic sugar for the lexicographic order constraint LeqLex so it can be specified on two VarArray like so: X >= Y

Parameters:other (VarArray) – Another VarArray of the same length.
Return type:LeqLex
__gt__(other)

Syntactic sugar for the lexicographic order constraint LessLex so it can be specified on two VarArray like so: X > Y

Parameters:other (VarArray) – Another VarArray of the same length.
Return type:LessLex
__le__(other)

Syntactic sugar for the lexicographic order constraint LeqLex so it can be specified on two VarArray like so: X <= Y

Parameters:other (VarArray) – Another VarArray of the same length.
Return type:LeqLex
__lt__(other)

Syntactic sugar for the lexicographic order constraint LessLex so it can be specified on two VarArray like so: X < Y

Parameters:other (VarArray) – Another VarArray of the same length.
Return type:LessLex
domain(solver=None)

Returns a string representing the current state of the content of the array.

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.
Return type:str
initial()

Returns a string representing the initial definition of the content of the array.

Return type:str
name()

Returns a string containing a brief view of the content of the array.

Return type:str
solution(solver=None)

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

Returns a string containing the valuation of the content of the array.

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.
Return type:str
class Numberjack.Matrix(optarg1=None, optarg2=None, optarg3=None, optarg4=None, optarg5=None)

A Matrix is a two-dimensional list of variables or Expression objects. Various methods are overloaded to allow easy declaration, formatted printing, and syntactic sugars for modelling. The following tables shows example calls to the constructor which results in different kinds of matrices.

Matrix(l)  # creates a Matrix from a list l
Matrix(n, m)  # creates a n x m Matrix of Boolean variables
Matrix(n, m, 'x')  # creates a n x m Matrix of Boolean variables with
                   # names 'x0.0..xn-1.m-1'
Matrix(n, m, u)  # creates a n x m Matrix of variables with domains
                 # [0..u-1]
Matrix(n, m, u, 'x')  # creates a n x m Matrix of variables with
                      # domains [0..u-1] and names 'x0.0..xn-1.m-1'
Matrix(n, m, l, u)  # creates a n x m Matrix of variables with domains
                    # [l..u]
Matrix(n, m, l, u, 'x')  # creates a n x m Matrix of variables with
                         # domains [l..u] and names 'x0.0..xn-1.m-1'

Matrices feature specific handlers to access (subsets of) rows and columns. The fields row, col, and flat respectively refer to the list of rows, columns and cells in the matrix. For instance:

m = Matrix(5,4,1,3,'cell_')
print m
>>> [[cell_0.0, cell_0.1, cell_0.2, cell_0.3],
>>>  [cell_1.0, cell_1.1, cell_1.2, cell_1.3],
>>>  [cell_2.0, cell_2.1, cell_2.2, cell_2.3],
>>>  [cell_3.0, cell_3.1, cell_3.2, cell_3.3],
>>>  [cell_4.0, cell_4.1, cell_4.2, cell_4.3]]
print m.row
>>> [[cell_0.0, cell_0.1, cell_0.2, cell_0.3],
>>>  [cell_1.0, cell_1.1, cell_1.2, cell_1.3],
>>>  [cell_2.0, cell_2.1, cell_2.2, cell_2.3],
>>>  [cell_3.0, cell_3.1, cell_3.2, cell_3.3],
>>>  [cell_4.0, cell_4.1, cell_4.2, cell_4.3]]
print m.col
>>> [[cell_0.0, cell_1.0, cell_2.0, cell_3.0, cell_4.0],
>>>  [cell_0.1, cell_1.1, cell_2.1, cell_3.1, cell_4.1],
>>>  [cell_0.2, cell_1.2, cell_2.2, cell_3.2, cell_4.2],
>>>  [cell_0.3, cell_1.3, cell_2.3, cell_3.3, cell_4.3]]
print m.flat
>>> [cell_0.0, cell_0.1, cell_0.2, cell_0.3, cell_1.0, cell_1.1, ...]

Matrices support Element constraints on row, column or flatten views.

domain(solver=None)

Returns a string representing the current state of the content of the matrix.

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.
Return type:str
initial()

Returns a string representing the initial definition of the content of the matrix.

Return type:str
name()

Returns a string containing a brief view of the content of the matrix.

Return type:str
solution(solver=None)

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

Returns a string containing the valuation of the content of the array.

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.
Return type:str

Previous topic

Installing Numberjack

Next topic

Constraints Reference

This Page