API Usage#
Gurobi provides APIs for developing optimization applications in C, C++, Java, .NET (covering both C# and Visual Basic), Python, MATLAB, and R. Each API is implemented as a wrapper around the core Gurobi C API. This allows you to interact with the Optimizer in your preferred language with minimal overhead.
API calls are handled through one or more environments and models. The Optimizer’s behaviour is controlled through parameters and data is queried through attributes. This section briefly illustates these concepts, with links to further information on each one. For a complete, equivalent example in each language, see the MIP1 example.
Note
The code snippets on this page are not complete. They are intended to illustrate the key steps required to build and solve a model using Gurobi. In particular, they perform no error handling or cleanup.
An Environment delineates a Gurobi session in which one or more models can be formulated, solved, and analyzed. The Environments section covers the range of uses of environments in Gurobi applications in more detail. Note that the R and MATLAB APIs do not give the user direct control of environments.
#include "gurobi_c.h"
GRBenv *env = NULL;
error = GRBloadenv(&env);
if (error) goto QUIT;
#include "gurobi_c++.h"
GRBEnv *env = new GRBEnv();
using Gurobi;
GRBEnv env = new GRBEnv();
import gurobi.*;
GRBenv env = new GRBenv();
% Environments are not managed by the user in MATLAB
import gurobipy as gp
from gurobipy import GRB
env = gp.Env()
library(gurobi)
# Environments are not managed by the user in R
The Model is a central data structure capturing all information associated with a single instance of your optimization problem. The model captures the variables, constraints, and objective function of the problem as described in Modeling Components.
GRBmodel *model = NULL;
error = GRBnewmodel(env, &model, "mip1", 0, NULL, NULL, NULL, NULL, NULL);
if (error) goto QUIT;
double obj[3];
obj[0] = 1; obj[1] = 1; obj[2] = 2;
vtype[0] = GRB_BINARY; vtype[1] = GRB_BINARY; vtype[2] = GRB_BINARY;
error = GRBaddvars(model, 3, 0, NULL, NULL, NULL, obj, NULL, NULL, vtype,
NULL);
if (error) goto QUIT;
error = GRBsetintattr(model, GRB_INT_ATTR_MODELSENSE, GRB_MAXIMIZE);
if (error) goto QUIT;
int ind[3];
double val[3];
ind[0] = 0; ind[1] = 1; ind[2] = 2;
val[0] = 1; val[1] = 2; val[2] = 3;
error = GRBaddconstr(model, 3, ind, val, GRB_LESS_EQUAL, 4.0, "c0");
if (error) goto QUIT;
GRBModel model = GRBModel(env);
GRBVar x = model.addVar(0.0, 1.0, 0.0, GRB_BINARY, "x");
GRBVar y = model.addVar(0.0, 1.0, 0.0, GRB_BINARY, "y");
GRBVar z = model.addVar(0.0, 1.0, 0.0, GRB_BINARY, "z");
model.setObjective(x + y + 2 * z, GRB_MAXIMIZE);
model.addConstr(x + 2 * y + 3 * z <= 4, "c0");
GRBModel model = new GRBModel(env);
GRBVar x = model.AddVar(0.0, 1.0, 0.0, GRB.BINARY, "x");
GRBVar y = model.AddVar(0.0, 1.0, 0.0, GRB.BINARY, "y");
GRBVar z = model.AddVar(0.0, 1.0, 0.0, GRB.BINARY, "z");
model.SetObjective(x + y + 2 * z, GRB.MAXIMIZE);
model.AddConstr(x + 2 * y + 3 * z <= 4.0, "c0");
GRBModel model = new GRBModel(env);
GRBVar x = model.addVar(0.0, 1.0, 0.0, GRB.BINARY, "x");
GRBVar y = model.addVar(0.0, 1.0, 0.0, GRB.BINARY, "y");
GRBVar z = model.addVar(0.0, 1.0, 0.0, GRB.BINARY, "z");
GRBLinExpr expr = new GRBLinExpr();
expr.addTerm(1.0, x); expr.addTerm(1.0, y); expr.addTerm(2.0, z);
model.setObjective(expr, GRB.MAXIMIZE);
expr = new GRBLinExpr();
expr.addTerm(1.0, x); expr.addTerm(2.0, y); expr.addTerm(3.0, z);
model.addConstr(expr, GRB.LESS_EQUAL, 4.0, "c0");
names = {'x'; 'y'; 'z'};
model.A = sparse([1 2 3; 1 1 0]);
model.obj = [1 1 2];
model.rhs = [4; 1];
model.sense = '<>';
model.vtype = 'B';
model.modelsense = 'max';
model.varnames = names;
model = gp.Model(env=env)
x = model.addVar(vtype=GRB.BINARY, name="x")
y = model.addVar(vtype=GRB.BINARY, name="y")
z = model.addVar(vtype=GRB.BINARY, name="z")
model.setObjective(x + y + 2 * z, GRB.MAXIMIZE)
model.addConstr(x + 2 * y + 3 * z <= 4, "c0")
model <- list()
model$A <- matrix(c(1,2,3,1,1,0), nrow=2, ncol=3, byrow=T)
model$obj <- c(1,1,2)
model$modelsense <- 'max'
model$rhs <- c(4,1)
model$sense <- c('<', '>')
model$vtype <- 'B'
Parameters are used to tune the behaviour of Gurobi’s algorithms, set solution quality requirements, and set termination criteria. They also control advanced features, logging output, client-server connections, and licensing. For example, you can set the TimeLimit parameter before calling the appropriate API routine to solve a model.
error = GRBsetdblparam(GRBgetenv(model), GRB_DBL_PAR_TIMELIMIT, 10.0);
if (error) goto QUIT;
error = GRBoptimize(model);
if (error) goto QUIT;
model->set(GRB_DoubleParam_TimeLimit, 10.0);
model.optimize()
model.Parameters.TimeLimit = 10.0;
model.optimize()
model.set(GRB.DoubleParam.TimeLimit, 10.0);
model.optimize()
params.TimeLimit = 10.0
result = gurobi(model, params);
model.Params.TimeLimit = 10.0
model.optimize()
params <- list(TimeLimit=10.0)
result <- gurobi(model, params)
Attributes are used to query both model data and solutions, as well as to modify models. For example, the X attribute is used to query the value of a variable in the solution to the model, after it has been solved.
double sol[3];
error = GRBgetdblattrarray(model, GRB_DBL_ATTR_X, 0, 3, sol);
if (error) goto QUIT;
cout << x.get(GRB_StringAttr_VarName) << " "
<< x.get(GRB_DoubleAttr_X) << endl;
cout << y.get(GRB_StringAttr_VarName) << " "
<< y.get(GRB_DoubleAttr_X) << endl;
cout << z.get(GRB_StringAttr_VarName) << " "
<< z.get(GRB_DoubleAttr_X) << endl;
Console.WriteLine(x.VarName + " " + x.X);
Console.WriteLine(y.VarName + " " + y.X);
Console.WriteLine(z.VarName + " " + z.X);
System.out.println(x.get(GRB.StringAttr.VarName)
+ " " +x.get(GRB.DoubleAttr.X));
System.out.println(y.get(GRB.StringAttr.VarName)
+ " " +y.get(GRB.DoubleAttr.X));
System.out.println(z.get(GRB.StringAttr.VarName)
+ " " +z.get(GRB.DoubleAttr.X));
for v=1:length(names)
fprintf('%s %d\n', names{v}, result.x(v));
end
print(f"x = {x.X}")
print(f"y = {y.X}")
print(f"z = {z.X}")
print(result$x)