docplex.mp.linear module

class docplex.mp.linear.AbstractLinearExpr(model)[source]

Bases: docplex.mp.operand.LinearOperand, docplex.mp.basic.Expr

get_coef(dvar)[source]

Returns the coefficient of a variable in the expression.

Note

If the variable is not present in the expression, the function returns 0.

Parameters:dvar – The variable for which the coefficient is being queried.
Returns:A floating-point number.
class docplex.mp.linear.ConstantExpr(model, cst)[source]

Bases: docplex.mp.basic._SubscriptionMixin, docplex.mp.linear.AbstractLinearExpr

contains_var(dvar)[source]

Checks whether a variable is present in the expression.

Param:dvar (docplex.mp.dvar.Var): A decision variable.
Returns:True if the variable is present in the expression, else False.
Return type:Boolean
iter_variables()[source]

Iterates over all variables in the expression.

Returns:An iterator over all variables present in the operand.
Return type:iterator
number_of_variables()[source]

Returns: integer: The number of variables in the expression.

exception docplex.mp.linear.DOCplexQuadraticArithException[source]

Bases: Exception

class docplex.mp.linear.LinearExpr[source]

Bases: docplex.mp.basic._SubscriptionMixin, docplex.mp.linear.AbstractLinearExpr

This class models linear expressions. This class is not intended to be instantiated. Expressions are built either using operators or using Model.linear_expr().

add(e)[source]

Adds an expression to self.

Note

This method does not create an new expression but modifies the self instance.

Parameters:e – The expression to be added. Can be a variable, an expression, or a number.
Returns:The modified self.

See also

The method plus() to compute a sum without modifying the self instance.

add_term(dvar, coeff)[source]

Adds a term (variable and coefficient) to the expression.

Parameters:
  • dvar (Var) – A decision variable.
  • coeff (float) – A floating-point number.
Returns:

The modified expression itself.

clone()[source]
Returns:A copy of the expression on the same model.
constant

This property is used to get or set the constant term of the expression.

contains_var(dvar)[source]

Checks whether a decision variable is part of an expression.

Parameters:dvar (Var) – A decision variable.
Returns:True if dvar is mentioned in the expression with a nonzero coefficient.
Return type:Boolean
divide(e)[source]

Divides this expression by an operand.

Parameters:e – The operand by which the self expression is divided. Only nonzero numbers are permitted.

Note

This method does not create a new expression but modifies the self instance.

Returns:The modified self.
equals(other)[source]

This method is used to test equality between expressions. Because of the overloading of operator == through the redefinition of the __eq__ method, you cannot use == to test for equality. The equals method to test whether a given expression is equivalent to a variable. Two linear expressions are equivalent if they have the same coefficient for all variables.

Parameters:other – a number or any expression.
Returns:A boolean value, True if the passed expression is equivalent, else False.

Note

A constant expression is considered equivalent to its constant number.

m.linear_expression(3).equals(3) returns True
equals_constant(scalar)[source]

Checks if the expression equals a constant term.

Parameters:scalar (float) – A floating-point number.
Returns:True if the expression equals this constant term.
Return type:Boolean
is_constant()[source]

Checks if the expression is a constant.

Returns:True if the expression consists of only a constant term.
Return type:Boolean
is_discrete()[source]

Checks if the expression contains only discrete variables and coefficients.

Example

If X is an integer variable, X, X+1, 2X+3 are discrete but X+0.3, 1.5X, 2X + 0.7 are not.

Returns:True if the expression contains only discrete variables and coefficients.
Return type:Boolean
iter_terms()[source]

Iterates over the terms in the expression.

Returns:An iterator over the (variable, coefficient) pairs in the expression.
multiply(e)[source]

Multiplies this expression by an expression.

Note

This method does not create a new expression but modifies the self instance.

Parameters:e – The expression that is used to multiply self.
Returns:The modified self.

See also

The method times() to compute a multiplication without modifying the self instance.

negate()[source]

Takes the negation of an expression.

Changes the expression by replacing each variable coefficient and the constant term by its opposite.

Note

This method does not create any new expression but modifies the self instance.

Returns:The modified self.
number_of_variables()[source]

Returns: integer: The number of variables in the expression.

plus(e)[source]

Computes the sum of the expression and some operand.

Parameters:e – the expression to add to self. Can be either a variable, an expression or a number.
Returns:a new expression equal to the sum of the self expression and e

Note

This method doe snot modify self.

quotient(e)[source]

Computes the division of this expression with an operand.

Note

This method does not modify the self instance but returns a new expression instance.

Parameters:e – The expression that is used to modify self. Only nonzero numbers are permitted.
Returns:A new instance of expression.
remove_term(dvar)[source]

Removes a term associated with a variable from the expression.

Parameters:dvar (Var) – A decision variable.
Returns:The modified expression.
solution_value

This property returns the solution value of the variable.

Raises:DOCplexException – if the model has not been solved.
subtract(e)[source]

Subtracts an expression from this expression. .. note:: This method does not create a new expression but modifies the self instance.

Parameters:e – The expression to be subtracted. Can be either a variable, an expression, or a number.
Returns:The modified self.

See also

The method minus() to compute a difference without modifying the self instance.

times(e)[source]

Computes the multiplication of this expression with an operand.

Note

This method does not modify the self instance but returns a new expression instance.

Parameters:e – The expression that is used to multiply self.
Returns:A new instance of expression.
class docplex.mp.linear.MonomialExpr(model, dvar, coeff, safe=False)[source]

Bases: docplex.mp.basic._SubscriptionMixin, docplex.mp.linear.AbstractLinearExpr

contains_var(dvar)[source]

Checks whether a variable is present in the expression.

Param:dvar (docplex.mp.dvar.Var): A decision variable.
Returns:True if the variable is present in the expression, else False.
Return type:Boolean
number_of_variables()[source]

Returns: integer: The number of variables in the expression.

class docplex.mp.linear.ZeroExpr(model)[source]

Bases: docplex.mp.basic._SubscriptionMixin, docplex.mp.linear.AbstractLinearExpr

contains_var(dvar)[source]

Checks whether a variable is present in the expression.

Param:dvar (docplex.mp.dvar.Var): A decision variable.
Returns:True if the variable is present in the expression, else False.
Return type:Boolean
number_of_variables()[source]

Returns: integer: The number of variables in the expression.