Step 12: Create variables

Shows lines to create variables in a model built by columns.

Go to the comment Step 12 in Dietlesson.cs, and add the following lines to create each of the variables.


      for (int j = 0; j < nFoods; j++) {

         Column col = model.Column(cost, data.foodCost[j]);

         for (int i = 0; i < nNutrs; i++) {
            col = col.And(model.Column(constraint[i], 
                                       data.nutrPerFood[i][j]));
         }

         Buy[j] = model.NumVar(col, data.foodMin[j], data.foodMax[j], type);

      }
   }

For each food j, a column object col is first created to represent how the new variable for that food is to be added to the objective function and constraints. Then that column object is used to construct the variable Buy[j] that represents the amount of food j to be purchased for the diet. At this time, the new variable will be installed in the objective function and constraints as defined by the column object col.