Try our new documentation site (beta).


int GRBaddqconstr ( GRBmodel *model,
    int numlnz,
    int *lind,
    double *lval,
    int numqnz,
    int *qrow,
    int *qcol,
    double *qval,
    char sense,
    double rhs,
    const char *constrname )

Add a new quadratic constraint to a model. Note that, due to our lazy update approach, the new constraint 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).

A quadratic constraint consists of a set of quadratic terms, a set of linear terms, a sense, and a right-hand side value: <span>$</span>x^TQx + q^Tx \le b<span>$</span>. The quadratic terms are input through the numqnz, qrow, qcol, and qval arguments, and the linear terms are input through the numlnz, lind, and lval arguments.

Important note: the algorithms Gurobi uses to solve quadratically constrained problems can only handle certain types of quadratic constraints. Constraints of the following forms are always accepted:

  • <span>$</span>x^TQx + q^Tx \le b<span>$</span>, where <span>$</span>Q<span>$</span> is Positive Semi-Definite (PSD)
  • <span>$</span>x^Tx \le y^{2}<span>$</span>, where <span>$</span>x<span>$</span> is a vector of variables, and <span>$</span>y<span>$</span> is a non-negative variable (a Second-Order Cone)
  • <span>$</span>x^Tx \le y z<span>$</span>, where <span>$</span>x<span>$</span> is a vector of variables, and <span>$</span>y<span>$</span> and <span>$</span>z<span>$</span> are non-negative variables (a rotated Second-Order Cone)
If you add a constraint that isn't in one of these forms (and Gurobi presolve is unable to transform the constraint into one of these forms), you'll get an error when you try to solve the model. Constraints where the quadratic terms only involve binary variables will always be transformed into one of these forms.

Return value:

A non-zero return value indicates that a problem occurred while adding the quadratic constraint. Refer to the Error Code table for a list of possible return values. Details on the error can be obtained by calling GRBgeterrormsg.


model: The model to which the new constraint should be added.

numlnz: The number of linear terms in the new quadratic constraint.

lind: Variable indices associated with linear terms.

lval: Numerical values associated with linear terms.

numqlnz: The number of quadratic terms in the new quadratic constraint.

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 associated arguments arrays provide the corresponding values for each quadratic term. To give an example, if you wish to input quadratic terms <span>$</span>2 x_0^2 + x_0 x_1 +
x_1^2<span>$</span>, you would call this routine with 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.

sense: Sense for the new quadratic constraint. Options are GRB_LESS_EQUAL or GRB_GREATER_EQUAL.

rhs: Right-hand-side value for the new quadratic constraint.

constrname: Name for the new quadratic constraint. This argument can be NULL, in which case the constraint is given a default name.

Example usage:

  int    lind[] = {1, 2};
  double lval[] = {2.0, 1.0};
  int    qrow[] = {0, 0, 1};
  int    qcol[] = {0, 1, 1};
  double qval[] = {2.0, 1.0, 1.0};
  /* 2 x0^2 + x0 x1 + x1^2 + 2 x1 + x2 <= 1 */
  error = GRBaddqconstr(model, 2, lind, lval, 3, qrow, qcol, qval,
                        GRB_LESS_EQUAL, 1.0, "New");

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.