dietmodel.py


dietmodel.py


# !/usr/bin/env python3.7 #版权所有2021,Gurobi优化狗万app足彩,LLC #解决经典饮食模型。这个文件实现了#一个制定和解决模型的函数,但它不包含模型数据。数据由调用程序传入#。运行示例' die2 .py'、# ' die3 .py'或' die4 .py'来调用这个函数。从gurobipy import GRB def solve(category, minNutrition, maxNutrition, foods, cost, nutritionValues):# Model m = gp.Model("diet") #为要购买的食物创建决策变量buy = m.d addvars (foods, name="buy") #目标是最小化成本m.setobject (buy.prod(cost), GRB.MINIMIZE) #营养约束m.d pors ((gp. prod))quicksum(nutritionvalue [f, c] * buy[f] for f in foods) == [minNutrition[c], maxNutrition[c] for c in categories), "_") def printSolution(): if m.s status == GRB。print('\nCost: %g' % m.ObjVal) print('\nBuy:') for f in foods: if buy[f]。x> 0.0001: print('%s %g' % (f, buy[f].X)) else: print('No solution') # Solve m.h ootsolution () print('\nAdding constraint: at most 6 servings of dairy') m.d ootconstr (buy. X) else: print('No solution') # Solve m.h ootsolution ()sum(['milk', 'ice cream']) <= 6, "limit_dairy") # Solve .optimize() printSolution()