Optimizing the model

Now that the model has been built, the next step is to optimize it:

  error = GRBoptimize(model);
  if (error) goto QUIT;
This routine performs the optimization and populates several internal model attributes, including the status of the optimization, the solution, etc. Once the function returns, we can query the values of these attributes. In particular, we can query the status of the optimization process by retrieving the value of the Status attribute...
  error = GRBgetintattr(model, GRB_INT_ATTR_STATUS, &optimstatus);
  if (error) goto QUIT;
The optimization status has many possible values. An optimal solution to the model may have been found, or the model have been determined to be infeasible or unbounded, or the solution process may have been interrupted. A list of possible statuses can be found in the Gurobi Reference Manual. For our example, we know that the model is feasible, and we haven't modified any parameters that might cause the optimization to stop early (e.g., a time limit), so the status will be GRB_OPTIMAL.

Another important model attribute is the value of the objective function for the computed solution. This is accessed through this call:

  error = GRBgetdoubleattr(model, GRB_DBL_ATTR_OBJVAL, &objval);
  if (error) goto QUIT;
Note that this call would return a non-zero error result if no solution was found for this model.

Once we know that the model was solved, we can extract the X attribute of the model, which contains the value for each variable in the computed solution:

  error = GRBgetdoublearrayattr(model, GRB_DBL_ATTR_X, 0, 3, x);
  if (error) goto QUIT;
  printf("  x=%.0f, y=%.0f, z=%.0f", x[0], x[1], x[2]);
This routine retrieves the values of an array-valued attribute. The third and fourth arguments indicate the index of the first array element to be retrieved, and the number of elements to retrieve, respectively. In this example we retrieve entries 0 through 2 (i.e., all three of them)

Try Gurobi for Free

Choose the evaluation license that fits you best, and start working with our Expert Team for technical guidance and support.

Evaluation License
Get a free, full-featured license of the Gurobi Optimizer to experience the performance, support, benchmarking and tuning services we provide as part of our product offering.
Academic License
Gurobi supports the teaching and use of optimization within academic institutions. We offer free, full-featured copies of Gurobi for use in class, and for research.
Cloud Trial

Request free trial hours, so you can see how quickly and easily a model can be solved on the cloud.

Search