The optimization problem solved in this example is to compose a diet from a set of foods, so that the nutritional requirements are satisfied and the total cost is minimized. Example diet.cpp illustrates:
diet.cpp
The problem contains a set of foods, which are the modeling variables; a set of nutritional requirements to be satisfied, which are the constraints; and an objective of minimizing the total cost of the food. There are two ways of looking at this problem:
Concert Technology is equally suited for both kinds of modeling; in fact, you can even mix both approaches in the same program. If a new food product is created, you can create a new variable for it regardless of how the model was originally built. Similarly, if a new nutrient is discovered, you can add a new constraint for it.
You walk into the store and compile a list of foods that are offered. For each food, you store the price per unit and the amount in stock. For some foods that you particularly like, you also set a minimum amount you would like to use in your diet. Then, for each of the foods, you create a modeling variable to represent the quantity to be purchased for your diet.
Now you get a medical book and look up which nutrients are known and relevant for you. For each nutrient, you note the minimum and maximum amounts that should be found in your diet. Also, you go through the list of foods and determine how much a food item will contribute for each nutrient. This gives you one constraint per nutrient, which can naturally be represented as a range constraint:
nutrMin[i] <= sum_j (nutrPer[i][j] * Buy[j]) <= nutrMax[i]
where i represents the number of the nutrient under consideration, nutrMin[i] and nutrMax[i] the minimum and maximum amount of nutrient i and nutrPer[i][j] the amount of nutrient i in food j. Finally, you specify your objective function:
i
nutrMin[i]
nutrMax[i]
nutrPer[i][j]
j
minimize sum_j (cost[j] * Buy[j])
This way of creating the model is shown in the function buildModelByRow, in example ilodiet.cpp.
buildModelByRow
ilodiet.cpp
You start with the medical book where you compile the list of nutrients that you want to ensure are properly represented in your diet. For each of the nutrients, you create an empty constraint:
nutrMin[i] ... nutrMax[i]
...
n
utrMax[i]
where ... is left to be filled once you walk into the store. Also, you set up the objective function to minimize the cost. We refer to constraint i as range[i] and to the objective as cost.
range[i]
cost
Now you walk into the store and, for each food, you check the price and nutritional content. With this data you create a variable representing the amount you want to buy of the food type and install it in the objective function and constraints. That is, you create the following column:
cost(foodCost[j]) "+" "sum_i" (range[i](nutrPer[i][j]))
where the notation "+" and "sum" indicate that you "add" the new variable j to the objective cost and constraints range[i]. The value in parentheses is the linear coefficient that is used for the new variable. We chose this notation for its similarity to the syntax actually used in Concert Technology, as demonstrated in the function buildModelByColumn, in example ilodiet.cpp.
+
sum
buildModelByColumn
All data defining the problem are read from a file. The nutrients per food are stored in a two-dimensional array. Concert Technology does not provide a predefined array class; however, by using the template class IloArray, you can create your own two-dimensional array class. This class is defined with the type definition:
IloArray
typedef IloArray<IloNumArray> IloNumArray2;
typedef IloArray<IloNumArray> IloNumArray2
and is then ready to use, just like any predefined Concert Technology class, for example IloNumArray, the one-dimensional array class for numerical data.
IloNumArray