8.6.15. LinExpr

class LinExpr

Represents a linear expression. Usually related to constraints or objective functions. A linear expression contains many linear terms and a constant term. Linear expressions can be involved in arithmetic operations. For example:

# constant term increased by 1
linExpr += 1
linExpr = linExpr +1
# add a linear term
linExpr += x
-linExpr
# a MLinExpr
linExpr + numpy.ones((2, 2))
# a QuadExpr
linExpr * x
# a MQuadExpr
linexpr * mLinExpr

Methods

__init__()

Construct a linear expression

add()

Add all terms of another linear expression to the current linear expression

addConstant()

Add a value to the constant term of the linear expression

addTerms()

Add one or more term(s)

clear()

Clear all included terms and set constant to 0

getCoeff()

Obtain the coefficient of a term in a linear expression

getConstant()

Obtain the constant term of a linear expression

getValue()

After solving the problem, obtain the value of the linear expression

getVar()

Get the variable of a term in a linear expression

remove()

Delete some terms contained in an expression

size()

Obtain the number of terms, excluding constant terms

__init__(arg1=0, arg2=None)

Construct a linear expression

Parameters
  • arg1=0 – When two parameters are provided during a call, arg1 is usually a coefficient or a list of coefficients.

  • arg2=None – When two parameters are provided during a call, arg2 is usually a variable or a list of variables.

example:

LinExpr((1, 2, 3), (x, y, z))
LinExpr(2, y)
LinExpr(x)
LinExpr(2 * x + 1)
LinExpr(1)
add(expr, mult=1.0)

Add all terms of another linear expression to the current linear expression

Parameters
  • expr – Another linear expression

  • mult=1.0 – Multiplier. Default value: 1.0.

example:

linExpr.add(expr, -1)
addConstant(c)

Add a value to the constant term of the linear expression

Parameters

c – The value to be added. A negative number indicates that value should be subtracted.

example:

linExpr.addConstant(-linExpr.getConstant())
addTerms(coeffs, vars)

Add one or more term(s)

Parameters
  • coeffs – The coefficient of the term(s) to be added, which may be a number or an array.

  • vars – The variable of the term(s) to be added, which can be a single variable or an array.

example:

linExpr.addTerms([1, 2], [x, y])
linExpr.addTerms(1, x)
clear()

Clear all included terms and set constant to 0

example:

linExpr = 2 * x +3 * y +1
linExpr.clear()
print(linExpr.size() == 0)
print(linExpr.getConstant() == 0)
getCoeff(index)

Obtain the coefficient of a term in a linear expression.

Parameters

index – The index of the term to obtain the coefficient.

example:

linExpr = 2 * x + 1 * y
print(linExpr.getCoeff(1) == 1.0)
getConstant()

Obtain the constant term of a linear expression.

example:

linExpr.addConstant(-linExpr.getConstant())
getValue()

After solving the problem, obtain the value of the linear expression

example:

m.optimize()
linExpr = 2 * x + y * 1
print(linExpr.getValue())
getVar(index)

Get the variable of a term in a linear expression

Parameters

index – The index of the term to obtain the variable from

example:

linExpr = 2 * x + 1 * y
print(linExpr.getVar(1).sameAs(y))
remove(item)

Delete some terms contained in an expression

Parameters

item – If item is a number, the term whose index is item is deleted. If item is a variable, all terms that contain this variable are deleted.

example:

linExpr = 2 * x +3 * y +4 * x
linExpr.remove(1)
linExpr.remove(x)
print(linExpr.size() == 0)
size()

Obtain the number of terms, excluding constant terms.

example:

linExpr = 2 * x +3 * y +1
print(linExpr.size() == 2)