The Linear Programming Solver |
Consider the problem of diet optimization. There are six different foods: bread, milk, cheese, potato, fish, and yogurt. The cost and nutrition values per unit are displayed in Table 8.2.
Table 8.2: Cost and Nutrition ValuesBread | Milk | Cheese | Potato | Fish | Yogurt | |
Cost | 2.0 | 3.5 | 8.0 | 1.5 | 11.0 | 1.0 |
Protein, g | 4.0 | 8.0 | 7.0 | 1.3 | 8.0 | 9.2 |
Fat, g | 1.0 | 5.0 | 9.0 | 0.1 | 7.0 | 1.0 |
Carbohydrates, g | 15.0 | 11.7 | 0.4 | 22.6 | 0.0 | 17.0 |
Calories | 90 | 120 | 106 | 97 | 130 | 180 |
The following SAS code creates the data set fooddata of Table 8.2:
data fooddata; infile datalines; input name $ cost prot fat carb cal; datalines; Bread 2 4 1 15 90 Milk 3.5 8 5 11.7 120 Cheese 8 7 9 0.4 106 Potato 1.5 1.3 0.1 22.6 97 Fish 11 8 7 0 130 Yogurt 1 9.2 1 17 180 ; run;
The objective is to find a minimum-cost diet that contains at least 300 calories, not more than 10 grams of protein, not less than 10 grams of carbohydrates, and not less than 8 grams of fat. In addition, the diet should contain at least 0.5 unit of fish and no more than 1 unit of milk.
You can model the problem and solve it by using PROC OPTMODEL as follows:
proc optmodel; /* declare index set */ set<str> FOOD; /* declare variables */ var diet{FOOD} >= 0; /* objective function */ num cost{FOOD}; min f=sum{i in FOOD}cost[i]*diet[i]; /* constraints */ num prot{FOOD}; num fat{FOOD}; num carb{FOOD}; num cal{FOOD}; num min_cal, max_prot, min_carb, min_fat; con cal_con: sum{i in FOOD}cal[i]*diet[i] >= 300; con prot_con: sum{i in FOOD}prot[i]*diet[i] <= 10; con carb_con: sum{i in FOOD}carb[i]*diet[i] >= 10; con fat_con: sum{i in FOOD}fat[i]*diet[i] >= 8; /* read parameters */ read data fooddata into FOOD=[name] cost prot fat carb cal; /* bounds on variables */ diet['Fish'].lb = 0.5; diet['Milk'].ub = 1.0; /* solve and print the optimal solution */ solve with lp/printfreq=1; /* print each iteration to log */ print diet;
The optimal solution and the optimal objective value are displayed in Output 8.1.1.
Output 8.1.1: Optimal Solution to the Diet Problem
Copyright © 2008 by SAS Institute Inc., Cary, NC, USA. All rights reserved.