7.3.5. MdoExprLinear

class mindoptpy.MdoExprLinear

Bases: mindoptpy.MdoExpr

This object implements the data structure to hold a linear constraint expression object, which consists of a set of coefficient-variable pairs.

Typical steps to input a linear constraint expression are:

  1. Create a set of empty variable objects by calling mindoptpy.MdoModel.add_var().

  2. Create an empty linear expression object, mindoptpy.MdoExprLinear().

  3. Input linear expression using overloaded operators such as +, or member function mindoptpy.MdoExprLinear.add_terms().

Note

  1. This is a temporary object with a short lifespan in general.

  2. In general, overloaded operators such as + are less efficient and thus shall be avoided in a loop. Note taht the most efficient way to build an expression object is to call mindoptpy.MdoExprLinear.add_terms().

Examples

MdoVar x1 = model.add_var()
MdoVar x3 = model.add_var()
expr1 = 1 * x1
expr1 = expr1 + x2
MdoVar x3 = model.add_var()
expr2 = expr1 + x3

Methods

add_constant()

This function adds a constant term to the expression object.

add_term()

This function adds a nonzero term.

add_terms()

This function adds a set of nonzero terms.

clear()

This function removes all nonzero elements from the expression object.

get_coeff()

This function returns the value of a nonzero element from the expression object, given a variable index.

get_constant()

This function returns the constant term in the expression object.

get_size()

This function returns the number of nonzero elements stored in the expression object.

get_terms()

This function returns the nonzero elements stored in the column object.

get_var()

This function returns the variable object from the expression object, given a variable object.

remove_term()

This function removes a nonzero element from the expression object, given a variable index.

remove_var()

This function removes a nonzero element from the expression object, given a variable object.

set_constant()

This function changes the constant term in the expression object.

get_dual_value()

This function returns the dual variable associated with this constraint.

add_constant(constant: float)

This function adds a constant term to the expression object.

Parameters

constant (float) – A constant term that will be added.

add_term(var: mindoptpy.MdoVar, coeff: float)

This function adds a nonzero term. A nonzero term is a pair of variable and coefficient.

Parameters
  • var (MdoVar) – A variable object associated with the nonzero term.

  • coeff (float) – A coefficient of the nonzero term.

add_terms(vars: Union[list, mindoptpy.MdoVar], coeffs: Union[list, float])

This function adds a set of nonzero terms. A nonzero term is a pair of variable and coefficient.

Parameters
  • conss (array-like or single variable object) – An array that holds the variable objects, or a single variable object.

  • coeffs (array-like or a single coefficient) – An array that holds the coefficients of the nonzero terms, or a single coefficient.

clear()

This function removes all nonzero elements from the expression object.

get_coeff(pos: int)

This function returns the value of a nonzero element from the expression object, given a variable index.

Parameters

pos (int) – The term that is currently stored at position pos.

Returns

The value of the nonzero coefficient.

Return type

float

get_constant()

This function returns the constant term in the expression object.

Returns

A constant term in the expression object.

Return type

float

get_terms()

This function returns the nonzero elements stored in the column object.

Returns

Return the nonzero elements stored in the column object.

Return type

array-like

get_var(pos: int)

This function returns the variable object from the expression object, given a variable index.

Parameters

pos (int) – The term that is currently stored at position pos.

Returns

A variable object.

Return type

MdoVar

remove_term(pos: int)

This function removes a nonzero element from the expression object, given a variable index.

Parameters

pos (int) – A nonzero element stored at position pos.

remove_var(var: mindoptpy.MdoVar)

This function removes a nonzero element from the expression object, given a variable object.

Parameters

var (MdoVar) – A variable object whose coefficient will be removed.

Returns

Return true if the specified coefficient is in the expression and has been removed.

Return type

bint

set_constant(constant: float)

This function changes the constant term in the expression object.

Parameters

constant (float) – A new constant term.

get_dual_value()

This function returns the dual variable associated with this constraint.

Returns

The dual variable associated with this constraint.

Return type

float