docplex.mp.constr module

class docplex.mp.constr.AbstractConstraint(model, name=None)[source]

Bases: docplex.mp.basic.ModelingObject, docplex.mp.basic._BendersAnnotatedMixin

is_added()[source]

Returns True if the constraint has been added to its model.

Example

c = (x+y == 1) m.add(c) c.is_added() >>> True c2 = (x + 2*y) >= 3 c2.is_added() >>> False

set_mandatory()[source]

Sets the constraint as mandatory.

This prevents relaxation from relaxing this constraint. To revert this, set the priority to any non-mandatory priority, or None.

class docplex.mp.constr.BinaryConstraint(model, left_expr, ctsense, right_expr, name=None)[source]

Bases: docplex.mp.constr.AbstractConstraint

get_left_expr()[source]

This property returns the left expression in the constraint.

Example

(X+Y <= Z+1) has left expression (X+Y).

get_right_expr()[source]

This property returns the right expression in the constraint.

Example

(X+Y <= Z+1) has right expression (Z+1).

iter_variables()[source]

Iterates over all variables mentioned in the constraint.

Note: This includes variables that are mentioned with a zero coefficient. For example, the iterator on the following constraint:

X <= X+Y + 1

will return X and Y, although X is mentioned with a zero coefficient.

Returns:An iterator object.
to_string()[source]

Returns a string representation of the constraint.

The operators in this representation are the usual operators <=, ==, and >=.

Example

The constraint (X+Y <= Z+1) is represented as “X+Y <= Z+1”.

Returns:A string.
type

This property returns the type of the constraint; type is an enumerated value of type ComparisonType, with three possible values:

  • LE for e1 <= e2 constraints
  • EQ for e1 == e2 constraints
  • GE for e1 >= e2 constraints

where e1 and e2 denote linear expressions.

class docplex.mp.constr.EquivalenceConstraint(model, binary_var, linear_ct, truth_value=1, name=None)[source]

Bases: docplex.mp.constr.LogicalConstraint

This class models equivalence constraints.

An equivalence constraint links (both ways) the value of a binary variable to the satisfaction of a linear constraint.

If the binary variable equals the truth value (default is 1), then the constraint is satisfied, conversely if the constraint is satisfied, the value of the variable is set to the truth value.

This class is not meant to be instantiated by the user.

class docplex.mp.constr.IfThenConstraint(model, if_ct, then_ct, negate=False)[source]

Bases: docplex.mp.constr.IndicatorConstraint

to_string()[source]

Displays the equivalence constraint in a (shortened) LP style: z <-> x+y+z == 2

Returns:A string.
class docplex.mp.constr.IndicatorConstraint(model, binary_var, linear_ct, active_value=1, name=None)[source]

Bases: docplex.mp.constr.LogicalConstraint

This class models indicator constraints.

An indicator constraint links (one-way) the value of a binary variable to the satisfaction of a linear constraint. If the binary variable equals the active value, then the constraint is satisfied, but otherwise the constraint may or may not be satisfied.

This class is not meant to be instantiated by the user.

To create an indicator constraint, use the factory method docplex.mp.model.Model.add_indicator() defined on docplex.mp.model.Model.

invalidate()[source]

Sets the binary variable to the opposite of its active value. Typically used by indicator constraints with a trivial infeasible linear part. For example, z=1 -> 4 <= 3 sets z to 0 and z=0 -> 4 <= 3 sets z to 1. This is equivalent to if z=a => False, then z cannot be equal to a.

class docplex.mp.constr.LinearConstraint(model, left_expr, ctsense, right_expr, name=None)[source]

Bases: docplex.mp.constr.BinaryConstraint, docplex.mp.operand.LinearOperand

The class that models all constraints of the form <expr1> <OP> <expr2>, where <expr1> and <expr2> are linear expressions.

basis_status

This property returns the basis status of the slack variable of the constraint, if any.

Returns:An enumerated value from the enumerated type docplex.constants.BasisStatus.

Note

for the model to hold basis information, the model must have been solved as a LP problem. In some cases, a model which failed to solve may still have a basis available. Use Model.has_basis() to check whether the model has basis information or not.

New in version 2.10

benders_annotation

This property is used to get or set the Benders annotation of a constraint. The value of the annotation must be a positive integer

dual_value

This property returns the dual value of the constraint.

Note

This method will raise an exception if the model has not been solved successfully.

This method is OK with small numbers of constraints. For large numbers of constraints (>100), consider using Model.dual_values() with a sequence of constraints.

See also

func:docplex.mp.model.Model.dual_values()

left_expr

This property returns the left expression in the constraint.

Example

(X+Y <= Z+1) has left expression (X+Y).

lhs

This property returns the left expression in the constraint.

Example

(X+Y <= Z+1) has left expression (X+Y).

rhs

This property returns the right expression in the constraint.

Example

(X+Y <= Z+1) has right expression (Z+1).

right_expr

This property returns the right expression in the constraint.

Example

(X+Y <= Z+1) has right expression (Z+1).

sense

This property is used to get or set the sense of the constraint; sense is an enumerated value of type ComparisonType, with three possible values:

  • LE for e1 <= e2 constraints
  • EQ for e1 == e2 constraints
  • GE for e1 >= e2 constraints

where e1 and e2 denote linear expressions.

slack_value

This property returns the slack value of the constraint.

Note

This method will raise an exception if the model has not been solved successfully.

This method is OK with small numbers of constraints. For large numbers of constraints (>100), consider using Model.slack_values() with a sequence of constraints.

See also

func:docplex.mp.model.Model.slack_values()

to_string()[source]

Returns a string representation of the constraint.

The operators in this representation are the usual operators <=, ==, and >=.

Example

The constraint (X+Y <= Z+1) is represented as “X+Y <= Z+1”.

Returns:A string.
class docplex.mp.constr.LogicalConstraint(model, binary_var, linear_ct, active_value=1, name=None)[source]

Bases: docplex.mp.constr.AbstractConstraint

This class models logical constraints.

An equivalence constraint links (both ways) the value of a binary variable to the satisfaction of a linear constraint.

If the binary variable equals the truth value (default is 1), then the constraint is satisfied, conversely if the constraint is satisfied, the value of the variable is set to the truth value.

This class is not meant to be instantiated by the user.

benders_annotation

This property is used to get or set the Benders annotation of a constraint. The value of the annotation must be a positive integer

to_string()[source]

Displays the equivalence constraint in a (shortened) LP style: z <-> x+y+z == 2

Returns:A string.
class docplex.mp.constr.NotEqualConstraint(model, negated_eqct, name=None)[source]

Bases: docplex.mp.constr.LinearConstraint

to_string()[source]

Returns a string representation of the constraint.

The operators in this representation are the usual operators <=, ==, and >=.

Example

The constraint (X+Y <= Z+1) is represented as “X+Y <= Z+1”.

Returns:A string.
class docplex.mp.constr.PwlConstraint(model, pwl_expr, name=None)[source]

Bases: docplex.mp.constr.AbstractConstraint

This class models piecewise linear constraints.

This class is not meant to be instantiated by the user. To create a piecewise constraint, use the factory method docplex.mp.model.Model.piecewise() defined on docplex.mp.model.Model.

expr

This property returns the linear expression of the piecewise linear constraint.

iter_variables()[source]

Iterates over all the variables of the piecewise linear constraint.

Returns:An iterator object.
pwl_func

This property returns the piecewise linear function of the piecewise linear constraint.

y

This property returns the output variable associated with the piecewise linear constraint.

class docplex.mp.constr.QuadraticConstraint(model, left_expr, ctsense, right_expr, name=None)[source]

Bases: docplex.mp.constr.BinaryConstraint

The class models quadratic constraints.

Quadratic constraints are of the form <qexpr1> <OP> <qexpr2>, where at least one of <qexpr1> or <qexpr2> is a quadratic expression.

benders_annotation

This property is used to get or set the Benders annotation of a constraint. The value of the annotation must be a positive integer

left_expr

This property returns the left expression in the constraint.

Example

(X+Y <= Z+1) has left expression (X+Y).

right_expr

This property returns the right expression in the constraint.

Example

(X+Y <= Z+1) has right expression (Z+1).

sense

This property is used to get or set the sense of the constraint; sense is an enumerated value of type ComparisonType, with three possible values:

  • LE for e1 <= e2 constraints
  • EQ for e1 == e2 constraints
  • GE for e1 >= e2 constraints

where e1 and e2 denote quadratic expressions.

slack_value

This property returns the slack value of the constraint.

Note

This method will raise an exception if the model has not been solved successfully.

class docplex.mp.constr.RangeConstraint(model, expr, lb, ub, name=None)[source]

Bases: docplex.mp.constr.AbstractConstraint

This class models range constraints.

A range constraint states that an expression must stay between two values, lb and ub.

This class is not meant to be instantiated by the user. To create a range constraint, use the factory method docplex.mp.model.Model.add_range() defined on docplex.mp.model.Model.

basis_status

This property returns the basis status of the slack variable of the constraint, if any.

Returns:An enumerated value from the enumerated type docplex.constants.BasisStatus.

Note

for the model to hold basis information, the model must have been solved as a LP problem. In some cases, a model which failed to solve may still have a basis available. Use Model.has_basis() to check whether the model has basis information or not.

New in version 2.10

benders_annotation

This property is used to get or set the Benders annotation of a constraint. The value of the annotation must be a positive integer

bounds

This property is used to get or set the (lower, upper) bounds of a range constraint.

dual_value

This property returns the dual value of the constraint.

Note

This method will raise an exception if the model has not been solved successfully.

expr

This property returns the linear expression of the range constraint.

iter_variables()[source]

Iterates over all the variables of the range constraint.

Returns:An iterator object.
lb

This property is used to get or set the lower bound of the range constraint.

slack_value

This property returns the slack value of the constraint.

Note

This method will raise an exception if the model has not been solved successfully.

ub

This property is used to get or set the upper bound of the range constraint.