ILOG CPLEX 10.1 Getting Started > Tutorials > Concert Technology Tutorial for C++ Users > Reading a Problem from a File: Example ilolpex2.cpp

This example shows how to read an optimization problem from a file, and solve it with a specified optimizer option. It prints solution information, including a Simplex basis, if available. Finally it prints the maximum infeasibility of any variable of the solution.

The file to read and the optimizer choice are passed to the program via command line parameters. For example, this command:

ilolpex2 example.mps d

reads the file example.mps and solves the problem with the dual simplex optimizer.

Example ilolpex2 demonstrates:

The general structure of this example is the same as for example ilolpex1.cpp. It starts by creating the environment and terminates with destroying it by calling the end method. The code in between is enclosed in try/catch statements for error handling.

Reading the Model from a File

The model is created by reading it from the file specified as the first command line argument argv[1]. This is done using the method importModel of an IloCplex object. Here the IloCplex object is used as a model reader rather than an optimizer. Calling importModel does not extract the model to the invoking cplex object. This must be done later by a call to cplex.extract(model). The objects obj, var, and rng are passed to importModel so that later on when results are queried the variables will be accessible.

Selecting the Optimizer

The selection of the optimizer option is done in the switch statement controlled by the second command line parameter. A call to setParam(IloCplex::RootAlg, alg) selects the desired IloCplex::Algorithm option.

Accessing Basis Information

After solving the model by calling the method solve, the results are accessed in the same way as in ilolpex1.cpp, with the exception of basis information for the variables. It is important to understand that not all optimizer options compute basis information, and thus it cannot be queried in all cases. In particular, basis information is not available when the model is solved using the barrier optimizer (IloCplex::Barrier) without crossover (parameter IloCplex::BarCrossAlg set to IloCplex::NoAlg).

Querying Quality Measures

Finally, the program prints the maximum primal infeasibility or bound violation of the solution. To cope with the finite precision of the numerical computations done on the computer, IloCplex allows some tolerances by which (for instance) optimality conditions may be violated. A long list of other quality measures is available.

Complete Program

You can view the complete program online in the standard distribution of the product at yourCPLEXinstallation/examples/src/ilolpex2.cpp.