首页 > 其他 > 详细

Pulp之四:官网上的应用样例

时间:2020-09-29 00:36:32      阅读:47      评论:0      收藏:0      [点我收藏+]

例1:配料分配的问题
  有家公司要生产猫粮,猫粮的配料有chicken, beef, mutton,rice, wheat,gel。它们的成本分别是$0.013, $0.008,$0.010,$0.002, $0.005, $0.001
为了满足营养标准,每100g成品必须至少有8gProtein,6gfat,但是不超过2g的fibre以及0.4g的salt。下面是营养成分表。

Stuff Protein Fat Fibre Salt
Chicken 0.100 0.080 0.001 0.002
Beef 0.200 0.100 0.005 0.005
Rice 0.000 0.010 0.100 0.002
Wheat bran 0.040 0.010 0.150 0.008

define:
x1:100g猫粮中chicken的含量
x2:100g猫粮中beef的含量
x3:100g猫粮中mutton的含量
x4:100g猫粮中rice的含量
x5:100g猫粮中wheat的含量
x6:100g猫粮中gel的含量

objective:
min(0.013x1+0.008x2+0.01x3+0.002x4+0.005x5+0.001x6)

s.t.
x1,x2,x3,x4,x5,x6 >= 0
0.100x1+0.200x2+0.150x3+0.000x4+0.040x5+0.000x6 >= 8.0
0.080x1+0.100x2+0.110x3+0.010x4+0.010x5+0.000x6 >= 6.0
0.001x1+0.005x2+0.003x3+0.100x4+0.150x5+0.000x6 <= 2.0
0.002x1+0.005x2+0.007x3+0.002x4+0.008x5+0.000x6 <= 2.0

开始编程:

from pulp import *

Creates a list of the Ingredients

Ingredients = [‘CHICKEN‘, ‘BEEF‘, ‘MUTTON‘, ‘RICE‘, ‘WHEAT‘, ‘GEL‘]

A dictionary of the costs of each of the Ingredients is created

costs = {‘CHICKEN‘: 0.013,
‘BEEF‘: 0.008,
‘MUTTON‘: 0.010,
‘RICE‘: 0.002,
‘WHEAT‘: 0.005,
‘GEL‘: 0.001}

A dictionary of the protein percent in each of the Ingredients is created

proteinPercent = {‘CHICKEN‘: 0.100,
‘BEEF‘: 0.200,
‘MUTTON‘: 0.150,
‘RICE‘: 0.000,
‘WHEAT‘: 0.040,
‘GEL‘: 0.000}

A dictionary of the fat percent in each of the Ingredients is created

fatPercent = {‘CHICKEN‘: 0.080,
‘BEEF‘: 0.100,
‘MUTTON‘: 0.110,
‘RICE‘: 0.010,
‘WHEAT‘: 0.010,
‘GEL‘: 0.000}

A dictionary of the fibre percent in each of the Ingredients is created

fibrePercent = {‘CHICKEN‘: 0.001,
‘BEEF‘: 0.005,
‘MUTTON‘: 0.003,
‘RICE‘: 0.100,
‘WHEAT‘: 0.150,
‘GEL‘: 0.000}

A dictionary of the salt percent in each of the Ingredients is created

saltPercent = {‘CHICKEN‘: 0.002,
‘BEEF‘: 0.005,
‘MUTTON‘: 0.007,
‘RICE‘: 0.002,
‘WHEAT‘: 0.008,
‘GEL‘: 0.000}

创建问题实例,求最小极值

prob = LpProblem("The Whiskas Problem", LpMinimize)

构建Lp变量字典,变量名以Ingr开头,如Ingr_CHICKEN,下界是0

ingredient_vars = LpVariable.dicts("Ingr",Ingredients,0)

添加目标方程

prob += lpSum([costs[i]*ingredient_vars[i] for i in Ingredients])

添加约束条件

prob += lpSum([ingredient_vars[i] for i in Ingredients]) == 100
prob += lpSum([proteinPercent[i] * ingredient_vars[i] for i in Ingredients]) >= 8.0
prob += lpSum([fatPercent[i] * ingredient_vars[i] for i in Ingredients]) >= 6.0
prob += lpSum([fibrePercent[i] * ingredient_vars[i] for i in Ingredients]) <= 2.0
prob += lpSum([saltPercent[i] * ingredient_vars[i] for i in Ingredients]) <= 0.4

求解

prob.solve()

查看解的状态

print("Status:", LpStatus[prob.status])

查看解

for v in prob.variables():
print(v.name, "=", v.varValue)

另外一种查看解的方式

for i in Ingredients:

print(ingredient_vars[i],"=",ingredient_vars[i].value())

技术分享图片

Pulp之四:官网上的应用样例

原文:https://www.cnblogs.com/treasury-manager/p/13747594.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!