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
.You generally build linear expressions by starting with an empty expression (using the
GRBLinExpr
constructor), and then adding terms. Terms can be added individually, usingaddTerm
, or in groups, usingaddTerms
, ormultAdd
. Terms can also be removed from an expression, usingremove
.To add a linear constraint to your model, you generally build one or two linear expression objects (
expr1
andexpr2
) and then pass them toGRBModel.addConstr
. To give a few examples:model.addConstr(expr1, GRB.LESS_EQUAL, expr2); model.addConstr(expr1, GRB.EQUAL, 1);
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
,getCoeff
, andgetConstant
methods. You can query the number of terms in the expression using thesize
method.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.
- Return value:
An empty expression object.
- Example:
// Create empty linear expression GRBLinExpr expr = new GRBLinExpr();
- GRBLinExpr GRBLinExpr(GRBLinExpr le)#
Linear expression constructor that copies an existing linear expression.
- Arguments:
le – Existing expression to copy.
- Return value:
A copy of the input expression object.
- Example:
// Copy existing linear expression GRBLinExpr copy = new GRBLinExpr(expr);
- 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.
- Arguments:
le – Linear expression to add.
- Example:
// Add le to linear expression expr expr.add(le);
- void addConstant(double c)#
Add a constant into a linear expression.
- Arguments:
c – Constant to add to expression.
- Example:
// Add constant to linear expression expr.addConstant(2.0);
- void addTerm(double coeff, GRBVar var)#
Add a single term into a linear expression.
- Arguments:
coeff – Coefficient for new term.
var – Variable for new term.
- Example:
// Add term 2 * x to expression expr.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.
- Arguments:
coeffs – Coefficients for new terms.
vars – Variables for new terms.
- Example:
// Add terms 2 * x + 3 * y + 4 * z double[] coeffs = {2.0, 3.0, 4.0}; GRBVar[] vars = {x, y, z}; expr.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
andlen
arguments allow you to specify which terms to add.- Arguments:
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:
double[] coeffs = {2.0, 3.0, 4.0}; GRBVar[] vars = {x, y, z}; // Add first 2 terms 2 * x + 3 * y to expression expr.addTerms(coeffs, vars, 0, 2);
- void clear()#
Set a linear expression to 0.
- Example:
expr.clear();
- double getConstant()#
Retrieve the constant term from a linear expression.
- Return value:
Constant from expression.
- Example:
// Get constant from expression double constant = expr.getConstant();
- double getCoeff(int i)#
Retrieve the coefficient from a single term of the expression.
- Arguments:
i – Index for coefficient of interest.
- Return value:
Coefficient for the term at index
i
in the expression.- Example:
// Get coefficient of first term in expression double coeff = expr.getCoeff(0);
- double getValue()#
Compute the value of a linear expression for the current solution.
- Return value:
Value of the expression for the current solution.
- Example:
// Get value of expression at current solution point double value = expr.getValue();
- GRBVar getVar(int i)#
Retrieve the variable object from a single term of the expression.
- Arguments:
i – Index for term of interest.
- Return value:
Variable for the term at index
i
in the expression.- Example:
// Get variable from first term in expression GRBVar v = expr.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 to the sum of itself and the constant times the argument expression.
- Arguments:
m – Constant multiplier for added expression.
le – Linear expression to add.
- Example:
// Add 2 * le to expr expr.multAdd(2.0, le);
- void remove(int i)#
Remove the term stored at index
i
of the expression.- Arguments:
i – The index of the term to be removed.
- Example:
// Remove first term from expression expr.remove(0);
- boolean remove(GRBVar var)#
Remove all terms associated with variable
var
from the expression.- Arguments:
var – The variable whose term should be removed.
- Return value:
Returns
true
if the variable appeared in the linear expression (and was removed).- Example:
// Remove terms associated with variable x boolean removed = expr.remove(x);
- int size()#
Retrieve the number of terms in the linear expression (not including the constant).
- Return value:
Number of terms in the expression.
- Example:
// Get size of linear expression int s = expr.size();