docplex.mp.constr module

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

Bases: docplex.mp.constr.AbstractConstraint

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.

logical_rhs

This property returns the target right-hand side used to trigger the linear constraint. Returns 1 if not complemented, else 0.

to_string()[source]

Displays the indicator constraint in the LP style: z = 1 -> x+y+z == 2

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

Bases: docplex.mp.constr.BinaryConstraint

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

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.

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).

get_sense()[source]

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.

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.
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.

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 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.

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.

__str__()[source]

Returns a string representation of the piecewise linear constraint.

Example

y == pwl_name(x + z) represents the piecewise linear constraint where the variable y is constrained to be equal to the value of the piecewise linear function whose name is ‘pwl_name’ applied to the expression (x + z).

Returns:A string.
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.

usage_counter

This property returns the usage counter of the piecewise linear function associated with 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.

get_sense()[source]

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.

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.

type

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.

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.

__str__()[source]

Returns a string representation of the range constraint.

Example

1 <= x+y+z <= 3 represents the range constraint where the expression (x+y+z) is constrained to stay between 1 and 3.

Returns:A string.
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.

get_expr()[source]

This property returns the linear expression of the range constraint.

get_lb()[source]

This property returns the lower bound of the range constraint.

get_ub()[source]

This property returns the upper bound of the range constraint.

iter_variables()[source]

Iterates over all the variables of the range constraint.

Returns:An iterator object.
lb

This property returns 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 returns the upper bound of the range constraint.