ILOG CPLEX 11.0 User's Manual > Languages and APIs > ILOG Concert Technology for Java Users > Example: Optimizing the Diet Problem in Java

The problem solved in this example is to minimize the cost of a diet that satisfies certain nutritional constraints. You might also want to compare this approach through the Java API of ILOG CPLEX with similar applications in other programming languages:

This example was chosen because it is simple enough to be viewed from a row as well as from a column perspective. Both ways are shown in the example. In this example, either perspective can be viewed as natural. Only one approach will seem natural for many models, but there is no general way of deciding which is more appropriate (rows or columns) in a particular case.

The example accepts a filename and two options -c and -i as command line arguments. Option -i allows you to create a MIP model where the quantities of foods to purchase must be integers. Option -c can be used to build the model by columns.

The example starts by evaluating the command line arguments and reading the input data file. The input data of the diet problem is read from a file using an object of the embedded class Diet.Data. Its constructor requires a file name as an argument. Using the class InputDataReader, it reads the data from that file. This class is distributed with the examples, but will not be considered here as it does not use ILOG CPLEX or Concert Technology in any special way.

Once the data has been read, the IloCplex modeler/optimizer is created.

IloCplex     cplex = new IloCplex();
IloNumVar[]  Buy   = new IloNumVar[nFoods];
 
if ( byColumn ) buildModelByColumn(cplex, data, Buy, varType);
   else buildModelByRow (cplex, data, Buy, varType);
 

Array IloNumVar[] Buy is also created where the modeling variables will be stored by buildModelByRow or buildModelByColumn.

You have already seen a method very similar to buildModelByRow. This function is called when byColumn is false, which is the case when the example is executed without the -c command line option; otherwise, buildModelByColumn is called. Note that unlike buildModelByRow, this method requires IloMPModeler rather than IloModeler as parameter since modeling by column is not available with IloModeler.

First, the function creates an empty minimization objective and empty ranged constraints, and adds them to the active model.

 IloObjective cost       = model.addMinimize();
    IloRange[]   constraint = new IloRange[nNutrs];
  
    for (int i = 0; i < nNutrs; i++) {
       constraint[i] = model.addRange(data.nutrMin[i], data.nutrMax[i]);
    }
 

Empty means that they use a 0 expression. After that the variables are created one by one, and installed in the objective and constraints modeling by column. For each variable, a column object must be created. Start by creating a column object for the objective by calling:

 IloColumn col = model.column(cost, data.foodCost[j]);
 

The column is then expanded to include the coefficients for all the constraints using col.and with the column objects that are created for each constraint, as in the following loop:

 for (int i = 0; i < nNutrs; i++) {
        col = col.and(model.column(constraint[i], data.nutrPerFood[i][j]));
      }
 

When the full column object has been constructed it is finally used to create and install the new variable using:

Buy[j] = model.numVar(col, data.foodMin[j], data.foodMax[j], type);

Once the model is created, solving it and querying the solution is straightforward. What remains to be pointed out is the exception handling. In case of an error, ILOG CPLEX will throw an exception of type IloException or one of its subclasses. Thus the entire ILOG CPLEX program is enclosed in try/catch statements. The InputDataReader can throw exceptions of type java.io.IOException or InputDataReader.InputDataReaderException.

Since none of these three possible exceptions is handled elsewhere, the main function ends by catching them and issuing appropriate error messages.

The call to the method cplex.end frees the memory that ILOG CPLEX uses.

The entire source code listing for the example is available as Diet.java in the standard distribution at yourCPLEXinstallation/examples/src.