GRBaddqpterms


GRBaddqpterms

int GRBaddqpterms( GRBmodel *model,
int numqnz,
int *qrow,
int *qcol,
double *qval )

Add new quadratic objective terms into an existing model. Note that new terms are (numerically) added into existing terms, and that adding a term in rowiand columnjis equivalent to adding a term in rowjand columni. You can add all quadratic objective terms in a single call, or you can add them incrementally in multiple calls.

Note that, due to our lazy update approach, the new quadratic terms won't actually be added until you update the model (usingGRBupdatemodel), optimize the model (usingGRBoptimize), or write the model to disk (usingGRBwrite).

To build an objective that contains both linear and quadratic terms, use this routine to add the quadratic terms and use theObjattribute to add the linear terms.

If you wish to change a quadratic term, you can either add the difference between the current term and the desired term using this routine, or you can callGRBdelqto delete all quadratic terms, and then rebuild your new quadratic objective from scratch.

Return value:

A non-zero return value indicates that a problem occurred while adding the quadratic terms. Refer to theError Codetable for a list of possible return values. Details on the error can be obtained by callingGRBgeterrormsg.

Arguments:

model: The model to which the new quadratic objective terms should be added.

numqnz: The number of new quadratic objective terms to add.

qrow: Row indices associated with quadratic terms. A quadratic term is represented using three values: a pair of indices (stored inqrowandqcol), and a coefficient (stored inqval). The three argument arrays provide the corresponding values for each quadratic term. To give an example, to represent<span>$</span>2 x_0^2 + x_0 x_1 + x_1^2<span>$</span>, you would havenumqnz=3,qrow[] = {0, 0, 1},qcol[] = {0, 1, 1}, andqval[] = {2.0, 1.0, 1.0}.

qcol: Column indices associated with quadratic terms. See the description of theqrowargument for more information.

qval: Numerical values associated with quadratic terms. See the description of theqrowargument for more information.

Important notes:

Note that building quadratic objectives requires some care, particularly if you are migrating an application from another solver. Some solvers require you to specify the entire<span>$</span>Q<span>$</span>matrix, while others only accept the lower triangle. In addition, some solvers include an implicit 0.5 multiplier on<span>$</span>Q<span>$</span>,而阿thers do not. The Gurobi interface is built around quadratic terms, rather than a<span>$</span>Q<span>$</span>matrix. If your quadratic objective contains a term2 x y, you can enter it as a single term,2 x y, or as a pair of terms,x yandy x.

Example usage:

int qrow[] = {0, 0, 1}; int qcol[] = {0, 1, 1}; double qval[] = {2.0, 1.0, 3.0}; /* minimize 2 x^2 + x*y + 3 y^2 */ error = GRBaddqpterms(model, 3, qrow, qcol, qval);