GRBLinExpr#
- GRBLinExpr#
Gurobi linear expression object. A linear expression consists of a constant term, plus a list of coefficient-variable pairs that capture the linear terms. Linear expressions are used to build constraints. They are temporary objects that typically have short lifespans.
The
GRBLinExpr
class is a sub-class of the abstract base classGRBExpr
.In .NET languages that support operator overloading, you generally build linear expressions using overloaded operators. For example, if
x
is aGRBVar
object, thenx + 1
is aGRBLinExpr
object. Expressions can be built from constants (e.g.,expr = 0
), variables (e.g.,expr = 1 * x + 2 * y
, or from other expressions (e.g.,expr2 = 2 * expr1 + x
, orexpr3 = expr1 + 2 * expr2
). You can also modify existing expressions (e.g.,expr += x
, orexpr2 -= expr1
).The other option for building expressions is to start with an empty expression (using the
GRBLinExpr
constructor), and then add terms. Terms can be added individually (usingAddTerm
) or in groups (usingAddTerms
orMultAdd
). Terms can also be removed from an expression, usingRemove
.Given all these options for building expressions, you may wonder which is fastest. For small expressions, you won’t need to worry about performance differences between them. If you are building lots of very large expressions (100s of terms), the most efficient approach will be a single call to
AddTerms
. UsingAddTerm
. to add individual terms is slightly less efficient, and using overloaded arithemetic operators is the least efficient option.To add a linear constraint to your model, you generally build one or two linear expression objects (
expr1
andexpr2
) and then use an overloaded comparison operator to build an argument forGRBModel.AddConstr
. To give a few examples:model.AddConstr(expr1 <= expr2) model.AddConstr(expr1 == 1) model.AddConstr(2*x + 3*y <= 4)
Once you add a constraint to your model, subsequent changes to the expression object you used to build the constraint will not change the constraint (you would use
GRBModel.ChgCoeff
for that).Individual terms in a linear expression can be queried using the
GetVar
andGetCoeff
methods. The constant can be queried using theConstant
property. You can query the number of terms in the expression using theSize
property.Note that a linear expression may contain multiple terms that involve the same variable. These duplicate terms are merged when creating a constraint from an expression, but they may be visible when inspecting individual terms in the expression (e.g., when using
GetVar
).- GRBLinExpr GRBLinExpr()#
Linear expression constructor that creates an empty linear expression.
- Returns:
An empty expression object.
- GRBLinExpr GRBLinExpr(double a)#
Linear expression constructor that creates a constant linear expression.
- Returns:
A linear expression object.
- GRBLinExpr GRBLinExpr(GRBLinExpr orig)#
Linear expression constructor that copies an existing expression.
- Parameters:
orig – Existing expression to copy.
- Returns:
A copy of the input expression object.
- void Add(GRBLinExpr le)#
Add one linear expression into another. Upon completion, the invoking linear expression will be equal to the sum of itself and the argument expression.
- Parameters:
le – Linear expression to add.
- void AddConstant(double c)#
Add a constant into a linear expression.
- Parameters:
c – Constant to add to expression.
- void AddTerm(double coeff, GRBVar var)#
Add a single term into a linear expression.
- Parameters:
coeff – Coefficient for new term.
var – Variable for new term.
- void AddTerms(double[] coeffs, GRBVar[] vars)#
Add a list of terms into a linear expression. Note that the lengths of the two argument arrays must be equal.
- Parameters:
coeffs – Coefficients for new terms.
vars – Variables for new terms.
- void AddTerms(double[] coeffs, GRBVar[] vars, int start, int len)#
Add new terms into a linear expression. This signature allows you to use arrays to hold the coefficients and variables that describe the terms in an array without being forced to add a term for each entry in the array. The
start
andlen
arguments allow you to specify which terms to add.- Parameters:
coeffs – Coefficients for new terms.
vars – Variables for new terms.
start – The first term in the list to add.
len – The number of terms to add.
- void Clear()#
Set a linear expression to 0.
You should use the overloaded
expr = 0
instead. Theclear
method is mainly included for consistency with our interfaces to non-overloaded languages.
- double Constant#
(Property) The constant term from the linear expression.
- double GetCoeff(int i)#
Retrieve the coefficient from a single term of the expression.
- Returns:
Coefficient for the term at index
i
in the expression.
- GRBVar GetVar(int i)#
Retrieve the variable object from a single term of the expression.
- Returns:
Variable for the term at index
i
in the expression.
- void MultAdd(double m, GRBLinExpr le)#
Add a constant multiple of one linear expression into another. Upon completion, the invoking linear expression is equal the sum of itself and the constant times the argument expression.
- Parameters:
m – Constant multiplier for added expression.
le – Linear expression to add.
- void Remove(int i)#
Remove the term stored at index
i
of the expression.- Parameters:
i – The index of the term to be removed.
- boolean Remove(GRBVar var)#
Remove all terms associated with variable
var
from the expression.- Parameters:
var – The variable whose term should be removed.
- Returns:
Returns
true
if the variable appeared in the linear expression (and was removed).
- int Size#
(Property) The number of terms in the linear expression (not including the constant).
- double Value#
(Property) The value of an expression for the current solution.