代码之家  ›  专栏  ›  技术社区  ›  Luis Ramon Ramirez Rodriguez

变量总和在混合问题上不匹配

  •  0
  • Luis Ramon Ramirez Rodriguez  · 技术社区  · 7 年前

    我试图复制这种混合 example 但使用较少的变量,这部分工作正常:

    import pulp
    from pulp import *
    
    # Creates a list of the Ingredients
    Ingredients = ['CHICKEN', 'BEEF', 'MUTTON', 'RICE']
    
    # A dictionary of the costs of each of the Ingredients is created
    costs = {'CHICKEN': 15, 
             'BEEF': 12, 
             'MUTTON': 17, 
             'RICE': 12
            }
    
    # A dictionary of the protein percent in each of the Ingredients is created
    proteinPercent = {'CHICKEN': 17, 
                      'BEEF': 2, 
                      'MUTTON': 16, 
                      'RICE': 8
                     }
    
    # A dictionary of the fat percent in each of the Ingredients is created
    fatPercent = {'CHICKEN': 10, 
                  'BEEF': 14, 
                  'MUTTON': 13, 
                  'RICE': 16, 
                  }
    
    # Create the 'prob' variable to contain the problem data
    prob = LpProblem("The Whiskas Problem", LpMinimize)
    
    # A dictionary called 'ingredient_vars' is created to contain the referenced Variables
    ingredient_vars = LpVariable.dicts("Ingr",Ingredients,0)
    
    # The objective function is added to 'prob' first
    prob += lpSum([costs[i]*ingredient_vars[i] for i in Ingredients]), "Total Cost of Ingredients per can"
    
    # The  constraints are added to 'prob'
    prob += lpSum([proteinPercent[i] * ingredient_vars[i] for i in Ingredients]) >= 15.5, "ProteinRequirement"
    prob += lpSum([fatPercent[i] * ingredient_vars[i] for i in Ingredients]) >= 12.3, "FatRequirement"
    
    
    prob.writeLP("WhiskasModel.lp")
    prob.solve()
    # The status of the solution is printed to the screen
    print ("Status:", LpStatus[prob.status])
    
    # Each of the variables is printed with it's resolved optimum value
    for v in prob.variables():
        print (v.name, "=", v.varValue)
    
    # The optimised objective function value is printed to the screen
    print ("Total Cost of Ingredients per can = ", value(prob.objective))
    

    它计算每种成分所需的最佳量:

    Status: Optimal
    Ingr_BEEF = 0.0
    Ingr_CHICKEN = 0.77916667
    Ingr_MUTTON = 0.0
    Ingr_RICE = 0.28177083
    Total Cost of Ingredients per can =  15.068750009999999
    

    但是,当我在代码中添加这方面的约束条件时,这并没有达到100%:

    prob += lpSum([ingredient_vars[i] for i in Ingredients]) == 100, "PercentagesSum"
    

    我得到这个结果:

    Status: Optimal
    Ingr_BEEF = 100.0
    Ingr_CHICKEN = 0.0
    Ingr_MUTTON = 0.0
    Ingr_RICE = 0.0
    Total Cost of Ingredients per can =  1200.0
    

    这是错误的,因为它不符合其他合同。

    编辑

    看来我把这个解释错了,我想: 如果我想产生3个单位,输入的总和应该是3。

    我想是这样的:

    # The constraints are added to 'prob'
    prob += lpSum([ingredient_vars[i] for i in Ingredients]) == 3, "PercentagesSum"
    prob += lpSum(ingredient_vars["CHICKEN"]) <= 2, "CHICKEN"
    prob += lpSum(ingredient_vars["BEEF"]) <= 1, "BEEF"
    prob += lpSum(ingredient_vars["MUTTON"]) <= 1, "MUTTON"
    prob += lpSum(ingredient_vars["RICE"]) <= 1, "RICE"
    

    其中2,1,1,1是每种原材料的可用数量。

    1 回复  |  直到 7 年前
        1
  •  2
  •   kabdulla    7 年前

    这是错误的,因为它不符合其他合同。[原文如此]

    违反了哪个约束?如果查看如何定义约束,您会发现它们都得到了满足。

    prob += lpSum([proteinPercent[i] * ingredient_vars[i] for i in Ingredients]) >= 15.5, "ProteinRequirement"
    

    如果溶液中含有100单位的牛肉,意味着你有2*100=200单位的蛋白质,远远超过了所需的15.5单位。

    prob += lpSum([fatPercent[i] * ingredient_vars[i] for i in Ingredients]) >= 12.3, "FatRequirement"
    

    这就意味着你有14*100=1400单位的蛋白质,远远超过了所需的12.3单位。

    真正的问题是,我认为你把这些单位弄糊涂了。当乘以百分比时,需要除以100。

    推荐文章