金融量化学习---Python, MySQL, Pandas

这里用来记录一些在金融领域,尤其是银行相关的资金、债券、票据中应用到的数据管理与分析, 编程等心得或笔记,以及个人的一点小小兴趣(易经八卦、藏密禅修)等

导航

Pulp之三:官网上的应用样例(1)-The_Whiskas_Problem (猫粮配料比例问题)

https://github.com/benalexkeen/Introduction-to-linear-programming

例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:(这里一瓶猫粮是100g)

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

目标函数:
min(0.013*x1+0.008*x2+0.01*x3+0.002*x4+0.005*x5+0.001*x6)

约束条件:we need to have at least 8g protein, 6g fat per 100g, and no more than 2g fibre, 0.4 salt

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

开始编程:

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变量字典,键名是Ingredients,值(变量名)以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())
The_Whiskas_Problem:
MINIMIZE
      0.008*Ingr_BEEF + 0.013*Ingr_CHICKEN + 0.001*Ingr_GEL + 0.01*Ingr_MUTTON + 0.002*Ingr_RICE + 0.005*Ingr_WHEAT + 0.0
SUBJECT TO
_C1: Ingr_BEEF + Ingr_CHICKEN + Ingr_GEL + Ingr_MUTTON + Ingr_RICE + Ingr_WHEAT = 100
_C2: 0.2 Ingr_BEEF + 0.1 Ingr_CHICKEN + 0.15 Ingr_MUTTON + 0.04 Ingr_WHEAT >= 8
_C3: 0.1 Ingr_BEEF + 0.08 Ingr_CHICKEN + 0.11 Ingr_MUTTON + 0.01 Ingr_RICE + 0.01 Ingr_WHEAT >= 6
_C4: 0.005 Ingr_BEEF + 0.001 Ingr_CHICKEN + 0.003 Ingr_MUTTON + 0.1 Ingr_RICE + 0.15 Ingr_WHEAT <= 2
_C5: 0.005 Ingr_BEEF + 0.002 Ingr_CHICKEN + 0.007 Ingr_MUTTON + 0.002 Ingr_RICE + 0.008 Ingr_WHEAT <= 0.4

VARIABLES
Ingr_BEEF Continuous
Ingr_CHICKEN Continuous
Ingr_GEL Continuous
Ingr_MUTTON Continuous
Ingr_RICE Continuous
Ingr_WHEAT Continuous

posted on 2020-09-28 23:58  chengjon  阅读(257)  评论(0编辑  收藏  举报