GRBModel#
- 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), andget
(retrieves the value of an attribute).While the Java garbage collector will eventually collect an unused
GRBModel
object, the vast majority of the memory associated with a model is stored outside of the Java heap. As a result, the garbage collector can’t see this memory usage, and thus it can’t take this quantity into account when deciding whether collection is necessary. We recommend that you callGRBModel.dispose
when you are done using a model.- GRBModel GRBModel(GRBEnv env)#
Constructor for
GRBModel
that creates an empty model. You can then calladdVar
andaddConstr
to populate the model with variables and constraints.- Arguments:
env – Environment for new model.
- Return value:
New model object. Model initially contains no variables or constraints.
- Example:
// Create environment GRBEnv env = new GRBEnv(); // Create model and attach it to environment GRBModel model = new GRBModel(env);
- GRBModel GRBModel(GRBEnv env, 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
,.7z
or.xz
are accepted.- Arguments:
env – Environment for new model.
filename – Name of the file containing the model.
- Return value:
New model object.
- Example:
// Create environment GRBEnv env = new GRBEnv(); // Create model from file myModel.mps and attach it to environment GRBModel model = new GRBModel(env, "myModel.mps");
- GRBModel GRBModel(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 callupdate
before copying it.- Arguments:
model – Model to copy.
- Return value:
New model object. Model is a clone of the original.
- Example:
// Create environment GRBEnv env = new GRBEnv(); // Create model and attach it to environment GRBModel model = new GRBModel(env); // ... // Update model before copying model.update(); // Copy model GRBModel copy = new GRBModel(model);
- GRBModel GRBModel(GRBModel model, 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.
- Arguments:
model – Model to copy.
targetenv – Environment to copy model into.
- Return value:
New model object. Model is a clone of the original.
- Example:
// Create environment GRBEnv env = new GRBEnv(); // Create model and attach it to environment GRBModel model = new GRBModel(env); // ... // Update model before copying model.update(); // Create another environment GRBEnv env2 = new GRBEnv(); // Copy model and attach it to env2 GRBModel copy = new GRBModel(model, env2);
- GRBConstr addConstr(GRBLinExpr lhsExpr, char sense, GRBLinExpr rhsExpr, String name)#
Add a single linear constraint to a model.
- Arguments:
lhsExpr – Left-hand side expression for new linear constraint.
sense – Sense for new linear constraint (
GRB.LESS_EQUAL
,GRB.EQUAL
, orGRB.GREATER_EQUAL
).rhsExpr – Right-hand side expression for new linear constraint.
name – Name for new constraint.
- Return value:
New constraint object.
- Example:
// Create variables GRBVar x = model.addVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "x"); GRBVar y = model.addVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "y"); GRBVar z = model.addVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "z"); // Create linear expression x + y GRBLinExpr lexpr = new GRBLinExpr(); lexpr.addTerm(1.0, x); lexpr.addTerm(1.0, y); // Create linear expression 2*z GRBLinExpr rexpr = new GRBLinExpr(); rexpr.addTerm(2.0, z); // Add linear constraint x + y = 2*z with name c1 GRBConstr constr = model.addConstr(lexpr, GRB.EQUAL, rexpr, "c1");
- GRBConstr addConstr(GRBLinExpr lhsExpr, char sense, GRBVar rhsVar, String name)#
Add a single linear constraint to a model.
- Arguments:
lhsExpr – Left-hand side expression for new linear constraint.
sense – Sense for new linear constraint (
GRB.LESS_EQUAL
,GRB.EQUAL
, orGRB.GREATER_EQUAL
).rhsVar – Right-hand side variable for new linear constraint.
name – Name for new constraint.
- Return value:
New constraint object.
- Example:
// Create variables GRBVar x = model.addVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "x"); GRBVar y = model.addVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "y"); GRBVar z = model.addVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "z"); // Create linear expression x + y GRBLinExpr lexpr = new GRBLinExpr(); lexpr.addTerm(1.0, x); lexpr.addTerm(1.0, y); // Add linear constraint x + y = z with name c1 GRBConstr constr = model.addConstr(lexpr, GRB.EQUAL, z, "c1");
- GRBConstr addConstr(GRBLinExpr lhsExpr, char sense, double rhs, String name)#
Add a single linear constraint to a model.
- Arguments:
lhsExpr – Left-hand side expression for new linear constraint.
sense – Sense for new linear constraint (
GRB.LESS_EQUAL
,GRB.EQUAL
, orGRB.GREATER_EQUAL
).rhs – Right-hand side value for new linear constraint.
name – Name for new constraint.
- Return value:
New constraint object.
- Example:
// Create variables GRBVar x = model.addVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "x"); GRBVar y = model.addVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "y"); // Create linear expression x + y GRBLinExpr lexpr = new GRBLinExpr(); lexpr.addTerm(1.0, x); lexpr.addTerm(1.0, y); // Add linear constraint x + y = 0 with name c1 GRBConstr constr = model.addConstr(lexpr, GRB.EQUAL, 0.0, "c1");
- GRBConstr addConstr(GRBVar lhsVar, char sense, GRBLinExpr rhsExpr, String name)#
Add a single linear constraint to a model.
- Arguments:
lhsVar – Left-hand side variable for new linear constraint.
sense – Sense for new linear constraint (
GRB.LESS_EQUAL
,GRB.EQUAL
, orGRB.GREATER_EQUAL
).rhsExpr – Right-hand side expression for new linear constraint.
name – Name for new constraint.
- Return value:
New constraint object.
- Example:
// Create variables GRBVar x = model.addVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "x"); GRBVar y = model.addVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "y"); GRBVar z = model.addVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "z"); // Create linear expression x + y GRBLinExpr rexpr = new GRBLinExpr(); rexpr.addTerm(1.0, x); rexpr.addTerm(1.0, y); // Add linear constraint z >= x + y with name c1 GRBConstr constr = model.addConstr(z, GRB.GREATER_EQUAL, rexpr, "c1");
- GRBConstr addConstr(GRBVar lhsVar, char sense, GRBVar rhsVar, String name)#
Add a single linear constraint to a model.
- Arguments:
lhsVar – Left-hand side variable for new linear constraint.
sense – Sense for new linear constraint (
GRB.LESS_EQUAL
,GRB.EQUAL
, orGRB.GREATER_EQUAL
).rhsVar – Right-hand side variable for new linear constraint.
name – Name for new constraint.
- Return value:
New constraint object.
- Example:
// Create variables GRBVar x = model.addVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "x"); GRBVar y = model.addVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "y"); // Add linear constraint x <= y with name c1 GRBConstr constr = model.addConstr(x, GRB.LESS_EQUAL, y, "c1");
- GRBConstr addConstr(GRBVar lhsVar, char sense, double rhs, String name)#
Add a single linear constraint to a model.
- Arguments:
lhsVar – Left-hand side variable for new linear constraint.
sense – Sense for new linear constraint (
GRB.LESS_EQUAL
,GRB.EQUAL
, orGRB.GREATER_EQUAL
).rhs – Right-hand side value for new linear constraint.
name – Name for new constraint.
- Return value:
New constraint object.
- Example:
// Create variable GRBVar x = model.addVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "x"); // Add linear constraint x <= 2 with name c1 GRBConstr constr = model.addConstr(x, GRB.LESS_EQUAL, 2.0, "c1");
- GRBConstr addConstr(double lhs, char sense, GRBVar rhsVar, String name)#
Add a single linear constraint to a model.
- Arguments:
lhs – Left-hand side value for new linear constraint.
sense – Sense for new linear constraint (
GRB.LESS_EQUAL
,GRB.EQUAL
, orGRB.GREATER_EQUAL
).rhsVar – Right-hand side variable for new linear constraint.
name – Name for new constraint.
- Return value:
New constraint object.
- Example:
// Create variable GRBVar x = model.addVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "x"); // Add linear constraint 1 <= x with name c1 GRBConstr constr = model.addConstr(1.0, GRB.LESS_EQUAL, x, "c1");
- GRBConstr addConstr(double lhs, char sense, GRBLinExpr rhsExpr, String name)#
Add a single linear constraint to a model.
- Arguments:
lhs – Left-hand side value for new linear constraint.
sense – Sense for new linear constraint (
GRB.LESS_EQUAL
,GRB.EQUAL
, orGRB.GREATER_EQUAL
).rhsExpr – Right-hand side expression for new linear constraint.
name – Name for new constraint.
- Return value:
New constraint object.
- Example:
// Create variables GRBVar x = model.addVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "x"); GRBVar y = model.addVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "y"); // Create linear expression x + y GRBLinExpr rexpr = new GRBLinExpr(); rexpr.addTerm(1.0, x); rexpr.addTerm(1.0, y); // Add linear constraint 1 >= x + y with name c1 GRBConstr constr = model.addConstr(1.0, GRB.GREATER_EQUAL, rexpr, "c1");
- GRBConstr[] addConstrs(int count)#
Add
count
new linear constraints to a model. The new constraints are all of the form0 <= 0
.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.- Arguments:
count – Number of constraints to add.
- Return value:
Array of new constraint objects.
- Example:
// Add 5 empty constraints (0 <= 0) GRBConstr[] constrs = model.addConstrs(5)
- GRBConstr[] addConstrs(GRBLinExpr[] lhsExprs, char[] senses, double[] rhss, String[] names)#
Add new linear constraints to a model. The number of added constraints is determined by the length of the input arrays (which must be consistent across all arguments).
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.- Arguments:
lhsExprs – Left-hand side expressions for the new linear constraints.
senses – Senses for new linear constraints (
GRB.LESS_EQUAL
,GRB.EQUAL
, orGRB.GREATER_EQUAL
).rhss – Right-hand side values for the new linear constraints.
names – Names for new constraints.
- Return value:
Array of new constraint objects.
- Example:
// Create variables GRBVar x = model.addVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "x"); GRBVar y = model.addVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "y"); // Create linear expressions x + y and x - 2.0 * y GRBLinExpr expr1 = new GRBLinExpr(); expr1.addTerm(1.0, x); expr1.addTerm(1.0, y); GRBLinExpr expr2 = new GRBLinExpr(); expr2.addTerm(1.0, x); expr2.addTerm(-2.0, y); GRBLinExpr[] lhsExprs = {expr1, expr2}; char[] senses = {GRB.LESS_EQUAL, GRB.GREATER_EQUAL}; double[] rhss = {1.0, 2.0}; String[] names = {"c1", "c2"}; // Add two linear constraints: // x + y <= 1.0 with name c1 // x - 2*y >= 2.0 with name c2 GRBConstr[] constrs = model.addConstrs(lhsExprs, senses, rhss, names);
- GRBConstr[] addConstrs(GRBLinExpr[] lhsExprs, char[] senses, double[] rhss, String[] names, int start, int len)#
Add new linear constraints to a model. This signature allows you to use arrays to hold the various constraint attributes (left-hand side, sense, etc.), without forcing you to add one constraint for each entry in the array. The
start
andlen
arguments allow you to specify which constraints to add.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.- Arguments:
lhsExprs – Left-hand side expressions for the new linear constraints.
senses – Senses for new linear constraints (
GRB.LESS_EQUAL
,GRB.EQUAL
, orGRB.GREATER_EQUAL
).rhss – Right-hand side values for the new linear constraints.
names – Names for new constraints.
start – The first constraint in the list to add.
len – The number of constraints to add.
- Return value:
Array of new constraint objects.
- Example:
// Create variables GRBVar x = model.addVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "x"); GRBVar y = model.addVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "y"); // Create linear expressions x + y and x - 2.0 * y GRBLinExpr expr1 = new GRBLinExpr(); expr1.addTerm(1.0, x); expr1.addTerm(1.0, y); GRBLinExpr expr2 = new GRBLinExpr(); expr2.addTerm(1.0, x); expr2.addTerm(-2.0, y); GRBLinExpr[] lhsExprs = {expr1, expr2}; char[] senses = {GRB.LESS_EQUAL, GRB.GREATER_EQUAL}; double[] rhss = {1.0, 2.0}; String[] names = {"c1", "c2"}; // Add two linear constraints: // x + y <= 1.0 with name c1 // x - 2*y >= 2.0 with name c2 GRBConstr[] constrs = model.addConstrs(lhsExprs, senses, rhss, names, 0, 2);
- GRBGenConstr addGenConstrMax(GRBVar resvar, GRBVar[] vars, double constant, 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\).
- Arguments:
resvar – The resultant variable of the new constraint.
vars – Array of variables that are the operands of the new constraint.
constant – The additional constant operand of the new constraint.
name – Name for the new general constraint.
- Return value:
New general constraint.
- Example:
// Create variables GRBVar resvar = model.addVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "resvar"); double[] ub = {10.0, 10.0, 10.0}; GRBVar[] x = model.addVars(null, ub, null, null, null); // Add constraint resvar = max{x[0], x[1], x[2], 2.0} GRBGenConstr gc = model.addGenConstrMax(resvar, x, 2.0, "maxconstr");
- GRBGenConstr addGenConstrMin(GRBVar resvar, GRBVar[] vars, double constant, 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\).
- Arguments:
resvar – The resultant variable of the new constraint.
vars – Array of variables that are the operands of the new constraint.
constant – The additional constant operand of the new constraint.
name – Name for the new general constraint.
- Return value:
New general constraint.
- Example:
// Create variables GRBVar resvar = model.addVar(-GRB.INFINITY, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "resvar"); double[] lb = {-10.0, -10.0, -10.0}; double[] ub = {0.0, 0.0, 0.0}; GRBVar[] x = model.addVars(lb, ub, null, null, null); // Add constraint resvar = min{x[0], x[1], x[2], -2.0} GRBGenConstr gc = model.addGenConstrMin(resvar, x, -2.0, "minconstr");
- 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\).
- Arguments:
resvar – The resultant variable of the new constraint.
argvar – The argument variable of the new constraint.
name – Name for the new general constraint.
- Return value:
New general constraint.
- Example:
// Create variables GRBVar resvar = model.addVar(-GRB.INFINITY, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "resvar"); GRBVar x = model.addVar(-2.0, 2.0, 0.0, GRB.CONTINUOUS, "x"); // Add constraint resvar = |x| GRBGenConstr gc = model.addGenConstrAbs(resvar, x, "absconstr");
- GRBGenConstr addGenConstrAnd(GRBVar resvar, GRBVar[] vars, 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.
- Arguments:
resvar – The resultant variable of the new constraint.
vars – Array of variables that are the operands of the new constraint.
name – Name for the new general constraint.
- Return value:
New general constraint.
- Example:
// Create binary variables GRBVar resvar = model.addVar(0.0, 1.0, 0.0, GRB.BINARY, "resvar"); GRBVar[] x = model.addVars(3, GRB.BINARY); // Add constraint resvar = AND{x[0], x[1], x[2]} GRBGenConstr gc = model.addGenConstrAnd(resvar, x, "andconstr");
- GRBGenConstr addGenConstrOr(GRBVar resvar, GRBVar[] vars, 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.
- Arguments:
resvar – The resultant variable of the new constraint.
vars – Array of variables that are the operands of the new constraint.
name – Name for the new general constraint.
- Return value:
New general constraint.
- Example:
// Create binary variables GRBVar resvar = model.addVar(0.0, 1.0, 0.0, GRB.BINARY, "resvar"); GRBVar[] x = model.addVars(3, GRB.BINARY); // Add constraint resvar = OR{x[0], x[1], x[2]} GRBGenConstr gc = model.addGenConstrOr(resvar, x, "orconstr");
- GRBGenConstr addGenConstrNorm(GRBVar resvar, GRBVar[] vars, 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\).
- Arguments:
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.
which – Which norm to use. Options are 0, 1, 2, and GRB.INFINITY.
name – Name for the new general constraint.
- Return value:
New general constraint.
- Example:
// Create variables GRBVar resvar = model.addVar(-GRB.INFINITY, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "resvar"); double[] lb = {-10.0, -10.0, -10.0}; double[] ub = {10.0, 10.0, 10.0}; GRBVar[] x = model.addVars(lb, ub, null, null, null); // Add constraint resvar = (x_0^2 + x_1^2 + x_2^2)^(0.5) GRBGenConstr gc = model.addGenConstrNorm(resvar, x, 2, "2normconstr");
- GRBGenConstr addGenConstrNL(GRBVar resvar, int[] opcode, double[] data, int[] parent, String name)#
Add a new general constraint of type
GRB.GENCONSTR_NL
to a model.A NL constraint \(r = f(x)\) states that the resultant variable \(r\) should be equal to the function value \(f(x)\) of the given function \(f\), provided as an expression tree described in Nonlinear Constraints.
- Arguments:
resvar – The resultant variable of the new constraint.
opcode – An array containing the operation codes for the nodes.
data – An array containing the auxiliary data for each node.
parent – An array providing the parent index of the nodes
name – Name for the new general constraint.
- Return value:
New general constraint.
- Example:
// Add nonlinear constraint x0 = sin(2.5 * x1) + x2 to the model GRBVar x0 = model.addVar(0.0, 1.0, 0.0, GRB.CONTINUOUS, "x0"); GRBVar x1 = model.addVar(0.0, 1.0, 0.0, GRB.CONTINUOUS, "x1"); GRBVar x2 = model.addVar(0.0, 1.0, 0.0, GRB.CONTINUOUS, "x2"); int[] opcode = {GRB.OPCODE_PLUS, GRB.OPCODE_SIN, GRB.OPCODE_MULTIPLY, GRB.OPCODE_CONSTANT, GRB.OPCODE_VARIABLE, GRB.OPCODE_VARIABLE}; double[] data = {-1.0, -1.0, -1.0, 2.5, 1.0, 2.0}; int[] parent = {-1, 0, 1, 2, 2, 0}; GRBGenConstr nlgc = model.addGenConstrNL(x0, opcode, data, parent, "nlconstr");
- GRBGenConstr addGenConstrIndicator(GRBVar binvar, int binval, 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.
- Arguments:
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
, orGRB.GREATER_EQUAL
.rhs – Right-hand side value for the linear constraint.
name – Name for the new general constraint.
- Return value:
New general constraint.
- Example:
// Create binary indicator variable GRBVar z = model.addVar(0.0, 1.0, 0.0, GRB.BINARY, "resvar"); // Create variables GRBVar x = model.addVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "x"); GRBVar y = model.addVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "y"); // Create linear expression x + y GRBLinExpr lexpr1 = new GRBLinExpr(); lexpr1.addTerm(1.0, x); lexpr1.addTerm(1.0, y); // Add constraint if z = 1 then x + y <= 2 GRBGenConstr gc = model.addGenConstrIndicator(z, 1, lexpr, GRB.LESS_EQUAL, 2.0, "indicatorconstr");
- GRBGenConstr addGenConstrPWL(GRBVar xvar, GRBVar yvar, double[] xpts, double[] ypts, 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.
- Arguments:
xvar – The \(x\) variable.
yvar – The \(y\) variable.
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 – Name for the new general constraint.
- Return value:
New general constraint.
- Example:
// Create variables GRBVar x = model.addVar(-1.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "x"); GRBVar y = model.addVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "y"); // Create point pairs for the PWL function double[] xpts = {-1, 0, 0, 0, 1}; double[] ypts = {2, 1, 0, 1, 2}; // Add pwl constraint y = PWL(x) GRBGenConstr gc = model.addGenConstrPWL(x, y, xpts, ypts, "pwlconstr");
- GRBGenConstr addGenConstrPoly(GRBVar xvar, GRBVar yvar, double[] p, String name, 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.
- Arguments:
xvar – The \(x\) variable.
yvar – The \(y\) variable.
p – The coefficients for the polynomial function (starting with the coefficient for the highest power).
name – Name for the new general constraint.
options – 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”).
- Return value:
New general constraint.
- Example:
// Create argument variable GRBVar x = model.addVar(-5.0, 5.0, 0.0, GRB.CONTINUOUS, "x"); // Create resultant variable GRBVar y = model.addVar(-GRB.INFINITY, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "y"); // Create array holding coefficients double[] p = {3.0, 2.0, 0.0, 1.0}; // Add constraint y = 3.0 * x^3 + 2.0 * x^2 + 1.0 GRBGenConstr gc = model.addGenConstrPoly(x, y, p, "polyconstr", "");
- GRBGenConstr addGenConstrExp(GRBVar xvar, GRBVar yvar, String name, 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.
- Arguments:
xvar – The \(x\) variable.
yvar – The \(y\) variable.
name – Name for the new general constraint.
options – 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”).
- Return value:
New general constraint.
- Example:
// Create argument variable GRBVar x = model.addVar(-5.0, 5.0, 0.0, GRB.CONTINUOUS, "x"); // Create resultant variable GRBVar y = model.addVar(-GRB.INFINITY, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "y"); // Add constraint y = exp(x) GRBGenConstr gc = model.addGenConstrExp(x, y, "expconstr", "");
- GRBGenConstr addGenConstrExpA(GRBVar xvar, GRBVar yvar, double a, String name, 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.
- Arguments:
xvar – The \(x\) variable.
yvar – The \(y\) variable.
a – The base of the function, \(a > 0\) .
name – Name for the new general constraint.
options – 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”).
- Return value:
New general constraint.
- Example:
// Create argument variable GRBVar x = model.addVar(1.0, 5.0, 0.0, GRB.CONTINUOUS, "x"); // Create resultant variable GRBVar y = model.addVar(-GRB.INFINITY, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "y"); // Add constraint y = 2.0^x GRBGenConstr gc = model.addGenConstrExpA(x, y, 2.0, "exp2constr", "");
- GRBGenConstr addGenConstrLog(GRBVar xvar, GRBVar yvar, String name, 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.
- Arguments:
xvar – The \(x\) variable.
yvar – The \(y\) variable.
name – Name for the new general constraint.
options – 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”).
- Return value:
New general constraint.
- Example:
// Create argument variable GRBVar x = model.addVar(1.0, 5.0, 0.0, GRB.CONTINUOUS, "x"); // Create resultant variable GRBVar y = model.addVar(-GRB.INFINITY, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "y"); // Add constraint y = log(x) GRBGenConstr gc = model.addGenConstrLog(x, y, "logconstr", "");
- GRBGenConstr addGenConstrLogA(GRBVar xvar, GRBVar yvar, double a, String name, 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.
- Arguments:
xvar – The \(x\) variable.
yvar – The \(y\) variable.
a – The base of the function, \(a > 0\) .
name – Name for the new general constraint.
options – 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”).
- Return value:
New general constraint.
- Example:
// Create argument variable GRBVar x = model.addVar(1.0, 5.0, 0.0, GRB.CONTINUOUS, "x"); // Create resultant variable GRBVar y = model.addVar(-GRB.INFINITY, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "y"); // Add constraint y = log_2(x) GRBGenConstr gc = model.addGenConstrLogA(x, y, 2.0, "log2constr", "");
- GRBGenConstr addGenConstrLogistic(GRBVar xvar, GRBVar yvar, String name, 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.
- Arguments:
xvar – The \(x\) variable.
yvar – The \(y\) variable.
name – Name for the new general constraint.
options – 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”).
- Return value:
New general constraint.
- Example:
// Create argument variable GRBVar x = model.addVar(-5.0, 5.0, 0.0, GRB.CONTINUOUS, "x"); // Create resultant variable GRBVar y = model.addVar(-GRB.INFINITY, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "y"); // Add constraint y = logistic(x) = 1.0 / (1.0 + exp(-x)) GRBGenConstr gc = model.addGenConstrLogistic(x, y, "logisticconstr", "");
- GRBGenConstr addGenConstrPow(GRBVar xvar, GRBVar yvar, double a, String name, 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.
- Arguments:
xvar – The \(x\) variable.
yvar – The \(y\) variable.
a – The exponent of the function.
name – Name for the new general constraint.
options – 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”).
- Return value:
New general constraint.
- Example:
// Create argument variable GRBVar x = model.addVar(-5.0, 5.0, 0.0, GRB.CONTINUOUS, "x"); // Create resultant variable GRBVar y = model.addVar(-GRB.INFINITY, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "y"); // Add constraint y = x^3 GRBGenConstr gc = model.addGenConstrPow(x, y, 3, "pow3constr", "");
- GRBGenConstr addGenConstrSin(GRBVar xvar, GRBVar yvar, String name, 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.
- Arguments:
xvar – The \(x\) variable.
yvar – The \(y\) variable.
name – Name for the new general constraint.
options – 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”).
- Return value:
New general constraint.
- Example:
// Create argument variable GRBVar x = model.addVar(-5.0, 5.0, 0.0, GRB.CONTINUOUS, "x"); // Create resultant variable GRBVar y = model.addVar(-GRB.INFINITY, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "y"); // Add constraint y = sin(x) GRBGenConstr gc = model.addGenConstrSin(x, y, "sinconstr", "");
- GRBGenConstr addGenConstrCos(GRBVar xvar, GRBVar yvar, String name, 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.
- Arguments:
xvar – The \(x\) variable.
yvar – The \(y\) variable.
name – Name for the new general constraint.
options – 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”).
- Return value:
New general constraint.
- Example:
// Create argument variable GRBVar x = model.addVar(-5.0, 5.0, 0.0, GRB.CONTINUOUS, "x"); // Create resultant variable GRBVar y = model.addVar(-GRB.INFINITY, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "y"); // Add constraint y = cos(x) GRBGenConstr gc = model.addGenConstrCos(x, y, "cosconstr", "");
- GRBGenConstr addGenConstrTan(GRBVar xvar, GRBVar yvar, String name, 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.
- Arguments:
xvar – The \(x\) variable.
yvar – The \(y\) variable.
name – Name for the new general constraint.
options – 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”).
- Return value:
New general constraint.
- Example:
// Create argument variable GRBVar x = model.addVar(-1.0, 1.0, 0.0, GRB.CONTINUOUS, "x"); // Create resultant variable GRBVar y = model.addVar(-GRB.INFINITY, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "y"); // Add constraint y = tan(x) GRBGenConstr gc = model.addGenConstrTan(x, y, "tanconstr", "");
- GRBQConstr addQConstr(GRBQuadExpr lhsExpr, char sense, 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.
- Arguments:
lhsExpr – Left-hand side quadratic expression for new quadratic constraint.
sense – Sense for new quadratic constraint (
GRB.LESS_EQUAL
,GRB.EQUAL
, orGRB.GREATER_EQUAL
).rhsExpr – Right-hand side quadratic expression for new quadratic constraint.
name – Name for new constraint.
- Return value:
New quadratic constraint object.
- Example:
// Create variables GRBVar x = model.addVar(-2.0, 2.0, 0.0, GRB.CONTINUOUS, "x"); GRBVar y = model.addVar(0.0, 3.0, 0.0, GRB.CONTINUOUS, "y"); // Create quadratic expression x^2 + x*y + y GRBQuadExpr lhsExpr = new GRBQuadExpr(); lhsExpr.addTerm(1.0, x, x); lhsExpr.addTerm(1.0, x, y); lhsExpr.addTerm(1.0, y); // Create quadratic expression y^2 GRBQuadExpr rhsExpr = new GRBQuadExpr(); rhsExpr.addTerm(1.0, y, y); // Add quadratic constraint x^2 + x*y + y = y^2 with name c1 GRBQConstr constr = model.addQConstr(lhsExpr, GRB.EQUAL, rhsExpr, "c1");
- GRBQConstr addQConstr(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.
- Arguments:
lhsExpr – Left-hand side quadratic expression for new quadratic constraint.
sense – Sense for new quadratic constraint (
GRB.LESS_EQUAL
,GRB.EQUAL
, orGRB.GREATER_EQUAL
).rhsVar – Right-hand side variable for new quadratic constraint.
name – Name for new constraint.
- Return value:
New quadratic constraint object.
- Example:
// Create variables GRBVar x = model.addVar(-2.0, 2.0, 0.0, GRB.CONTINUOUS, "x"); GRBVar y = model.addVar(0.0, 3.0, 0.0, GRB.CONTINUOUS, "y"); // Create quadratic expression x^2 + x*y + y GRBQuadExpr lhsExpr = new GRBQuadExpr(); lhsExpr.addTerm(1.0, x, x); lhsExpr.addTerm(1.0, x, y); lhsExpr.addTerm(1.0, y); // Add quadratic constraint x^2 + x*y + y = y with name c1 GRBQConstr constr = model.addQConstr(lhsExpr, GRB.EQUAL, y, "c1");
- GRBQConstr addQConstr(GRBQuadExpr lhsExpr, char sense, GRBLinExpr 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.
- Arguments:
lhsExpr – Left-hand side quadratic expression for new quadratic constraint.
sense – Sense for new quadratic constraint (
GRB.LESS_EQUAL
,GRB.EQUAL
, orGRB.GREATER_EQUAL
).rhsExpr – Right-hand side linear expression for new quadratic constraint.
name – Name for new constraint.
- Return value:
New quadratic constraint object.
- Example:
// Create variables GRBVar x = model.addVar(-2.0, 2.0, 0.0, GRB.CONTINUOUS, "x"); GRBVar y = model.addVar(0.0, 3.0, 0.0, GRB.CONTINUOUS, "y"); // Create quadratic expression x^2 + x*y + y GRBQuadExpr lhsExpr = new GRBQuadExpr(); lhsExpr.addTerm(1.0, x, x); lhsExpr.addTerm(1.0, x, y); lhsExpr.addTerm(1.0, y); // Create linear expression 2*x - y GRBLinExpr rhsExpr = new GRBLinExpr(); rhsExpr.addTerm(2.0, x); rhsExpr.addTerm(-1.0, y); // Add quadratic constraint x^2 + x*y + y = 2*x - y with name c1 GRBQConstr constr = model.addQConstr(lhsExpr, GRB.EQUAL, rhsExpr, "c1");
- GRBQConstr addQConstr(GRBQuadExpr lhsExpr, char sense, double rhs, 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.
- Arguments:
lhsExpr – Left-hand side quadratic expression for new quadratic constraint.
sense – Sense for new quadratic constraint (
GRB.LESS_EQUAL
,GRB.EQUAL
, orGRB.GREATER_EQUAL
).rhs – Right-hand side value for new quadratic constraint.
name – Name for new constraint.
- Return value:
New quadratic constraint object.
- Example:
// Create variables GRBVar x = model.addVar(-2.0, 2.0, 0.0, GRB.CONTINUOUS, "x"); GRBVar y = model.addVar(0.0, 3.0, 0.0, GRB.CONTINUOUS, "y"); // Create quadratic expression x^2 + x*y + y GRBQuadExpr lhsExpr = new GRBQuadExpr(); lhsExpr.addTerm(1.0, x, x); lhsExpr.addTerm(1.0, x, y); lhsExpr.addTerm(1.0, y); // Add quadratic constraint x^2 + x*y + y = 0 with name c1 GRBQConstr constr = model.addQConstr(lhsExpr, GRB.EQUAL, 0, "c1");
- GRBQConstr addQConstr(GRBLinExpr lhsExpr, char sense, 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.
- Arguments:
lhsExpr – Left-hand side linear expression for new quadratic constraint.
sense – Sense for new quadratic constraint (
GRB.LESS_EQUAL
,GRB.EQUAL
, orGRB.GREATER_EQUAL
).rhsExpr – Right-hand side quadratic expression for new quadratic constraint.
name – Name for new constraint.
- Return value:
New quadratic constraint object.
- Example:
// Create variables GRBVar x = model.addVar(-2.0, 2.0, 0.0, GRB.CONTINUOUS, "x"); GRBVar y = model.addVar(0.0, 3.0, 0.0, GRB.CONTINUOUS, "y"); // Create linear expression 2*x - y GRBLinExpr lhsExpr = new GRBLinExpr(); lhsExpr.addTerm(2.0, x); lhsExpr.addTerm(-1.0, y); // Create quadratic expression x^2 + x*y + y GRBQuadExpr rhsExpr = new GRBQuadExpr(); rhsExpr.addTerm(1.0, x, x); rhsExpr.addTerm(1.0, x, y); rhsExpr.addTerm(1.0, y); // Add quadratic constraint 2*x - y = x^2 + x*y + y with name c1 GRBQConstr constr = model.addQConstr(lhsExpr, GRB.EQUAL, rhsExpr, "c1");
- GRBQConstr addQConstr(GRBVar lhsVar, char sense, 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.
- Arguments:
lhsVar – Left-hand side variable for new quadratic constraint.
sense – Sense for new quadratic constraint (
GRB.LESS_EQUAL
,GRB.EQUAL
, orGRB.GREATER_EQUAL
).rhsExpr – Right-hand side quadratic expression for new quadratic constraint.
name – Name for new constraint.
- Return value:
New quadratic constraint object.
- Example:
// Create variables GRBVar x = model.addVar(-2.0, 2.0, 0.0, GRB.CONTINUOUS, "x"); GRBVar y = model.addVar(0.0, 3.0, 0.0, GRB.CONTINUOUS, "y"); // Create quadratic expression x^2 + x*y + y GRBQuadExpr rhsExpr = new GRBQuadExpr(); rhsExpr.addTerm(1.0, x, x); rhsExpr.addTerm(1.0, x, y); rhsExpr.addTerm(1.0, y); // Add quadratic constraint x <= x^2 + x*y + y with name c1 GRBQConstr constr = model.addQConstr(x, GRB.LESS_EQUAL, rhsExpr, "c1");
- GRBQConstr addQConstr(double lhs, char sense, 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.
- Arguments:
lhs – Left-hand side value for new quadratic constraint.
sense – Sense for new quadratic constraint (
GRB.LESS_EQUAL
,GRB.EQUAL
, orGRB.GREATER_EQUAL
).rhsExpr – Right-hand side quadratic expression for new quadratic constraint.
name – Name for new constraint.
- Return value:
New quadratic constraint object.
- Example:
// Create variables GRBVar x = model.addVar(-2.0, 2.0, 0.0, GRB.CONTINUOUS, "x"); GRBVar y = model.addVar(0.0, 3.0, 0.0, GRB.CONTINUOUS, "y"); // Create quadratic expression x^2 + x*y + y GRBQuadExpr rhsExpr = new GRBQuadExpr(); rhsExpr.addTerm(1.0, x, x); rhsExpr.addTerm(1.0, x, y); rhsExpr.addTerm(1.0, y); // Add quadratic constraint 0 <= x^2 + x*y + y with name c1 GRBQConstr constr = model.addQConstr(0.0, GRB.LESS_EQUAL, rhsExpr, "c1");
- GRBConstr addRange(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
andupper
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}\]- Arguments:
expr – Linear expression for new range constraint.
lower – Lower bound for linear expression.
upper – Upper bound for linear expression.
name – Name for new constraint.
- Return value:
New constraint object.
- Example:
// Create variables GRBVar x = model.addVar(0.0, 10.0, 0.0, GRB.CONTINUOUS, "x"); GRBVar y = model.addVar(-10.0, 3.0, 0.0, GRB.CONTINUOUS, "y"); // Create linear expression x + y GRBLinExpr lexpr = new GRBLinExpr(); lexpr.addTerm(1.0, x); lexpr.addTerm(1.0, y); // Add range constraints -3 <= x + y <= 2 with name c1 GRBConstr constr = model.addRange(lexpr, -3.0, 2.0, "c1");
- GRBConstr[] addRanges(GRBLinExpr[] exprs, double[] lower, double[] upper, String[] names)#
Add new range constraints to a model. A range constraint states that the value of the input expression must be between the specified
lower
andupper
bounds in any solution.- Arguments:
exprs – Linear expressions for the new range constraints.
lower – Lower bounds for linear expressions.
upper – Upper bounds for linear expressions.
names – Names for new range constraints.
- Return value:
Array of new constraint objects.
- Example:
// Create variables GRBVar x = model.addVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "x"); GRBVar y = model.addVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "y"); // Create linear expressions x + y and x - 2.0 * y GRBLinExpr lexpr1 = new GRBLinExpr(); lexpr1.addTerm(1.0, x); lexpr1.addTerm(1.0, y); GRBLinExpr lexpr2 = new GRBLinExpr(); lexpr2.addTerm(1.0, x); lexpr2.addTerm(-2.0, y); GRBLinExpr[] lexprs = {lexpr1, lexpr2}; double[] lower = {-3.0, 2.0}; double[] upper = {-1.0, 5.0}; String[] names = {"c1", "c2"}; // Add constraints -3.0 <= x + y <= 2.0 and -1.0 <= x - 2.0 * y <= 5.0 GRBConstr[] constrs = model.addRanges(lexprs, lower, upper, names);
- GRBSOS addSOS(GRBVar[] vars, double[] weights, int type)#
Add an SOS constraint to the model. Please refer to the SOS Constraints section in the Reference Manual for additional details.
- Arguments:
vars – Array of variables that participate in the SOS constraint.
weights – Weights for the variables in the SOS constraint.
type – SOS type (can be
GRB.SOS_TYPE1
orGRB.SOS_TYPE2
).
- Return value:
New SOS constraint.
- Example:
// Create variables GRBVar x = model.addVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "x"); GRBVar y = model.addVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "y"); // Create helper arrays GRBVar[] vars = {x, y}; double[] weights = {1.0, 2.0}; // Add SOS1 constraint over x and y GRBSOS constr = model.addSOS(vars, weights, GRB.SOS_TYPE1);
- 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.
- Arguments:
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
, orGRB.SEMIINT
).name – Name for new variable.
- Return value:
New variable object.
- Example:
// Create variable GRBVar x = model.addVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "x");
- GRBVar addVar(double lb, double ub, double obj, char type, GRBConstr[] constrs, double[] coeffs, String name)#
Add a single decision variable and the associated non-zero coefficients to a model.
- Arguments:
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
, orGRB.SEMIINT
).constrs – Array of constraints in which the variable participates.
coeffs – Array of coefficients for each constraint in which the variable participates. The lengths of the
constrs
andcoeffs
arrays must be identical.name – Name for new variable.
- Return value:
New variable object.
- Example:
// Add 3 trivial linear constraints to model GRBConstr[] constrs = model.addConstrs(3); // Constraint coefficients for variable x double[] coeffs = {1.0, 2.0, 3.0}; // Add variable x with coeffs 1.0, 2.0, 3.0 to the newly created trivial constraints GRBVar x = model.addVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, constrs, coeffs, "x");
- GRBVar addVar(double lb, double ub, double obj, char type, GRBColumn col, String name)#
Add a single decision variable to a model. This signature allows you to specify the set of constraints to which the new variable belongs using a
GRBColumn
object.- Arguments:
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
, orGRB.SEMIINT
).col – GRBColumn object for specifying a set of constraints to which new variable belongs.
name – Name for new variable.
- Return value:
New variable object.
- Example:
// Add 3 trivial linear constraints to model GRBConstr[] constrs = model.addConstrs(3); // Constraint coefficients for variable x double[] coeffs = {1.0, 2.0, 3.0}; // Create and fill GRBColumn object GRBColumn col = new GRBColumn(); col.addTerms(coeffs, constrs); // Add variable x with coefs 1.0, 2.0, 3.0 to the newly created trivial constraints GRBVar x = model.addVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, col, "x");
- GRBVar[] addVars(int count, char type)#
Add
count
new decision variables to a model. All associated attributes take their default values, except the variabletype
, which is specified as an argument.- Arguments:
count – Number of variables to add.
type – Variable type for new variables (
GRB.CONTINUOUS
,GRB.BINARY
,GRB.INTEGER
,GRB.SEMICONT
, orGRB.SEMIINT
).
- Return value:
Array of new variable objects.
- Example:
// Add 3 binary variables GRBVar[] x = model.addVars(3, GRB.BINARY);
- GRBVar[] addVars(double[] lb, double[] ub, double[] obj, char[] type, String[] names)#
Add new decision variables to a model. The number of added variables is determined by the length of the input arrays (which must be consistent across all arguments).
- Arguments:
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
, orGRB.SEMIINT
). Can benull
, 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.
- Return value:
Array of new variable objects.
- Example:
// Create 3 variables with default lower bound and default type double[] ub = {1, 1, 2}; double[] obj = {-2, -1, -1}; String[] names = {"x0", "x1", "x2"}; GRBVar[] x = model.addVars(null, ub, obj, null, names);
- GRBVar[] addVars(double[] lb, double[] ub, double[] obj, char[] type, String[] names, int start, int len)#
Add new decision variables to a model. This signature allows you to use arrays to hold the various variable attributes (lower bound, upper bound, etc.), without forcing you to add a variable for each entry in the array. The
start
andlen
arguments allow you to specify which variables to add.- Arguments:
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
, orGRB.SEMIINT
). Can benull
, 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.start – The first variable in the list to add.
len – The number of variables to add.
- Return value:
Array of new variable objects.
- Example:
// Create 3 variables with default lower bound and default type double[] ub = {1, 1, 2}; double[] obj = {-2, -1, -1}; String[] names = {"x0", "x1", "x2"}; // Add only the first two variables GRBVar[] x = model.addVars(null, ub, obj, null, names, 0, 2);
- GRBVar[] addVars(double[] lb, double[] ub, double[] obj, char[] type, String[] names, GRBColumn[] cols)#
Add new decision variables to a model. This signature allows you to specify the list of constraints to which each new variable belongs using an array of
GRBColumn
objects.- Arguments:
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
, orGRB.SEMIINT
). Can benull
, 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.
- Return value:
Array of new variable objects.
- Example:
// Create 3 variables with default lower bound and default type double[] ub = {1, 1, 2}; double[] obj = {-2, -1, -1}; String[] names = {"x0", "x1", "x2"}; // Create an array of previously created GRBColumn objects GRBColumn[] col = {col1, col2, col3}; GRBVar[] x = model.addVars(null, ub, obj, null, names, col);
- void chgCoeff(GRBConstr constr, GRBVar var, double newval)#
Change one coefficient in the model. The desired change is captured using a
GRBVar
object, aGRBConstr
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 (usingGRBModel.optimize
), or write the model to disk (usingGRBModel.write
).- Arguments:
constr – Constraint for coefficient to be changed.
var – Variable for coefficient to be changed.
newval – Desired new value for coefficient.
- Example:
// Change coefficient of variable x in constraint c1 model.chgCoeff(c1, x, 1.0);
- void chgCoeffs(GRBConstr[] constrs, GRBVar[] vars, double[] newvals)#
Change a list of coefficients in the model. Each desired change is captured using a
GRBVar
object, aGRBConstr
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. The lengths of the input arrays must all be the same. 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 (usingGRBModel.optimize
), or write the model to disk (usingGRBModel.write
).- Arguments:
constrs – Constraints for coefficients to be changed.
vars – Variables for coefficients to be changed.
newvals – Desired new values for coefficients.
- Example:
// Create arrays using previously created constraints and variables GRBConstr[] constrs = {c1, c2, c3}; GRBVar[] vars = {x, y, z}; double[] vals = {1.0, 2.0, 3.0}; // Change coefficients of variables x, y, z in constraints c1, c2, and c3, respectively model.chgCoeffs(constrs, vars, vals);
- 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 (seeGRBModel.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.
- Example:
// Compute IIS for infeasible model if (model.get(GRB.IntAttr.Status) == GRB.Status.INFEASIBLE) { model.computeIIS(); }
- 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.Use
getMultiobjEnv
to create a multi-objective environment.- Example:
GRBEnv env0 = model.getConcurrentEnv(0); GRBEnv env1 = model.getConcurrentEnv(1); env0.set(GRB.IntParam.Method, 0); env1.set(GRB.IntParam.Method, 1); model.optimize(); model.discardConcurrentEnvs();
- 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.- Example:
GRBEnv env0 = model.getMultiobjEnv(0); GRBEnv env1 = model.getMultiobjEnv(1); env0.set(GRB.IntParam.Method, 0); env1.set(GRB.IntParam.Method, 1); model.optimize(); model.discardMultiobjEnvs();
- void dispose()#
Release the resources associated with a
GRBModel
object. While the Java garbage collector will eventually reclaim these resources, we recommend that you call thedispose
method when you are done using a model.You should not attempt to use a
GRBModel
object after callingdispose
on it.- Example:
GRBEnv env = new GRBEnv(); GRBModel model = new GRBModel(env); // ... model.optimize(); model.dispose(); env.dispose();
- double feasRelax(int relaxobjtype, boolean minrelax, GRBVar[] vars, double[] lbpen, double[] ubpen, GRBConstr[] constrs, double[] rhspen)#
Modifies the
GRBModel
object to create a feasibility relaxation. Note that you need to calloptimize
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. Thelbpen
,ubpen
, andrhspen
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. Thelbpen
,ubpen
, andrhspen
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. Thelbpen
,ubpen
, andrhspen
arguments specify the cost of violating a lower bound, upper bound, and linear constraint, respectively.To give an example, if a constraint with
rhspen
valuep
is violated by 2.0, it would contribute2*p
to the feasibility relaxation objective forrelaxobjtype=0
, it would contribute2*2*p
forrelaxobjtype=1
, and it would contributep
forrelaxobjtype=2
.The
minrelax
argument is a boolean that controls the type of feasibility relaxation that is created. Ifminrelax=false
, optimizing the returned model gives a solution that minimizes the cost of the violation. Ifminrelax=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 thatfeasRelax
must solve an optimization problem to find the minimum possible relaxation whenminrelax=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
andcrelax
, that indicate whether variable bounds and/or constraints can be violated. Ifvrelax
/crelax
istrue
, 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.
- Arguments:
relaxobjtype – The cost function used when finding the minimum cost relaxation.
minrelax – The type of feasibility relaxation to perform.
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
.constrs – Linear constraints that are allowed to be violated.
rhspen – Penalty for violating a linear constraint. One entry for each constraint in argument
constrs
.
- Return value:
Zero if
minrelax
is false. Ifminrelax
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.- Example:
// Compute feasibility relaxation for infeasible model if (model.get(GRB.IntAttr.Status) == GRB.Status.INFEASIBLE) { GRBVar[] vars = model.getVars(); double[] ubpen = new double[model.get(GRB.IntAttr.NumVars)]; for (int i = 0; i < model.get(GRB.IntAttr.NumVars); i++) { ubpen[i] = 1.0; } model.feasRelax(0, false, vars, null, ubpen, null, null); model.optimize(); }
- double feasRelax(int relaxobjtype, boolean minrelax, boolean vrelax, boolean crelax)#
Modifies the
GRBModel
object to create a feasibility relaxation. Note that you need to calloptimize
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. Thelbpen
,ubpen
, andrhspen
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. Thelbpen
,ubpen
, andrhspen
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. Thelbpen
,ubpen
, andrhspen
arguments specify the cost of violating a lower bound, upper bound, and linear constraint, respectively.To give an example, if a constraint with
rhspen
valuep
is violated by 2.0, it would contribute2*p
to the feasibility relaxation objective forrelaxobjtype=0
, it would contribute2*2*p
forrelaxobjtype=1
, and it would contributep
forrelaxobjtype=2
.The
minrelax
argument is a boolean that controls the type of feasibility relaxation that is created. Ifminrelax=false
, optimizing the returned model gives a solution that minimizes the cost of the violation. Ifminrelax=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 thatfeasRelax
must solve an optimization problem to find the minimum possible relaxation whenminrelax=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
andcrelax
, that indicate whether variable bounds and/or constraints can be violated. Ifvrelax
/crelax
istrue
, 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.
- Arguments:
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.
- Return value:
Zero if
minrelax
is false. Ifminrelax
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.- Example:
// Compute feasibility relaxation for infeasible model if (model.get(GRB.IntAttr.Status) == GRB.Status.INFEASIBLE) { model.feasRelax(1, false, false, true); model.optimize(); }
- GRBModel fixedModel()#
Create the fixed model associated with a MIP model. A solution (e.g. obtained through a call to the
optimize
method) or a MIP start must be available in the MIP model. If no solution is available, the MIP start specified with StartNumber is used.In the model, each integer variable is fixed to the value that variable takes in the MIP solution or MIP start. In addition, continuous variables may be fixed to satisfy SOS or general constraints. The result is that the model has neither integrality constraints, SOS constraints, nor general constraints any more.
Note
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.
Note
On a multi-objective model, all but the first objectives are ignored. All scenarios are ignored as well, if any.
- Return value:
Fixed model associated with calling object.
- Example:
GRBModel fixedModel = model.fixedModel();
- void convertToFixed()#
Turn the MIP model into a continuous one, in place. A solution (e.g. obtained through a call to the
optimize
method) or a MIP start must be available in the MIP model. If no solution is available, the MIP start specified with StartNumber is used.In the model, each integer variable is fixed to the value that variable takes in the MIP solution or MIP start. In addition, continuous variables may be fixed to satisfy SOS or general constraints. The result is that the model has neither integrality constraints, SOS constraints, nor general constraints any more.
Note
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.
Note
An error is raised if the converted model contains more than one objective or scenario, or if the model contains concurrent environments or tune environments.
Convert the current model to a fixed model.
- Example:
model.convertToFixed();
- double get(GRB.DoubleParam param)#
Query the value of a double-valued parameter.
- Arguments:
param – The parameter being queried.
- Return value:
The current value of the requested parameter.
- Example:
// Get value of TimeLimit parameter double val = model.get(GRB.DoubleParam.TimeLimit);
- int get(GRB.IntParam param)#
Query the value of an int-valued parameter.
- Arguments:
param – The parameter being queried.
- Return value:
The current value of the requested parameter.
- Example:
// Get value of PumpPasses parameter int val = model.get(GRB.IntParam.PumpPasses);
- String get(GRB.StringParam param)#
Query the value of a string-valued parameter.
- Arguments:
param – The parameter being queried.
- Return value:
The current value of the requested parameter.
- Example:
// Get value of LogFile parameter String val = model.get(GRB.StringParam.LogFile);
- char[] get(GRB.CharAttr attr, GRBVar[] vars)#
Query a char-valued variable attribute for an array of variables.
- Arguments:
attr – The attribute being queried.
vars – The variables whose attribute values are being queried.
- Return value:
The current values of the requested attribute for each input variable.
- Example:
// Get variable type attribute values GRBVar[] vars = model.getVars(); char[] vtype = model.get(GRB.CharAttr.VType, vars);
- char[] get(GRB.CharAttr attr, GRBVar[] vars, int start, int len)#
Query a char-valued variable attribute for a sub-array of variables.
- Arguments:
attr – The attribute being queried.
vars – A one-dimensional array of variables whose attribute values are being queried.
start – The index of the first variable of interest in the list.
len – The number of variables.
- Return value:
The current values of the requested attribute for each input variable.
- Example:
// Get variable type attribute values for variables indexed from 0 to 2 GRBVar[] vars = model.getVars(); char[] vtype = model.get(GRB.CharAttr.VType, vars, 0, 3);
- char[][] get(GRB.CharAttr attr, GRBVar[][] vars)#
Query a char-valued variable attribute for a two-dimensional array of variables.
- Arguments:
attr – The attribute being queried.
vars – A two-dimensional array of variables whose attribute values are being queried.
- Return value:
The current values of the requested attribute for each input variable.
- Example:
// Get variable type attribute values GRBVar[][] vars = new GRBVar[10][10]; // ... char[][] vtype = model.get(GRB.CharAttr.VType, vars);
- char[][][] get(GRB.CharAttr attr, GRBVar[][][] vars)#
Query a char-valued variable attribute for a three-dimensional array of variables.
- Arguments:
attr – The attribute being queried.
vars – A three-dimensional array of variables whose attribute values are being queried.
- Return value:
The current values of the requested attribute for each input variable.
- Example:
// Get variable type attribute values GRBVar[][][] vars = new GRBVar[10][10][10]; // ... char[][][] vtype = model.get(GRB.CharAttr.VType, vars);
- char[] get(GRB.CharAttr attr, GRBConstr[] constrs)#
Query a char-valued constraint attribute for an array of constraints.
- Arguments:
attr – The attribute being queried.
constrs – The constraints whose attribute values are being queried.
- Return value:
The current values of the requested attribute for each input constraint.
- Example:
// Get sense attribute values for all linear constraints GRBConstr[] constrs = model.getConstrs(); char[] sense = model.get(GRB.CharAttr.Sense, constrs);
- char[] get(GRB.CharAttr attr, GRBConstr[] constrs, int start, int len)#
Query a char-valued constraint attribute for a sub-array of constraints.
- Arguments:
attr – The attribute being queried.
constrs – A one-dimensional array of constraints whose attribute values are being queried.
start – The index of the first constraint of interest in the list.
len – The number of constraints.
- Return value:
The current values of the requested attribute for each input constraint.
- Example:
// Get linear constraint sense attribute values for constraints indexed from 0 to 2 GRBConstr[] constrs = model.getConstrs(); char[] sense = model.get(GRB.CharAttr.Sense, constrs, 0, 3);
- char[][] get(GRB.CharAttr attr, GRBConstr[][] constrs)#
Query a char-valued constraint attribute for a two-dimensional array of constraints.
- Arguments:
attr – The attribute being queried.
constrs – A two-dimensional array of constraints whose attribute values are being queried.
- Return value:
The current values of the requested attribute for each input constraint.
- Example:
// Get sense attribute values for a 2d array of linear constraints GRBConstr[][] constrs = new GRBConstr[10][10]; // ... char[][] sense = model.get(GRB.CharAttr.Sense, constrs);
- char[][][] get(GRB.CharAttr attr, GRBConstr[][][] constrs)#
Query a char-valued constraint attribute for a three-dimensional array of constraints.
- Arguments:
attr – The attribute being queried.
constrs – A three-dimensional array of constraints whose attribute values are being queried.
- Return value:
The current values of the requested attribute for each input constraint.
- Example:
// Get sense attribute values for a 3d array of linear constraints GRBConstr[][][] constrs = new GRBConstr[10][10][10]; // ... char[][][] sense = model.get(GRB.CharAttr.Sense, constrs);
- char[] get(GRB.CharAttr attr, GRBQConstr[] qconstrs)#
Query a char-valued quadratic constraint attribute for an array of quadratic constraints.
- Arguments:
attr – The attribute being queried.
qconstrs – The quadratic constraints whose attribute values are being queried.
- Return value:
The current values of the requested attribute for each input quadratic constraint.
- Example:
// Get sense attribute values for all quadratic constraints GRBQConstr[] constrs = model.getQConstrs(); char[] qsense = model.get(GRB.CharAttr.QCSense, constrs);
- char[] get(GRB.CharAttr attr, GRBQConstr[] qconstrs, int start, int len)#
Query a char-valued quadratic constraint attribute for a sub-array of quadratic constraints.
- Arguments:
attr – The attribute being queried.
qconstrs – A one-dimensional array of quadratic constraints whose attribute values are being queried.
start – The index of the first quadratic constraint of interest in the list.
len – The number of quadratic constraints.
- Return value:
The current values of the requested attribute for each input quadratic constraint.
- Example:
// Get sense attribute values for quadratic constraints indexed from 0 to 2 GRBQConstr[] constrs = model.getQConstrs(); char[] qsense = model.get(GRB.CharAttr.QCSense, constrs, 0, 3);
- char[][] get(GRB.CharAttr attr, GRBQConstr[][] qconstrs)#
Query a char-valued quadratic constraint attribute for a two-dimensional array of quadratic constraints.
- Arguments:
attr – The attribute being queried.
qconstrs – A two-dimensional array of quadratic constraints whose attribute values are being queried.
- Return value:
The current values of the requested attribute for each input quadratic constraint.
- Example:
// Get sense attribute values for a 2d array of quadratic constraints GRBQConstr[][] constrs = new GRBConstr[10][10]; // ... char[][] qsense = model.get(GRB.CharAttr.QCSense, constrs);
- char[][][] get(GRB.CharAttr attr, GRBQConstr[][][] qconstrs)#
Query a char-valued quadratic constraint attribute for a three-dimensional array of quadratic constraints.
- Arguments:
attr – The attribute being queried.
qconstrs – A three-dimensional array of quadratic constraints whose attribute values are being queried.
- Return value:
The current values of the requested attribute for each input quadratic constraint.
- Example:
// Get sense attribute values for a 3d array of quadratic constraints GRBQConstr[][][] constrs = new GRBConstr[10][10][10]; // ... char[][][] qsense = model.get(GRB.CharAttr.QCSense, constrs);
- double get(GRB.DoubleAttr attr)#
Query the value of a double-valued model attribute.
- Arguments:
attr – The attribute being queried.
- Return value:
The current value of the requested attribute.
- Example:
// Get value ObjVal attribute double val = model.get(GRB.DoubleAttr.ObjVal);
- double[] get(GRB.DoubleAttr attr, GRBVar[] vars)#
Query a double-valued variable attribute for an array of variables.
- Arguments:
attr – The attribute being queried.
vars – The variables whose attribute values are being queried.
- Return value:
The current values of the requested attribute for each input variable.
- Example:
// Get lower bound attribute values of all variables GRBVar[] vars = model.getVars(); double[] lb = model.get(GRB.DoubleAttr.LB, vars);
- double[] get(GRB.DoubleAttr attr, GRBVar[] vars, int start, int len)#
Query a double-valued variable attribute for a sub-array of variables.
- Arguments:
attr – The attribute being queried.
vars – A one-dimensional array of variables whose attribute values are being queried.
start – The index of the first variable of interest in the list.
len – The number of variables.
- Return value:
The current values of the requested attribute for each input variable.
- Example:
// Get lower bound attribute values for variables indexed from 0 to 2 GRBVar[] vars = model.getVars(); double[] lb = model.get(GRB.DoubleAttr.LB, vars, 0, 3);
- double[][] get(GRB.DoubleAttr attr, GRBVar[][] vars)#
Query a double-valued variable attribute for a two-dimensional array of variables.
- Arguments:
attr – The attribute being queried.
vars – A two-dimensional array of variables whose attribute values are being queried.
- Return value:
The current values of the requested attribute for each input variable.
- Example:
// Get lower bound attribute values for a 2d array of variables GRBVar[][] vars = new GRBVar[10][10]; // ... double[][] lb = model.get(GRB.DoubleAttr.LB, vars);
- double[][][] get(GRB.DoubleAttr attr, GRBVar[][][] vars)#
Query a double-valued variable attribute for a three-dimensional array of variables.
- Arguments:
attr – The attribute being queried.
vars – A three-dimensional array of variables whose attribute values are being queried.
- Return value:
The current values of the requested attribute for each input variable.
- Example:
// Get lower bound attribute values for a 3d array of variables GRBVar[][][] vars = new GRBVar[10][10][10]; // ... double[][][] lb = model.get(GRB.DoubleAttr.LB, vars);
- double[] get(GRB.DoubleAttr attr, GRBConstr[] constrs)#
Query a double-valued constraint attribute for an array of constraints.
- Arguments:
attr – The attribute being queried.
constrs – The constraints whose attribute values are being queried.
- Return value:
The current values of the requested attribute for each input constraint.
- Example:
// Get RHS attribute values of all linear constraints GRBConstr[] constrs = model.getConstrs(); double[] rhs = model.get(GRB.DoubleAttr.RHS, constrs);
- double[] get(GRB.DoubleAttr attr, GRBConstr[] constrs, int start, int len)#
Query a double-valued constraint attribute for a sub-array of constraints.
- Arguments:
attr – The attribute being queried.
constrs – A one-dimensional array of constraints whose attribute values are being queried.
start – The first constraint of interest in the list.
len – The number of constraints.
- Return value:
The current values of the requested attribute for each input constraint.
- Example:
// Get RHS attribute values for linear constraints indexed from 0 to 2 GRBConstr[] constrs = model.getConstrs(); double[] rhs = model.get(GRB.DoubleAttr.RHS, constrs, 0, 3);
- double[][] get(GRB.DoubleAttr attr, GRBConstr[][] constrs)#
Query a double-valued constraint attribute for a two-dimensional array of constraints.
- Arguments:
attr – The attribute being queried.
constrs – A two-dimensional array of constraints whose attribute values are being queried.
- Return value:
The current values of the requested attribute for each input constraint.
- Example:
// Get RHS attribute values for a 2d array of constraints GRBConstr[][] constrs = new GRBConstr[10][10]; // ... double[][] rhs = model.get(GRB.DoubleAttr.RHS, constrs);
- double[][][] get(GRB.DoubleAttr attr, GRBConstr[][][] constrs)#
Query a double-valued constraint attribute for a three-dimensional array of constraints.
- Arguments:
attr – The attribute being queried.
constrs – A three-dimensional array of constraints whose attribute values are being queried.
- Return value:
The current values of the requested attribute for each input constraint.
- Example:
// Get RHS attribute values for a 3d array of constraints GRBConstr[][][] constrs = new GRBConstr[10][10][10]; // ... double[][][] rhs = model.get(GRB.DoubleAttr.RHS, constrs);
- double[] get(GRB.DoubleAttr attr, GRBQConstr[] qconstrs)#
Query a double-valued quadratic constraint attribute for an array of quadratic constraints.
- Arguments:
attr – The attribute being queried.
qconstrs – The quadratic constraints whose attribute values are being queried.
- Return value:
The current values of the requested attribute for each input quadratic constraint.
- Example:
// Get RHS attribute values of all quadratic constraints GRBQConstr[] constrs = model.getQConstrs(); double[] qrhs = model.get(GRB.DoubleAttr.QCRHS, constrs);
- double[] get(GRB.DoubleAttr attr, GRBQConstr[] qconstrs, int start, int len)#
Query a double-valued quadratic constraint attribute for a sub-array of quadratic constraints.
- Arguments:
attr – The attribute being queried.
qconstrs – A one-dimensional array of quadratic constraints whose attribute values are being queried.
start – The first quadratic constraint of interest in the list.
len – The number of quadratic constraints.
- Return value:
The current values of the requested attribute for each input quadratic constraint.
- Example:
// Get RHS attribute values for quadratic constraints indexed from 0 to 2 GRBQConstr[] constrs = model.getQConstrs(); double[] qrhs = model.get(GRB.DoubleAttr.QCRHS, constrs, 0, 3);
- double[][] get(GRB.DoubleAttr attr, GRBQConstr[][] qconstrs)#
Query a double-valued quadratic constraint attribute for a two-dimensional array of quadratic constraints.
- Arguments:
attr – The attribute being queried.
qconstrs – A two-dimensional array of quadratic constraints whose attribute values are being queried.
- Return value:
The current values of the requested attribute for each input quadratic constraint.
- Example:
// Get RHS attribute values for a 2d array of quadratic constraints GRBQConstr[][] constrs = new GRBQConstr[10][10]; // ... double[][] qrhs = model.get(GRB.DoubleAttr.QCRHS, constrs);
- double[][][] get(GRB.DoubleAttr attr, GRBQConstr[][][] qconstrs)#
Query a double-valued quadratic constraint attribute for a three-dimensional array of quadratic constraints.
- Arguments:
attr – The attribute being queried.
qconstrs – A three-dimensional array of quadratic constraints whose attribute values are being queried.
- Return value:
The current values of the requested attribute for each input quadratic constraint.
- Example:
// Get RHS attribute values for a 3d array of quadratic constraints GRBQConstr[][][] constrs = new GRBQConstr[10][10][10]; // ... double[][][] qrhs = model.get(GRB.DoubleAttr.QCRHS, constrs);
- int get(GRB.IntAttr attr)#
Query the value of an int-valued model attribute.
- Arguments:
attr – The attribute being queried.
- Return value:
The current value of the requested attribute.
- Example:
// Get number of variables in the model int numvars = model.get(GRB.IntAttr.NumVars);
- int[] get(GRB.IntAttr attr, GRBVar[] vars)#
Query an int-valued variable attribute for an array of variables.
- Arguments:
attr – The attribute being queried.
vars – The variables whose attribute values are being queried.
- Return value:
The current values of the requested attribute for each input variable.
- Example:
// Get VBasis attribute values of all variables GRBVar[] vars = model.getVars(); int[] vbasis = model.get(GRB.IntAttr.VBasis, vars);
- int[] get(GRB.IntAttr attr, GRBVar[] vars, int start, int len)#
Query an int-valued variable attribute for a sub-array of variables.
- Arguments:
attr – The attribute being queried.
vars – A one-dimensional array of variables whose attribute values are being queried.
start – The index of the first variable of interest in the list.
len – The number of variables.
- Return value:
The current values of the requested attribute for each input variable.
- Example:
// Get VBasis attribute values for variables indexed from 0 to 2 GRBVar[] vars = model.getVars(); int[] vbasis = model.get(GRB.IntAttr.VBasis, vars, 0, 3);
- int[][] get(GRB.IntAttr attr, GRBVar[][] vars)#
Query an int-valued variable attribute for a two-dimensional array of variables.
- Arguments:
attr – The attribute being queried.
vars – A two-dimensional array of variables whose attribute values are being queried.
- Return value:
The current values of the requested attribute for each input variable.
- Example:
// Get VBasis attribute values for a 2d array of variables GRBVar[][] vars = new GRBVar[10][10]; // ... int[][] vbasis = model.get(GRB.IntAttr.VBasis, vars);
- int[][][] get(GRB.IntAttr attr, GRBVar[][][] vars)#
Query an int-valued variable attribute for a three-dimensional array of variables.
- Arguments:
attr – The attribute being queried.
vars – A three-dimensional array of variables whose attribute values are being queried.
- Return value:
The current values of the requested attribute for each input variable.
- Example:
// Get VBasis attribute values for a 3d array of variables GRBVar[][][] vars = new GRBVar[10][10][10]; // ... int[][][] vbasis = model.get(GRB.IntAttr.VBasis, vars);
- int[] get(GRB.IntAttr attr, GRBConstr[] constrs)#
Query an int-valued constraint attribute for an array of constraints.
- Arguments:
attr – The attribute being queried.
constrs – The constraints whose attribute values are being queried.
- Return value:
The current values of the requested attribute for each input constraint.
- Example:
// Get CBasis attribute values of all linear constraints GRBConstr[] constrs = model.getConstrs(); int[] cbasis = model.get(GRB.IntAttr.CBasis, constrs);
- int[] get(GRB.IntAttr attr, GRBConstr[] constrs, int start, int len)#
Query an int-valued constraint attribute for a sub-array of constraints.
- Arguments:
attr – The attribute being queried.
constrs – A one-dimensional array of constraints whose attribute values are being queried.
start – The index of the first constraint of interest in the list.
len – The number of constraints.
- Return value:
The current values of the requested attribute for each input constraint.
- Example:
// Get CBasis attribute values for linear constraints indexed from 0 to 2 GRBConstr[] constrs = model.getConstrs(); int[] cbasis = model.get(GRB.IntAttr.CBasis, constrs, 0, 3);
- int[][] get(GRB.IntAttr attr, GRBConstr[][] constrs)#
Query an int-valued constraint attribute for a two-dimensional array of constraints.
- Arguments:
attr – The attribute being queried.
constrs – A two-dimensional array of constraints whose attribute values are being queried.
- Return value:
The current values of the requested attribute for each input constraint.
- Example:
// Get CBasis attribute values for a 2d array of constraints GRBConstr[][] constrs = new GRBConstr[10][10]; // ... int[][] cbasis = model.get(GRB.IntAttr.CBasis, constrs);
- int[][][] get(GRB.IntAttr attr, GRBConstr[][][] constrs)#
Query an int-valued constraint attribute for a three-dimensional array of constraints.
- Arguments:
attr – The attribute being queried.
constrs – A three-dimensional array of constraints whose attribute values are being queried.
- Return value:
The current values of the requested attribute for each input constraint.
- Example:
// Get CBasis attribute values for a 3d array of constraints GRBConstr[][][] constrs = new GRBConstr[10][10][10]; // ... int[][][] cbasis = model.get(GRB.IntAttr.CBasis, constrs);
- int[] get(GRB.IntAttr attr, GRBQConstr[] qconstrs)#
Query an int-valued quadratic constraint attribute for an array of quadratic constraints.
- Arguments:
attr – The attribute being queried.
qconstrs – The quadratic constraints whose attribute values are being queried.
- Return value:
The current values of the requested attribute for each input quadratic constraint.
- Example:
// Get IISQConstr attribute values of all quadratic constraints GRBQConstr[] qconstrs = model.getQConstrs(); int[] iisconstrs = model.get(GRB.IntAttr.IISQConstr, qconstrs);
- int[] get(GRB.IntAttr attr, GRBQConstr[] qconstrs, int start, int len)#
Query an int-valued quadratic constraint attribute for a sub-array of quadratic constraints.
- Arguments:
attr – The attribute being queried.
qconstrs – A one-dimensional array of quadratic constraints whose attribute values are being queried.
start – The index of the first quadratic constraint of interest in the list.
len – The number of quadratic constraints.
- Return value:
The current values of the requested attribute for each input quadratic constraint.
- Example:
// Get IISQConstr attribute values for quadratic constraints indexed from 0 to 2 GRBQConstr[] qconstrs = model.getQConstrs(); int[] iisconstrs = model.get(GRB.IntAttr.IISQConstr, qconstrs, 0, 3);
- int[][] get(GRB.IntAttr attr, GRBQConstr[][] qconstrs)#
Query an int-valued quadratic constraint attribute for a two-dimensional array of quadratic constraints.
- Arguments:
attr – The attribute being queried.
qconstrs – A two-dimensional array of quadratic constraints whose attribute values are being queried.
- Return value:
The current values of the requested attribute for each input quadratic constraint.
- Example:
// Get IISQConstr attribute values for a 2d array of quadratic constraints GRBQConstr[][] qconstrs = new GRBQConstr[10][10]; // ... int[][] iisconstr = model.get(GRB.IntAttr.IISQConstr, qconstrs);
- int[][][] get(GRB.IntAttr attr, GRBQConstr[][][] qconstrs)#
Query an int-valued quadratic constraint attribute for a three-dimensional array of quadratic constraints.
- Arguments:
attr – The attribute being queried.
qconstrs – A three-dimensional array of quadratic constraints whose attribute values are being queried.
- Return value:
The current values of the requested attribute for each input quadratic constraint.
- Example:
// Get IISQConstr attribute values for a 3d array of quadratic constraints GRBQConstr[][][] qconstrs = new GRBQConstr[10][10][10]; // ... int[][][] iisconstrs = model.get(GRB.IntAttr.IISQConstr, qconstrs);
- int[] get(GRB.IntAttr attr, GRBGenConstr[] genconstrs)#
Query an int-valued general constraint attribute for an array of general constraints.
- Arguments:
attr – The attribute being queried.
genconstrs – The general constraints whose attribute values are being queried.
- Return value:
The current values of the requested attribute for each input general constraint.
- Example:
// Get GenConstrType attribute values for all general constraints GRBGenConstr[] genconstrs = model.getGenConstrs(); // ... int[] types = model.get(GRB.IntAttr.GenConstrType, genconstrs);
- int[] get(GRB.IntAttr attr, GRBGenConstr[] genconstrs, int start, int len)#
Query an int-valued general constraint attribute for a sub-array of general constraints.
- Arguments:
attr – The attribute being queried.
genconstrs – A one-dimensional array of general constraints whose attribute values are being queried.
start – The index of the first general constraint of interest in the list.
len – The number of general constraints.
- Return value:
The current values of the requested attribute for each input general constraint.
- Example:
// Get GenConstrType attribute values for general constraints indexed from 0 to 2 GRBGenConstr[] genconstrs = model.getGenConstrs(); // ... int[] types = model.get(GRB.IntAttr.GenConstrType, genconstrs, 0, 3);
- int[][] get(GRB.IntAttr attr, GRBGenConstr[][] genconstrs)#
Query an int-valued general constraint attribute for a two-dimensional array of general constraints.
- Arguments:
attr – The attribute being queried.
genconstrs – A two-dimensional array of general constraints whose attribute values are being queried.
- Return value:
The current values of the requested attribute for each input general constraint.
- Example:
// Get GenConstrType attribute values a 2d array of general constraints GRBGenConstr[][] genconstrs = new GRBGenConstr[10][10]; // ... int[][] types = model.get(GRB.IntAttr.GenConstrType, genconstrs);
- int[][][] get(GRB.IntAttr attr, GRBGenConstr[][][] genconstrs)#
Query an int-valued general constraint attribute for a three-dimensional array of general constraints.
- Arguments:
attr – The attribute being queried.
genconstrs – A three-dimensional array of general constraints whose attribute values are being queried.
- Return value:
The current values of the requested attribute for each input general constraint.
- Example:
// Get GenConstrType attribute values a 3d array of general constraints GRBGenConstr[][][] genconstrs = new GRBGenConstr[10][10][10]; // ... int[][][] types = model.get(GRB.IntAttr.GenConstrType, genconstrs);
- String get(GRB.StringAttr attr)#
Query the value of a string-valued model attribute.
- Arguments:
attr – The attribute being queried.
- Return value:
The current value of the requested attribute.
- Example:
// Get ModelName attribute String modelname = model.get(GRB.StringAttr.ModelName);
- String[] get(GRB.StringAttr attr, GRBVar[] vars)#
Query a String-valued variable attribute for an array of variables.
- Arguments:
attr – The attribute being queried.
vars – The variables whose attribute values are being queried.
- Return value:
The current values of the requested attribute for each input variable.
- Example:
// Get variable name attribute values of all variables GRBVar[] vars = model.getVars(); String[] varnames = model.get(GRB.StringAttr.VarName, vars);
- String[] get(GRB.StringAttr attr, GRBVar[] vars, int start, int len)#
Query a String-valued variable attribute for a sub-array of variables.
- Arguments:
attr – The attribute being queried.
vars – A one-dimensional array of variables whose attribute values are being queried.
start – The index of the first variable of interest in the list.
len – The number of variables.
- Return value:
The current values of the requested attribute for each input variable.
- Example:
// Get variable name attribute values for variables indexed from 0 to 2 GRBVar[] vars = model.getVars(); String[] varnames = model.get(GRB.StringAttr.VarName, vars, 0, 3);
- String[][] get(GRB.StringAttr attr, GRBVar[][] vars)#
Query a String-valued variable attribute for a two-dimensional array of variables.
- Arguments:
attr – The attribute being queried.
vars – A two-dimensional array of variables whose attribute values are being queried.
- Return value:
The current values of the requested attribute for each input variable.
- Example:
// Get variable name attribute values GRBVar[][] vars = new GRBVar[10][10]; // ... String[][] varnames = model.get(GRB.StringAttr.VarName, vars);
- String[][][] get(GRB.StringAttr attr, GRBVar[][][] vars)#
Query a String-valued variable attribute for a three-dimensional array of variables.
- Arguments:
attr – The attribute being queried.
vars – A three-dimensional array of variables whose attribute values are being queried.
- Return value:
The current values of the requested attribute for each input variable.
- Example:
// Get variable name attribute values GRBVar[][][] vars = new GRBVar[10][10][10]; // ... String[][][] varnames = model.get(GRB.StringAttr.VarName, vars);
- String[] get(GRB.StringAttr attr, GRBConstr[] constrs)#
Query a String-valued constraint attribute for an array of constraints.
- Arguments:
attr – The attribute being queried.
constrs – The constraints whose attribute values are being queried.
- Return value:
The current values of the requested attribute for each input constraint.
- Example:
// Get constraint name attribute values of all linear constraints GRBConstr[] constrs = model.getConstrs(); String[] constrnames = model.get(GRB.StringAttr.ConstrName, constrs);
- String[] get(GRB.StringAttr attr, GRBConstr[] constrs, int start, int len)#
Query a String-valued constraint attribute for a sub-array of constraints.
- Arguments:
attr – The attribute being queried.
constrs – A one-dimensional array of constraints whose attribute values are being queried.
start – The index of the first constraint of interest in the list.
len – The number of constraints.
- Return value:
The current values of the requested attribute for each input constraint.
- Example:
// Get constraint name attribute values for linear constraints indexed from 0 to 2 GRBConstr[] constrs = model.getConstrs(); String[] constrnames = model.get(GRB.StringAttr.ConstrName, constrs, 0, 3);
- String[][] get(GRB.StringAttr attr, GRBConstr[][] constrs)#
Query a String-valued constraint attribute for a two-dimensional array of constraints.
- Arguments:
attr – The attribute being queried.
constrs – A two-dimensional array of constraints whose attribute values are being queried.
- Return value:
The current values of the requested attribute for each input constraint.
- Example:
// Get constraint name attribute values GRBConstr[][] constrs = new GRBConstr[10][10]; // ... String[][] constrnames = model.get(GRB.StringAttr.ConstrName, constrs);
- String[][][] get(GRB.StringAttr attr, GRBConstr[][][] constrs)#
Query a String-valued constraint attribute for a three-dimensional array of constraints.
- Arguments:
attr – The attribute being queried.
constrs – A three-dimensional array of constraints whose attribute values are being queried.
- Return value:
The current values of the requested attribute for each input constraint.
- Example:
// Get constraint name attribute values GRBConstr[][][] constrs = new GRBConstr[10][10][10]; // ... String[][][] constrnames = model.get(GRB.StringAttr.ConstrName, constrs);
- String[] get(GRB.StringAttr attr, GRBQConstr[] qconstrs)#
Query a String-valued quadratic constraint attribute for an array of quadratic constraints.
- Arguments:
attr – The attribute being queried.
qconstrs – The quadratic constraints whose attribute values are being queried.
- Return value:
The current values of the requested attribute for each input quadratic constraint.
- Example:
// Get constraint name values of all quadratic constraints GRBQConstr[] qconstrs = model.getQConstrs(); String[] qconstrnames = model.get(GRB.StringAttr.QCName, qconstrs);
- String[] get(GRB.StringAttr attr, GRBQConstr[] qconstrs, int start, int len)#
Query a String-valued quadratic constraint attribute for a sub-array of quadratic constraints.
- Arguments:
attr – The attribute being queried.
qconstrs – A one-dimensional array of quadratic constraints whose attribute values are being queried.
start – The index of the first quadratic constraint of interest in the list.
len – The number of quadratic constraints.
- Return value:
The current values of the requested attribute for each input quadratic constraint.
- Example:
// Get constraint name values for quadratic constraints indexed from 0 to 2 GRBQConstr[] qconstrs = model.getQConstrs(); String[] qconstrnames = model.get(GRB.StringAttr.QCName, qconstrs, 0, 3);
- String[][] get(GRB.StringAttr attr, GRBQConstr[][] qconstrs)#
Query a String-valued quadratic constraint attribute for a two-dimensional array of quadratic constraints.
- Arguments:
attr – The attribute being queried.
qconstrs – A two-dimensional array of quadratic constraints whose attribute values are being queried.
- Return value:
The current values of the requested attribute for each input quadratic constraint.
- Example:
// Get constraint name values for a 2d array of quadratic constraints GRBQConstr[][] qconstrs = new GRBQConstr[10][10]; // ... String[][] qconstrnames = model.get(GRB.StringAttr.QCName, qconstrs);
- String[][][] get(GRB.StringAttr attr, GRBQConstr[][][] qconstrs)#
Query a String-valued quadratic constraint attribute for a three-dimensional array of quadratic constraints.
- Arguments:
attr – The attribute being queried.
qconstrs – A three-dimensional array of quadratic constraints whose attribute values are being queried.
- Return value:
The current values of the requested attribute for each input quadratic constraint.
- Example:
// Get constraint name values for a 3d array of quadratic constraints GRBQConstr[][][] qconstrs = new GRBQConstr[10][10][10]; // ... String[][][] qconstrnames = model.get(GRB.StringAttr.QCName, qconstrs);
- String[] get(GRB.StringAttr attr, GRBGenConstr[] genconstrs)#
Query a String-valued general constraint attribute for an array of general constraints.
- Arguments:
attr – The attribute being queried.
genconstrs – The general constraints whose attribute values are being queried.
- Return value:
The current values of the requested attribute for each input general constraint.
- Example:
// Get general constraint name attribute values GRBGenConstr[] genconstrs = model.getGenConstrs(); String[] genconstrnames = model.get(GRB.StringAttr.GenConstrName, genconstrs);
- String[] get(GRB.StringAttr attr, GRBGenConstr[] genconstrs, int start, int len)#
Query a String-valued general constraint attribute for a sub-array of general constraints.
- Arguments:
attr – The attribute being queried.
genconstrs – A one-dimensional array of general constraints whose attribute values are being queried.
start – The index of the first general constraint of interest in the list.
len – The number of general constraints.
- Return value:
The current values of the requested attribute for each input general constraint.
- Example:
// Get general constraint name attribute values for constraints indexed from 0 to 2 GRBGenConstr[] genconstrs = model.getGenConstrs(); String[] genconstrnames = model.get(GRB.StringAttr.GenConstrName, genconstrs, 0, 3);
- String[][] get(GRB.StringAttr attr, GRBGenConstr[][] genconstrs)#
Query a String-valued constraint attribute for a two-dimensional array of general constraints.
- Arguments:
attr – The attribute being queried.
genconstrs – A two-dimensional array of general constraints whose attribute values are being queried.
- Return value:
The current values of the requested attribute for each input general constraint.
- Example:
// Get general constraint name attribute values for a 2d array of general constraints GRBGenConstr[][] genconstrs = new GRBGenConstr[10][10]; // ... String[][] genconstrnames = model.get(GRB.StringAttr.GenConstrName, genconstrs);
- String[][][] get(GRB.StringAttr attr, GRBGenConstr[][][] genconstrs)#
Query a String-valued constraint attribute for a three-dimensional array of general constraints.
- Arguments:
attr – The attribute being queried.
genconstrs – A three-dimensional array of general constraints whose attribute values are being queried.
- Return value:
The current values of the requested attribute for each input general constraint.
- Example:
// Get general constraint name attribute values for a 3d array of general constraints GRBGenConstr[][][] genconstrs = new GRBGenConstr[10][10][10]; // ... String[][][] genconstrnames = model.get(GRB.StringAttr.GenConstrName, genconstrs);
- double getCoeff(GRBConstr constr, GRBVar var)#
Query the coefficient of variable
var
in linear constraintconstr
(note that the result can be zero).- Arguments:
constr – The requested constraint.
var – The requested variable.
- Return value:
The current value of the requested coefficient.
- Example:
// Get coefficient of variable x in constraint c1 double coeff = model.getCoeff(c1, x);
- 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.- Arguments:
var – The variable of interest.
- Return value:
A
GRBColumn
object that captures the set of constraints in which the variable participates.- Example:
// Get column of coefficient and constraint pairs for variable x GRBColumn col = model.getCol(x)
- 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.- Arguments:
num – The concurrent environment number.
- Return value:
The concurrent environment for the model.
- Example:
GRBEnv env0 = model.getConcurrentEnv(0); GRBEnv env1 = model.getConcurrentEnv(1); env0.set(GRB.IntParam.Method, 0); // primal simplex env1.set(GRB.IntParam.Method, 1); // dual simplex model.optimize(); model.discardConcurrentEnvs();
- GRBConstr getConstrByName(String name)#
Retrieve a linear constraint from its name. If multiple linear constraints have the same name, this method chooses one arbitrarily. Returns null if no constraint has that name.
- Arguments:
name – The name of the desired linear constraint.
- Return value:
The requested linear constraint.
- Example:
// Retrieve linear constraint named "constr1" GRBConstr c1 = model.getConstrByName("constr1");
- GRBConstr[] getConstrs()#
Retrieve an array of all linear constraints in the model.
- Return value:
All linear constraints in the model.
- Example:
// Retrieve all linear constraints GRBConstr[] constrs = model.getConstrs();
- void getGenConstrMax(GRBGenConstr genc, GRBVar[] resvar, GRBVar[] vars, int[] len, double[] constant)#
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 thevars
argument. The routine returns the total number of operand variables in the specified general constraint inlen
. That allows you to make certain that thevars
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
.- Arguments:
genc – The general constraint object.
resvar – Store the resultant variable of the constraint at
resvar[0]
.vars – Array to store the operand variables of the constraint.
len – Store the number of operand variables of the constraint at
len[0]
.constant – Store the additional constant operand of the constraint at
constant[0]
.
- Example:
GRBVar[] resvar = new GRBVar[1]; GRBVar[] vars; int[] len = new int[1]; double[] constant = new double[1]; // Retrieve general constraint type int type = genc.get(GRB.IntAttr.GenConstrType); if (type == GRB.GENCONSTR_MAX) { model.getGenConstrMax(genc, resvar, null, len, null); // Initialize correct array size vars = new GRBVar[len[0]]; model.getGenConstrMax(genc, resvar, vars, len, constant); }
- void getGenConstrMin(GRBGenConstr genc, GRBVar[] resvar, GRBVar[] vars, int[] len, double[] constant)#
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 thevars
argument. The routine returns the total number of operand variables in the specified general constraint inlen
. That allows you to make certain that thevars
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
.- Arguments:
genc – The general constraint object.
resvar – Store the resultant variable of the constraint at
resvar[0]
.vars – Array to store the operand variables of the constraint.
len – Store the number of operand variables of the constraint at
len[0]
.constant – Store the additional constant operand of the constraint at
constant[0]
.
- Example:
GRBVar[] resvar = new GRBVar[1]; GRBVar[] vars; int[] len = new int[1]; double[] constant = new double[1]; // Retrieve general constraint type int type = genc.get(GRB.IntAttr.GenConstrType); if (type == GRB.GENCONSTR_MIN) { model.getGenConstrMin(genc, resvar, null, len, null); // Initialize correct array size vars = new GRBVar[len[0]]; model.getGenConstrMin(genc, resvar, vars, len, constant); }
- void getGenConstrAbs(GRBGenConstr genc, GRBVar[] resvar, GRBVar[] argvar)#
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
.- Arguments:
genc – The general constraint object.
resvar – Store the resultant variable of the constraint at
resvar[0]
.argvar – Store the argument variable of the constraint at
argvar[0]
.
- Example:
GRBVar[] resvar = new GRBVar[1]; GRBVar[] argvar = new GRBVar[1]; // Retrieve general constraint type int type = genc.get(GRB.IntAttr.GenConstrType); if (type == GRB.GENCONSTR_ABS) { model.getGenConstrAbs(genc, resvar, argvar); }
- void getGenConstrAnd(GRBGenConstr genc, GRBVar[] resvar, GRBVar[] vars, int[] len)#
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 thevars
argument. The routine returns the total number of operand variables in the specified general constraint inlen
. That allows you to make certain that thevars
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
.- Arguments:
genc – The general constraint object.
resvar – Store the resultant variable of the constraint at
resvar[0]
.vars – Array to store the operand variables of the constraint.
len – Store the number of operand variables of the constraint at
len[0]
.
- Example:
GRBVar[] resvar = new GRBVar[1]; GRBVar[] vars; int[] len = new int[1]; // Retrieve general constraint type int type = genc.get(GRB.IntAttr.GenConstrType); if (type == GRB.GENCONSTR_AND) { model.getGenConstrAnd(genc, resvar, null, len); // Initialize correct array size vars = new GRBVar[len[0]]; model.getGenConstrAnd(genc, resvar, vars, len); }
- void getGenConstrOr(GRBGenConstr genc, GRBVar[] resvar, GRBVar[] vars, int[] len)#
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 thevars
argument. The routine returns the total number of operand variables in the specified general constraint inlen
. That allows you to make certain that thevars
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
.- Arguments:
genc – The general constraint object.
resvar – Store the resultant variable of the constraint at
resvar[0]
.vars – Array to store the operand variables of the constraint.
len – Store the number of operand variables of the constraint at
len[0]
.
- Example:
GRBVar[] resvar = new GRBVar[1]; GRBVar[] vars; int[] len = new int[1]; // Retrieve general constraint type int type = genc.get(GRB.IntAttr.GenConstrType); if (type == GRB.GENCONSTR_OR) { model.getGenConstrOr(genc, resvar, null, len); // Initialize correct array size vars = new GRBVar[len[0]]; model.getGenConstrOr(genc, resvar, vars, len); }
- void getGenConstrNorm(GRBGenConstr genc, GRBVar[] resvar, GRBVar[] vars, int[] len, double[] which)#
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 thevars
argument. The routine returns the total number of operand variables in the specified general constraint inlen
. That allows you to make certain that thevars
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
.- Arguments:
genc – The general constraint object.
resvar – Store the resultant variable of the constraint at
resvar[0]
.vars – Array to store the operand variables of the constraint.
len – Store the number of operand variables of the constraint at
len[0]
.which – Store the norm type (possible values are 0, 1, 2, or GRB.INFINITY).
- Example:
GRBVar[] resvar = new GRBVar[1]; GRBVar[] vars; int[] len = new int[1]; double[] which = new double[1]; // Retrieve general constraint type int type = genc.get(GRB.IntAttr.GenConstrType); if (type == GRB.GENCONSTR_NORM) { model.getGenConstrNorm(genc, resvar, null, len, null); // Initialize correct array size vars = new GRBVar[len[0]]; model.getGenConstrNorm(genc, resvar, vars, len, which); }
- void getGenConstrNL(GRBGenConstr genc, GRBVar[] resvar, int[] nnodes, int[] opcode, double[] data, int[] parent)#
Retrieve the data associated with a general constraint of type NL. 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 theopcode
,data
andparent
arguments. The routine returns the total number of nodes that form the expression tree for the specified general constraint innnodes
. That allows you to make certain that theopcode
,data
andparent
arrays are of sufficient size to hold the result of the second call.See also
addGenConstrNL
for a description of the semantics of this general constraint type.Any of the following arguments can be
null
.- Arguments:
genc – The general constraint object.
resvar – Stores the resultant variable of the constraint at
resvar[0]
.nnodes – Stores the number of nodes required to describe the nonlinear expression at
nnodes[0]
.opcode – Stores the array containing the operation codes for the nodes.
data – Stores the array containing auxiliary data for each node.
parent – Stores the array providing the parent index of each node.
- Example:
GRBVar resvar = new GRBVar[1]; int nnodes = new int[1]; int[] opcode; double[] data; int[] parent; // Retrieve general constraint type int type = genc.get(GRB.IntAttr.GenConstrType); if (type == GRB.GENCONSTR_NL) { // Get number of nodes model.getGenConstrNL(genc, resvar, nnodes, null, null, null); // Initialize correct array sizes opcode = new int[nnodes[0]]; data = new double[nnodes[0]]; parent = new int[nnodes[0]]; // Get array data model.getGenConstrNL(genc, resvar, nnodes, opcode, data, parent); }
- void getGenConstrIndicator(GRBGenConstr genc, GRBVar[] binvar, int[] binval, GRBLinExpr[] expr, char[] sense, double[] rhs)#
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
.- Arguments:
genc – The general constraint object.
binvar – Store the binary indicator variable of the constraint at
binvar[0]
.binval – Store the value that the indicator variable has to take in order to trigger the linear constraint at
binval[0]
.expr – Create a
GRBLinExpr
object to store the left-hand side expression of the linear constraint that is triggered by the indicator atexpr[0]
.sense – Store the sense for the linear constraint at
sense[0]
. Options areGRB.LESS_EQUAL
,GRB.EQUAL
, orGRB.GREATER_EQUAL
.rhs – Store the right-hand side value for the linear constraint at
rhs[0]
.
- Example:
GRBVar[] binvar = new GRBVar[1]; int[] binval = new int[1]; GRBLinExpr[] expr = new GRBLinExpr[1]; char[] sense = new char[1]; double[] rhs = new double[1]; // Retrieve general constraint type int type = genc.get(GRB.IntAttr.GenConstrType); if (type == GRB.GENCONSTR_INDICATOR) { model.getGenConstrIndicator(genc, binvar, binval, expr, sense, rhs); }
- void getGenConstrPWL(GRBGenConstr genc, GRBVar[] xvar, GRBVar[] yvar, int[] npts, 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 thexpts
andypts
arguments. The routine returns the length for thexpts
andypts
arrays innpts
. That allows you to make certain that thexpts
andypts
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
.- Arguments:
genc – The general constraint object.
xvar – Store the \(x\) variable.
yvar – Store the \(y\) variable.
npts – 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.
- Example:
GRBVar[] xvar = new GRBVar[1]; GRBVar[] yvar = new GRBVar[1]; int[] npts = new int[1]; double[] xpts, ypts; // Retrieve general constraint type int type = genc.get(GRB.IntAttr.GenConstrType); if (type == GRB.GENCONSTR_PWL) { model.getGenConstrPWL(genc, xvar, yvar, npts, null, null); // Initialize correct array sizes xpts = new double[npts[0]]; ypts = new double[npts[0]]; model.getGenConstrPWL(genc, xvar, yvar, npts, xpts, ypts); }
- void getGenConstrPoly(GRBGenConstr genc, GRBVar[] xvar, GRBVar[] yvar, int[] plen, 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 thep
argument. The routine returns the length of thep
array inplen
. That allows you to make certain that thep
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
.- Arguments:
genc – The general constraint object.
xvar – Store the \(x\) variable.
yvar – Store the \(y\) variable.
plen – 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.
- Example:
GRBVar[] xvar = new GRBVar[1]; GRBVar[] yvar = new GRBVar[1]; int[] plen = new int[1]; double[] p; // Retrieve general constraint type int type = genc.get(GRB.IntAttr.GenConstrType); if (type == GRB.GENCONSTR_POLY) { model.getGenConstrPoly(genc, xvar, yvar, plen, null); // Initialize correct array sizes p = new double[plen[0]]; model.getGenConstrPoly(genc, xvar, yvar, plen, p); }
- void getGenConstrExp(GRBGenConstr genc, GRBVar[] xvar, GRBVar[] yvar)#
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
.- Arguments:
genc – The general constraint object.
xvar – Store the \(x\) variable.
yvar – Store the \(y\) variable.
- Example:
GRBVar[] xvar = new GRBVar[1]; GRBVar[] yvar = new GRBVar[1]; // Retrieve general constraint type int type = genc.get(GRB.IntAttr.GenConstrType); if (type == GRB.GENCONSTR_EXP) { model.getGenConstrExp(genc, xvar, yvar); }
- void getGenConstrExpA(GRBGenConstr genc, GRBVar[] xvar, GRBVar[] yvar, double[] a)#
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
.- Arguments:
genc – The general constraint object.
xvar – Store the \(x\) variable.
yvar – Store the \(y\) variable.
a – Store the base of the function.
- Example:
GRBVar[] xvar = new GRBVar[1]; GRBVar[] yvar = new GRBVar[1]; double[] a = new double[1]; // Retrieve general constraint type int type = genc.get(GRB.IntAttr.GenConstrType); if (type == GRB.GENCONSTR_EXPA) { model.getGenConstrExpA(genc, xvar, yvar, a); }
- void getGenConstrLog(GRBGenConstr genc, GRBVar[] xvar, GRBVar[] yvar)#
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
.- Arguments:
genc – The general constraint object.
xvar – Store the \(x\) variable.
yvar – Store the \(y\) variable.
- Example:
GRBVar[] xvar = new GRBVar[1]; GRBVar[] yvar = new GRBVar[1]; // Retrieve general constraint type int type = genc.get(GRB.IntAttr.GenConstrType); if (type == GRB.GENCONSTR_LOG) { model.getGenConstrLog(genc, xvar, yvar); }
- void getGenConstrLogA(GRBGenConstr genc, GRBVar[] xvar, GRBVar[] yvar, double[] a)#
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
.- Arguments:
genc – The general constraint object.
xvar – Store the \(x\) variable.
yvar – Store the \(y\) variable.
a – Store the base of the function.
- Example:
GRBVar[] xvar = new GRBVar[1]; GRBVar[] yvar = new GRBVar[1]; double[] a = new double[1]; // Retrieve general constraint type int type = genc.get(GRB.IntAttr.GenConstrType); if (type == GRB.GENCONSTR_LOGA) { model.getGenConstrLogA(genc, xvar, yvar, a); }
- void getGenConstrLogistic(GRBGenConstr genc, GRBVar[] xvar, GRBVar[] yvar)#
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
.- Arguments:
genc – The general constraint object.
xvar – Store the \(x\) variable.
yvar – Store the \(y\) variable.
- Example:
GRBVar[] xvar = new GRBVar[1]; GRBVar[] yvar = new GRBVar[1]; // Retrieve general constraint type int type = genc.get(GRB.IntAttr.GenConstrType); if (type == GRB.GENCONSTR_LOGISTIC) { model.getGenConstrLogistic(genc, xvar, yvar); }
- void getGenConstrPow(GRBGenConstr genc, GRBVar[] xvar, GRBVar[] yvar, double[] a)#
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
.- Arguments:
genc – The general constraint object.
xvar – Store the \(x\) variable.
yvar – Store the \(y\) variable.
a – Store the exponent of the function.
- Example:
GRBVar[] xvar = new GRBVar[1]; GRBVar[] yvar = new GRBVar[1]; double[] a = new double[1]; // Retrieve general constraint type int type = genc.get(GRB.IntAttr.GenConstrType); if (type == GRB.GENCONSTR_POW) { model.getGenConstrPow(genc, xvar, yvar, a); }
- void getGenConstrSin(GRBGenConstr genc, GRBVar[] xvar, GRBVar[] yvar)#
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
.- Arguments:
genc – The general constraint object.
xvar – Store the \(x\) variable.
yvar – Store the \(y\) variable.
- Example:
GRBVar[] xvar = new GRBVar[1]; GRBVar[] yvar = new GRBVar[1]; // Retrieve general constraint type int type = genc.get(GRB.IntAttr.GenConstrType); if (type == GRB.GENCONSTR_SIN) { model.getGenConstrSin(genc, xvar, yvar); }
- void getGenConstrCos(GRBGenConstr genc, GRBVar[] xvar, GRBVar[] yvar)#
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
.- Arguments:
genc – The general constraint object.
xvar – Store the \(x\) variable.
yvar – Store the \(y\) variable.
- Example:
GRBVar[] xvar = new GRBVar[1]; GRBVar[] yvar = new GRBVar[1]; // Retrieve general constraint type int type = genc.get(GRB.IntAttr.GenConstrType); if (type == GRB.GENCONSTR_COS) { model.getGenConstrCos(genc, xvar, yvar); }
- void getGenConstrTan(GRBGenConstr genc, GRBVar[] xvar, GRBVar[] yvar)#
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
.- Arguments:
genc – The general constraint object.
xvar – Store the \(x\) variable.
yvar – Store the \(y\) variable.
- Example:
GRBVar[] xvar = new GRBVar[1]; GRBVar[] yvar = new GRBVar[1]; // Retrieve general constraint type int type = genc.get(GRB.IntAttr.GenConstrType); if (type == GRB.GENCONSTR_TAN) { model.getGenConstrTan(genc, xvar, yvar); }
- GRBGenConstr[] getGenConstrs()#
Retrieve an array of all general constraints in the model.
- Return value:
All general constraints in the model.
- Example:
GRBGenConstr[] constrs = model.getGenConstrs();
- 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.- Return value:
A JSON string.
- Example:
String solution = model.getJSONSolution();
- 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.- Arguments:
index – The optimization pass index, starting from 0.
- Return value:
The multi-objective environment for that optimization pass when solving the model.
- Example:
GRBEnv env0 = model.getMultiobjEnv(0); GRBEnv env1 = model.getMultiobjEnv(1); env0.set(GRB.IntParam.Method, 0); env1.set(GRB.IntParam.Method, 1); model.optimize(); model.discardMultiobjEnvs();
- GRBExpr 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.
- Return value:
The model objective.
- Example:
GRBExpr obj = model.getObjective(); double objVal = obj.getValue();
- 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.
- Arguments:
index – The index for the requested alternative objective.
- Return value:
The requested alternative objective.
- Example:
// Get objective with index 1 GRBLinExpr obj = model.getObjective(1); double objVal = obj.getValue();
- 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.
- Arguments:
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.
- Return value:
The number of points that define the piecewise-linear objective function.
- Example:
int npts = model.getPWLObj(var, null, null); double[] x = new double[npts]; double[] y = new double[npts]; // Get PWL objective model.getPWLObj(var, x, y);
- GRBQuadExpr getQCRow(GRBQConstr qc)#
Retrieve the left-hand side expression from a quadratic constraint. The result is returned as a
GRBQuadExpr
object.- Arguments:
qc – The quadratic constraint of interest.
- Return value:
A
GRBQuadExpr
object that captures the left-hand side of the quadratic constraint.- Example:
GRBQConstr[] constrs = model.getQConstrs(); // Get left-hand side of first quadratic constraint GRBQuadExpr qexpr = model.getQCRow(constrs[0]);
- GRBQConstr[] getQConstrs()#
Retrieve an array of all quadratic constraints in the model.
- Return value:
All quadratic constraints in the model.
- Example:
// Get all quadratic constraints GRBQConstr[] constrs = model.getQConstrs();
- 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.- Arguments:
constr – The constraint of interest. A
GRBConstr
object, typically obtained fromaddConstr
orgetConstrs
.- Return value:
A
GRBLinExpr
object that captures the set of variables that participate in the constraint.- Example:
GRBConstr[] constrs = model.getConstrs(); // Get left-hand side of first linear constraint GRBLinExpr lexpr = model.getRow(constrs[0]);
- int getSOS(GRBSOS sos, GRBVar[] vars, double[] weights, int[] type)#
Retrieve the list of variables that participate in an SOS constraint, and the associated coefficients. The return value is the length of this list. Note that the argument arrays must be long enough to accommodate the result. Call the method with
null
array arguments to determine the appropriate array lengths.- Arguments:
sos – The SOS set of interest.
vars – A list of variables that participate in
sos
. Can benull
.weights – The SOS weights for each participating variable. Can be
null
.type – The type of the SOS set (either
GRB.SOS_TYPE1
orGRB.SOS_TYPE2
) is returned intype[0]
.
- Return value:
The number of entries placed in the output arrays. Note that you should consult the return value to determine the length of the result; the arrays sizes won’t necessarily match the result size.
- Example:
GRBSOS[] constrs = model.getSOSs(); // Get variables in first SOS constraint int len = model.getSOS(constrs[0], null, null, null); GRBVar[] vars = new GRBVar[len]; double[] weights = new double[len]; int[] type = new int[len]; model.getSOS(constrs[0], vars, weights, type);
- GRBSOS[] getSOSs()#
Retrieve an array of all SOS constraints in the model.
- Return value:
All SOS constraints in the model.
- Example:
// Get all SOS constraints GRBSOS[] constrs = model.getSOSs();
- void getTuneResult(int i)#
Use this method to retrieve the results of a previous
tune
call. Calling this method with argumentn
causes tuned parameter setn
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, orwrite
to write the changed parameters to a.prm
file.Please refer to the Parameter Tuning section in the Reference Manual for details on the tuning tool.
- Arguments:
i – 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.
- Example:
model.tune(); model.getTuneResult(0);
- GRBVar getVarByName(String name)#
Retrieve a variable from its name. If multiple variables have the same name, this method chooses one arbitrarily. Returns null if no variable has that name.
- Arguments:
name – The name of the desired variable.
- Return value:
The requested variable.
- Example:
// Get variable with name "var1" GRBVar v = model.getVarByName("var1");
- GRBVar[] getVars()#
Retrieve an array of all variables in the model.
- Return value:
All variables in the model.
- Example:
// Get all variables GRBVar[] vars = model.getVars();
- 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 in the Reference Manual for more information on attributes.
Please consult this section in the Reference Manual 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.
- Example:
model.optimize();
- 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 callsync
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.
- Example:
// Start asynchronous optimization model.optimizeasync(); // Poll the status while optimization is in progress while (model.get(GRB.IntAttr.Status) == GRB.INPROGRESS) { Thread.sleep(100); // 100 ms sleep System.out.println("ObjVal=" + model.get(GRB.DoubleAttr.ObjVal) + " ObjBound=" + model.get(GRB.DoubleAttr.ObjBound)); } // Synchronize model model.sync(); // Query solution and process results // ...
- 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.
- Return value:
A unique string identifier for the batch request.
- Example:
String 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.
- Return value:
Presolved version of original model.
- Example:
GRBModel presolved = model.presolve();
- void read(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.- Arguments:
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
,.7z
or.xz
.filename – Name of the file to read.
- Example:
model.read("myModel.mps");
- 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 (usingGRBModel.optimize
), or write the model to disk (usingGRBModel.write
).- Arguments:
constr – The linear constraint to remove.
- Example:
GRBConstr[] constrs = model.getConstrs(); // Remove first linear constraint model.remove(constrs[0]);
- 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 (usingGRBModel.optimize
), or write the model to disk (usingGRBModel.write
).- Arguments:
genconstr – The general constraint to remove.
- Example:
GRBGenConstr[] constrs = model.getGenConstrs(); // Remove first general constraint model.remove(constrs[0]);
- 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 (usingGRBModel.optimize
), or write the model to disk (usingGRBModel.write
).- Arguments:
qconstr – The quadratic constraint to remove.
- Example:
GRBQConstr[] constrs = model.getQConstrs(); // Remove first quadratic constraint model.remove(constrs[0]);
- 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 (usingGRBModel.optimize
), or write the model to disk (usingGRBModel.write
).- Arguments:
sos – The SOS constraint to remove.
- Example:
GRBSOS[] constrs = model.getSOSs(); // Remove first SOS constraint model.remove(constrs[0]);
- 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 (usingGRBModel.optimize
), or write the model to disk (usingGRBModel.write
).- Arguments:
var – The variable to remove.
- Example:
GRBVar[] vars = model.getVars(); // Remove first variable model.remove(vars[0]);
- void reset()#
Reset the model to an unsolved state, discarding any previously computed solution information.
- Example:
model.reset();
- void reset(int clearall)#
Reset the model to an unsolved state, discarding any previously computed solution information.
- Arguments:
clearall – 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). Pass 0 to just discard the solution.
- Example:
model.reset(1);
- 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 forGRBCallback
for additional information.Note that a model can only have a single callback method, so this call will replace an existing callback.
- Arguments:
cb – New callback object. To disable a previously set callback, call this method with a
null
argument.- Example:
class MyCallback extends GRBCallback { // ... } MyCallback cb = new MyCallback(); model.setCallback(cb);
- void set(GRB.DoubleParam param, double newval)#
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).- Arguments:
param – The parameter being modified.
newval – The desired new value for the parameter.
- Example:
model.set(GRB.DoubleParam.TimeLimit, 10.0);
- void set(GRB.IntParam param, int newval)#
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).- Arguments:
param – The parameter being modified.
newval – The desired new value for the parameter.
- Example:
model.set(GRB.IntParam.PumpPasses, 10);
- void set(GRB.StringParam param, String newval)#
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).- Arguments:
param – The parameter being modified.
newval – The desired new value for the parameter.
- Example:
model.set(GRB.StringParam.LogFile, "myLog.log");
- void set(String param, String newval)#
Set the value of any parameter using strings alone.
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).- Arguments:
param – The name of the parameter being modified.
newval – The desired new value for the parameter.
- Example:
// Set the TimeLimit parameter via strings model.set("TimeLimit", "10.0");
- void set(GRB.CharAttr attr, GRBVar[] vars, char[] newvals)#
Set a char-valued variable attribute for an array of variables.
- Arguments:
attr – The attribute being modified.
vars – The variables whose attribute values are being modified.
newvals – The desired new values for the attribute for each input variable.
- Example:
// Add 3 binary variables GRBVar[] x = model.addVars(3, GRB.BINARY); // Change the type of the 3 variables to CONTINUOUS char[] newvals = {'C', 'C', 'C'}; model.set(GRB.CharAttr.VType, x, newvals);
- void set(GRB.CharAttr attr, GRBVar[] vars, char[] newvals, int start, int len)#
Set a char-valued variable attribute for a sub-array of variables.
- Arguments:
attr – The attribute being modified.
vars – A one-dimensional array of variables whose attribute values are being modified.
newvals – The desired new values for the attribute for each input variable.
start – The index of the first variable of interest in the list.
len – The number of variables.
- Example:
// Add 3 binary variables GRBVar[] x = model.addVars(3, GRB.BINARY); // Change the type of the first 2 variables to CONTINUOUS char[] newvals = {'C', 'C'}; model.set(GRB.CharAttr.VType, x, newvals, 0, 2);
- void set(GRB.CharAttr attr, GRBVar[][] vars, char[][] newvals)#
Set a char-valued variable attribute for a two-dimensional array of variables.
- Arguments:
attr – The attribute being modified.
vars – A two-dimensional array of variables whose attribute values are being modified.
newvals – The desired new values for the attribute for each input variable.
- Example:
// Change the type of a 2x2 array of variables to BINARY GRBVar[][] x = new GRBVar[2][2]; // ... char[][] newvals = {{'B', 'B'},{'B', 'B'}}; model.set(GRB.CharAttr.VType, x, newvals);
- void set(GRB.CharAttr attr, GRBVar[][][] vars, char[][][] newvals)#
Set a char-valued variable attribute for a three-dimensional array of variables.
- Arguments:
attr – The attribute being modified.
vars – A three-dimensional array of variables whose attribute values are being modified.
newvals – The desired new values for the attribute for each input variable.
- Example:
// Change the type of a 2x2x2 array of variables to BINARY GRBVar[][][] x = new GRBVar[2][2][2]; // ... char[][][] newvals = {{{'B', 'B'},{'B', 'B'}}, {{'B', 'B'},{'B', 'B'}}}; model.set(GRB.CharAttr.VType, x, newvals);
- void set(GRB.CharAttr attr, GRBConstr[] constrs, char[] newvals)#
Set a char-valued constraint attribute for an array of constraints.
- Arguments:
attr – The attribute being modified.
constrs – The constraints whose attribute values are being modified.
newvals – The desired new values for the attribute for each input constraint.
- Example:
// Add 3 trivial linear constraints to model GRBConstr[] constrs = model.addConstrs(3); // Change the sense of the 3 constraints char[] newvals = {'<', '=', '>'}; model.set(GRB.CharAttr.Sense, constrs, newvals);
- void set(GRB.CharAttr attr, GRBConstr[] constrs, char[] newvals, int start, int len)#
Set a char-valued constraint attribute for a sub-array of constraints.
- Arguments:
attr – The attribute being modified.
constrs – A one-dimensional array of constraints whose attribute values are being modified.
newvals – The desired new values for the attribute for each input constraint.
start – The index of the first constraint of interest in the list.
len – The number of constraints.
- Example:
// Add 3 trivial linear constraints to model GRBConstr[] constrs = model.addConstrs(3); // Change the sense of the first 2 constraints char[] newvals = {'<', '='}; model.set(GRB.CharAttr.Sense, constrs, newvals, 0, 2);
- void set(GRB.CharAttr attr, GRBConstr[][] constrs, char[][] newvals)#
Set a char-valued constraint attribute for a two-dimensional array of constraints.
- Arguments:
attr – The attribute being modified.
constrs – A two-dimensional array of constraints whose attribute values are being modified.
newvals – The desired new values for the attribute for each input constraint.
- Example:
// Change the sense of a 2x2 array of constraints GRBConstr[][] constrs = new GRBConstr[2][2]; // ... char[][] newvals = {{'<', '='}, {'<', '='}}; model.set(GRB.CharAttr.Sense, constrs, newvals);
- void set(GRB.CharAttr attr, GRBConstr[][][] constrs, char[][][] newvals)#
Set a char-valued constraint attribute for a three-dimensional array of constraints.
- Arguments:
attr – The attribute being modified.
constrs – A three-dimensional array of constraints whose attribute values are being modified.
newvals – The desired new values for the attribute for each input constraint.
- Example:
// Change the sense of a 2x2x2 array of constraints GRBConstr[][][] constrs = new GRBConstr[2][2][2]; // ... char[][][] newvals = {{{'<', '='}, {'<', '='}},{{'<', '='}, {'<', '='}}}; model.set(GRB.CharAttr.Sense, constrs, newvals);
- void set(GRB.CharAttr attr, GRBQConstr[] qconstrs, char[] newvals)#
Set a char-valued quadratic constraint attribute for an array of quadratic constraints.
- Arguments:
attr – The attribute being modified.
qconstrs – The quadratic constraints whose attribute values are being modified.
newvals – The desired new values for the attribute for each input quadratic constraint.
- Example:
// Add 3 quadratic constraints GRBQConstr c1 = model.addQConstr(x*x + y*y == 0, "c1"); GRBQConstr c2 = model.addQConstr(x*y - y*y == 0, "c2"); GRBQConstr c3 = model.addQConstr(x*x - x*y == 0, "c3"); GRBQConstr[] constrs = {c1, c2, c3}; // Change the sense of the 3 constraints char[] newvals = {'<', '=', '>'}; model.set(GRB.CharAttr.QCSense, constrs, newvals);
- void set(GRB.CharAttr attr, GRBQConstr[] qconstrs, char[] newvals, int start, int len)#
Set a char-valued quadratic constraint attribute for a sub-array of quadratic constraints.
- Arguments:
attr – The attribute being modified.
qconstrs – A one-dimensional array of quadratic constraints whose attribute values are being modified.
newvals – The desired new values for the attribute for each input quadratic constraint.
start – The index of the first quadratic constraint of interest in the list.
len – The number of quadratic constraints.
- Example:
// Add 3 quadratic constraints GRBQConstr c1 = model.addQConstr(x*x + y*y == 0, "c1"); GRBQConstr c2 = model.addQConstr(x*y - y*y == 0, "c2"); GRBQConstr c3 = model.addQConstr(x*x - x*y == 0, "c3"); GRBQConstr[] constrs = {c1, c2, c3}; // Change the sense of the first 2 constraints char[] newvals = {'<', '='}; model.set(GRB.CharAttr.QCSense, constrs, newvals, 0, 2);
- void set(GRB.CharAttr attr, GRBQConstr[][] qconstrs, char[][] newvals)#
Set a char-valued quadratic constraint attribute for a two-dimensional array of quadratic constraints.
- Arguments:
attr – The attribute being modified.
qconstrs – A two-dimensional array of quadratic constraints whose attribute values are being modified.
newvals – The desired new values for the attribute for each input quadratic constraint.
- Example:
// Change the sense of a 2x2 array of quadratic constraints GRBQConstr[][] qconstrs = new GRBQConstr[2][2]; // ... char[][] newvals = {{'<', '='}, {'<', '='}}; model.set(GRB.CharAttr.QCSense, qconstrs, newvals);
- void set(GRB.CharAttr attr, GRBQConstr[][][] qconstrs, char[][][] newvals)#
Set a char-valued quadratic constraint attribute for a three-dimensional array of quadratic constraints.
- Arguments:
attr – The attribute being modified.
qconstrs – A three-dimensional array of quadratic constraints whose attribute values are being modified.
newvals – The desired new values for the attribute for each input quadratic constraint.
- Example:
// Change the sense of a 2x2x2 array of quadratic constraints char[][][] newvals = {{{'<', '='}, {'<', '='}},{{'<', '='}, {'<', '='}}}; model.set(GRB.CharAttr.QCSense, qconstrs, newvals);
- void set(GRB.DoubleAttr attr, double newval)#
Set the value of a double-valued model attribute.
- Arguments:
attr – The attribute being modified.
newval – The desired new value for the attribute.
- Example:
// Adjust objective constant model.set(GRB.DoubleAttr.ObjCon, 2.0);
- void set(GRB.DoubleAttr attr, GRBVar[] vars, double[] newvals)#
Set a double-valued variable attribute for an array of variables.
- Arguments:
attr – The attribute being modified.
vars – The variables whose attribute values are being modified.
newvals – The desired new values for the attribute for each input variable.
- Example:
// Add 3 continuous variables GRBVar[] x = model.addVars(3, GRB.CONTINUOUS); // Change the lower bound of the 3 variables double[] newvalues = {0.1, 0.2, 0.3}; model.Set(GRB.DoubleAttr.LB, x, newvalues);
- void set(GRB.DoubleAttr attr, GRBVar[] vars, double[] newvals, int start, int len)#
Set a double-valued variable attribute for a sub-array of variables.
- Arguments:
attr – The attribute being modified.
vars – A one-dimensional array of variables whose attribute values are being modified.
newvals – The desired new values for the attribute for each input variable.
start – The index of the first variable of interest in the list.
len – The number of variables.
- Example:
// Add 3 continuous variables GRBVar[] x = model.addVars(3, GRB.CONTINUOUS); // Change the lower bound of the first 2 variables double[] newvalues = {0.1, 0.2}; model.Set(GRB.DoubleAttr.LB, x, newvalues, 0, 2);
- void set(GRB.DoubleAttr attr, GRBVar[][] vars, double[][] newvals)#
Set a double-valued variable attribute for a two-dimensional array of variables.
- Arguments:
attr – The attribute being modified.
vars – A two-dimensional array of variables whose attribute values are being modified.
newvals – The desired new values for the attribute for each input variable.
- Example:
// Change the lower bound of a 2x2 array of variables GRBVar[][] x = new GRBVar[2][2]; // ... double[][] newvals = {{0.1, 0.2}, {0.3, 0.4}}; model.set(GRB.DoubleAttr.LB, x, newvals);
- void set(GRB.DoubleAttr attr, GRBVar[][][] vars, double[][][] newvals)#
Set a double-valued variable attribute for a three-dimensional array of variables.
- Arguments:
attr – The attribute being modified.
vars – A three-dimensional array of variables whose attribute values are being modified.
newvals – The desired new values for the attribute for each input variable.
- Example:
// Change the lower bound of a 2x2x2 array of variables GRBVar[][][] x = new GRBVar[2][2][2]; // ... double[][][] newvals = {{{0.1, 0.2}, {0.3, 0.4}}, {{0.5, 0.6}, {0.7, 0.8}}}; model.set(GRB.DoubleAttr.LB, x, newvals);
- void set(GRB.DoubleAttr attr, GRBConstr[] constrs, double[] newvals)#
Set a double-valued constraint attribute for an array of constraints.
- Arguments:
attr – The attribute being modified.
constrs – The constraints whose attribute values are being modified.
newvals – The desired new values for the attribute for each input constraint.
- Example:
// Add 3 linear constraints to the model GRBConstr[] constrs = model.addConstrs(3); // Change the right-hand sides of the constraints double[] newvals = {1.0, 2.0, 3.0}; model.set(GRB.DoubleAttr.RHS, constrs, newvals);
- void set(GRB.DoubleAttr attr, GRBConstr[] constrs, double[] newvals, int start, int len)#
Set a double-valued constraint attribute for a sub-array of constraints.
- Arguments:
attr – The attribute being modified.
constrs – A one-dimensional array of constraints whose attribute values are being modified.
newvals – The desired new values for the attribute for each input constraint.
start – The first constraint of interest in the list.
len – The number of constraints.
- Example:
// Add 3 linear constraints to the model GRBConstr[] constrs = model.addConstrs(3); // Change the right-hand sides of the first 2 constraints double[] newvals = {1.0, 2.0}; model.set(GRB.DoubleAttr.RHS, constrs, newvals, 0, 2);
- void set(GRB.DoubleAttr attr, GRBConstr[][] constrs, double[][] newvals)#
Set a double-valued constraint attribute for a two-dimensional array of constraints.
- Arguments:
attr – The attribute being modified.
constrs – A two-dimensional array of constraints whose attribute values are being modified.
newvals – The desired new values for the attribute for each input constraint.
- Example:
// Change the RHS of a 2x2 array of constraints GRBConstr[][] constrs = new GRBConstr[2][2]; // ... double[][] newvals = {{1.0, 2.0}, {3.0, 4.0}}; model.set(GRB.DoubleAttr.RHS, constrs, newvals);
- void set(GRB.DoubleAttr attr, GRBConstr[][][] constrs, double[][][] newvals)#
Set a double-valued constraint attribute for a three-dimensional array of constraints.
- Arguments:
attr – The attribute being modified.
constrs – A three-dimensional array of constraints whose attribute values are being modified.
newvals – The desired new values for the attribute for each input constraint.
- Example:
// Change the RHS of a 2x2x2 array of constraints GRBConstr[][][] constrs = new GRBConstr[2][2][2]; // ... double[][][] newvals = {{{1.0, 2.0}, {3.0, 4.0}}, {{5.0, 6.0}, {7.0, 8.0}}}; model.set(GRB.DoubleAttr.RHS, constrs, newvals);
- void set(GRB.DoubleAttr attr, GRBQConstr[] qconstrs, double[] newvals)#
Set a double-valued quadratic constraint attribute for an array of quadratic constraints.
- Arguments:
attr – The attribute being modified.
qconstrs – The quadratic constraints whose attribute values are being modified.
newvals – The desired new values for the attribute for each input quadratic constraint.
- Example:
// Add 3 quadratic constraints GRBQConstr c1 = model.addQConstr(x*x + y*y == 0, "qc1"); GRBQConstr c2 = model.addQConstr(x*y - y*y == 0, "qc2"); GRBQConstr c3 = model.addQConstr(x*x - x*y == 0, "qc3"); GRBQConstr[] qconstrs = {c1, c2, c3}; // Change the RHS values of the quadratic constraints double[] newvals = {1.0, 2.0, 3.0}; model.set(GRB.DoubleAttr.QCRHS, qconstrs, newvals);
- void set(GRB.DoubleAttr attr, GRBQConstr[] qconstrs, double[] newvals, int start, int len)#
Set a double-valued quadratic constraint attribute for a sub-array of quadratic constraints.
- Arguments:
attr – The attribute being modified.
qconstrs – A one-dimensional array of quadratic constraints whose attribute values are being modified.
newvals – The desired new values for the attribute for each input quadratic constraint.
start – The first quadratic constraint of interest in the list.
len – The number of quadratic constraints.
- Example:
// Add 3 quadratic constraints GRBQConstr c1 = model.addQConstr(x*x + y*y == 0, "qc1"); GRBQConstr c2 = model.addQConstr(x*y - y*y == 0, "qc2"); GRBQConstr c3 = model.addQConstr(x*x - x*y == 0, "qc3"); GRBQConstr[] qconstrs = {c1, c2, c3}; // Change the RHS values of the first 2 quadratic constraints double[] newvals = {1.0, 2.0}; model.set(GRB.DoubleAttr.QCRHS, qconstrs, newvals, 0, 2);
- void set(GRB.DoubleAttr attr, GRBQConstr[][] qconstrs, double[][] newvals)#
Set a double-valued quadratic constraint attribute for a two-dimensional array of quadratic constraints.
- Arguments:
attr – The attribute being modified.
qconstrs – A two-dimensional array of quadratic constraints whose attribute values are being modified.
newvals – The desired new values for the attribute for each input quadratic constraint.
- Example:
// Change the RHS of a 2x2 array of quadratic constraints GRBQConstr[][] qconstrs = new GRBQConstr[2][2]; // ... double[][] newvals = {{1.0, 2.0}, {3.0, 4.0}}; model.set(GRB.DoubleAttr.QCRHS, qconstrs, newvals);
- void set(GRB.DoubleAttr attr, GRBQConstr[][][] qconstrs, double[][][] newvals)#
Set a double-valued quadratic constraint attribute for a three-dimensional array of quadratic constraints.
- Arguments:
attr – The attribute being modified.
qconstrs – A three-dimensional array of quadratic constraints whose attribute values are being modified.
newvals – The desired new values for the attribute for each input quadratic constraint.
- Example:
// Change the RHS of a 2x2x2 array of quadratic constraints GRBQConstr[][][] qconstrs = new GRBQConstr[2][2][2]; // ... double[][][] newvals = {{{1.0, 2.0}, {3.0, 4.0}}, {{5.0, 6.0}, {7.0, 8.0}}}; model.set(GRB.DoubleAttr.QCRHS, qconstrs, newvals);
- void set(GRB.IntAttr attr, int newval)#
Set the value of an int-valued model attribute.
- Arguments:
attr – The attribute being modified.
newval – The desired new value for the attribute.
- Example:
// Set the model's sense model.set(GRB.IntAttr.ModelSense, -1);
- void set(GRB.IntAttr attr, GRBVar[] vars, int[] newvals)#
Set an int-valued variable attribute for an array of variables.
- Arguments:
attr – The attribute being modified.
vars – The variables whose attribute values are being modified.
newvals – The desired new values for the attribute for each input variable.
- Example:
// Add 3 binary variables GRBVar[] x = model.addVars(3, GRB.BINARY); // Change the VBasis of the variables int[] newvals = {0, -1, -2}; model.set(GRB.IntAttr.VBasis, x, newvals);
- void set(GRB.IntAttr attr, GRBVar[] vars, int[] newvals, int start, int len)#
Set an int-valued variable attribute for a sub-array of variables.
- Arguments:
attr – The attribute being modified.
vars – A one-dimensional array of variables whose attribute values are being modified.
newvals – The desired new values for the attribute for each input variable.
start – The index of the first variable of interest in the list.
len – The number of variables.
- Example:
// Add 3 binary variables GRBVar[] x = model.addVars(3, GRB.BINARY); // Change the VBasis of the first 2 variables int[] newvals = {0, -1}; model.set(GRB.IntAttr.VBasis, x, newvals, 0, 2);
- void set(GRB.IntAttr attr, GRBVar[][] vars, int[][] newvals)#
Set an int-valued variable attribute for a two-dimensional array of variables.
- Arguments:
attr – The attribute being modified.
vars – A two-dimensional array of variables whose attribute values are being modified.
newvals – The desired new values for the attribute for each input variable.
- Example:
// Set the VBasis for a 2x2 array of variables GRBVar[][] x = new GRBVar[2][2]; // ... int[][] newvals = {{0, -1}, {-2, -3}}; model.set(GRB.IntAttr.VBasis, x, newvals);
- void set(GRB.IntAttr attr, GRBVar[][][] vars, int[][][] newvals)#
Set an int-valued variable attribute for a three-dimensional array of variables.
- Arguments:
attr – The attribute being modified.
vars – A three-dimensional array of variables whose attribute values are being modified.
newvals – The desired new values for the attribute for each input variable.
- Example:
// Set the VBasis for a 2x2x2 array of variables GRBVar[][][] x = new GRBVar[2][2][2]; // ... int[][][] newvals = {{{0, -1}, {0, -1}}, {{0, -1}, {0, -1}}}; model.set(GRB.IntAttr.VBasis, x, newvals);
- void set(GRB.IntAttr attr, GRBConstr[] constrs, int[] newvals)#
Set an int-valued constraint attribute for an array of constraints.
- Arguments:
attr – The attribute being modified.
constrs – The constraints whose attribute values are being modified.
newvals – The desired new values for the attribute for each input constraint.
- Example:
// Add 3 constraints GRBConstr[] constrs = model.addConstrs(3); // Set the CBasis of the constraints int[] newvals = {0, 0, 0}; model.set(GRB.IntAttr.CBasis, constrs, newvals);
- void set(GRB.IntAttr attr, GRBConstr[] constrs, int[] newvals, int start, int len)#
Set an int-valued constraint attribute for a sub-array of constraints.
- Arguments:
attr – The attribute being modified.
constrs – A one-dimensional array of constraints whose attribute values are being modified.
newvals – The desired new values for the attribute for each input constraint.
start – The index of the first constraint of interest in the list.
len – The number of constraints.
- Example:
// Add 3 constraints GRBConstr[] constrs = model.addConstrs(3); // Set the CBasis of the first 2 constraints int[] newvals = {0, -1}; model.set(GRB.IntAttr.CBasis, constrs, newvals, 0, 2);
- void set(GRB.IntAttr attr, GRBConstr[][] constrs, int[][] newvals)#
Set an int-valued constraint attribute for a two-dimensional array of constraints.
- Arguments:
attr – The attribute being modified.
constrs – A two-dimensional array of constraints whose attribute values are being modified.
newvals – The desired new values for the attribute for each input constraint.
- Example:
// Set the CBasis of a 2x2 array of constraints GRBConstr[][] constrs = new GRBConstr[2][2]; // ... int[][] newvals = {{0, -1}, {-1, 0}}; model.set(GRB.IntAttr.CBasis, constrs, newvals);
- void set(GRB.IntAttr attr, GRBConstr[][][] constrs, int[][][] newvals)#
Set an int-valued constraint attribute for a three-dimensional array of constraints.
- Arguments:
attr – The attribute being modified.
constrs – A three-dimensional array of constraints whose attribute values are being modified.
newvals – The desired new values for the attribute for each input constraint.
- Example:
// Set the CBasis of a 2x2x2 array of constraints GRBConstr[][][] constrs = new GRBConstr[2][2][2]; // ... int[][][] newvals = {{{0, -1}, {-1, 0}}, {{0, -1}, {-1, 0}}}; model.set(GRB.IntAttr.CBasis, constrs, newvals);
- void set(GRB.StringAttr attr, String newval)#
Set the value of a String-valued model attribute.
- Arguments:
attr – The attribute being modified.
newval – The desired new value for the attribute.
- Example:
// Set model name model.set(GRB.StringAttr.ModelName, "myModel");
- void set(GRB.StringAttr attr, GRBVar[] vars, String[] newvals)#
Set a String-valued variable attribute for an array of variables.
- Arguments:
attr – The attribute being modified.
vars – The variables whose attribute values are being modified.
newvals – The desired new values for the attribute for each input variable.
- Example:
// Add 3 binary variables GRBVar[] x = model.addVars(3, GRB.BINARY); // Set the variable name attribute String[] newvals = {"x1", "x2", "x3"}; model.set(GRB.StringAttr.VarName, x, newvals);
- void set(GRB.StringAttr attr, GRBVar[] vars, String[] newvals, int start, int len)#
Set a String-valued variable attribute for a sub-array of variables.
- Arguments:
attr – The attribute being modified.
vars – A one-dimensional array of variables whose attribute values are being modified.
newvals – The desired new values for the attribute for each input variable.
start – The index of the first variable of interest in the list.
len – The number of variables.
- Example:
// Add 3 binary variables GRBVar[] x = model.addVars(3, GRB.BINARY); // Set the name of the first 2 variables String[] newvals = {"x1", "x2"}; model.set(GRB.StringAttr.VarName, x, newvals, 0, 2);
- void set(GRB.StringAttr attr, GRBVar[][] vars, String[][] newvals)#
Set a String-valued variable attribute for a two-dimensional array of variables.
- Arguments:
attr – The attribute being modified.
vars – A two-dimensional array of variables whose attribute values are being modified.
newvals – The desired new values for the attribute for each input variable.
- Example:
// Set the variable names for a 2x2 array of variables GRBVar[][] x = new GRBVar[2][2]; // ... String[][] newvals = {{"x1", "x2"}, {"x3", "x4"}}; model.set(GRB.StringAttr.VarName, x, newvals);
- void set(GRB.StringAttr attr, GRBVar[][][] vars, String[][][] newvals)#
Set a String-valued variable attribute for a three-dimensional array of variables.
- Arguments:
attr – The attribute being modified.
vars – A three-dimensional array of variables whose attribute values are being modified.
newvals – The desired new values for the attribute for each input variable.
- Example:
// Set the variable names for the 2x2x2 array of variables GRBVar[][][] x = new GRBVar[2][2][2]; // ... String[][][] newvals = {{{"x1", "x2"}, {"x3", "x4"}}, {{"x5", "x6"}, {"x7", "x8"}}}; model.set(GRB.StringAttr.VarName, x, newvals);
- void set(GRB.StringAttr attr, GRBConstr[] constrs, String[] newvals)#
Set a String-valued constraint attribute for an array of constraints.
- Arguments:
attr – The attribute being modified.
constrs – The constraints whose attribute values are being modified.
newvals – The desired new values for the attribute for each input constraint.
- Example:
// Add 3 constraints GRBConstr[] constrs = model.addConstrs(3); // Set the names of the constraints String[] newvals = {"c1", "c2", "c3"}; model.set(GRB.StringAttr.ConstrName, constrs, newvals);
- void set(GRB.StringAttr attr, GRBConstr[] constrs, String[] newvals, int start, int len)#
Set a String-valued constraint attribute for a sub-array of constraints.
- Arguments:
attr – The attribute being modified.
constrs – A one-dimensional array of constraints whose attribute values are being modified.
newvals – The desired new values for the attribute for each input constraint.
start – The index of the first constraint of interest in the list.
len – The number of constraints.
- Example:
// Add 3 constraints GRBConstr[] constrs = model.addConstrs(3); // Set the names of the first 2 constraints String[] newvals = {"c1", "c2"}; model.set(GRB.StringAttr.ConstrName, constrs, newvals, 0, 2);
- void set(GRB.StringAttr attr, GRBConstr[][] constrs, String[][] newvals)#
Set a String-valued constraint attribute for a two-dimensional array of constraints.
- Arguments:
attr – The attribute being modified.
constrs – A two-dimensional array of constraints whose attribute values are being modified.
newvals – The desired new values for the attribute for each input constraint.
- Example:
// Set the names of a 2x2 array of constraints GRBConstr[][] constrs = new GRBConstr[2][2]; // ... String[][] newvals = {{"c1", "c2"}, {"c3", "c4"}}; model.set(GRB.StringAttr.ConstrName, constrs, newvals);
- void set(GRB.StringAttr attr, GRBConstr[][][] constrs, String[][][] newvals)#
Set a String-valued constraint attribute for a three-dimensional array of constraints.
- Arguments:
attr – The attribute being modified.
constrs – A three-dimensional array of constraints whose attribute values are being modified.
newvals – The desired new values for the attribute for each input constraint.
- Example:
// Set the names of a 2x2x2 array of constraints GRBConstr[][][] constrs = new GRBConstr[2][2][2]; // ... String[][][] newvals = {{{"c1", "c2"}, {"c3", "c4"}}, {{"c5", "c6"}, {"c7", "c8"}}}; model.set(GRB.StringAttr.ConstrName, constrs, newvals);
- void set(GRB.StringAttr attr, GRBQConstr[] qconstrs, String[] newvals)#
Set a String-valued quadratic constraint attribute for an array of quadratic constraints.
- Arguments:
attr – The attribute being modified.
qconstrs – The quadratic constraints whose attribute values are being modified.
newvals – The desired new values for the attribute for each input quadratic constraint.
- Example:
// Add 3 quadratic constraints GRBQConstr c1 = model.addQConstr(x*x + y*y == 0, "qc1"); GRBQConstr c2 = model.addQConstr(x*y - y*y == 0, "qc2"); GRBQConstr c3 = model.addQConstr(x*x - x*y == 0, "qc3"); GRBQConstr[] qconstrs = {c1, c2, c3}; // Set the names of the quadratic constraints String[] newvals = {"qc1", "qc2", "qc3"}; model.set(GRB.StringAttr.QCName, qconstrs, newvals);
- void set(GRB.StringAttr attr, GRBQConstr[] qconstrs, String[] newvals, int start, int len)#
Set a String-valued quadratic constraint attribute for a sub-array of quadratic constraints.
- Arguments:
attr – The attribute being modified.
qconstrs – A one-dimensional array of quadratic constraints whose attribute values are being modified.
newvals – The desired new values for the attribute for each input quadratic constraint.
start – The index of the first quadratic constraint of interest in the list.
len – The number of quadratic constraints.
- Example:
// Add 3 quadratic constraints GRBQConstr c1 = model.addQConstr(x*x + y*y == 0, "qc1"); GRBQConstr c2 = model.addQConstr(x*y - y*y == 0, "qc2"); GRBQConstr c3 = model.addQConstr(x*x - x*y == 0, "qc3"); GRBQConstr[] qconstrs = {c1, c2, c3}; // Set the names of the first 2 quadratic constraints String[] newvals = {"qc1", "qc2"}; model.set(GRB.StringAttr.QCName, qconstrs, newvals, 0, 2);
- void set(GRB.StringAttr attr, GRBQConstr[][] qconstrs, String[][] newvals)#
Set a String-valued quadratic constraint attribute for a two-dimensional array of quadratic constraints.
- Arguments:
attr – The attribute being modified.
qconstrs – A two-dimensional array of quadratic constraints whose attribute values are being modified.
newvals – The desired new values for the attribute for each input quadratic constraint.
- Example:
// Set the names of a 2x2 array of quadratic constraints GRBQConstr[][] qconstrs = new GRBQConstr[2][2]; // ... String[][] newvals = {{"qc1", "qc2"}, {"qc3", "qc4"}}; model.set(GRB.StringAttr.QCName, qconstrs, newvals);
- void set(GRB.StringAttr attr, GRBQConstr[][][] qconstrs, String[][][] newvals)#
Set a String-valued quadratic constraint attribute for a three-dimensional array of quadratic constraints.
- Arguments:
attr – The attribute being modified.
qconstrs – A three-dimensional array of quadratic constraints whose attribute values are being modified.
newvals – The desired new values for the attribute for each input quadratic constraint.
- Example:
// Set the names of a 2x2x2 array of quadratic constraints GRBQConstr[][][] qconstrs = new GRBQConstr[2][2][2]; // ... String[][][] newvals = {{{"qc1", "qc2"}, {"qc3", "qc4"}}, {{"qc5", "qc6"}, {"qc7", "qc8"}}}; model.set(GRB.StringAttr.QCName, qconstrs, newvals);
- void set(GRB.StringAttr attr, GRBGenConstr[] genconstrs, String[] newvals)#
Set a String-valued general constraint attribute for an array of general constraints.
- Arguments:
attr – The attribute being modified.
genconstrs – The general constraints whose attribute values are being modified.
newvals – The desired new values for the attribute for each input general constraint.
- Example:
// Add 3 general constraints GRBGenConstr[] genconstrs = model.addGenConstrs(3); // Set the names of the general constraints String[] newvals = {"gc1", "gc2", "gc3"}; model.set(GRB.StringAttr.GenConstrName, genconstrs, newvals);
- void set(GRB.StringAttr attr, GRBGenConstr[] genconstrs, String[] newvals, int start, int len)#
Set a String-valued general constraint attribute for a sub-array of general constraints.
- Arguments:
attr – The attribute being modified.
genconstrs – A one-dimensional array of general constraints whose attribute values are being modified.
newvals – The desired new values for the attribute for each input general constraint.
start – The index of the first general constraint of interest in the list.
len – The number of general constraints.
- Example:
// Add 3 general constraints GRBGenConstr[] genconstrs = model.addGenConstrs(3); // Set the names of the first 2 general constraints String[] newvals = {"gc1", "gc2"}; model.set(GRB.StringAttr.GenConstrName, genconstrs, newvals, 0, 2);
- void set(GRB.StringAttr attr, GRBGenConstr[][] genconstrs, String[][] newvals)#
Set a String-valued general constraint attribute for a two-dimensional array of general constraints.
- Arguments:
attr – The attribute being modified.
genconstrs – A two-dimensional array of general constraints whose attribute values are being modified.
newvals – The desired new values for the attribute for each input general constraint.
- Example:
// Set the names of a 2x2 array of general constraints GRBGenConstr[][] genconstrs = new GRBGenConstr[2][2]; // ... String[][] newvals = {{"gc1", "gc2"}, {"gc3", "gc4"}}; model.set(GRB.StringAttr.GenConstrName, genconstrs, newvals);
- void set(GRB.StringAttr attr, GRBGenConstr[][][] genconstrs, String[][][] newvals)#
Set a String-valued general constraint attribute for a three-dimensional array of general constraints.
- Arguments:
attr – The attribute being modified.
genconstrs – A three-dimensional array of general constraints whose attribute values are being modified.
newvals – The desired new values for the attribute for each input general constraint.
- Example:
// Set the names of a 2x2x2 array (genconstrs) of general constraints GRBGenConstr[][][] genconstrs = new GRBGenConstr[2][2][2]; // ... String[][][] newvals = {{{"gc1", "gc2"}, {"gc3", "gc4"}}, {{"gc5", "gc6"}, {"gc7", "gc8"}}}; model.set(GRB.StringAttr.GenConstrName, genconstrs, newvals);
- void setLogCallback(java.util.function.Consumer<String> logCallback)#
Sets a logging callback function to query all output posted by the model object. Can be set after a model was created.
- Arguments:
logCallback – The logging callback function.
- Example:
// Set a simple logging callback that prints logs model.setLogCallback(log -> System.out.println("Log: " + log));
- void setObjective(GRBExpr expr, int sense)#
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.
- Arguments:
expr – New model objective.
sense – New optimization sense (
GRB.MINIMIZE
for minimization,GRB.MAXIMIZE
for maximization).
- Example:
// Set objective and sense model.setObjective(expression, GRB.MINIMIZE);
- void setObjective(GRBExpr expr)#
Set the model objective equal to quadratic expression (for multi-objective optimization, see
setObjectiveN
). The sense of the objective is determined by the value of the ModelSense attribute.Note that this method replaces the entire existing objective, while the Obj attribute can be used to modify individual linear terms.
- Arguments:
expr – New model objective.
- Example:
// Set objective without specifying sense (use default) model.setObjective(expr);
- void setObjectiveN(GRBLinExpr expr, int index, int priority, double weight, double abstol, double reltol, 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.
- Arguments:
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.
- Example:
// Primary objective: expr1 model.setObjectiveN(expr1, 0, 2, 1.0, 2.0, 0.1, "PrimaryObj"); // Secondary objective: expr2 model.setObjectiveN(expr2, 1, 1, 1.0, 0.0, 0.0, "SecondaryObj");
- void setPWLObj(GRBVar var, 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.
Set the piecewise-linear objective function for a variable.
- Arguments:
var – The variable whose objective function is being set.
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.
- Example:
GRBVar v = model.addVar(0.0, 1.0, 0.0, GRB.CONTINUOUS, "var"); double[] xVals = {0.0, 0.5, 1.0}; double[] yVals = {0.5, 0.25, 0.75}; model.setPWLObj(v, xVals, yVals);
- 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.
- Return value:
Model for a single scenario.
- Example:
model.set(GRB.IntAttr.NumScenarios, 1); model.update(); GRBModel singleScenario = model.singleScenarioModel();
- 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. Thesync
call forces the calling program to wait until the asynchronous optimization call completes. You must callsync
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 thatoptimize
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.- Example:
model.optimizeasync(); // ... model.sync();
- 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
.- Example:
model.terminate();
- 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 in the Reference Manual for details on the tuning tool.
- Example:
model.tune(); // Get best tuning result model.getTuneResult(0);
- void update()#
Process any pending model modifications.
- Example:
model.update();
- void write(String filename)#
This method is the general entry point for writing optimization data to a file. It can be used to write optimization models, solution 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.
Finally, note that when IgnoreNames=1, the names of variables and constraints are replaced with default names when writing a file.
- Arguments:
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 (seeGRBModel.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
orzip
for Windows, andgzip
,bzip2
, orunzip
for Linux or macOS), then the files can be compressed, so additional suffixes of.zip
,.gz
,.bz2
,.7z
or.xz
are accepted.- Example:
model.write("myModel.lp");