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 row i and column j is equivalent to adding a term in row j and column i. 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 (using GRBupdatemodel), optimize the model (using GRBoptimize), or write the model to disk (using GRBwrite).

To build an objective that contains both linear and quadratic terms, use this routine to add the quadratic terms and use the Obj attribute 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 call GRBdelq to 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 the Error Code table for a list of possible return values. Details on the error can be obtained by calling GRBgeterrormsg.

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 in qrow and qcol), and a coefficient (stored in qval). 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 have numqnz=3, qrow[] = {0, 0, 1}, qcol[] = {0, 1, 1}, and qval[] = {2.0, 1.0, 1.0}.

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

qval: Numerical values associated with quadratic terms. See the description of the qrow argument 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 multipler on <span>$</span>Q<span>$</span>, while others 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 term 2 x y, you can enter it as a single term, 2 x y, or as a pair of terms, x y and y 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);

Try Gurobi for Free

Choose the evaluation license that fits you best, and start working with our Expert Team for technical guidance and support.

Evaluation License
Get a free, full-featured license of the Gurobi Optimizer to experience the performance, support, benchmarking and tuning services we provide as part of our product offering.
Academic License
Gurobi supports the teaching and use of optimization within academic institutions. We offer free, full-featured copies of Gurobi for use in class, and for research.
Cloud Trial

Request free trial hours, so you can see how quickly and easily a model can be solved on the cloud.

Search