Movatterモバイル変換


[0]ホーム

URL:


ContentsMenuExpandLight modeDark modeAuto light/dark mode
Gurobi Optimizer Reference Manual
Light LogoDark Logo

Concepts

Features

Reference

Gurobi
Back to top

GRBLinExpr#

GRBLinExpr#

Gurobi linear expression object. A linear expression consists of aconstant term, plus a list of coefficient-variable pairs that capturethe linear terms. Linear expressions are used to build constraints. Theyare temporary objects that typically have short lifespans.

TheGRBLinExpr class is a sub-class of the abstract base classGRBExpr.

You generally build linear expressions by starting with an emptyexpression (using theGRBLinExpr constructor), and thenadding terms. Terms can be added individually, usingaddTerm, or in groups, usingaddTerms, ormultAdd. Terms can also be removedfrom an expression, usingremove.

To add a linear constraint to your model, you generally build one or twolinear expression objects (expr1 andexpr2) and then pass themtoGRBModel.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 theexpression object you used to build the constraint will not change theconstraint (you would useGRBModel.chgCoeff for that).

Individual terms in a linear expression can be queried using thegetVar,getCoeff, andgetConstant methods. You can querythe number of terms in the expression using thesize method.

Note that a linear expression may contain multiple terms that involvethe same variable. These duplicate terms are merged when creating aconstraint from an expression, but they may be visible when inspectingindividual terms in the expression (e.g., when usinggetVar).

GRBLinExprGRBLinExpr()#

Linear expression constructor that creates an empty linear expression.

Return value:

An empty expression object.

Example:
// Create empty linear expressionGRBLinExprexpr=newGRBLinExpr();
GRBLinExprGRBLinExpr(GRBLinExprle)#

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 expressionGRBLinExprcopy=newGRBLinExpr(expr);
voidadd(GRBLinExprle)#

Add one linear expression into another. Upon completion, the invokinglinear expression will be equal to the sum of itself and the argumentexpression.

Arguments:

le – Linear expression to add.

Example:
// Add le to linear expression exprexpr.add(le);
voidaddConstant(doublec)#

Add a constant into a linear expression.

Arguments:

c – Constant to add to expression.

Example:
// Add constant to linear expressionexpr.addConstant(2.0);
voidaddTerm(doublecoeff,GRBVarvar)#

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 expressionexpr.addTerm(2.0,x);
voidaddTerms(double[]coeffs,GRBVar[]vars)#

Add a list of terms into a linear expression. Note that the lengths ofthe 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 * zdouble[]coeffs={2.0,3.0,4.0};GRBVar[]vars={x,y,z};expr.addTerms(coeffs,vars);
voidaddTerms(double[]coeffs,GRBVar[]vars,intstart,intlen)#

Add new terms into a linear expression. This signature allows you to usearrays to hold the coefficients and variables that describe the terms inan array without being forced to add a term for each entry in the array.Thestart andlen arguments allow you to specify which terms toadd.

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 expressionexpr.addTerms(coeffs,vars,0,2);
voidclear()#

Set a linear expression to 0.

Example:
expr.clear();
doublegetConstant()#

Retrieve the constant term from a linear expression.

Return value:

Constant from expression.

Example:
// Get constant from expressiondoubleconstant=expr.getConstant();
doublegetCoeff(inti)#

Retrieve the coefficient from a single term of the expression.

Arguments:

i – Index for coefficient of interest.

Return value:

Coefficient for the term at indexi in the expression.

Example:
// Get coefficient of first term in expressiondoublecoeff=expr.getCoeff(0);
doublegetValue()#

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 pointdoublevalue=expr.getValue();
GRBVargetVar(inti)#

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 indexi in the expression.

Example:
// Get variable from first term in expressionGRBVarv=expr.getVar(0);
voidmultAdd(doublem,GRBLinExprle)#

Add a constant multiple of one linear expression into another. Uponcompletion, the invoking linear expression is equal to the sum of itselfand the constant times the argument expression.

Arguments:
  • m – Constant multiplier for added expression.

  • le – Linear expression to add.

Example:
// Add 2 * le to exprexpr.multAdd(2.0,le);
voidremove(inti)#

Remove the term stored at indexi of the expression.

Arguments:

i – The index of the term to be removed.

Example:
// Remove first term from expressionexpr.remove(0);
booleanremove(GRBVarvar)#

Remove all terms associated with variablevar from the expression.

Arguments:

var – The variable whose term should be removed.

Return value:

Returnstrue if the variable appeared in the linearexpression (and was removed).

Example:
// Remove terms associated with variable xbooleanremoved=expr.remove(x);
intsize()#

Retrieve the number of terms in the linear expression (not including theconstant).

Return value:

Number of terms in the expression.

Example:
// Get size of linear expressionints=expr.size();

Help and Feedback

On this page

[8]ページ先頭

©2009-2025 Movatter.jp