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.

../_images/api-light.svg../_images/api-dark.svg

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;

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;

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;

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;