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.

method IloModel::add returns the modeling object as an IloExtractable, whichcannot be assigned to a variable of a derived class such as IloObjective. Similarly, anarray of range constraints with 0 (zero) expressions is created, added to the model, andstored in the array range.In the following loop, the variables of the model are created one by one in columns; thus, thenew variables are immediately installed in the model. An IloNumColumn object col iscreated and initialized to define how each new variable will be appended to the existingobjective and constraints.The IloNumColumn object col is initialized to contain the objective coefficient for the newvariable. This is created with cost(foodCost[j]), that is using the overloadedoperator() for IloObjective. Next, an IloNumColumn object is created for everyconstraint, representing the coefficient the new variable has in that constraint. Again theseIloNumColumn objects are created with the overloaded operator(), this time ofIloRange. The IloNumColumn objects are merged together to an aggregateIloNumColumn object using operator +=. The coefficient for row i is created withrange[i](nutrPer[i][j]), which calls the overloaded operator() for IloRangeobjects.When a column is completely constructed, a new variable is created for it and added to thearray of modeling variables Buy. The construction of the variable is performed by theconstructor:IloNumVar(col, foodMin[j], foodMax[j], type)which creates the new variable with lower bound foodMin[j], upper bound foodMax[j]and type type, and adds it to the existing objective and ranges with the coefficients specifiedin column col. After creating the variable for this column, the IloColumn object is deletedby calling col.end.Solving the Model with IloCplexAfter the model has been populated, it is time to create the cplex object and extract themodel to it by calling:IloCplex cplex(mod);It is then ready to solve the model, but for demonstration purposes the extracted model willfirst be written to the file diet.lp. Doing so can help you debug your model, as the filecontains exactly what <strong>ILOG</strong> <strong>CPLEX</strong> sees. If it does not match what you expected, it willprobably help you locate the code that generated the wrong part.The model is then solved by calling method solve. Finally, the solution status and solutionvector are output to the output channel cplex.out. By default this channel is initialized tocout. All logging during optimization is also output to this channel. To turn off logging, youwould set the out stream of cplex to a null stream by callingcplex.setOut(env.getNullStream()).<strong>ILOG</strong> <strong>CPLEX</strong> <strong>11.0</strong> — USER’ S MANUAL 69

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

Saved successfully!

Ooh no, something went wrong!