Module docplex.cp.expression

This module contains the basic classes representing the expressions required to describe a constraint programming model.

In particular, it defines the following classes:

None of these classes should be created explicitly. There are various factory functions to do so, such as:

Moreover, some automatic conversions are also provided to generate CP Optimizer objects from Python objects. For example:

  • an integer is converted into CP constant,
  • an iterator of objects is converted in CP array of objects of the corresponding type,
  • a tuple of tuples is converted into tuple set.

Detailed description

class docplex.cp.expression.CpoAlias(expr, name)[source]

Bases: docplex.cp.expression.CpoExpr

CPO model expression representing a symbolic alias on an expression.

Constructor

Parameters:
  • expr – Target expression
  • name – Name of the alias
class docplex.cp.expression.CpoBoolVar(dom, name=None)[source]

Bases: docplex.cp.expression.CpoIntVar

is_binary()[source]

Check if the domain of this variable is reduced to boolean, 0, 1

Returns:True if this variable is a boolean variable
is_bool_var()[source]

Check if the domain of this variable is reduced to boolean, 0, 1

Returns:True if this variable is a boolean variable
class docplex.cp.expression.CpoExpr(type, name)[source]

Bases: object

This class is the super class of all classes representing a CPO expression.

Constructor

Parameters:
  • type – Expression type, object of class CpoType.
  • name – Expression name.
compare(other)[source]

Compare this expression with another.

Parameters:other – Other CpoExpr to compare with.
Returns:integer <0 if self < other, 0 if self == other, >0 if self > other
equals(other)[source]

Checks the equality of this expression with another object.

Implementation is required with a different name than __eq__ name because this function is already overloaded to construct model expression with operator ‘==’.

Parameters:other – Other object to compare with.
Returns:True if ‘other’ is equal to this object, False otherwise.
get_max_depth()[source]

Gets the maximum expression depth.

Returns:Max expression depth.
get_name()[source]

Get the name of the expression.

Returns:Name of the expression, None if not defined
get_node_count()[source]

Gets the total expression node count.

Node count is the total number of function calls and atoms in this expression. If a sub-expression is present multiple times, it is counted only once.

Returns:Expression node count
get_priority()[source]

Get the expression operation priority.

This method is extended by functions requiring a different priority.

Returns:-1 (no priority)
get_type()[source]

Get the type of this expression.

Returns:Expression type
has_name()[source]

Check if this expression has a name

Returns:True if this expression has a name, False otherwise.
is_kind_of(tp)[source]

Checks if the type of this expression type is compatible with another type.

Parameters:tp – Other type to check.
Returns:True if this expression type is a kind of tp.
is_type(xtyp)[source]

Check if the type of this expression is a given one

Parameters:xtyp – Expected type
Returns:True if expression type is the expected one
is_variable()[source]

Check if this expression is a variable

Returns:True if this expression is a variable
pretty_print(out=None, mxdepth=None, mxargs=None, adsize=False, indent='', curdepth=0)[source]

Pretty print expression in a way that shows its structure.

Parameters:
  • out – (optional) Print output (stream or file name, None for stdout)
  • mxdepth – (optional) Max print depth. Default is None (no limit)
  • mxargs – (optional) Max number of function or array arguments. Default is None (no limit)
  • adsize – (optional) Add size of expressions in nodes
  • indent – (optional) Indentation string
  • curdepth – (internal) Current print depth
set_name(name)[source]

Set the name of the expression.

This method returns this expression. It enables fluent calls such as: mdl.add((a + b == 1).set_name(“myname”))

Parameters:name – Expression name, possibly None.
Returns:This expression
class docplex.cp.expression.CpoFloatVar(min, max, name=None)[source]

Bases: docplex.cp.expression.CpoVariable

equals(other)[source]

Checks the equality of this expression with another object.

Implementation is required with a different name than __eq__ name because this function is already overloaded to construct model expression with operator ‘==’.

Parameters:other – Other object to compare with.
Returns:True if ‘other’ is equal to this object, False otherwise.
class docplex.cp.expression.CpoFunctionCall(oper, rtype, oprnds)[source]

Bases: docplex.cp.expression.CpoExpr

This class represent all model expression nodes that call a predefined modeler function.

All modeling functions are available in module docplex.cp.modeler.

Constructor

Parameters:
  • oper – Operation descriptor
  • rtype – Returned type
  • oprnds – List of operand expressions.
get_priority()[source]

Get the operation priority.

Returns:Operation priority
class docplex.cp.expression.CpoIntVar(dom, name=None)[source]

Bases: docplex.cp.expression.CpoVariable

This class represents an integer variable that can be used in a CPO model.

This object should not be created explicitly, but using one of the following factory method:

domain_contains(value)[source]

Check whether a given value is in the domain of the variable

Parameters:val – Value to check
Returns:True if the value is in the domain, False otherwise
domain_iterator()[source]

Iterator on the individual values of an integer variable domain.

Returns:Value iterator on the domain of this variable.
equals(other)[source]

Checks if this expression is equivalent to another

Parameters:other – Other object to compare with.
Returns:True if ‘other’ is semantically identical to this object, False otherwise.
get_domain()[source]

Gets the domain of the variable.

The domain of the variable can be:

  • A single integer value if the variable is fixed,

  • A list or a tuple of:

    • single integer values,
    • tuple of 2 integers representing an interval, bounds included
Returns:Domain of the variable.
get_domain_max()[source]

Gets the domain upper bound.

Returns:Domain upper bound.
get_domain_min()[source]

Gets the domain lower bound.

Returns:Domain lower bound.
is_binary()[source]

Check if the domain of this variable is reduced to boolean, 0, 1

Returns:True if this variable is a boolean variable
is_bool_var()[source]

Check if the domain of this variable is reduced to boolean, 0, 1

Returns:True if this variable is a boolean variable
lb

This property is used to get lower bound of the variable domain.

set_domain(domain)[source]

Sets the domain of the variable.

The domain of the variable is a list or tuple of:

  • discrete integer values,
  • list or tuple of 2 integers representing an interval.

For example, here are valid domain definitions:

set_domain([1, 3, 4, 5, 9]) set_domain([1, (3, 5), 9])
Parameters:domain – List of integers or interval tuples representing the variable domain.
ub

This property is used to get upper bound of the variable domain.

class docplex.cp.expression.CpoIntervalVar(start, end, length, size, intensity, granularity, presence, name=None)[source]

Bases: docplex.cp.expression.CpoVariable

This class represents an interval variable that can be used in a CPO model.

This object should not be created explicitly, but using one of the following factory method interval_var(), interval_var_list(), or interval_var_dict().

get_end()[source]

Gets the end of the interval.

Returns:End of the interval (interval expressed as a tuple of 2 integers)
get_granularity()[source]

Get the scale of the intensity function.

Returns:Scale of the intensity function, None for default (100)
get_intensity()[source]

Gets the intensity function of this interval var.

Returns:Intensity function (None, or StepFunction).
get_length()[source]

Gets the length interval.

Returns:Length of the interval (interval expressed as a tuple of 2 integers).
get_size()[source]

Gets the size of the interval.

Returns:Size of the interval (interval expressed as a tuple of 2 integers).
get_start()[source]

Gets the start of the interval.

Returns:Start of the interval (interval expressed as a tuple of 2 integers).
is_absent()[source]

Check if this interval variable must be absent.

Returns:True if this interval variable must be absent, False otherwise.
is_optional()[source]

Check if this interval variable is optional.

Returns:True if this interval variable is optional, False otherwise.
is_present()[source]

Check if this interval variable must be present.

Returns:True if this interval variable must be present, False otherwise.
set_absent()[source]

Specifies that this interval variable must be absent.

set_end(intv)[source]

Sets the end of the interval.

Parameters:intv – End of the interval (single integer or interval expressed as a tuple of 2 integers).
set_end_max(mx)[source]

Sets the maximum value of the end of the interval.

Parameters:mx – Max value of the end of the interval.
set_end_min(mn)[source]

Sets the minimum value of the end interval.

Parameters:mn – Min value of the end of the interval.
set_granularity(granularity)[source]

Sets the scale of the intensity function.

Parameters:granularity – Scale of the intensity function (integer).
set_intensity(intensity)[source]

Sets the intensity function of this interval var.

Parameters:intensity – Intensity function (None, or StepFunction).
set_length(intv)[source]

Sets the length interval.

Parameters:intv – Length of the interval (single integer or interval expressed as a tuple of 2 integers).
set_length_max(mx)[source]

Sets the maximum value of the length interval.

Parameters:mx – Max value of the length of the interval.
set_length_min(mn)[source]

Sets the minimum value of the length interval.

Parameters:mn – Min value of the length of the interval min value.
set_optional()[source]

Specifies that this interval variable is optional.

set_present()[source]

Specifies that this IntervalVar must be present.

set_size(intv)[source]

Sets the size of the interval.

Parameters:intv – Size of the interval (single integer or interval expressed as a tuple of 2 integers).
set_size_max(mx)[source]

Sets the maximum value of the size interval.

Parameters:mx – Max value of the size of the interval.
set_size_min(mn)[source]

Sets the minimum value of the size interval.

Parameters:mn – Min value of the size of the interval.
set_start(intv)[source]

Sets the start interval.

Parameters:intv – Start of the interval (single integer or interval expressed as a tuple of 2 integers).
set_start_max(mx)[source]

Sets the maximum value of the start interval.

Parameters:mx – Max value of the start of the interval.
set_start_min(mn)[source]

Sets the minimum value of the start interval.

Parameters:mn – Min value of the start of the interval.
class docplex.cp.expression.CpoSequenceVar(vars, types=None, name=None)[source]

Bases: docplex.cp.expression.CpoVariable

This class represents an sequence variable that can be used in a CPO model.

Constructor

Creates an instance of sequence variable on the set of interval variables defined by the array ‘vars’. A list of non-negative integer types can be optionally specified. List of variables and types must be of the same size and interval variable vars[i] will have type types[i] in the sequence variable.

Parameters:
  • vars – List of IntervalVars that constitute the sequence.
  • types – List of variable types as integers, same size as vars, or None (default).
  • name – Name of the sequence, None for automatic naming.
get_interval_variables()[source]

Gets the array of variables.

Returns:Array of interval variables that are in the sequence.
get_types()[source]

Gets the array of types.

Returns:Array of variable types (array of integers), None if no type defined.
get_vars()[source]

Gets the array of variables in this sequence variable.

Returns:Array of interval variables
class docplex.cp.expression.CpoStateFunction(trmtx=None, name=None)[source]

Bases: docplex.cp.expression.CpoVariable

This class represents a state function expression node.

State functions are used by interval variables to represent the evolution of a state variable over time.

Constructor

Parameters:
  • trmtx (optional) – An optional transition matrix defining the transition distance between consecutive states of the state function. Transition matrix is given as a list of rows (iterable of iterables of positive integers), or as the result of a call to the method transition_matrix().
  • name (optional) – Name of the state function.
get_transition_matrix()[source]

Returns the transition matrix.

Returns:Transition matrix, None if none.
set_transition_matrix(trmtx)[source]

Sets the transition matrix.

Args: trmtx : A transition matrix defining the transition distance between consecutive states of the state function.

Transition matrix is given as a list of rows (iterable of iterables of positive integers), or as the result of a call to the method transition_matrix().
class docplex.cp.expression.CpoTransitionMatrix(size=None, values=None, name=None)[source]

Bases: docplex.cp.expression.CpoValue

This class represents a transition matrix that is used in CPO model to represent transition distances.

A transition matrix is a square matrix of non-negative integers that represents a minimal distance between two interval variables. An instance of transition matrix can be used in the no_overlap constraint and in state functions.

  • In a no_overlap constraint the transition matrix represents the minimal distance between two non-overlapping interval variables. The matrix is indexed using the integer types of interval variables in the sequence variable of the no_overlap constraint.
  • In a state function, the transition matrix represents the minimal distance between two integer states of the function.

A transition matrix can be created:

  • Deprecated. Giving only its size. In this case, a transition matrix is created by this constructor with all values initialized to zero. Matrix values can then be set using set_value() method.
  • Giving the matrix values as a list of rows, each row being a list of integers. Matrix values can not be changed after it has been created.

Constructor

Parameters:
  • size (optional) – Matrix size (width and height),
  • name (optional) – Name of the matrix. None by default.
  • values (optional) – Matrix values expressed as a list of rows.
get_all_values()[source]

Returns an iterator on all matrix values, in row/column order

Returns:Iterator on all values
get_matrix()[source]

Returns the complete transition matrix.

Returns:Transition matrix as a list of integers that is the concatenation of all matrix rows.
get_size()[source]

Returns the size of the matrix.

Returns:Matrix size.
get_value(from_state, to_state)[source]

Returns a value in the transition matrix.

Parameters:
  • from_state – Index of the from state.
  • to_state – Index of the to state.
Returns:

Transition value.

set_value(from_state, to_state, value)[source]

Sets a value in the transition matrix.

Parameters:
  • from_state – Index of the from state.
  • to_state – Index of the to state.
  • value – Transition value.
class docplex.cp.expression.CpoValue(value, type)[source]

Bases: docplex.cp.expression.CpoExpr

CPO model expression representing a constant value.

Constructor

Parameters:
  • value – Constant value.
  • type – Value type.
class docplex.cp.expression.CpoVariable(type, name)[source]

Bases: docplex.cp.expression.CpoExpr

This class is an abstract class extended by all expression nodes that represent a CPO variable.

Constructor

Parameters:
  • type – Expression type.
  • name – Variable name.
is_variable()[source]

Check if this expression is a variable

Returns:True if this expression is a variable
docplex.cp.expression.DEFAULT_INTEGER_VARIABLE_DOMAIN = ((-9007199254740991, 9007199254740991),)

Default integer variable domain

docplex.cp.expression.DEFAULT_INTERVAL = (0, 4503599627370494)

Default interval.

docplex.cp.expression.INFINITY = inf

Infinity

docplex.cp.expression.INTERVAL_MAX = 4503599627370494

Maximum interval variable range value

docplex.cp.expression.INTERVAL_MIN = -4503599627370494

Minimum interval variable range value

docplex.cp.expression.INT_MAX = 9007199254740991

Maximum integer value.

docplex.cp.expression.INT_MIN = -9007199254740991

Minimum integer value.

class docplex.cp.expression.IntegerDomain[source]

Bases: tuple

Class representing the domain of an integer variable.

docplex.cp.expression.NEGATIVE_INFINITY = -inf

Negative infinity

docplex.cp.expression.POSITIVE_INFINITY = inf

Positive infinity

docplex.cp.expression.binary_var(name=None)[source]

Creates a binary integer variable.

An binary variable is an integer variable with domain limited to 0 and 1

Parameters:name (optional) – Variable name, default is None for automatic name.
Returns:CpoIntVar expression
docplex.cp.expression.binary_var_dict(keys, name=None)[source]

Creates a dictionary of binary variables.

This methods creates a dictionary of binary variables associated to a list of keys given as first parameter.

If a name is given, each variable of the list is created with this name concatenated with the string representation of the corresponding key. The parameter ‘name’ can also be a function that is called to build the variable name with the variable key as parameter.

Parameters:
  • keys – Iterable of variable keys.
  • name (optional) – Variable name prefix, or function to be called on dictionary key (example: str). If not given, a name prefix is generated automatically.
Returns:

Dictionary of CpoIntVar objects (OrderedDict).

docplex.cp.expression.binary_var_list(size, name=None)[source]

Creates a list of binary variables.

This methods creates a list of binary variables.

If a name is given, each variable of the list is created with this name concatenated with the index of the variable in the list, starting by zero.

Parameters:
  • size – Size of the list of variables
  • name (optional) – Variable name prefix. If not given, a name prefix is generated automatically.
Returns:

List of binary integer variables.

docplex.cp.expression.build_cpo_expr(val)[source]

Builds a model expression from a given Python value.

If active, this method uses a cache to return the same CpoExpr for the same value.

Parameters:val – Value to convert (possibly already an expression).
Returns:Corresponding expression.
Raises:CpoException if conversion is not possible.
docplex.cp.expression.build_cpo_expr_array(val)[source]

Builds a mode larray expression from a Python value.

If active, this method uses the value cache to return the same CpoExpr for the same value.

Parameters:val – Value to convert. Iterator or iterators of integers.
Returns:Model array expression, not editable.
Raises:Exception if conversion is not possible.
docplex.cp.expression.build_cpo_transition_matrix(val)[source]

Builds a TransitionMatrix model expression from a Python value.

If active, this method uses the value cache to return the same CpoExpr for the same value.

Parameters:val – Value to convert. Iterator or iterators of integers, or existing TransitionMatrix expression.
Returns:Model transition matrix, not editable.
Raises:Exception if conversion is not possible.
docplex.cp.expression.build_cpo_tupleset(val)[source]

Builds a TupleSet model expression from a Python value.

If active, this method uses the value cache to return the same CpoExpr for the same value.

Parameters:val – Value to convert. Iterator or iterators of integers, or existing TupleSet expression.
Returns:Model tupleset, not editable.
Raises:Exception if conversion is not possible.
docplex.cp.expression.compare_expressions(x1, x2)[source]

Compare two expressions for declaration order

Parameters:
  • x1 – Expression 1
  • x2 – Expression 2
Returns:

Integer value that is negative if v1 < v2, zero if v1 == v2 and positive if v1 > v2.

docplex.cp.expression.get_domain_max(d)[source]

Retrieves the upper bound of an integer or interval variable domain.

The domain can be either:

  • a single number value,
  • a list of numbers, or tuple of 2 numbers representing an interval.

This method returns the last number of the domain.

Parameters:d – Domain
Returns:Domain upper bound
docplex.cp.expression.get_domain_min(d)[source]

Retrieves the lower bound of an integer or interval variable domain.

The domain can be either:

  • a single number value,
  • a list of numbers, or tuple of 2 numbers representing an interval.

This method returns the first number of the domain.

Parameters:d – Domain
Returns:Domain lower bound
docplex.cp.expression.get_node_count(expr)[source]

Get the number of model expression nodes in an expression or list of expressions

Parameters:expr – Expression or list of expressions
Returns:Total number of nodes
docplex.cp.expression.integer_var(min=None, max=None, name=None, domain=None)[source]

Creates an integer variable.

An integer variable is a decision variable with a set of potential values called ‘domain of the variable’. This domain can be expressed either:

  • as a single interval, with a minimum and a maximum bounds included in the domain,
  • or as an extensive list of values and/or intervals.

When the domain is given extensively, an interval of the domain is represented by a tuple (min, max). Examples of variable domains expressed extensively are:

  • (1, 2, 3, 4)
  • (1, 2, (3, 7), 9)
  • ((1, 2), (7, 9))

Following integer variable declarations are equivalent:

  • v = integer_var(0, 9, “X”)
  • v = integer_var(domain=(0, 1, 2, 3, 4, 5, 6, 7, 8, 9), name=”X”)
  • v = integer_var(domain=(0, (1, 5), (6, 7), 8, 9), name=”X”)
  • v = integer_var(domain=((0, 9)), name=”X”)
Parameters:
  • min – Domain min value. Optional if domain is given extensively.
  • max – Domain max value. Optional if domain is given extensively.
  • name – Optional variable name. If not given, a name is automatically generated.
  • domain – Variable domain expressed as extensive list of values and/or intervals expressed as tuples of integers. Unused if min and max are provided.
Returns:

CpoIntVar expression

docplex.cp.expression.integer_var_dict(keys, min=None, max=None, name=None, domain=None)[source]

Creates a dictionary of integer variables.

This methods creates a dictionary of integer variables associated to a list of keys given as first parameter. All other parameters are identical to those requested by the method integer_var() that allows to create a single integer variable. See the documentation of integer_var() for details.

If a name is given, each variable of the list is created with this name concatenated with the string representation of the corresponding key. The parameter ‘name’ can also be a function that is called to build the variable name with the variable key as parameter.

Parameters:
  • keys – Iterable of variable keys.
  • min – Domain min value. Optional if domain is given extensively.
  • max – Domain max value. Optional if domain is given extensively.
  • name – Optional variable name. If not given, a name is automatically generated.
  • domain – Variable domain expressed as extensive list of values and/or intervals expressed as tuples of integers. Unused if min and max are provided.
Returns:

Dictionary of CpoIntVar objects.

docplex.cp.expression.integer_var_list(size, min=None, max=None, name=None, domain=None)[source]

Creates a list of integer variables.

This methods creates a list of integer variables whose size is given as first parameter. All other parameters are identical to those requested by the method integer_var() that allows to create a single integer variable. See the documentation of integer_var() for details.

If a name is given, each variable of the list is created with this name concatenated with the index of the variable in the list, starting by zero.

Parameters:
  • size – Size of the list of variables
  • min – Domain min value. Optional if domain is given extensively.
  • max – Domain max value. Optional if domain is given extensively.
  • name – Optional variable name prefix.
  • domain – Variable domain expressed as extensive list of values and/or intervals expressed as tuples of integers. Unused if min and max are provided.
Returns:

List of integer variables.

docplex.cp.expression.interval_var(start=None, end=None, length=None, size=None, intensity=None, granularity=None, optional=False, name=None)[source]

Creates an interval variable.

An interval decision variable represents an unknown of a scheduling problem, in particular an interval of time during which something happens (an activity is carried out) whose position in time is unknown. An interval is characterized by a start value, an end value and a size. The start and end of an interval variable must be in [INTERVAL_MIN..INTERVAL_MAX]. An important feature of interval decision variables is that they can be optional, that is, it is possible to model that an interval variable can be absent from the solution schedule.

Sometimes the intensity of work is not the same during the whole interval. For example, consider a worker who does not work during weekends (his work intensity during weekends is 0%) and on Friday he works only for half a day (his intensity during Friday is 50%). For this worker, 7 man-days work will span for longer than just 7 days. In this example 7 man-days represent what is called the size of the interval: that is, the length of the interval would be if the intensity function was always at 100%. To model such situations, a range for the size of an interval variable and an integer stepwise intensity function can be specified. The length of the interval will be at least long enough to cover the work requirements given by the interval size, taking into account the intensity function.

More information is available here.

Parameters:
  • start (optional) – Allowed range for the start of the interval (single integer or interval expressed as a tuple of 2 integers). Default value is [0..INTERVAL_MAX].
  • end (optional) – Allowed range for the end the interval (single integer or interval expressed as a tuple of 2 integers). Default value is [0..INTERVAL_MAX].
  • length (optional) – Allowed range for the length the interval (single integer or interval expressed as a tuple of 2 integers). Default value is [0..INTERVAL_MAX].
  • size (optional) – Allowed range for the size the interval (single integer or interval expressed as a tuple of 2 integers). Default value is [0..INTERVAL_MAX].
  • intensity (optional) – StepFunction that specifies relation between size and length of the interval.
  • granularity (optional) – Scale of the intensity function.
  • optional (optional) – Optional presence indicator.
  • name (optional) – Name of the variable. If not given, a name is generated automatically.
Returns:

IntervalVar expression.

docplex.cp.expression.interval_var_dict(keys, start=None, end=None, length=None, size=None, intensity=None, granularity=None, optional=False, name=None)[source]

Creates a list of interval variables.

If a name is given, each variable of the array is created with this name concatenated with the index of the variable in the list.

Parameters:
  • keys – Iterable of variable keys.
  • start (optional) – Allowed range for the start of the interval (single integer or interval expressed as a tuple of 2 integers). Default value is [0..INTERVAL_MAX].
  • end (optional) – Allowed range for the end the interval (single integer or interval expressed as a tuple of 2 integers). Default value is [0..INTERVAL_MAX].
  • length (optional) – Allowed range for the length the interval (single integer or interval expressed as a tuple of 2 integers). Default value is [0..INTERVAL_MAX].
  • size (optional) – Allowed range for the size the interval (single integer or interval expressed as a tuple of 2 integers). Default value is [0..INTERVAL_MAX].
  • intensity (optional) – StepFunction that specifies relation between size and length of the interval.
  • granularity (optional) – Scale of the intensity function.
  • optional (optional) – Optional presence indicator.
  • name (optional) – Variable name prefix, or function to be called on dictionary key (example: str). If not given, a name prefix is generated automatically.
Returns:

Dictionary of CpoIntervalVar objects.

docplex.cp.expression.interval_var_list(asize, start=None, end=None, length=None, size=None, intensity=None, granularity=None, optional=False, name=None)[source]

Creates a list of interval variables.

If a name is given, each variable of the array is created with this name concatenated with the index of the variable in the list.

Parameters:
  • asize – Size of the list of variables
  • start (optional) – Allowed range for the start of the interval (single integer or interval expressed as a tuple of 2 integers). Default value is [0..INTERVAL_MAX].
  • end (optional) – Allowed range for the end the interval (single integer or interval expressed as a tuple of 2 integers). Default value is [0..INTERVAL_MAX].
  • length (optional) – Allowed range for the length the interval (single integer or interval expressed as a tuple of 2 integers). Default value is [0..INTERVAL_MAX].
  • size (optional) – Allowed range for the size the interval (single integer or interval expressed as a tuple of 2 integers). Default value is [0..INTERVAL_MAX].
  • intensity (optional) – StepFunction that specifies relation between size and length of the interval.
  • granularity (optional) – Scale of the intensity function.
  • optional (optional) – Optional presence indicator.
  • name (optional) – Name of the variable. If not given, a name is generated automatically.
Returns:

List of interval variables.

docplex.cp.expression.is_cpo_expr(expr, type=None)[source]

Check if an expression is a CPO model expression

Parameters:
  • expr – Value to check
  • type (optional) – Precise CPO type, in Type_*
Returns:

True if parameter is a CPO expression, of the expected type if given

docplex.cp.expression.pretty_print(expr, out=None, mxdepth=None, mxargs=None, adsize=False, indent='', curdepth=0)[source]

Pretty print expression in a way that shows its structure.

Parameters:
  • expr – Expression to print
  • out – (optional) Print output (stream or file name, None for stdout)
  • mxdepth – (optional) Max print depth. Default is None (no limit)
  • mxargs – (optional) Max number of function or array arguments. Default is None (no limit)
  • adsize – (optional) Add size of expressions in nodes
  • indent – (optional) Indentation string
  • curdepth – (internal) Current print depth
docplex.cp.expression.sequence_var(vars, types=None, name=None)[source]

Creates a new sequence variable (list of interval variables).

This method creates an instance of sequence variable on the set of interval variables defined by the array ‘vars’. A list of non-negative integer types can be optionally specified. List of variables and types must be of the same size and interval variable vars[i] will have type types[i] in the sequence variable.

Parameters:
  • vars – List of IntervalVars that constitute the sequence.
  • types – List of variable types as integers, same size as vars, or None (default).
  • name – Name of the sequence, None for automatic naming.
Returns:

IntervalVar expression.

docplex.cp.expression.state_function(trmtx=None, name=None)[source]

Create a new State Function

Parameters:
  • trmtx (optional) – An optional transition matrix defining the transition distance between consecutive states of the state function. Transition matrix is given as a list of rows (iterable of iterables of positive integers), or as the result of a call to the method transition_matrix().
  • name (optional) – Name of the state function
Returns:

CpoStateFunction expression

docplex.cp.expression.transition_matrix(szvals, name=None)[source]

Creates a new transition matrix (square matrix of integers).

A transition matrix is a square matrix of non-negative integers that represents a minimal distance between two interval variables. An instance of transition matrix can be used in the no_overlap constraint and in state functions.

  • In a no_overlap constraint the transition matrix represents the minimal distance between two non-overlapping interval variables. The matrix is indexed using the integer types of interval variables in the sequence variable of the no_overlap constraint.
  • In a state function, the transition matrix represents the minimal distance between two integer states of the function.

A transition matrix can be created:

  • Deprecated. Giving only its size. In this case, a transition matrix is created by this constructor with all values initialized to zero. Matrix values can then be set using set_value() method.
  • Giving the matrix values as a list of rows, each row being a list of integers. Matrix values can not be changed after it has been created.
Parameters:
  • szvals – Matrix values expressed as a list of rows (iterable of iterables of positive integers).
  • name (optional) – Name of the matrix. None by default.
Returns:

TransitionMatrix expression.

docplex.cp.expression.tuple_set(tset, name=None)[source]

Create a tuple set.

A tuple set is essentially a matrix of integers, but not necessarily square. Boolean expressions allowed_assignments() and forbidden_assignments() use a tuple set to express allowed/forbidden combinations of values for a collection of variables. Each allowed/forbidden combination is called a tuple and it is represented by a row in the tuple set matrix.

Note that modeling methods allowed_assignments() and forbidden_assignments() automatically create a tuple_set object if given argument is an iterable of iterables. There is then no need to explicitly call this factory method to create a tuple set, except to assign a name to it.

Parameters:
  • tset – List of tuples, as iterable of iterables of integers
  • name – Object name (default is None).
Returns:

TupleSet expression.