zoukankan      html  css  js  c++  java
  • 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
    

  • 相关阅读:
    【原创】主机不能访问虚拟机CentOS7中的站点
    phpStudy中MySQL版本升级到5.7.17方法
    phpStudy for Linux (lnmp+lamp一键安装包)
    Linux的wget命令详解【转载】
    重置密码解决MySQL for Linux错误 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
    mysql5.7密码过期ERROR 1862 (HY000): Your password has expired. To log in you must change
    电赛菜鸟营培训(二)——STM32F103CB之中断控制
    电赛菜鸟营培训(零)——Keil环境搭建
    电赛菜鸟营培训(一)——STM32F103CB之LED控制
    AppInventor学习笔记(四)——打地鼠应用学习
  • 原文地址:https://www.cnblogs.com/treasury-manager/p/13747594.html
Copyright © 2011-2022 走看看