12.07.2015 Views

ILOG CPLEX 11.0 User's Manual

ILOG CPLEX 11.0 User's Manual

ILOG CPLEX 11.0 User's Manual

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

The example starts by evaluating the command line arguments and reading the input datafile. The input data of the diet problem is read from a file using an object of the embeddedclass Diet.Data. Its constructor requires a file name as an argument. Using the classInputDataReader, it reads the data from that file. This class is distributed with theexamples, but will not be considered here as it does not use <strong>ILOG</strong> <strong>CPLEX</strong> or ConcertTechnology 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 bybuildModelByRow or buildModelByColumn.You have already seen a method very similar to buildModelByRow. This function is calledwhen byColumn is false, which is the case when the example is executed without the -ccommand line option; otherwise, buildModelByColumn is called. Note that unlikebuildModelByRow, this method requires IloMPModeler rather than IloModeler asparameter 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, acolumn 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 usingcol.and with the column objects that are created for each constraint, as in the followingloop: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 thenew variable using:Buy[j] = model.numVar(col, data.foodMin[j], data.foodMax[j], type);<strong>ILOG</strong> <strong>CPLEX</strong> <strong>11.0</strong> — USER’ S MANUAL 93

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!