> Languages and APIs > The ILOG CPLEX Callable Library > Example: Optimizing the Diet Problem in the Callable Library

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.c illustrates these points:

Problem Representation

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 to look at this problem:

The diet problem 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 introduced, you can create a new variable for it, regardless of how the model was originally built. Similarly, is a new nutrient is discovered, you can add a new constraint for it.

Creating a Model Row by Row

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 they have 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 amount 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] 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 to minimize, like this:

cost = j (cost[j] * buy[j])

This way to create the model is shown in function populatebyrow in example diet.c.

Creating a Model Column by Column

You start with the medical book where you compile the list of nutrients that you want to make sure are properly represented in your diet. For each of the nutrients, you create an empty constraint:

nutrmin[i] ... nutrmax[i]

where ... is left to be filled once you walk into your store. You also set up the objective function to minimize the cost. Constraint i is referred to as rng[i] and the objective is referred to as cost.

Now you walk into the store and, for each food, you check its 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" (rng[i](nutrper[i][j]))

where the notation "+" and "sum" indicates that you "add" the new variable j to the objective cost and constraints rng[i]. The value in parentheses is the linear coefficient that is used for the new variable).