diet_cs.cs


/ * 2023年版权,Gurobi优狗万app足彩化,LLC * / / *解决经典的饮食模式,显示如何将约束添加到现有的模型。* /使用系统;使用Gurobi;类diet_cs{静态void Main(){尝试{/ /营养指南,根据农业部/ /为美国人设计的膳食指南,2005 / / http://www.health.gov/DietaryGuidelines/dga2005/ string[]类别= new string[]{“热量”、“蛋白质”、“胖”、“钠”};int nCategories = Categories.Length;双新双[][]minNutrition = {1800、91、0、0};双新双[][]maxNutrition ={2200,伽马线暴。无穷,65年,1779};/ /组食物string [] = new string[]{“汉堡包”、“鸡”、“热狗”、“薯条”,“通心粉”、“披萨”,“沙拉”,“牛奶”,“冰淇淋”};int nFoods = Foods.Length;双新双[][]成本= {2.49,2.89,1.50,1.89,2.09,1.99,2.49,0.89,1.59}; // Nutrition values for the foods double[,] nutritionValues = new double[,] { { 410, 24, 26, 730 }, // hamburger { 420, 32, 10, 1190 }, // chicken { 560, 20, 32, 1800 }, // hot dog { 380, 4, 19, 270 }, // fries { 320, 12, 10, 930 }, // macaroni { 320, 15, 12, 820 }, // pizza { 320, 31, 12, 1230 }, // salad { 100, 8, 2.5, 125 }, // milk { 330, 8, 10, 180 } // ice cream }; // Model GRBEnv env = new GRBEnv(); GRBModel model = new GRBModel(env); model.ModelName = "diet"; // Create decision variables for the nutrition information, // which we limit via bounds GRBVar[] nutrition = new GRBVar[nCategories]; for (int i = 0; i < nCategories; ++i) { nutrition[i] = model.AddVar(minNutrition[i], maxNutrition[i], 0, GRB.CONTINUOUS, Categories[i]); } // Create decision variables for the foods to buy // // Note: For each decision variable we add the objective coefficient // with the creation of the variable. GRBVar[] buy = new GRBVar[nFoods]; for (int j = 0; j < nFoods; ++j) { buy[j] = model.AddVar(0, GRB.INFINITY, cost[j], GRB.CONTINUOUS, Foods[j]); } // The objective is to minimize the costs // // Note: The objective coefficients are set during the creation of // the decision variables above. model.ModelSense = GRB.MINIMIZE; // Nutrition constraints for (int i = 0; i < nCategories; ++i) { GRBLinExpr ntot = 0.0; for (int j = 0; j < nFoods; ++j) ntot.AddTerm(nutritionValues[j,i], buy[j]); model.AddConstr(ntot == nutrition[i], Categories[i]); } // Solve model.Optimize(); PrintSolution(model, buy, nutrition); Console.WriteLine("\nAdding constraint: at most 6 servings of dairy"); model.AddConstr(buy[7] + buy[8] <= 6.0, "limit_dairy"); // Solve model.Optimize(); PrintSolution(model, buy, nutrition); // Dispose of model and env model.Dispose(); env.Dispose(); } catch (GRBException e) { Console.WriteLine("Error code: " + e.ErrorCode + ". " + e.Message); } } private static void PrintSolution(GRBModel model, GRBVar[] buy, GRBVar[] nutrition) { if (model.Status == GRB.Status.OPTIMAL) { Console.WriteLine("\nCost: " + model.ObjVal); Console.WriteLine("\nBuy:"); for (int j = 0; j < buy.Length; ++j) { if (buy[j].X > 0.0001) { Console.WriteLine(buy[j].VarName + " " + buy[j].X); } } Console.WriteLine("\nNutrition:"); for (int i = 0; i < nutrition.Length; ++i) { Console.WriteLine(nutrition[i].VarName + " " + nutrition[i].X); } } else { Console.WriteLine("No solution"); } } }