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 class GRBExpr.

In .NET languages that support operator overloading, you generally build linear expressions using overloaded operators. For example, if x is a GRBVar object, then x + 1 is a GRBLinExpr 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, or expr3 = expr1 + 2 * expr2). You can also modify existing expressions (e.g., expr += x, or expr2 -= 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 (using AddTerm) or in groups (using AddTerms or MultAdd). Terms can also be removed from an expression, using Remove.

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. Using AddTerm. 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 and expr2) and then use an overloaded comparison operator to build an argument for GRBModel.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 and GetCoeff methods. The constant can be queried using the Constant property. You can query the number of terms in the expression using the Size 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.

Example:
// Create empty linear expression
GRBLinExpr lexpr = new GRBLinExpr();
GRBLinExpr GRBLinExpr(double a)#

Linear expression constructor that creates a constant linear expression.

Returns:

A linear expression object.

Example:
// Create constant linear expression with value 1.0
GRBLinExpr lexpr = new GRBLinExpr(1.0);
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.

Example:
// Copy existing linear expression
GRBLinExpr lexpr = new GRBLinExpr(lexpr2);
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.

Example:
// Add x + y to linear expression
lexpr.Add(x + y);
void AddConstant(double c)#

Add a constant into a linear expression.

Parameters:

c – Constant to add to expression.

Example:
// Add 2.0 to linear expression
lexpr.AddConstant(2.0);
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.

Example:
// Add 2.0 x to linear expression
lexpr.AddTerm(2.0, x);
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.

Example:
// Add 2.0 x + 3.0 y + 4.0 z to linear expression
double[] coeffs = { 2.0, 3.0, 4.0 };
GRBVar[] vars = { x, y, z };
lexpr.AddTerms(coeffs, vars);
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 and len 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.

Example:
// Add 2.0 x + 3.0 y to linear expression
double[] coeffs = { 2.0, 3.0, 4.0 };
GRBVar[] vars = { x, y, z };
lexpr.AddTerms(coeffs, vars, 0, 2);
void Clear()#

Set a linear expression to 0.

You should use the overloaded expr = 0 instead. The clear method is mainly included for consistency with our interfaces to non-overloaded languages.

Example:
lexpr.Clear();
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.

Example:
// Get first coefficient of linear expression
double coeff = lexpr.GetCoeff(0);
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.

Example:
// Get first variable of linear expression
GRBVar var = lexpr.GetVar(0);
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.

Example:
// Add 2.0 * (x + y) to linear expression
lexpr.MultAdd(2.0, x + y);
void Remove(int i)#

Remove the term stored at index i of the expression.

Parameters:

i – The index of the term to be removed.

Example:
// Remove first term of linear expression
lexpr.Remove(0);
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).

Example:
// Remove variable x from linear expression
bool removed = lexpr.Remove(x);
int Size#

(Property) The number of terms in the linear expression (not including the constant).

Example:
int s = lexpr.Size;
double Value#

(Property) The value of an expression for the current solution.

Example:
double val = lexpr.Value;