Load and solve a model from a file#

One of the most basic tasks you can perform with the Gurobi libraries is to read a model from a file, optimize it, and report the result. The lp example is a simple illustration of how this is done in the various supported Gurobi languages. While the specifics vary from one language to another, the basic structure remains the same for all languages.

After initializing the Gurobi environment, the examples begin by reading the model from the specified file.

error = GRBreadmodel(env, argv[1], &model);
if (error) goto QUIT;

The next step is to invoke the Gurobi Optimizer on the model.

error = GRBoptimize(model);
if (error) goto QUIT;

A successful optimize call populates a set of solution attributes in the model. For example, once the call completes, the X variable attribute contains the solution value for each variable. Similarly, for continuous models, the Pi constraint attribute contains the dual value for each constraint.

The examples then retrieve the value of the model Status attribute to determine the result of the optimization. In the lp example, an optimal solution is written to a solution file (model.sol).

There are many other things you can do once you have read and solved the model. For example, lp example checks the solution status – which is highly recommended. If the model is found to be infeasible, this example computes an Irreducible Inconsistent Subsystem (IIS) to isolate the source of the infeasibility.