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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

With what you have learned so far, you can implement a method that creates such a model.static void buildModelByRow(IloModelerDataIloNumVar[]int nFoods = data.nFoods;int nNutrs = data.nNutrs;model,data,Buy,IloNumVarType type)throws IloException {for (int j = 0; j < nFoods; j++) {Buy[j] = model.numVar(data.foodMin[j], data.foodMax[j], type);}model.addMinimize(model.scalProd(data.foodCost, Buy));for (int i = 0; i < nNutrs; i++) {model.addRange(data.nutrMin[i],model.scalProd(data.nutrPerFood[i], Buy),data.nutrMax[i]);}}The function receives several parameters. The parameter model is used for two things:◆creating other modeling objects, and◆ representing the model being created.The argument data contains the data for the model to be built. The argument Buy is anarray, initialized to length data.nFoods, containing the model's variables. Finally,parameter type is used to specify the type of the variables being created.The function starts by creating the modeling variables, one by one, and storing them in arrayBuy. Each variable j is initialized to have bounds data.foodMin[j] anddata.foodMax[j] and to be of type type.The variables are first used to construct the objective function expression with methodmodel.scalProd(foodCost, Buy). This expression is immediately used to create theminimization objective which is directly added to the active model by addMinimize.In the loop that follows, the nutritional constraints are added. For each nutrient i theexpression representing the amount of nutrient in a diet with food levels Buy is computedusing model.scalProd(nutrPerFood[i], Buy). This amount of nutrient must bewithin the ranged constraint bounds nutrMin[i] and nutrMax[i]. This constraint iscreated and added to the active model with addRange.Note that function buildModelByRow uses interface IloModeler rather than IloCplex.This allows the function to be called without change in another implementation ofIloModeler, such as IloSolver.80 <strong>ILOG</strong> <strong>CPLEX</strong> <strong>11.0</strong> — USER’ S MANUAL

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

Saved successfully!

Ooh no, something went wrong!