docplex.mp.linear module¶
-
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.
- 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_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.
-
iter_variables
()[source]¶ Iterates over all variables mentioned in the linear expression.
Returns: An iterator object.
-
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.
Var
[source]¶ Bases:
docplex.mp.basic.ModelingObject
,docplex.mp.operand.LinearOperand
This class models decision variables. Decision variables are instantiated by
docplex.mp.model.Model
methods such asdocplex.mp.model.Model.var()
.-
__float__
()[source]¶ Converts a decision variable to a floating-point number.
This is only possible when the model has been solved successfully, otherwise an exception is raised. If the model has been solved, it returns the variable’s solution value.
Returns: The variable’s solution value.
Return type: float
Raises: DOCplexException
if the model has not been solved successfully.
-
__int__
()[source]¶ Converts a decision variable to a integer number.
This is only possible for discrete variables, and when the model has been solved successfully. If the model has been solved, returns the variable’s solution value.
Returns: The variable’s solution value.
Return type: int
Raises: DOCplexException
if the model has not been solved successfully.
DOCplexException
if the variable is not discrete.
-
get_lb
()[source]¶ This property is used to get or set the lower bound of the variable.
Possible values for the lower bound depend on the variable type. Binary variables accept only 0 or 1 as bounds. An integer variable will convert the lower bound value to the ceiling integer value of the argument.
-
get_ub
()[source]¶ This property is used to get or set the upper bound of the variable.
Possible values for the upper bound depend on the variable type. Binary variables accept only 0 or 1 as bounds. An integer variable will convert the upper bound value to the floor integer value of the argument.
To reset the upper bound to its default infinity value, use
docplex.mp.model.Model.infinity()
.
-
is_binary
()[source]¶ Checks if the variable is binary.
Returns: True if the variable is of type Binary. Return type: Boolean
-
is_continuous
()[source]¶ Checks if the variable is continuous.
Returns: True if the variable is of type Continuous. Return type: Boolean
-
is_discrete
()[source]¶ Checks if the variable is discrete.
Returns: True if the variable is of type Binary or Integer. Return type: Boolean
-
is_integer
()[source]¶ Checks if the variable is integer.
Returns: True if the variable is of type Integer. Return type: Boolean
-
lb
¶ This property is used to get or set the lower bound of the variable.
Possible values for the lower bound depend on the variable type. Binary variables accept only 0 or 1 as bounds. An integer variable will convert the lower bound value to the ceiling integer value of the argument.
-
name
¶ This property is used to get or set the name of the modeling object.
-
reduced_cost
¶ Returns the reduced cost of the variable.
Note
This method will raise an exception if the model has not been solved successfully as a LP.
Returns: The reduced cost of the variable (a float value).
-
solution_value
¶ This property returns the solution value of the variable.
Raises: DOCplexException
if the model has not been solved succesfully.
-
to_bool
()[source]¶ Converts a variable value to True or False.
This is only possible for discrete variables and assumes there is a solution.
Raises: DOCplexException
if the model has not been solved successfully.
DOCplexException
if the variable is not discrete.
Returns: True if the variable value is nonzero, else False.
Return type: Boolean
-
ub
¶ This property is used to get or set the upper bound of the variable.
Possible values for the upper bound depend on the variable type. Binary variables accept only 0 or 1 as bounds. An integer variable will convert the upper bound value to the floor integer value of the argument.
To reset the upper bound to its default infinity value, use
docplex.mp.model.Model.infinity()
.
-
vartype
¶ This property returns the variable type, an instance of
VarType
.
-