ILOG CPLEX 11.0 Getting Started > Tutorials > Callable Library Tutorial > Reading a Problem from a File: Example lpex2.c

The previous example, lpex1.c, shows a way to copy problem data into a problem object as part of an application that calls routines from the ILOG CPLEX Callable Library. Frequently, however, a file already exists containing a linear programming problem in the industry standard MPS format, the ILOG CPLEX LP format, or the ILOG CPLEX binary SAV format. In example lpex2.c, ILOG CPLEX file-reading and optimization routines read such a file to solve the problem.

Example lpex2.c uses command line arguments to specify the name of the input file and the optimizer to call.

Usage: lpex2 filename optimizer

Where: filename is a file with extension MPS, SAV, or LP (lower case is allowed), and optimizer is one of the following letters:

default 
primal simplex 
dual simplex 
network with dual simplex cleanup 
barrier with crossover 
barrier without crossover 
sifting 
concurrent 

For example, this command:

lpex2 example.mps d

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

To illustrate the ease of reading a problem, the example uses the routine CPXreadcopyprob. This routine detects the type of the file, reads the file, and copies the data into the ILOG CPLEX problem object that is created with a call to CPXcreateprob. The user need not be concerned with the memory management of the data. Memory management is handled transparently by CPXreadcopyprob.

After calling CPXopenCPLEX and turning on the screen indicator by setting the CPX_PARAM_SCRIND parameter to CPX_ON, the example creates an empty problem object with a call to CPXcreateprob. This call returns a pointer, lp, to the new problem object. Then the data is read in by the routine CPXreadcopyprob. After the data is copied, the appropriate optimization routine is called, based on the command line argument.

After optimization, a call to CPXgetstat retrieves the status of the solution . The cases of infeasibility or unboundedness in the model are handled in a simple fashion here; a more complex application program might treat these cases in more detail. With these two cases out of the way, the program then calls CPXsolninfo to examine the nature of the solution. Certain that a solution exists, the application then calls CPXgetobjval to obtain the objective function value for this solution and report it.

Next, preparations are made to print the solution value and basis status of each individual variable, by allocating arrays of appropriate size; these sizes are detected by calls to the routines CPXgetnumcols and CPXgetnumrows. Note that a basis is not guaranteed to exist, depending on which optimizer was selected at run time, so some of these steps, including the call to CPXgetbase, are dependent on the solution type returned by CPXsolninfo.

The primal solution values of the variables are obtained by a call to CPXgetx, and then these values (along with the basis statuses if available) are printed, in a loop, for each variable. After that, a call to CPXgetdblquality provides a measure of the numerical roundoff error present in the solution, by obtaining the maximum amount by which any variable's lower or upper bound is violated.

After the TERMINATE: label, the data for the solution (x, cstat, and rstat) are freed. Then the problem object is freed by CPXfreeprob. After the problem is freed, the ILOG CPLEX environment is freed by CPXcloseCPLEX.

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