Program Description

All definitions needed for a ILOG CPLEX Callable Library program are imported by including file <ilcplex/cplex.h> at the beginning of the program. After a number of lines that establish the calling sequences for the routines that are to be used, the program's main() function begins by checking for correct command line arguments, printing a usage reminder and exiting in case of errors.

Next, the data defining the problem are read from a file specified in the command line at run time. The details of this are handled in the routine readdata(). In this file, cost, lower bound, and upper bound are specified for each type of food; then minimum and maximum levels of several nutrients needed in the diet are specified; finally, a table giving levels of each nutrient found in each unit of food is given. The result of a successful call to this routine is two variables nfoods and nnutr containing the number of foods and nutrients in the data file, arrays cost, lb, ub containing the information on the foods, arrays nutrmin, nutrmax containing nutritional requirements for the proposed diet, and array nutrper containing the nutritional value of the foods.

Preparations to build and solve the model with ILOG CPLEX begin with the call to CPXopenCPLEX(). This establishes an ILOG CPLEX environment to contain the LP problem, and succeeds only if a valid ILOG CPLEX license is found.

After calls to set parameters, one to control the output that comes to the user's terminal, and another to turn on data checking for debugging purposes, a problem object is initialized through the call to CPXcreateprob(). This call returns a pointer to an empty problem object, which now can be populated with data.

Two alternative approaches to filling this problem object are implemented in this program, populatebyrow() and populatebycolumn(), and which one is executed is determined at run time by a calling parameter on the command line. The routine populatebyrow() operates by first defining all the columns through a call to CPXnewcols() and then repeatedly calls CPXaddrows() to enter the data of the constraints. The routine populatebycolumn() takes the complementary approach of establishing all the rows first with a call to CPXnewrows() and then sequentially adds the column data by calls to CPXaddcols().

Solving the Model with CPXlpopt()

The model is at this point ready to be solved, and this is accomplished through the call to CPXlpopt(), which by default uses the dual simplex optimizer.

After this, the program finishes by making a call to CPXsolution() to obtain the values for each variable in this optimal solution, printing these values, and writing the problem to a disk file (for possible evaluation by the user) via the call to CPXwriteprob(). It then terminates after freeing all the arrays that have been allocated along the way.


Previous Page: Example: Dietary Optimization  Return to Top Next Page: Complete Program