docplex.mp.linear module¶

class
docplex.mp.linear.
AbstractLinearExpr
(model)[source]¶ Bases:
docplex.mp.operand.LinearOperand
,docplex.mp.basic.Expr

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


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 floatingpoint number.
Returns: The modified expression itself.
 dvar (

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

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.


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


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
