Optimizing the model
Now that the model has been built, the next step is to optimize it:
/* Optimize model */ 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...
/* Capture solution information */ 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 may 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 = GRBgetdblattr(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 = GRBgetdblattrarray(model, GRB_DBL_ATTR_X, 0, 3, sol); if (error) goto QUIT;
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)