GRBModel#

class GRBModel#

Gurobi model object. Commonly used methods include addVar (adds a new decision variable to the model), addConstr (adds a new constraint to the model), optimize (optimizes the current model), and get (retrieves the value of an attribute).

GRBModel GRBModel(const GRBEnv &env)#

Constructor for GRBModel that creates an empty model. You can then call addVar and addConstr to populate the model with variables and constraints.

Parameters:

env – Environment for new model.

Returns:

New model object. Model initially contains no variables or constraints.

GRBModel GRBModel(const GRBEnv &env, const string &filename)#

Constructor for GRBModel that reads a model from a file. Note that the type of the file is encoded in the file name suffix. Valid suffixes are .mps, .rew, .lp, .rlp, .dua, .dlp, .ilp, or .opb. The files can be compressed, so additional suffixes of .zip, .gz, .bz2, or .7z are accepted.

Parameters:
  • env – Environment for new model.

  • modelname – Name of the file containing the model.

Returns:

New model object.

GRBModel GRBModel(const GRBModel &model)#

Constructor for GRBModel that creates a copy of an existing model. Note that due to the lazy update approach in Gurobi, you have to call update before copying it.

Parameters:

model – Model to copy.

Returns:

New model object. Model is a clone of the original.

GRBModel GRBModel(const GRBModel &model, const GRBEnv &targetenv)#

Copy an existing model to a different environment. Multiple threads can not work simultaneously within the same environment. Copies of models must therefore reside in different environments for multiple threads to operate on them simultaneously.

Note that this method itself is not thread safe, so you should either call it from the main thread or protect access to it with a lock.

Note that pending updates will not be applied to the model, so you should call update before copying if you would like those to be included in the copy.

For Compute Server users, note that you can copy a model from a client to a Compute Server environment, but it is not possible to copy models from a Compute Server environment to another (client or Compute Server) environment.

Parameters:
  • model – Model to copy.

  • targetenv – Environment to copy model into.

Returns:

New model object. Model is a clone of the original.

GRBConstr addConstr(const GRBLinExpr &lhsExpr, char sense, const GRBLinExpr &rhsExpr, string name = "")#

Add a single linear constraint to a model.

Parameters:
  • lhsExpr – Left-hand side expression for new linear constraint.

  • sense – Sense for new linear constraint (GRB_LESS_EQUAL, GRB_EQUAL, or GRB_GREATER_EQUAL).

  • rhsExpr – Right-hand side expression for new linear constraint.

  • name – (optional) Name for new constraint.

Returns:

New constraint object.

GRBConstr addConstr(const GRBLinExpr &lhsExpr, char sense, GRBVar rhsVar, string name = "")#

Add a single linear constraint to a model.

Parameters:
  • lhsExpr – Left-hand side expression for new linear constraint.

  • sense – Sense for new linear constraint (GRB_LESS_EQUAL, GRB_EQUAL, or GRB_GREATER_EQUAL).

  • rhsVar – Right-hand side variable for new linear constraint.

  • name – (optional) Name for new constraint.

Returns:

New constraint object.

GRBConstr addConstr(const GRBLinExpr &lhsExpr, char sense, double rhsVal, string name = "")#

Add a single linear constraint to a model.

Parameters:
  • lhsExpr – Left-hand side expression for new linear constraint.

  • sense – Sense for new linear constraint (GRB_LESS_EQUAL, GRB_EQUAL, or GRB_GREATER_EQUAL).

  • rhsVal – Right-hand side value for new linear constraint.

  • name – (optional) Name for new constraint.

Returns:

New constraint object.

GRBConstr addConstr(GRBVar lhsVar, char sense, GRBVar rhsVar, string name = "")#

Add a single linear constraint to a model.

Parameters:
  • lhsVar – Left-hand side variable for new linear constraint.

  • sense – Sense for new linear constraint (GRB_LESS_EQUAL, GRB_EQUAL, or GRB_GREATER_EQUAL).

  • rhsVar – Right-hand side variable for new linear constraint.

  • name – (optional) Name for new constraint.

Returns:

New constraint object.

GRBConstr addConstr(GRBVar lhsVar, char sense, double rhsVal, string name = "")#

Add a single linear constraint to a model.

Parameters:
  • lhsVar – Left-hand side variable for new linear constraint.

  • sense – Sense for new linear constraint (GRB_LESS_EQUAL, GRB_EQUAL, or GRB_GREATER_EQUAL).

  • rhsVal – Right-hand side value for new linear constraint.

  • name – (optional) Name for new constraint.

Returns:

New constraint object.

GRBConstr addConstr(GRBTempConstr &tc, string name = "")#

Add a single linear constraint to a model.

Parameters:
  • tc – Temporary constraint object, created using an overloaded comparison operator. See GRBTempConstr for more information.

  • name – (optional) Name for new constraint.

Returns:

New constraint object.

GRBConstr *addConstrs(int count)#

Add count new linear constraints to a model.

We recommend that you build your model one constraint at a time (using addConstr), since it introduces no significant overhead and we find that it produces simpler code. Feel free to use these methods if you disagree, though.

Parameters:

count – Number of constraints to add to the model. The new constraints are all of the form 0 <= 0.

Returns:

Array of new constraint objects. Note that the result is heap-allocated, and must be returned to the heap by the user.

GRBConstr *addConstrs(const GRBLinExpr *lhsExprs, const char *senses, const double *rhsVals, const string *names, int count)#

Add count new linear constraints to a model.

We recommend that you build your model one constraint at a time (using addConstr), since it introduces no significant overhead and we find that it produces simpler code. Feel free to use these methods if you disagree, though.

Parameters:
  • lhsExprs – Left-hand side expressions for the new linear constraints.

  • senses – Senses for new linear constraints (GRB_LESS_EQUAL, GRB_EQUAL, or GRB_GREATER_EQUAL).

  • rhsVals – Right-hand side values for the new linear constraints.

  • names – Names for new constraints.

  • count – Number of constraints to add.

Returns:

Array of new constraint objects. Note that the result is heap-allocated, and must be returned to the heap by the user.

GRBGenConstr addGenConstrMax(GRBVar resvar, const GRBVar *vars, int len, double constant = -GRB_INFINITY, string name = "")#

Add a new general constraint of type GRB_GENCONSTR_MAX to a model.

A MAX constraint \(r = \max\{x_1,\ldots,x_n,c\}\) states that the resultant variable \(r\) should be equal to the maximum of the operand variables \(x_1,\ldots,x_n\) and the constant \(c\).

Parameters:
  • resvar – The resultant variable of the new constraint.

  • vars – Array of variables that are the operands of the new constraint.

  • len – Number of operands in the new constraint (length of vars array).

  • constant – (optional) The additional constant operand of the new constraint.

  • name – (optional) Name for the new general constraint.

Returns:

New general constraint.

GRBGenConstr addGenConstrMin(GRBVar resvar, const GRBVar *vars, int len, double constant = GRB_INFINITY, string name = "")#

Add a new general constraint of type GRB_GENCONSTR_MIN to a model.

A MIN constraint \(r = \min\{x_1,\ldots,x_n,c\}\) states that the resultant variable \(r\) should be equal to the minimum of the operand variables \(x_1,\ldots,x_n\) and the constant \(c\).

Parameters:
  • resvar – The resultant variable of the new constraint.

  • vars – Array of variables that are the operands of the new constraint.

  • len – Number of operands in the new constraint (length of vars array).

  • constant – (optional) The additional constant operand of the new constraint.

  • name – (optional) Name for the new general constraint.

Returns:

New general constraint.

GRBGenConstr addGenConstrAbs(GRBVar resvar, GRBVar argvar, string name = "")#

Add a new general constraint of type GRB_GENCONSTR_ABS to a model.

An ABS constraint \(r = \mbox{abs}\{x\}\) states that the resultant variable \(r\) should be equal to the absolute value of the argument variable \(x\).

Parameters:
  • resvar – The resultant variable of the new constraint.

  • argvar – The argument variable of the new constraint.

  • name – (optional) Name for the new general constraint.

Returns:

New general constraint.

GRBGenConstr addGenConstrAnd(GRBVar resvar, const GRBVar *vars, int len, string name = "")#

Add a new general constraint of type GRB_GENCONSTR_AND to a model.

An AND constraint \(r = \mbox{and}\{x_1,\ldots,x_n\}\) states that the binary resultant variable \(r\) should be \(1\) if and only if all of the operand variables \(x_1,\ldots,x_n\) are equal to \(1\). If any of the operand variables is \(0\), then the resultant should be \(0\) as well.

Note that all variables participating in such a constraint will be forced to be binary, independent of how they were created.

Parameters:
  • resvar – The resultant binary variable of the new constraint.

  • vars – Array of binary variables that are the operands of the new constraint.

  • len – Number of operands in the new constraint (length of vars array).

  • name – (optional) Name for the new general constraint.

Returns:

New general constraint.

GRBGenConstr addGenConstrOr(GRBVar resvar, const GRBVar *vars, int len, string name = "")#

Add a new general constraint of type GRB_GENCONSTR_OR to a model.

An OR constraint \(r = \mbox{or}\{x_1,\ldots,x_n\}\) states that the binary resultant variable \(r\) should be \(1\) if and only if any of the operand variables \(x_1,\ldots,x_n\) is equal to \(1\). If all operand variables are \(0\), then the resultant should be \(0\) as well.

Note that all variables participating in such a constraint will be forced to be binary, independent of how they were created.

Parameters:
  • resvar – The resultant binary variable of the new constraint.

  • vars – Array of binary variables that are the operands of the new constraint.

  • len – Number of operands in the new constraint (length of vars array).

  • name – (optional) Name for the new general constraint.

Returns:

New general constraint.

GRBGenConstr addGenConstrNorm(GRBVar resvar, const GRBVar *vars, int len, double which, string name = "")#

Add a new general constraint of type GRB_GENCONSTR_NORM to a model.

A NORM constraint \(r = \mbox{norm}\{x_1,\ldots,x_n\}\) states that the resultant variable \(r\) should be equal to the vector norm of the argument vector \(x_1,\ldots,x_n\).

Parameters:
  • resvar – The resultant variable of the new constraint.

  • vars – Array of variables that are the operands of the new constraint. Note that this array may not contain duplicates.

  • len – Number of operands in the new constraint (length of vars array).

  • which – Which norm to use. Options are 0, 1, 2, and GRB_INFINITY.

  • name – (optional) Name for the new general constraint.

Returns:

New general constraint.

GRBGenConstr addGenConstrIndicator(GRBVar binvar, int binval, const GRBLinExpr &expr, char sense, double rhs, string name = "")#

Add a new general constraint of type GRB_GENCONSTR_INDICATOR to a model.

An INDICATOR constraint \(z = f \rightarrow a^Tx \leq b\) states that if the binary indicator variable \(z\) is equal to \(f\), where \(f \in \{0,1\}\), then the linear constraint \(a^Tx \leq b\) should hold. On the other hand, if \(z = 1-f\), the linear constraint may be violated. The sense of the linear constraint can also be specified to be \(=\) or \(\geq\).

Note that the indicator variable \(z\) of a constraint will be forced to be binary, independent of how it was created.

Parameters:
  • binvar – The binary indicator variable.

  • binval – The value for the binary indicator variable that would force the linear constraint to be satisfied (\(0\) or \(1\)).

  • expr – Left-hand side expression for the linear constraint triggered by the indicator.

  • sense – Sense for the linear constraint. Options are GRB_LESS_EQUAL, GRB_EQUAL, or GRB_GREATER_EQUAL.

  • rhs – Right-hand side value for the linear constraint.

  • name – (optional) Name for the new general constraint.

Returns:

New general constraint.

GRBGenConstr addGenConstrIndicator(GRBVar binvar, int binval, const GRBTempConstr &constr, string name = "")#

Add a new general constraint of type GRB_GENCONSTR_INDICATOR to a model.

An INDICATOR constraint \(z = f \rightarrow a^Tx \leq b\) states that if the binary indicator variable \(z\) is equal to \(f\), where \(f \in \{0,1\}\), then the linear constraint \(a^Tx \leq b\) should hold. On the other hand, if \(z = 1-f\), the linear constraint may be violated. The sense of the linear constraint can also be specified to be \(=\) or \(\geq\).

Note that the indicator variable \(z\) of a constraint will be forced to be binary, independent of how it was created.

Parameters:
  • binvar – The binary indicator variable.

  • binval – The value for the binary indicator variable that would force the linear constraint to be satisfied (\(0\) or \(1\)).

  • constr – Temporary constraint object defining the linear constraint that is triggered by the indicator. The temporary constraint object is created using an overloaded comparison operator. See GRBTempConstr for more information.

  • name – (optional) Name for the new general constraint.

Returns:

New general constraint.

GRBGenConstr addGenConstrPWL(GRBVar xvar, GRBVar yvar, int npts, const double *xpts, const double *ypts, std::string name = "")#

Add a new general constraint of type GRB_GENCONSTR_PWL to a model.

A piecewise-linear (PWL) constraint states that the relationship \(y = f(x)\) must hold between variables \(x\) and \(y\), where \(f\) is a piecewise-linear function. The breakpoints for \(f\) are provided as arguments. Refer to the description of piecewise-linear objectives for details of how piecewise-linear functions are defined.

Parameters:
  • xvar – The \(x\) variable.

  • yvar – The \(y\) variable.

  • npts – The number of points that define the piecewise-linear function.

  • xpts – The \(x\) values for the points that define the piecewise-linear function. Must be in non-decreasing order.

  • ypts – The \(y\) values for the points that define the piecewise-linear function.

  • name – (optional) Name for the new general constraint.

Returns:

New general constraint.

GRBGenConstr addGenConstrPoly(GRBVar xvar, GRBVar yvar, int plen, const double *p, std::string name = "", std::string options = "")#

Add a new general constraint of type GRB_GENCONSTR_POLY to a model.

A polynomial function constraint states that the relationship \(y = p_0 x^d + p_1 x^{d-1} + ... + p_{d-1} x + p_{d}\) should hold between variables \(x\) and \(y\).

A piecewise-linear approximation of the function is added to the model. The details of the approximation are controlled using the following four attributes (or using the parameters with the same names): FuncPieces, FuncPieceError, FuncPieceLength, and FuncPieceRatio. Alternatively, the function can be treated as a nonlinear constraint by setting the attribute FuncNonlinear. For details, consult the General Constraint discussion.

Parameters:
  • xvar – The \(x\) variable.

  • yvar – The \(y\) variable.

  • plen – The length of coefficient array p. If \(x^d\) is the highest power term, then plen should be \(d+1\).

  • p – The coefficients for the polynomial function (starting with the coefficient for the highest power).

  • name – (optional) Name for the new general constraint.

  • options – (optional) A string that can be used to set the attributes that control the piecewise-linear approximation of this function constraint. To assign a value to an attribute, follow the attribute name with an equal sign and the desired value (with no spaces). Assignments for different attributes should be separated by spaces (e.g. “FuncPieces=-1 FuncPieceError=0.001”).

Returns:

New general constraint.

GRBGenConstr addGenConstrExp(GRBVar xvar, GRBVar yvar, std::string name = "", std::string options = "")#

Add a new general constraint of type GRB_GENCONSTR_EXP to a model.

A natural exponential function constraint states that the relationship \(y = \exp(x)\) should hold for variables \(x\) and \(y\).

A piecewise-linear approximation of the function is added to the model. The details of the approximation are controlled using the following four attributes (or using the parameters with the same names): FuncPieces, FuncPieceError, FuncPieceLength, and FuncPieceRatio. Alternatively, the function can be treated as a nonlinear constraint by setting the attribute FuncNonlinear. For details, consult the General Constraint discussion.

Parameters:
  • xvar – The \(x\) variable.

  • yvar – The \(y\) variable.

  • name – (optional) Name for the new general constraint.

  • options – (optional) A string that can be used to set the attributes that control the piecewise-linear approximation of this function constraint. To assign a value to an attribute, follow the attribute name with an equal sign and the desired value (with no spaces). Assignments for different attributes should be separated by spaces (e.g. “FuncPieces=-1 FuncPieceError=0.001”).

Returns:

New general constraint.

GRBGenConstr addGenConstrExpA(GRBVar xvar, GRBVar yvar, double a, std::string name = "", std::string options = "")#

Add a new general constraint of type GRB_GENCONSTR_EXPA to a model.

An exponential function constraint states that the relationship \(y = a^x\) should hold for variables \(x\) and \(y\), where \(a > 0\) is the (constant) base.

A piecewise-linear approximation of the function is added to the model. The details of the approximation are controlled using the following four attributes (or using the parameters with the same names): FuncPieces, FuncPieceError, FuncPieceLength, and FuncPieceRatio. Alternatively, the function can be treated as a nonlinear constraint by setting the attribute FuncNonlinear. For details, consult the General Constraint discussion.

Parameters:
  • xvar – The \(x\) variable.

  • yvar – The \(y\) variable.

  • a – The base of the function, \(a > 0\).

  • name – (optional) Name for the new general constraint.

  • options – (optional) A string that can be used to set the attributes that control the piecewise-linear approximation of this function constraint. To assign a value to an attribute, follow the attribute name with an equal sign and the desired value (with no spaces). Assignments for different attributes should be separated by spaces (e.g. “FuncPieces=-1 FuncPieceError=0.001”).

Returns:

New general constraint.

GRBGenConstr addGenConstrLog(GRBVar xvar, GRBVar yvar, std::string name = "", std::string options = "")#

Add a new general constraint of type GRB_GENCONSTR_LOG to a model.

A natural logarithmic function constraint states that the relationship \(y = log(x)\) should hold for variables \(x\) and \(y\).

A piecewise-linear approximation of the function is added to the model. The details of the approximation are controlled using the following four attributes (or using the parameters with the same names): FuncPieces, FuncPieceError, FuncPieceLength, and FuncPieceRatio. Alternatively, the function can be treated as a nonlinear constraint by setting the attribute FuncNonlinear. For details, consult the General Constraint discussion.

Parameters:
  • xvar – The \(x\) variable.

  • yvar – The \(y\) variable.

  • name – (optional) Name for the new general constraint.

  • options – (optional) A string that can be used to set the attributes that control the piecewise-linear approximation of this function constraint. To assign a value to an attribute, follow the attribute name with an equal sign and the desired value (with no spaces). Assignments for different attributes should be separated by spaces (e.g. “FuncPieces=-1 FuncPieceError=0.001”).

Returns:

New general constraint.

GRBGenConstr addGenConstrLogA(GRBVar xvar, GRBVar yvar, double a, std::string name = "", std::string options = "")#

Add a new general constraint of type GRB_GENCONSTR_LOGA to a model.

A logarithmic function constraint states that the relationship \(y = log_a(x)\) should hold for variables \(x\) and \(y\), where \(a > 0\) is the (constant) base.

A piecewise-linear approximation of the function is added to the model. The details of the approximation are controlled using the following four attributes (or using the parameters with the same names): FuncPieces, FuncPieceError, FuncPieceLength, and FuncPieceRatio. Alternatively, the function can be treated as a nonlinear constraint by setting the attribute FuncNonlinear. For details, consult the General Constraint discussion.

Parameters:
  • xvar – The \(x\) variable.

  • yvar – The \(y\) variable.

  • a – The base of the function, \(a > 0\).

  • name – (optional) Name for the new general constraint.

  • options – (optional) A string that can be used to set the attributes that control the piecewise-linear approximation of this function constraint. To assign a value to an attribute, follow the attribute name with an equal sign and the desired value (with no spaces). Assignments for different attributes should be separated by spaces (e.g. “FuncPieces=-1 FuncPieceError=0.001”).

Returns:

New general constraint.

GRBGenConstr addGenConstrLogistic(GRBVar xvar, GRBVar yvar, std::string name = "", std::string options = "")#

Add a new general constraint of type GRB_GENCONSTR_LOGISTIC to a model.

A logistic function constraint states that the relationship \(y = \frac{1}{1 + e^{-x}}\) should hold for variables \(x\) and \(y\).

A piecewise-linear approximation of the function is added to the model. The details of the approximation are controlled using the following four attributes (or using the parameters with the same names): FuncPieces, FuncPieceError, FuncPieceLength, and FuncPieceRatio. Alternatively, the function can be treated as a nonlinear constraint by setting the attribute FuncNonlinear. For details, consult the General Constraint discussion.

Parameters:
  • xvar – The \(x\) variable.

  • yvar – The \(y\) variable.

  • name – (optional) Name for the new general constraint.

  • options – (optional) A string that can be used to set the attributes that control the piecewise-linear approximation of this function constraint. To assign a value to an attribute, follow the attribute name with an equal sign and the desired value (with no spaces). Assignments for different attributes should be separated by spaces (e.g. “FuncPieces=-1 FuncPieceError=0.001”).

Returns:

New general constraint.

GRBGenConstr addGenConstrPow(GRBVar xvar, GRBVar yvar, double a, std::string name = "", std::string options = "")#

Add a new general constraint of type GRB_GENCONSTR_POW to a model.

A power function constraint states that the relationship \(y = x^a\) should hold for variables \(x\) and \(y\), where \(a\) is the (constant) exponent.

If the exponent \(a\) is negative, the lower bound on \(x\) must be strictly positive. If the exponent isn’t an integer, the lower bound on \(x\) must be non-negative.

A piecewise-linear approximation of the function is added to the model. The details of the approximation are controlled using the following four attributes (or using the parameters with the same names): FuncPieces, FuncPieceError, FuncPieceLength, and FuncPieceRatio. Alternatively, the function can be treated as a nonlinear constraint by setting the attribute FuncNonlinear. For details, consult the General Constraint discussion.

Parameters:
  • xvar – The \(x\) variable.

  • yvar – The \(y\) variable.

  • a – The exponent of the function.

  • name – (optional) Name for the new general constraint.

  • options – (optional) A string that can be used to set the attributes that control the piecewise-linear approximation of this function constraint. To assign a value to an attribute, follow the attribute name with an equal sign and the desired value (with no spaces). Assignments for different attributes should be separated by spaces (e.g. “FuncPieces=-1 FuncPieceError=0.001”).

Returns:

New general constraint.

GRBGenConstr addGenConstrSin(GRBVar xvar, GRBVar yvar, std::string name = "", std::string options = "")#

Add a new general constraint of type GRB_GENCONSTR_SIN to a model.

A sine function constraint states that the relationship \(y = sin(x)\) should hold for variables \(x\) and \(y\).

A piecewise-linear approximation of the function is added to the model. The details of the approximation are controlled using the following four attributes (or using the parameters with the same names): FuncPieces, FuncPieceError, FuncPieceLength, and FuncPieceRatio. Alternatively, the function can be treated as a nonlinear constraint by setting the attribute FuncNonlinear. For details, consult the General Constraint discussion.

Parameters:
  • xvar – The \(x\) variable.

  • yvar – The \(y\) variable.

  • name – (optional) Name for the new general constraint.

  • options – (optional) A string that can be used to set the attributes that control the piecewise-linear approximation of this function constraint. To assign a value to an attribute, follow the attribute name with an equal sign and the desired value (with no spaces). Assignments for different attributes should be separated by spaces (e.g. “FuncPieces=-1 FuncPieceError=0.001”).

Returns:

New general constraint.

GRBGenConstr addGenConstrCos(GRBVar xvar, GRBVar yvar, std::string name = "", std::string options = "")#

Add a new general constraint of type GRB_GENCONSTR_COS to a model.

A cosine function constraint states that the relationship \(y = cos(x)\) should hold for variables \(x\) and \(y\).

A piecewise-linear approximation of the function is added to the model. The details of the approximation are controlled using the following four attributes (or using the parameters with the same names): FuncPieces, FuncPieceError, FuncPieceLength, and FuncPieceRatio. Alternatively, the function can be treated as a nonlinear constraint by setting the attribute FuncNonlinear. For details, consult the General Constraint discussion.

Parameters:
  • xvar – The \(x\) variable.

  • yvar – The \(y\) variable.

  • name – (optional) Name for the new general constraint.

  • options – (optional) A string that can be used to set the attributes that control the piecewise-linear approximation of this function constraint. To assign a value to an attribute, follow the attribute name with an equal sign and the desired value (with no spaces). Assignments for different attributes should be separated by spaces (e.g. “FuncPieces=-1 FuncPieceError=0.001”).

Returns:

New general constraint.

GRBGenConstr addGenConstrTan(GRBVar xvar, GRBVar yvar, std::string name = "", std::string options = "")#

Add a new general constraint of type GRB_GENCONSTR_TAN to a model.

A tangent function constraint states that the relationship \(y = tan(x)\) should hold for variables \(x\) and \(y\).

A piecewise-linear approximation of the function is added to the model. The details of the approximation are controlled using the following four attributes (or using the parameters with the same names): FuncPieces, FuncPieceError, FuncPieceLength, and FuncPieceRatio. Alternatively, the function can be treated as a nonlinear constraint by setting the attribute FuncNonlinear. For details, consult the General Constraint discussion.

Parameters:
  • xvar – The \(x\) variable.

  • yvar – The \(y\) variable.

  • name – (optional) Name for the new general constraint.

  • options – (optional) A string that can be used to set the attributes that control the piecewise-linear approximation of this function constraint. To assign a value to an attribute, follow the attribute name with an equal sign and the desired value (with no spaces). Assignments for different attributes should be separated by spaces (e.g. “FuncPieces=-1 FuncPieceError=0.001”).

Returns:

New general constraint.

GRBQConstr addQConstr(const GRBQuadExpr &lhsExpr, char sense, const GRBQuadExpr &rhsExpr, string name = "")#

Add a quadratic constraint to a model.

Important

Gurobi can handle both convex and non-convex quadratic constraints. The differences between them can be both important and subtle. Refer to this discussion for additional information.

Parameters:
  • lhsExpr – Left-hand side expression for new quadratic constraint.

  • sense – Sense for new quadratic constraint (GRB_LESS_EQUAL, GRB_EQUAL, or GRB_GREATER_EQUAL).

  • rhsExpr – Right-hand side expression for new quadratic constraint.

  • name – (optional) Name for new constraint.

Returns:

New quadratic constraint object.

GRBQConstr addQConstr(const GRBQuadExpr &lhsExpr, char sense, GRBVar rhsVar, string name = "")#

Add a quadratic constraint to a model.

Important

Gurobi can handle both convex and non-convex quadratic constraints. The differences between them can be both important and subtle. Refer to this discussion for additional information.

Parameters:
  • lhsExpr – Left-hand side expression for new quadratic constraint.

  • sense – Sense for new quadratic constraint (GRB_LESS_EQUAL, GRB_EQUAL, or GRB_GREATER_EQUAL).

  • rhsVar – Right-hand side variable for new quadratic constraint.

  • name – (optional) Name for new constraint.

Returns:

New quadratic constraint object.

GRBQConstr addQConstr(GRBTempConstr &tc, string name = "")#

Add a quadratic constraint to a model.

Important

Gurobi can handle both convex and non-convex quadratic constraints. The differences between them can be both important and subtle. Refer to this discussion for additional information.

Parameters:
  • tc – Temporary constraint object, created using an overloaded comparison operator. See GRBTempConstr for more information.

  • name – (optional) Name for new constraint.

Returns:

New quadratic constraint object.

GRBConstr addRange(const GRBLinExpr &expr, double lower, double upper, string name = "")#

Add a single range constraint to a model. A range constraint states that the value of the input expression must be between the specified lower and upper bounds in any solution.

Note that range constraints are stored internally as equality constraints. We add an extra variable to the model to capture the range information. Thus, the Sense attribute on a range constraint will always be GRB_EQUAL. In particular introducing a range constraint

\[L \leq a^T x \leq U\]

is equivalent to adding a slack variable \(s\) and the following constraints

\[\begin{split}\begin{array}{rl} a^T x - s & = L \\ 0 \leq s & \leq U - L. \end{array}\end{split}\]
Parameters:
  • expr – Linear expression for new range constraint.

  • lower – Lower bound for linear expression.

  • upper – Upper bound for linear expression.

  • name – (optional) Name for new constraint.

Returns:

New constraint object.

GRBConstr *addRanges(const GRBLinExpr *exprs, const double *lower, const double *upper, const string *names, int count)#

Add new range constraints to a model. A range constraint states that the value of the input expression must be between the specified lower and upper bounds in any solution.

Parameters:
  • exprs – Linear expressions for the new range constraints.

  • lower – Lower bounds for linear expressions.

  • upper – Upper bounds for linear expressions.

  • name – Names for new range constraints.

  • count – Number of range constraints to add.

Returns:

Array of new constraint objects. Note that the result is heap-allocated, and must be returned to the heap by the user.

GRBSOS addSOS(const GRBVar *vars, const double *weights, int len, int type)#

Add an SOS constraint to the model. Please refer to this section for details on SOS constraints.

Parameters:
  • vars – Array of variables that participate in the SOS constraint.

  • weights – Weights for the variables in the SOS constraint.

  • len – Number of members in the new SOS set (length of vars and weights arrays).

  • type – SOS type (can be GRB_SOS_TYPE1 or GRB_SOS_TYPE2).

Returns:

New SOS constraint.

GRBVar addVar(double lb, double ub, double obj, char type, string name = "")#

Add a single decision variable to a model; non-zero entries will be added later.

Parameters:
  • lb – Lower bound for new variable.

  • ub – Upper bound for new variable.

  • obj – Objective coefficient for new variable.

  • type – Variable type for new variable (GRB_CONTINUOUS, GRB_BINARY, GRB_INTEGER, GRB_SEMICONT, or GRB_SEMIINT).

  • name – (optional) Name for new variable.

Returns:

New variable object.

GRBVar addVar(double lb, double ub, double obj, char type, int numnz, const GRBConstr *constrs, const double *coeffs, string name = "")#

Add a single decision variable and the associated non-zero coefficients to a model.

Parameters:
  • lb – Lower bound for new variable.

  • ub – Upper bound for new variable.

  • obj – Objective coefficient for new variable.

  • type – Variable type for new variable (GRB_CONTINUOUS, GRB_BINARY, GRB_INTEGER, GRB_SEMICONT, or GRB_SEMIINT).

  • numnz – Number of constraints in which this new variable participates.

  • constrs – Array of constraints in which the variable participates.

  • coeffs – Array of coefficients for each constraint in which the variable participates.

  • name – (optional) Name for new variable.

Returns:

New variable object.

GRBVar addVar(double lb, double ub, double obj, char type, const GRBColumn &col, string name = "")#

Add a single decision variable and the associated non-zero coefficients to a model.

Parameters:
  • lb – Lower bound for new variable.

  • ub – Upper bound for new variable.

  • obj – Objective coefficient for new variable.

  • type – Variable type for new variable (GRB_CONTINUOUS, GRB_BINARY, GRB_INTEGER, GRB_SEMICONT, or GRB_SEMIINT).

  • col – GRBColumn object for specifying a set of constraints to which new variable belongs.

  • name – (optional) Name for new variable.

Returns:

New variable object.

GRBVar *addVars(int count, char type = GRB_CONTINUOUS)#

Add count new decision variables to a model. All associated attributes take their default values, except the variable type, which is specified as an argument.

Parameters:
  • count – Number of variables to add.

  • type – (optional) Variable type for new variables (GRB_CONTINUOUS, GRB_BINARY, GRB_INTEGER, GRB_SEMICONT, or GRB_SEMIINT).

Returns:

Array of new variable objects. Note that the result is heap-allocated, and must be returned to the heap by the user.

GRBVar *addVars(const double *lb, const double *ub, const double *obj, const char *type, const string *names, int count)#

Add count new decision variables to a model. This signature allows you to use arrays to hold the various variable attributes (lower bound, upper bound, etc.).

Parameters:
  • lb – Lower bounds for new variables. Can be NULL, in which case the variables get lower bounds of 0.0.

  • ub – Upper bounds for new variables. Can be NULL, in which case the variables get infinite upper bounds.

  • obj – Objective coefficients for new variables. Can be NULL, in which case the variables get objective coefficients of 0.0.

  • type – Variable types for new variables (GRB_CONTINUOUS, GRB_BINARY, GRB_INTEGER, GRB_SEMICONT, or GRB_SEMIINT). Can be NULL, in which case the variables are assumed to be continuous.

  • names – Names for new variables. Can be NULL, in which case all variables are given default names.

  • count – The number of variables to add.

Returns:

Array of new variable objects. Note that the result is heap-allocated, and must be returned to the heap by the user.

GRBVar *addVars(const double *lb, const double *ub, const double *obj, const char *type, const string *names, const GRBColumn *cols, int count)#

Add count new decision variables to a model. This signature allows you to specify the set of constraints to which each new variable belongs using an array of GRBColumn objects.

Parameters:
  • lb – Lower bounds for new variables. Can be NULL, in which case the variables get lower bounds of 0.0.

  • ub – Upper bounds for new variables. Can be NULL, in which case the variables get infinite upper bounds.

  • obj – Objective coefficients for new variables. Can be NULL, in which case the variables get objective coefficients of 0.0.

  • type – Variable types for new variables (GRB_CONTINUOUS, GRB_BINARY, GRB_INTEGER, GRB_SEMICONT, or GRB_SEMIINT). Can be NULL, in which case the variables are assumed to be continuous.

  • names – Names for new variables. Can be NULL, in which case all variables are given default names.

  • cols – GRBColumn objects for specifying a set of constraints to which each new column belongs.

  • count – The number of variables to add.

Returns:

Array of new variable objects. Note that the result is heap-allocated, and must be returned to the heap by the user.

void chgCoeff(GRBConstr constr, GRBVar var, double newvalue)#

Change one coefficient in the model. The desired change is captured using a GRBVar object, a GRBConstr object, and a desired coefficient for the specified variable in the specified constraint. If you make multiple changes to the same coefficient, the last one will be applied.

Note that, due to our lazy update approach, the change won’t actually take effect until you update the model (using GRBModel::update), optimize the model (using GRBModel::optimize), or write the model to disk (using GRBModel::write).

Parameters:
  • constr – Constraint for coefficient to be changed.

  • var – Variable for coefficient to be changed.

  • newvalue – Desired new value for coefficient.

void chgCoeffs(const GRBConstr *constrs, const GRBVar *vars, const double *vals, int len)#

Change a list of coefficients in the model. Each desired change is captured using a GRBVar object, a GRBConstr object, and a desired coefficient for the specified variable in the specified constraint. The entries in the input arrays each correspond to a single desired coefficient change. If you make multiple changes to the same coefficient, the last one will be applied.

Note that, due to our lazy update approach, the change won’t actually take effect until you update the model (using GRBModel::update), optimize the model (using GRBModel::optimize), or write the model to disk (using GRBModel::write).

Parameters:
  • constrs – Constraints for coefficients to be changed.

  • vars – Variables for coefficients to be changed.

  • vals – Desired new values for coefficients.

  • len – Number of coefficients to change (length of vars, constrs, and vals arrays).

void computeIIS()#

Compute an Irreducible Inconsistent Subsystem (IIS).

An IIS is a subset of the constraints and variable bounds with the following properties:

  • It is still infeasible, and

  • If a single constraint or bound is removed, the subsystem becomes feasible.

Note that an infeasible model may have multiple IISs. The one returned by Gurobi is not necessarily the smallest one; there may exist others with fewer constraints or bounds.

IIS results are returned in a number of attributes: IISConstr, IISLB, IISUB, IISSOS, IISQConstr, and IISGenConstr. Each indicates whether the corresponding model element is a member of the computed IIS.

Note that for models with general function constraints, piecewise-linear approximation of the constraints may cause unreliable IIS results.

The IIS log provides information about the progress of the algorithm, including a guess at the eventual IIS size.

If an IIS computation is interrupted before completion, Gurobi will return the smallest infeasible subsystem found to that point.

The IISConstrForce, IISLBForce, IISUBForce, IISSOSForce, IISQConstrForce, and IISGenConstrForce attributes allow you mark model elements to either include or exclude from the computed IIS. Setting the attribute to 1 forces the corresponding element into the IIS, setting it to 0 forces it out of the IIS, and setting it to -1 allows the algorithm to decide.

To give an example of when these attributes might be useful, consider the case where an initial model is known to be feasible, but it becomes infeasible after adding constraints or tightening bounds. If you are only interested in knowing which of the changes caused the infeasibility, you can force the unmodified bounds and constraints into the IIS. That allows the IIS algorithm to focus exclusively on the new constraints, which will often be substantially faster.

Note that setting any of the Force attributes to 0 may make the resulting subsystem feasible, which would then make it impossible to construct an IIS. Trying anyway will result in a IIS_NOT_INFEASIBLE error. Similarly, setting this attribute to 1 may result in an IIS that is not irreducible. More precisely, the system would only be irreducible with respect to the model elements that have force values of -1 or 0.

This method populates the IISConstr, IISQConstr, and IISGenConstr constraint attributes, the IISSOS, SOS attribute, and the IISLB and IISUB variable attributes. You can also obtain information about the results of the IIS computation by writing a .ilp format file (see GRBModel::write). This file contains only the IIS from the original model.

Use the IISMethod parameter to adjust the behavior of the IIS algorithm.

Note that this method can be used to compute IISs for both continuous and MIP models.

void discardConcurrentEnvs()#

Discard concurrent environments for a model.

The concurrent environments created by getConcurrentEnv will be used by every subsequent call to the concurrent optimizer until the concurrent environments are discarded.

void discardMultiobjEnvs()#

Discard all multi-objective environments associated with the model, thus restoring multi objective optimization to its default behavior.

Please refer to the discussion of Multiple Objectives for information on how to specify multiple objective functions and control the trade-off between them.

Use getMultiobjEnv to create a multi-objective environment.

double feasRelax(int relaxobjtype, bool minrelax, int vlen, const GRBVar *vars, const double *lbpen, const double *ubpen, int clen, const GRBConstr *constrs, const double *rhspen)#

Modifies the GRBModel object to create a feasibility relaxation. Note that you need to call optimize on the result to compute the actual relaxed solution.

The feasibility relaxation is a model that, when solved, minimizes the amount by which the solution violates the bounds and linear constraints of the original model. This method provides a number of options for specifying the relaxation.

If you specify relaxobjtype=0, the objective of the feasibility relaxation is to minimize the sum of the weighted magnitudes of the bound and constraint violations. The lbpen, ubpen, and rhspen arguments specify the cost per unit violation in the lower bounds, upper bounds, and linear constraints, respectively.

If you specify relaxobjtype=1, the objective of the feasibility relaxation is to minimize the weighted sum of the squares of the bound and constraint violations. The lbpen, ubpen, and rhspen arguments specify the coefficients on the squares of the lower bound, upper bound, and linear constraint violations, respectively.

If you specify relaxobjtype=2, the objective of the feasibility relaxation is to minimize the weighted count of bound and constraint violations. The lbpen, ubpen, and rhspen arguments specify the cost of violating a lower bound, upper bound, and linear constraint, respectively.

To give an example, if a constraint with rhspen value p is violated by 2.0, it would contribute 2*p to the feasibility relaxation objective for relaxobjtype=0, it would contribute 2*2*p for relaxobjtype=1, and it would contribute p for relaxobjtype=2.

The minrelax argument is a boolean that controls the type of feasibility relaxation that is created. If minrelax=false, optimizing the returned model gives a solution that minimizes the cost of the violation. If minrelax=true, optimizing the returned model finds a solution that minimizes the original objective, but only from among those solutions that minimize the cost of the violation. Note that feasRelax must solve an optimization problem to find the minimum possible relaxation when minrelax=true, which can be quite expensive.

There are two signatures for this method. The more complex one takes a list of variables and constraints, as well as penalties associated with relaxing the corresponding lower bounds, upper bounds, and constraints. If a variable or constraint is not included in one of these lists, the associated bounds or constraints may not be violated. The simpler signature takes a pair of boolean arguments, vrelax and crelax, that indicate whether variable bounds and/or constraints can be violated. If vrelax/crelax is true, then every bound/constraint is allowed to be violated, respectively, and the associated cost is 1.0.

For an example of how this routine transforms a model, and more details about the variables and constraints created, please see this section.

Note that this is a destructive method: it modifies the model on which it is invoked. If you don’t want to modify your original model, use the GRBModel constructor to create a copy before invoking this method.

Create a feasibility relaxation model.

Parameters:
  • relaxobjtype – The cost function used when finding the minimum cost relaxation.

  • minrelax – The type of feasibility relaxation to perform.

  • vlen – The length of the list of variables whose bounds are allowed to be violated.

  • vars – Variables whose bounds are allowed to be violated.

  • lbpen – Penalty for violating a variable lower bound. One entry for each variable in argument vars.

  • ubpen – Penalty for violating a variable upper bound. One entry for each variable in argument vars.

  • clen – The length of the list of linear constraints that are allowed to be violated.

  • constrs – Linear constraints that are allowed to be violated.

  • rhspen – Penalty for violating a linear constraint. One entry for each constraint in argument constrs.

Returns:

Zero if minrelax is false. If minrelax is true, the return value is the objective value for the relaxation performed. If the value is less than 0, it indicates that the method failed to create the feasibility relaxation.

double feasRelax(int relaxobjtype, bool minrelax, bool vrelax, bool crelax)#

Modifies the GRBModel object to create a feasibility relaxation. Note that you need to call optimize on the result to compute the actual relaxed solution.

The feasibility relaxation is a model that, when solved, minimizes the amount by which the solution violates the bounds and linear constraints of the original model. This method provides a number of options for specifying the relaxation.

If you specify relaxobjtype=0, the objective of the feasibility relaxation is to minimize the sum of the weighted magnitudes of the bound and constraint violations. The lbpen, ubpen, and rhspen arguments specify the cost per unit violation in the lower bounds, upper bounds, and linear constraints, respectively.

If you specify relaxobjtype=1, the objective of the feasibility relaxation is to minimize the weighted sum of the squares of the bound and constraint violations. The lbpen, ubpen, and rhspen arguments specify the coefficients on the squares of the lower bound, upper bound, and linear constraint violations, respectively.

If you specify relaxobjtype=2, the objective of the feasibility relaxation is to minimize the weighted count of bound and constraint violations. The lbpen, ubpen, and rhspen arguments specify the cost of violating a lower bound, upper bound, and linear constraint, respectively.

To give an example, if a constraint with rhspen value p is violated by 2.0, it would contribute 2*p to the feasibility relaxation objective for relaxobjtype=0, it would contribute 2*2*p for relaxobjtype=1, and it would contribute p for relaxobjtype=2.

The minrelax argument is a boolean that controls the type of feasibility relaxation that is created. If minrelax=false, optimizing the returned model gives a solution that minimizes the cost of the violation. If minrelax=true, optimizing the returned model finds a solution that minimizes the original objective, but only from among those solutions that minimize the cost of the violation. Note that feasRelax must solve an optimization problem to find the minimum possible relaxation when minrelax=true, which can be quite expensive.

There are two signatures for this method. The more complex one takes a list of variables and constraints, as well as penalties associated with relaxing the corresponding lower bounds, upper bounds, and constraints. If a variable or constraint is not included in one of these lists, the associated bounds or constraints may not be violated. The simpler signature takes a pair of boolean arguments, vrelax and crelax, that indicate whether variable bounds and/or constraints can be violated. If vrelax/crelax is true, then every bound/constraint is allowed to be violated, respectively, and the associated cost is 1.0.

For an example of how this routine transforms a model, and more details about the variables and constraints created, please see this section.

Note that this is a destructive method: it modifies the model on which it is invoked. If you don’t want to modify your original model, use the GRBModel constructor to create a copy before invoking this method.

Simplified method for creating a feasibility relaxation model.

Parameters:
  • relaxobjtype – The cost function used when finding the minimum cost relaxation.

  • minrelax – The type of feasibility relaxation to perform.

  • vrelax – Indicates whether variable bounds can be relaxed (with a cost of 1.0 for any violations.

  • crelax – Indicates whether linear constraints can be relaxed (with a cost of 1.0 for any violations.

Returns:

Zero if minrelax is false. If minrelax is true, the return value is the objective value for the relaxation performed. If the value is less than 0, it indicates that the method failed to create the feasibility relaxation.

GRBModel fixedModel()#

Create the fixed model associated with a MIP model. The MIP model must have a solution loaded (e.g., after a call to the optimize method). In the fixed model, each integer variable is fixed to the value that variable takes in the MIP solution. In addition, continuous variables may be fixed to satisfy SOS or general constraints. The result is a model without any integrality constraints, SOS constraints, or general constraints.

Note that, while the fixed problem is always a continuous model, it may contain a non-convex quadratic objective or non-convex quadratic constraints. As a result, it may still be solved using the MIP algorithm.

Returns:

Fixed model associated with calling object.

double get(GRB_DoubleParam param)#

Query the value of a double-valued parameter.

Parameters:

param – The parameter being queried.

Returns:

The current value of the requested parameter.

int get(GRB_IntParam param)#

Query the value of an int-valued parameter.

Parameters:

param – The parameter being queried.

Returns:

The current value of the requested parameter.

string get(GRB_StringParam param)#

Query the value of a string-valued parameter.

Parameters:

param – The parameter being queried.

Returns:

The current value of the requested parameter.

char *get(GRB_CharAttr attr, const GRBVar *vars, int count)#

Query a char-valued variable attribute for an array of variables.

Parameters:
  • attr – The attribute being queried.

  • vars – An array of variables whose attribute values are being queried.

  • count – The number of variable attributes to retrieve.

Returns:

The current values of the requested attribute for each input variable. Note that the result is heap-allocated, and must be returned to the heap by the user.

char *get(GRB_CharAttr attr, const GRBConstr *constrs, int count)#

Query a char-valued constraint attribute for an array of constraints.

Parameters:
  • attr – The attribute being queried.

  • constrs – An array of constraints whose attribute values are being queried.

  • count – The number of constraint attributes to retrieve.

Returns:

The current values of the requested attribute for each input constraint. Note that the result is heap-allocated, and must be returned to the heap by the user.

char *get(GRB_CharAttr attr, const GRBQConstr *qconstrs, int count)#

Query a char-valued quadratic constraint attribute for an array of quadratic constraints.

Parameters:
  • attr – The attribute being queried.

  • constrs – An array of quadratic constraints whose attribute values are being queried.

  • count – The number of quadratic constraint attributes to retrieve.

Returns:

The current values of the requested attribute for each input quadratic constraint. Note that the result is heap-allocated, and must be returned to the heap by the user.

double get(GRB_DoubleAttr attr)#

Query the value of a double-valued model attribute.

Parameters:

attr – The attribute being queried.

Returns:

The current value of the requested attribute.

double *get(GRB_DoubleAttr attr, const GRBVar *vars, int count)#

Query a double-valued variable attribute for an array of variables.

Parameters:
  • attr – The attribute being queried.

  • vars – An array of variables whose attribute values are being queried.

  • count – The number of variable attributes to retrieve.

Returns:

The current values of the requested attribute for each input variable. Note that the result is heap-allocated, and must be returned to the heap by the user.

double *get(GRB_DoubleAttr attr, const GRBConstr *constrs, int count)#

Query a double-valued constraint attribute for an array of constraints.

Parameters:
  • attr – The attribute being queried.

  • constrs – An array of constraints whose attribute values are being queried.

  • count – The number of constraint attributes to retrieve.

Returns:

The current values of the requested attribute for each input constraint. Note that the result is heap-allocated, and must be returned to the heap by the user.

double *get(GRB_DoubleAttr attr, const GRBQConstr *qconstrs, int count)#

Query a double-valued quadratic constraint attribute for an array of quadratic constraints.

Parameters:
  • attr – The attribute being queried.

  • constrs – An array of quadratic constraints whose attribute values are being queried.

  • count – The number of quadratic constraint attributes to retrieve.

Returns:

The current values of the requested attribute for each input quadratic constraint. Note that the result is heap-allocated, and must be returned to the heap by the user.

int get(GRB_IntAttr attr)#

Query the value of an int-valued model attribute.

Parameters:

attr – The attribute being queried.

Returns:

The current value of the requested attribute.

int *get(GRB_IntAttr attr, const GRBVar *vars, int count)#

Query an int-valued variable attribute for an array of variables.

Parameters:
  • attr – The attribute being queried.

  • vars – An array of variables whose attribute values are being queried.

  • count – The number of variable attributes to retrieve.

Returns:

The current values of the requested attribute for each input variable. Note that the result is heap-allocated, and must be returned to the heap by the user.

int *get(GRB_IntAttr attr, const GRBConstr *constrs, int count)#

Query an int-valued constraint attribute for an array of constraints.

Parameters:
  • attr – The attribute being queried.

  • constrs – An array of constraints whose attribute values are being queried.

  • count – The number of constraint attributes to retrieve.

Returns:

The current values of the requested attribute for each input constraint. Note that the result is heap-allocated, and must be returned to the heap by the user.

string get(GRB_StringAttr attr)#

Query the value of a string-valued model attribute.

Parameters:

attr – The attribute being queried.

Returns:

The current value of the requested attribute.

string *get(GRB_StringAttr attr, const GRBVar *vars, int count)#

Query a string-valued variable attribute for an array of variables.

Parameters:
  • attr – The attribute being queried.

  • vars – An array of variables whose attribute values are being queried.

  • count – The number of variable attributes to retrieve.

Returns:

The current values of the requested attribute for each input variable. Note that the result is heap-allocated, and must be returned to the heap by the user.

string *get(GRB_StringAttr attr, const GRBConstr *constrs, int count)#

Query a string-valued constraint attribute for an array of constraints.

Parameters:
  • attr – The attribute being queried.

  • constrs – An array of constraints whose attribute values are being queried.

  • count – The number of constraint attributes to retrieve.

Returns:

The current values of the requested attribute for each input constraint. Note that the result is heap-allocated, and must be returned to the heap by the user.

string *get(GRB_StringAttr attr, const GRBQConstr *qconstrs, int count)#

Query a string-valued quadratic constraint attribute for an array of quadratic constraints.

Parameters:
  • attr – The attribute being queried.

  • constrs – An array of quadratic constraints whose attribute values are being queried.

  • count – The number of quadratic constraint attributes to retrieve.

Returns:

The current values of the requested attribute for each input quadratic constraint. Note that the result is heap-allocated, and must be returned to the heap by the user.

double getCoeff(GRBConstr constr, GRBVar var)#

Query the coefficient of variable var in linear constraint constr (note that the result can be zero).

Parameters:
  • constr – The requested constraint.

  • var – The requested variable.

Returns:

The current value of the requested coefficient.

GRBColumn getCol(GRBVar var)#

Retrieve the list of constraints in which a variable participates, and the associated coefficients. The result is returned as a GRBColumn object.

Parameters:

var – The variable of interest.

Returns:

A GRBColumn object that captures the set of constraints in which the variable participates.

GRBEnv getConcurrentEnv(int num)#

Create/retrieve a concurrent environment for a model.

This method provides fine-grained control over the concurrent optimizer. By creating your own concurrent environments and setting appropriate parameters on these environments (e.g., the Method parameter), you can control exactly which strategies the concurrent optimizer employs. For example, if you create two concurrent environments, and set Method to primal simplex for one and dual simplex for the other, subsequent concurrent optimizer runs will use the two simplex algorithms rather than the default choices.

Note that you must create contiguously numbered concurrent environments, starting with num=0. For example, if you want three concurrent environments, they must be numbered 0, 1, and 2.

Once you create concurrent environments, they will be used for every subsequent concurrent optimization on that model. Use discardConcurrentEnvs to revert back to default concurrent optimizer behavior.

Parameters:

num – The concurrent environment number.

Returns:

The concurrent environment for the model.

GRBConstr getConstrByName(const string &name)#

Retrieve a linear constraint from its name. If multiple linear constraints have the same name, this method chooses one arbitrarily.

Parameters:

name – The name of the desired linear constraint.

Returns:

The requested linear constraint.

GRBConstr *getConstrs()#

Retrieve an array of all linear constraints in the model.

Returns:

An array of all linear constraints in the model. Note that this array is heap-allocated, and must be returned to the heap by the user.

void getGenConstrMax(GRBGenConstr genc, GRBVar *resvarP, GRBVar *vars, int *lenP, double *constantP)#

Retrieve the data associated with a general constraint of type MAX. Calling this method for a general constraint of a different type leads to an exception. You can query the GenConstrType attribute to determine the type of the general constraint.

Typical usage is to call this routine twice. In the first call, you specify the requested general constraint, with a NULL value for the vars argument. The routine returns the total number of operand variables in the specified general constraint in lenP. That allows you to make certain that the vars array is of sufficient size to hold the result of the second call.

See also addGenConstrMax for a description of the semantics of this general constraint type.

Any of the following arguments can be NULL.

Parameters:
  • genc – The general constraint object.

  • resvarP – Pointer to store the resultant variable of the constraint.

  • vars – Array to store the operand variables of the constraint.

  • lenP – Pointer to store the number of operand variables of the constraint.

  • constantP – Pointer to store the additional constant operand of the constraint.

void getGenConstrMin(GRBGenConstr genc, GRBVar *resvarP, GRBVar *vars, int *lenP, double *constantP)#

Retrieve the data associated with a general constraint of type MIN. Calling this method for a general constraint of a different type leads to an exception. You can query the GenConstrType attribute to determine the type of the general constraint.

Typical usage is to call this routine twice. In the first call, you specify the requested general constraint, with a NULL value for the vars argument. The routine returns the total number of operand variables in the specified general constraint in lenP. That allows you to make certain that the vars array is of sufficient size to hold the result of the second call.

See also addGenConstrMin for a description of the semantics of this general constraint type.

Any of the following arguments can be NULL.

Parameters:
  • genc – The general constraint object.

  • resvarP – Pointer to store the resultant variable of the constraint.

  • vars – Array to store the operand variables of the constraint.

  • lenP – Pointer to store the number of operand variables of the constraint.

  • constantP – Pointer to store the additional constant operand of the constraint.

void getGenConstrAbs(GRBGenConstr genc, GRBVar *resvarP, GRBVar *argvarP)#

Retrieve the data associated with a general constraint of type ABS. Calling this method for a general constraint of a different type leads to an exception. You can query the GenConstrType attribute to determine the type of the general constraint.

See also addGenConstrAbs for a description of the semantics of this general constraint type.

Any of the following arguments can be NULL.

Parameters:
  • genc – The general constraint object.

  • resvarP – Pointer to store the resultant variable of the constraint.

  • argvarP – Pointer to store the argument variable of the constraint.

void getGenConstrAnd(GRBGenConstr genc, GRBVar *resvarP, GRBVar *vars, int *lenP)#

Retrieve the data associated with a general constraint of type AND. Calling this method for a general constraint of a different type leads to an exception. You can query the GenConstrType attribute to determine the type of the general constraint.

Typical usage is to call this routine twice. In the first call, you specify the requested general constraint, with a NULL value for the vars argument. The routine returns the total number of operand variables in the specified general constraint in lenP. That allows you to make certain that the vars array is of sufficient size to hold the result of the second call.

See also addGenConstrAnd for a description of the semantics of this general constraint type.

Any of the following arguments can be NULL.

Parameters:
  • genc – The general constraint object.

  • resvarP – Pointer to store the resultant variable of the constraint.

  • vars – Array to store the operand variables of the constraint.

  • lenP – Pointer to store the number of operand variables of the constraint.

void getGenConstrOr(GRBGenConstr genc, GRBVar *resvarP, GRBVar *vars, int *lenP)#

Retrieve the data associated with a general constraint of type OR. Calling this method for a general constraint of a different type leads to an exception. You can query the GenConstrType attribute to determine the type of the general constraint.

Typical usage is to call this routine twice. In the first call, you specify the requested general constraint, with a NULL value for the vars argument. The routine returns the total number of operand variables in the specified general constraint in lenP. That allows you to make certain that the vars array is of sufficient size to hold the result of the second call.

See also addGenConstrOr for a description of the semantics of this general constraint type.

Any of the following arguments can be NULL.

Parameters:
  • genc – The general constraint object.

  • resvarP – Pointer to store the resultant variable of the constraint.

  • vars – Array to store the operand variables of the constraint.

  • lenP – Pointer to store the number of operand variables of the constraint.

void getGenConstrNorm(GRBGenConstr genc, GRBVar *resvarP, GRBVar *vars, int *lenP, double *whichP)#

Retrieve the data associated with a general constraint of type NORM. Calling this method for a general constraint of a different type leads to an exception. You can query the GenConstrType attribute to determine the type of the general constraint.

Typical usage is to call this routine twice. In the first call, you specify the requested general constraint, with a NULL value for the vars argument. The routine returns the total number of operand variables in the specified general constraint in lenP. That allows you to make certain that the vars array is of sufficient size to hold the result of the second call.

See also addGenConstrNorm for a description of the semantics of this general constraint type.

Any of the following arguments can be NULL.

Parameters:
  • genc – The general constraint object.

  • resvarP – Pointer to store the resultant variable of the constraint.

  • vars – Array to store the operand variables of the constraint.

  • lenP – Pointer to store the number of operand variables of the constraint.

  • whichP – Pointer to store the norm type (possible values are 0, 1, 2, or GRB_INFINITY).

void getGenConstrIndicator(GRBGenConstr genc, GRBVar *binvarP, int *binvalP, GRBLinExpr *exprP, char *senseP, double *rhsP)#

Retrieve the data associated with a general constraint of type INDICATOR. Calling this method for a general constraint of a different type leads to an exception. You can query the GenConstrType attribute to determine the type of the general constraint.

See also addGenConstrIndicator for a description of the semantics of this general constraint type.

Any of the following arguments can be NULL.

Parameters:
  • genc – The general constraint object.

  • binvarP – Pointer to store the binary indicator variable of the constraint.

  • binvalP – Pointer to store the value that the indicator variable has to take in order to trigger the linear constraint.

  • exprP – Pointer to a GRBLinExpr object to store the left-hand side expression of the linear constraint that is triggered by the indicator.

  • senseP – Pointer to store the sense for the linear constraint. Options are GRB_LESS_EQUAL, GRB_EQUAL, or GRB_GREATER_EQUAL.

  • rhsP – Pointer to store the right-hand side value for the linear constraint.

void getGenConstrPWL(GRBGenConstr genc, GRBVar *xvarP, GRBVar *yvarP, int *nptsP, double *xpts, double *ypts)#

Retrieve the data associated with a general constraint of type PWL. Calling this method for a general constraint of a different type leads to an exception. You can query the GenConstrType attribute to determine the type of the general constraint.

Typical usage is to call this routine twice. In the first call, you specify the requested general constraint, with a NULL value for the xpts and ypts arguments. The routine returns the length for the xpts and ypts arrays in nptsP. That allows you to make certain that the xpts and ypts arrays are of sufficient size to hold the result of the second call.

See also addGenConstrPWL for a description of the semantics of this general constraint type.

Any of the following arguments can be NULL.

Parameters:
  • genc – The general constraint object.

  • xvarP – Pointer to store the \(x\) variable.

  • yvarP – Pointer to store the \(y\) variable.

  • nptsP – Pointer to store the number of points that define the piecewise-linear function.

  • xpts – The \(x\) values for the points that define the piecewise-linear function.

  • ypts – The \(y\) values for the points that define the piecewise-linear function.

void getGenConstrPoly(GRBGenConstr genc, GRBVar *xvarP, GRBVar *yvarP, int *plenP, double *p)#

Retrieve the data associated with a general constraint of type POLY. Calling this method for a general constraint of a different type leads to an exception. You can query the GenConstrType attribute to determine the type of the general constraint.

Typical usage is to call this routine twice. In the first call, you specify the requested general constraint, with a NULL value for the p argument. The routine returns the length of the p array in plenP. That allows you to make certain that the p array is of sufficient size to hold the result of the second call.

See also addGenConstrPoly for a description of the semantics of this general constraint type.

Any of the following arguments can be NULL.

Parameters:
  • genc – The general constraint object.

  • xvarP – Pointer to store the \(x\) variable.

  • yvarP – Pointer to store the \(y\) variable.

  • plenP – Pointer to store the array length for p. If \(x^d\) is the highest power term, then \(d+1\) will be returned.

  • p – The coefficients for polynomial function.

void getGenConstrExp(GRBGenConstr genc, GRBVar *xvarP, GRBVar *yvarP)#

Retrieve the data associated with a general constraint of type EXP. Calling this method for a general constraint of a different type leads to an exception. You can query the GenConstrType attribute to determine the type of the general constraint.

See also addGenConstrExp for a description of the semantics of this general constraint type.

Any of the following arguments can be NULL.

Parameters:
  • genc – The general constraint object.

  • xvarP – Pointer to store the \(x\) variable.

  • yvarP – Pointer to store the \(y\) variable.

void getGenConstrExpA(GRBGenConstr genc, GRBVar *xvarP, GRBVar *yvarP, double *aP)#

Retrieve the data associated with a general constraint of type EXPA. Calling this method for a general constraint of a different type leads to an exception. You can query the GenConstrType attribute to determine the type of the general constraint.

See also addGenConstrExpA for a description of the semantics of this general constraint type.

Any of the following arguments can be NULL.

Parameters:
  • genc – The general constraint object.

  • xvarP – Pointer to store the \(x\) variable.

  • yvarP – Pointer to store the \(y\) variable.

  • aP – Pointer to store the base of the function.

void getGenConstrLog(GRBGenConstr genc, GRBVar *xvarP, GRBVar *yvarP)#

Retrieve the data associated with a general constraint of type LOG. Calling this method for a general constraint of a different type leads to an exception. You can query the GenConstrType attribute to determine the type of the general constraint.

See also addGenConstrLog for a description of the semantics of this general constraint type.

Any of the following arguments can be NULL.

Parameters:
  • genc – The general constraint object.

  • xvarP – Pointer to store the \(x\) variable.

  • yvarP – Pointer to store the \(y\) variable.

void getGenConstrLogA(GRBGenConstr genc, GRBVar *xvarP, GRBVar *yvarP, double *aP)#

Retrieve the data associated with a general constraint of type LOGA. Calling this method for a general constraint of a different type leads to an exception. You can query the GenConstrType attribute to determine the type of the general constraint.

See also addGenConstrLogA for a description of the semantics of this general constraint type.

Any of the following arguments can be NULL.

Parameters:
  • genc – The general constraint object.

  • xvarP – Pointer to store the \(x\) variable.

  • yvarP – Pointer to store the \(y\) variable.

  • aP – Pointer to store the base of the function.

void getGenConstrLogistic(GRBGenConstr genc, GRBVar *xvarP, GRBVar *yvarP)#

Retrieve the data associated with a general constraint of type LOGISTIC. Calling this method for a general constraint of a different type leads to an exception. You can query the GenConstrType attribute to determine the type of the general constraint.

See also addGenConstrLogistic for a description of the semantics of this general constraint type.

Any of the following arguments can be NULL.

Parameters:
  • genc – The general constraint object.

  • xvarP – Pointer to store the \(x\) variable.

  • yvarP – Pointer to store the \(y\) variable.

void getGenConstrPow(GRBGenConstr genc, GRBVar *xvarP, GRBVar *yvarP, double *aP)#

Retrieve the data associated with a general constraint of type POW. Calling this method for a general constraint of a different type leads to an exception. You can query the GenConstrType attribute to determine the type of the general constraint.

See also addGenConstrPow for a description of the semantics of this general constraint type.

Any of the following arguments can be NULL.

Parameters:
  • genc – The general constraint object.

  • xvarP – Pointer to store the \(x\) variable.

  • yvarP – Pointer to store the \(y\) variable.

  • aP – Pointer to store the exponent of the function.

void getGenConstrSin(GRBGenConstr genc, GRBVar *xvarP, GRBVar *yvarP)#

Retrieve the data associated with a general constraint of type SIN. Calling this method for a general constraint of a different type leads to an exception. You can query the GenConstrType attribute to determine the type of the general constraint.

See also addGenConstrSin for a description of the semantics of this general constraint type.

Any of the following arguments can be NULL.

Parameters:
  • genc – The general constraint object.

  • xvarP – Pointer to store the \(x\) variable.

  • yvarP – Pointer to store the \(y\) variable.

void getGenConstrCos(GRBGenConstr genc, GRBVar *xvarP, GRBVar *yvarP)#

Retrieve the data associated with a general constraint of type COS. Calling this method for a general constraint of a different type leads to an exception. You can query the GenConstrType attribute to determine the type of the general constraint.

See also addGenConstrCos for a description of the semantics of this general constraint type.

Any of the following arguments can be NULL.

Parameters:
  • genc – The general constraint object.

  • xvarP – Pointer to store the \(x\) variable.

  • yvarP – Pointer to store the \(y\) variable.

void getGenConstrTan(GRBGenConstr genc, GRBVar *xvarP, GRBVar *yvarP)#

Retrieve the data associated with a general constraint of type TAN. Calling this method for a general constraint of a different type leads to an exception. You can query the GenConstrType attribute to determine the type of the general constraint.

See also addGenConstrTan for a description of the semantics of this general constraint type.

Any of the following arguments can be NULL.

Parameters:
  • genc – The general constraint object.

  • xvarP – Pointer to store the \(x\) variable.

  • yvarP – Pointer to store the \(y\) variable.

GRBGenConstr *getGenConstrs()#

Retrieve an array of all general constraints in the model.

Returns:

An array of all general constraints in the model. Note that this array is heap-allocated, and must be returned to the heap by the user.

std::string getJSONSolution()#

After a call to optimize, this method returns the resulting solution and related model attributes as a JSON string. Please refer to the JSON solution format section for details.

Returns:

A JSON string.

GRBEnv getMultiobjEnv(int index)#

Create/retrieve a multi-objective environment for the optimization pass with the given index. This environment enables fine-grained control over the multi-objective optimization process. Specifically, by changing parameters on this environment, you modify the behavior of the optimization that occurs during the corresponding pass of the multi-objective optimization.

Each multi-objective environment starts with a copy of the current model environment.

Please refer to the discussion of Multiple Objectives for information on how to specify multiple objective functions and control the trade-off between them.

Please refer to the discussion on Combining Blended and Hierarchical Objectives for information on the optimization passes to solve multi-objective models.

Use discardMultiobjEnvs to discard multi-objective environments and return to standard behavior.

Parameters:

index – The optimization pass index, starting from 0.

Returns:

The multi-objective environment for that optimization pass when solving the model.

GRBQuadExpr getObjective()#

Retrieve the optimization objective.

Note that the constant and linear portions of the objective can also be retrieved using the ObjCon and Obj attributes.

Returns:

The model objective.

GRBLinExpr getObjective(int index)#

Retrieve an alternative optimization objective. Alternative objectives will always be linear. You can also use this routine to retrieve the primary objective (using index = 0), but you will get an exception if the primary objective contains quadratic terms.

Please refer to the discussion of Multiple Objectives for more information on the use of alternative objectives.

Note that alternative objectives can also be retrieved using the ObjNCon and ObjN attributes.

Parameters:

index – The index for the requested alternative objective.

Returns:

The requested alternate objective.

int getPWLObj(GRBVar var, double x[], double y[])#

Retrieve the piecewise-linear objective function for a variable. The return value gives the number of points that define the function, and the \(x\) and \(y\) arguments give the coordinates of the points, respectively. The \(x\) and \(y\) arguments must be large enough to hold the result. Call this method with NULL values for \(x\) and \(y\) if you just want the number of points.

Refer to this discussion for additional information on what the values in \(x\) and \(y\) mean.

Parameters:
  • var – The variable whose objective function is being retrieved.

  • x – The \(x\) values for the points that define the piecewise-linear function. These will always be in non-decreasing order.

  • y – The \(y\) values for the points that define the piecewise-linear function.

Returns:

The number of points that define the piecewise-linear objective function.

GRBQuadExpr getQCRow(GRBQConstr qconstr)#

Retrieve the left-hand side expression from a quadratic constraint. The result is returned as a GRBQuadExpr object.

Parameters:

qconstr – The quadratic constraint of interest.

Returns:

A GRBQuadExpr object that captures the left-hand side of the quadratic constraint.

GRBQConstr *getQConstrs()#

Retrieve an array of all quadratic constraints in the model.

Returns:

An array of all quadratic constraints in the model. Note that this array is heap-allocated, and must be returned to the heap by the user.

GRBLinExpr getRow(GRBConstr constr)#

Retrieve a list of variables that participate in a constraint, and the associated coefficients. The result is returned as a GRBLinExpr object.

Parameters:

constr – The constraint of interest. A GRBConstr object, typically obtained from addConstr or getConstrs.

Returns:

A GRBLinExpr object that captures the set of variables that participate in the constraint.

int getSOS(GRBSOS sos, GRBVar *vars, double *weights, int *typeP)#

Retrieve the list of variables that participate in an SOS constraint, and the associated coefficients. The return value is the length of this list. If you would like to allocate space for the result before retrieving the result, call the method first with NULL array arguments to determine the appropriate array lengths.

Parameters:
  • sos – The SOS set of interest.

  • vars – A list of variables that participate in sos.

  • weights – The SOS weights for each participating variable.

  • typeP – The type of the SOS set (either GRB_SOS_TYPE1 or GRB_SOS_TYPE2).

Returns:

The length of the result arrays.

GRBSOS *getSOSs()#

Retrieve an array of all SOS constraints in the model.

Returns:

An array of all SOS constraints in the model. Note that this array is heap-allocated, and must be returned to the heap by the user.

void getTuneResult(int n)#

Use this method to retrieve the results of a previous tune call. Calling this method with argument n causes tuned parameter set n to be copied into the model. Parameter sets are stored in order of decreasing quality, with parameter set 0 being the best. The number of available sets is stored in attribute TuneResultCount.

Once you have retrieved a tuning result, you can call optimize to use these parameter settings to optimize the model, or write to write the changed parameters to a .prm file.

Please refer to the parameter tuning section for details on the tuning tool.

Parameters:

n – The index of the tuning result to retrieve. The best result is available as index 0. The number of stored results is available in attribute TuneResultCount.

GRBVar getVarByName(const string &name)#

Retrieve a variable from its name. If multiple variables have the same name, this method chooses one arbitrarily.

Parameters:

name – The name of the desired variable.

Returns:

The requested variable.

GRBVar *getVars()#

Retrieve an array of all variables in the model.

Returns:

An array of all variables in the model. Note that this array is heap-allocated, and must be returned to the heap by the user.

void optimize()#

Optimize the model. The algorithm used for the optimization depends on the model type (simplex or barrier for a continuous model; branch-and-cut for a MIP model). Upon successful completion, this method will populate the solution related attributes of the model. See the Attributes section for more information on attributes.

Please consult this section for a discussion of some of the practical issues associated with solving a precisely defined mathematical model using finite-precision floating-point arithmetic.

Note that this method will process all pending model modifications.

void optimizeasync()#

Optimize a model asynchronously. This routine returns immediately. Your program can perform other computations while optimization proceeds in the background. To check the state of the asynchronous optimization, query the Status attribute for the model. A value of IN_PROGRESS indicates that the optimization has not yet completed. When you are done with your foreground tasks, you must call sync to sync your foreground program with the asynchronous optimization task.

Note that the set of Gurobi calls that you are allowed to make while optimization is running in the background is severely limited. Specifically, you can only perform attribute queries, and only for a few attributes (listed below). Any other calls on the running model, or on any other models that were built within the same Gurobi environment, will fail with error code OPTIMIZATION_IN_PROGRESS.

Note that there are no such restrictions on models built in other environments. Thus, for example, you could create multiple environments, and then have a single foreground program launch multiple simultaneous asynchronous optimizations, each in its own environment.

As already noted, you are allowed to query the value of the Status attribute while an asynchronous optimization is in progress. The other attributes that can be queried are: ObjVal, ObjBound, IterCount, NodeCount, and BarIterCount. In each case, the returned value reflects progress in the optimization to that point. Any attempt to query the value of an attribute not on this list will return an OPTIMIZATION_IN_PROGRESS error.

string optimizeBatch()#

Submit a new batch request to the Cluster Manager. Returns the BatchID (a string), which uniquely identifies the job in the Cluster Manager and can be used to query the status of this request (from this program or from any other). Once the request has completed, the BatchID can also be used to retrieve the associated solution. To submit a batch request, you must tag at least one element of the model by setting one of the VTag, CTag or QCTag attributes. For more details on batch optimization, please refer to the Batch Optimization section.

Note that this routine will process all pending model modifications.

Example:
// submit batch request
batchID = model->optimizeBatch();
GRBModel presolve()#

Perform presolve on a model.

Please note that the presolved model computed by this function may be different from the presolved model computed when optimizing the model.

Returns:

Presolved version of original model.

void read(const string &filename)#

This method is the general entry point for importing data from a file into a model. It can be used to read basis files for continuous models, start vectors for MIP models, variable hints for MIP models, branching priorities for MIP models, or parameter settings. The type of data read is determined by the file suffix. File formats are described in the File Format section.

Note that reading a file does not process all pending model modifications. These modifications can be processed by calling GRBModel::update.

Note also that this is not the method to use if you want to read a new model from a file. For that, use the GRBModel constructor. One variant of the constructor takes the name of the file that contains the new model as its argument.

Parameters:

filename – Name of the file to read. The suffix on the file must be either .bas (for an LP basis), .mst or .sol (for a MIP start), .hnt (for MIP hints), .ord (for a priority order), .attr (for a collection of attribute settings), or .prm (for a parameter file). The suffix may optionally be followed by .zip, .gz, .bz2, or .7z.

void remove(GRBConstr constr)#

Remove a linear constraint from the model. Note that, due to our lazy update approach, the change won’t actually take effect until you update the model (using GRBModel::update), optimize the model (using GRBModel::optimize), or write the model to disk (using GRBModel::write).

Parameters:

constr – The linear constraint to remove.

void remove(GRBGenConstr genconstr)#

Remove a general constraint from the model. Note that, due to our lazy update approach, the change won’t actually take effect until you update the model (using GRBModel::update), optimize the model (using GRBModel::optimize), or write the model to disk (using GRBModel::write).

Parameters:

genconstr – The general constraint to remove.

void remove(GRBQConstr qconstr)#

Remove a quadratic constraint from the model. Note that, due to our lazy update approach, the change won’t actually take effect until you update the model (using GRBModel::update), optimize the model (using GRBModel::optimize), or write the model to disk (using GRBModel::write).

Parameters:

qconstr – The quadratic constraint to remove.

void remove(GRBSOS sos)#

Remove an SOS constraint from the model. Note that, due to our lazy update approach, the change won’t actually take effect until you update the model (using GRBModel::update), optimize the model (using GRBModel::optimize), or write the model to disk (using GRBModel::write).

Parameters:

sos – The SOS constraint to remove.

void remove(GRBVar var)#

Remove a variable from the model. Note that, due to our lazy update approach, the change won’t actually take effect until you update the model (using GRBModel::update), optimize the model (using GRBModel::optimize), or write the model to disk (using GRBModel::write).

Parameters:

var – The variable to remove.

void reset(int clearall = 0)#

Reset the model to an unsolved state, discarding any previously computed solution information. Note that, due to our lazy update approach, the change won’t actually take effect until you update the model (using GRBModel::update), optimize the model (using GRBModel::optimize), or write the model to disk (using GRBModel::write).

Parameters:

clearall – (optional) A value of 1 discards additional information that affects the solution process but not the actual model (currently MIP starts, variable hints, branching priorities, lazy flags, and partition information). The default value of 0 just discards the solution.

void setCallback(GRBCallback *cb)#

Set the callback object for a model. The callback() method on this object will be called periodically from the Gurobi solver. You will have the opportunity to obtain more detailed information about the state of the optimization from this callback. See the documentation for GRBCallback for additional information.

Note that a model can only have a single callback method, so this call will replace an existing callback. To disable a previously set callback, call this method with a NULL argument.

void set(GRB_DoubleParam param, double newvalue)#

Set the value of a double-valued parameter.

The difference between setting a parameter on a model and setting it on an environment (i.e., through GRBEnv::set) is that the former modifies the parameter for a single model, while the latter modifies the parameter for every model that is subsequently built using that environment (and leaves the parameter unchanged for models that were previously built using that environment).

Parameters:
  • param – The parameter being modified.

  • newvalue – The desired new value for the parameter.

void set(GRB_IntParam param, int newvalue)#

Set the value of an int-valued parameter.

The difference between setting a parameter on a model and setting it on an environment (i.e., through GRBEnv::set) is that the former modifies the parameter for a single model, while the latter modifies the parameter for every model that is subsequently built using that environment (and leaves the parameter unchanged for models that were previously built using that environment).

Parameters:
  • param – The parameter being modified.

  • newvalue – The desired new value for the parameter.

void set(GRB_StringParam param, string newvalue)#

Set the value of a string-valued parameter.

The difference between setting a parameter on a model and setting it on an environment (i.e., through GRBEnv::set) is that the former modifies the parameter for a single model, while the latter modifies the parameter for every model that is subsequently built using that environment (and leaves the parameter unchanged for models that were previously built using that environment).

Parameters:
  • param – The parameter being modified.

  • newvalue – The desired new value for the parameter.

void set(GRB_CharAttr attr, const GRBVar *vars, char *newvalues, int count)#

Set a char-valued variable attribute for an array of variables.

Parameters:
  • attr – The attribute being modified.

  • vars – An array of variables whose attribute values are being modified.

  • newvalues – The desired new values for the attribute for each input variable.

  • count – The number of variable attributes to set.

void set(GRB_CharAttr attr, const GRBConstr *constrs, char *newvalues, int count)#

Set a char-valued constraint attribute for an array of constraints.

Parameters:
  • attr – The attribute being modified.

  • constrs – An array of constraints whose attribute values are being modified.

  • newvalues – The desired new values for the attribute for each input constraint.

  • count – The number of constraint attributes to set.

void set(GRB_CharAttr attr, const GRBQConstr *qconstrs, char *newvalues, int count)#

Set a char-valued quadratic constraint attribute for an array of quadratic constraints.

Parameters:
  • attr – The attribute being modified.

  • constrs – An array of quadratic constraints whose attribute values are being modified.

  • newvalues – The desired new values for the attribute for each input quadratic constraint.

  • count – The number of quadratic constraint attributes to set.

void set(GRB_DoubleAttr attr, double newvalue)#

Set the value of a double-valued model attribute.

Parameters:
  • attr – The attribute being modified.

  • newvalue – The desired new value for the attribute.

void set(GRB_DoubleAttr attr, const GRBVar *vars, double *newvalues, int count)#

Set a double-valued variable attribute for an array of variables.

Parameters:
  • attr – The attribute being modified.

  • vars – An array of variables whose attribute values are being modified.

  • newvalues – The desired new values for the attribute for each input variable.

  • count – The number of variable attributes to set.

void set(GRB_DoubleAttr attr, const GRBConstr *constrs, double *newvalues, int count)#

Set a double-valued constraint attribute for an array of constraints.

Parameters:
  • attr – The attribute being modified.

  • constrs – An array of constraints whose attribute values are being modified.

  • newvalues – The desired new values for the attribute for each input constraint.

  • count – The number of constraint attributes to set.

void set(GRB_DoubleAttr attr, const GRBQConstr *qconstrs, double *newvalues, int count)#

Set a double-valued quadratic constraint attribute for an array of quadratic constraints.

Parameters:
  • attr – The attribute being modified.

  • constrs – An array of quadratic constraints whose attribute values are being modified.

  • newvalues – The desired new values for the attribute for each input quadratic constraint.

  • count – The number of quadratic constraint attributes to set.

void set(GRB_IntAttr attr, int newvalue)#

Set the value of an int-valued model attribute.

Parameters:
  • attr – The attribute being modified.

  • newvalue – The desired new value for the attribute.

void set(GRB_IntAttr attr, const GRBVar *vars, int *newvalues, int count)#

Set an int-valued variable attribute for an array of variables.

Parameters:
  • attr – The attribute being modified.

  • vars – An array of variables whose attribute values are being modified.

  • newvalues – The desired new values for the attribute for each input variable.

  • count – The number of variable attributes to set.

void set(GRB_IntAttr attr, const GRBConstr *constrs, int *newvalues, int count)#

Set an int-valued constraint attribute for an array of constraints.

Parameters:
  • attr – The attribute being modified.

  • constrs – An array of constraints whose attribute values are being modified.

  • newvalues – The desired new values for the attribute for each input constraint.

  • count – The number of constraint attributes to set.

void set(GRB_StringAttr attr, string newvalue)#

Set the value of a string-valued model attribute.

Parameters:
  • attr – The attribute being modified.

  • newvalue – The desired new value for the attribute.

void set(GRB_StringAttr attr, const GRBVar *vars, string *newvalues, int count)#

Set a string-valued variable attribute for an array of variables.

Parameters:
  • attr – The attribute being modified.

  • vars – An array of variables whose attribute values are being modified.

  • newvalues – The desired new values for the attribute for each input variable.

  • count – The number of variable attributes to set.

void set(GRB_StringAttr attr, const GRBConstr *constrs, string *newvalues, int count)#

Set a string-valued constraint attribute for an array of constraints.

Parameters:
  • attr – The attribute being modified.

  • constrs – An array of constraints whose attribute values are being modified.

  • newvalues – The desired new values for the attribute for each input constraint.

  • count – The number of constraint attributes to set.

void set(GRB_StringAttr attr, const GRBQConstr *qconstrs, string *newvalues, int count)#

Set a string-valued quadratic constraint attribute for an array of quadratic constraints.

Parameters:
  • attr – The attribute being modified.

  • constrs – An array of quadratic constraints whose attribute values are being modified.

  • newvalues – The desired new values for the attribute for each input quadratic constraint.

  • count – The number of quadratic constraint attributes to set.

void setObjective(GRBLinExpr linexpr, int sense = 0)#

Set the model objective equal to a linear expression (for multi-objective optimization, see setObjectiveN).

Note that you can also modify the linear portion of a model objective using the Obj variable attribute. If you wish to mix and match these two approaches, please note that this method replaces the entire existing objective, while the Obj attribute can be used to modify individual linear terms.

Parameters:
  • linexpr – New linear model objective.

  • sense – (optional) Optimization sense (GRB_MINIMIZE for minimization, GRB_MAXIMIZE for maximization). Omit this argument to use the ModelSense attribute value to determine the sense.

void setObjective(GRBQuadExpr quadexpr, int sense = 0)#

Set the model objective equal to a quadratic expression. Note that this method replaces the entire existing objective, including the linear terms, even if the given quadratic expression has no linear terms.

Parameters:
  • quadexpr – New quadratic model objective.

  • sense – (optional) Optimization sense (GRB_MINIMIZE for minimization, GRB_MAXIMIZE for maximization). Omit this argument to use the ModelSense attribute value.

void setObjectiveN(GRBLinExpr expr, int index, int priority = 0, double weight = 1, double abstol = 0, double reltol = 0, string name = "")#

Set an alternative optimization objective equal to a linear expression.

Please refer to the discussion of Multiple Objectives for more information on the use of alternative objectives.

Note that you can also modify an alternative objective using the ObjN variable attribute. If you wish to mix and match these two approaches, please note that this method replaces the entire existing objective, while the ObjN attribute can be used to modify individual terms.

Parameters:
  • expr – New alternative objective.

  • index – Index for new objective. If you use an index of 0, this routine will change the primary optimization objective.

  • priority – Priority for the alternative objective. This initializes the ObjNPriority attribute for this objective.

  • weight – Weight for the alternative objective. This initializes the ObjNWeight attribute for this objective.

  • abstol – Absolute tolerance for the alternative objective. This initializes the ObjNAbsTol attribute for this objective.

  • reltol – Relative tolerance for the alternative objective. This initializes the ObjNRelTol attribute for this objective.

  • name – Name of the alternative objective. This initializes the ObjNName attribute for this objective.

void setPWLObj(GRBVar var, int npoints, double x[], double y[])#

Set a piecewise-linear objective function for a variable.

The arguments to this method specify a list of points that define a piecewise-linear objective function for a single variable. Specifically, the \(x\) and \(y\) arguments give coordinates for the vertices of the function.

For additional details on piecewise-linear objective functions, refer to this discussion.

Parameters:
  • var – The variable whose objective function is being set.

  • npoints – Number of points that define the piecewise-linear function.

  • x – The \(x\) values for the points that define the piecewise-linear function. Must be in non-decreasing order.

  • y – The \(y\) values for the points that define the piecewise-linear function.

GRBModel singleScenarioModel()#

Capture a single scenario from a multi-scenario model. Use the ScenarioNumber parameter to indicate which scenario to capture.

The model on which this method is invoked must be a multi-scenario model, and the result will be a single-scenario model.

Returns:

Model for a single scenario.

void sync()#

Wait for a previous asynchronous optimization call to complete.

Calling optimizeasync returns control to the calling routine immediately. The caller can perform other computations while optimization proceeds, and can check on the progress of the optimization by querying various model attributes. The sync call forces the calling program to wait until the asynchronous optimization call completes. You must call sync before the corresponding model object is deleted.

The sync call throws an exception if the optimization itself ran into any problems. In other words, exceptions thrown by this method are those that optimize itself would have thrown, had the original method not been asynchronous.

Note that you need to call sync even if you know that the asynchronous optimization has already completed.

void terminate()#

Generate a request to terminate the current optimization. This method can be called at any time during an optimization (from a callback, from another thread, from an interrupt handler, etc.). Note that, in general, the request won’t be acted upon immediately.

When the optimization stops, the Status attribute will be equal to GRB_INTERRUPTED.

void tune()#

Perform an automated search for parameter settings that improve performance. Upon completion, this method stores the best parameter sets it found. The number of stored parameter sets can be determined by querying the value of the TuneResultCount attribute. The actual settings can be retrieved using getTuneResult.

Please refer to the parameter tuning section for details on the tuning tool.

void update()#

Process any pending model modifications.

void write(const string &filename)#

This method is the general entry point for writing optimization data to a file. It can be used to write optimization models, solutions vectors, basis vectors, start vectors, or parameter settings. The type of data written is determined by the file suffix. File formats are described in the File Format section.

Note that writing a model to a file will process all pending model modifications. This is also true when writing other model information such as solutions, bases, etc.

Note also that when you write a Gurobi parameter file (PRM), both integer or double parameters not at their default value will be saved, but no string parameter will be saved into the file.

Parameters:

filename – The name of the file to be written. The file type is encoded in the file name suffix. Valid suffixes are .mps, .rew, .lp, or .rlp for writing the model itself, .dua or .dlp for writing the dualized model (only pure LP), .ilp for writing just the IIS associated with an infeasible model (see GRBModel::computeIIS for further information), .sol for writing the solution selected by the SolutionNumber parameter, .mst for writing a start vector, .hnt for writing a hint file, .bas for writing an LP basis, .prm for writing modified parameter settings, .attr for writing model attributes, or .json for writing solution information in JSON format. If your system has compression utilities installed (e.g., 7z or zip for Windows, and gzip, bzip2, or unzip for Linux or macOS), then the files can be compressed, so additional suffixes of .gz, .bz2, or .7z are accepted.