diet_vb.vb
“版权所有,2021,Gurobi优狗万app足彩化,LLC”解决经典饮食模型,展示如何添加约束到现有模型。尝试“营养指南,基于美国农业部膳食指南,2005年”http://www.health.gov/DietaryGuidelines/dga2005/ Dim Categories As String() = New String(){"卡路里","蛋白质","脂肪",_ "钠"}Dim nCategories As Integer = Categories。Length Dim minNutrition As Double() = New Double() {1800, 91, 0,0} Dim maxNutrition As Double() = New Double() {2200, GRB。集合食品Dim foods As String() = New String(){"汉堡包","鸡肉","热狗",_ "炸薯条","通心粉","披萨",_ "沙拉","牛奶","冰淇淋"}Dim nFoods As Integer = foods。长度的成本双()= New双(){r 2.49, 2.89, 1.5, 1.89, 2.09, 1.99, 2.49 _, 0.89, 1.59}“食品营养值”汉堡'鸡'热狗“炸薯条”通心粉“披萨”沙拉“牛奶”冰淇淋昏暗nutritionValues双新双(,)(,)={{730}410,24岁,26日,_{420年,32岁,10,1190}_{560、20、1800},_{380、4、19日_{320, 12, 10, 930}, _{320, 15, 12, 820}, _{320, 31, 12, 1230}, _{100, 8, 2.5, 125}, _{330, 8, 10, 180} '模型模糊env作为新的GRBModel(env)模型。ModelName = "饮食"的营养信息创建决策变量,我们限制通过边界的营养GRBVar () = New GRBVar (nCategories - 1){}我作为nCategories Integer = 0 - 1营养(i) = model.AddVar (minNutrition(我),maxNutrition(我),0,_伽马线暴。接下来“为购买的食品创建决策变量”“注意:对于每个决策变量,我们在创建变量时添加了客观系数”。if (j) = 0 To nFoods - 1 buy(j) = model. if (j) = 0 To nFoods - 1 buy(j) = model. if (j) = 0 To nFoods - 1 buy(j) = model. if (j) = 0;AddVar(0,伽马射线爆发。无穷,成本(j),伽马线暴。连续的,_食品(j))下一个“目标是最小化成本”注:目标系数是在创建“上述决策变量”期间设置的。模型。ModelSense = GRB.MINIMIZE ' Nutrition constraints For i As Integer = 0 To nCategories - 1 Dim ntot As GRBLinExpr = 0 For j As Integer = 0 To nFoods - 1 ntot.AddTerm(nutritionValues(j, i), buy(j)) Next model.AddConstr(ntot = nutrition(i), Categories(i)) Next ' Solve model.Optimize() PrintSolution(model, buy, nutrition) Console.WriteLine(vbLf & "Adding constraint: at most 6 servings of dairy") model.AddConstr(buy(7) + buy(8) <= 6, "limit_dairy") ' Solve model.Optimize() PrintSolution(model, buy, nutrition) ' Dispose of model and env model.Dispose() env.Dispose() Catch e As GRBException Console.WriteLine("Error code: " & e.ErrorCode & ". " & e.Message) End Try End Sub Private Shared Sub PrintSolution(ByVal model As GRBModel, ByVal buy As GRBVar(), _ ByVal nutrition As GRBVar()) If model.Status = GRB.Status.OPTIMAL Then Console.WriteLine(vbLf & "Cost: " & model.ObjVal) Console.WriteLine(vbLf & "Buy:") For j As Integer = 0 To buy.Length - 1 If buy(j).X > 0.0001 Then Console.WriteLine(buy(j).VarName & " " & buy(j).X) End If Next Console.WriteLine(vbLf & "Nutrition:") For i As Integer = 0 To nutrition.Length - 1 Console.WriteLine(nutrition(i).VarName & " " & nutrition(i).X) Next Else Console.WriteLine("No solution") End If End Sub End Class