当lowBound设置为零时,为什么PulP为混合问题返回负值?

阿卜杜勒纳西尔·格扎维

我正在使用Python PulP解决混合问题。输出很奇怪,它返回负值。我想做什么


from pulp import *
import pandas as pd 

data = pd.read_csv('feed.csv')

# Get required data from user
Ingredients = ['CORN', 'SBM', 'LIMESTONE', 'SALT', 'BONE_MEAL', 'PALM_OIL']


def cost():
    costs = {}
    for i in Ingredients:
        costs[i] = data.loc[data['feed'] == i, 'cost'].iloc[0]
    return costs

def protein():
    proteinPercent = {}
    for i in Ingredients:
        proteinPercent[i] = data.loc[data['feed'] == i, 'protein'].iloc[0]
    return proteinPercent

def energy():
    energyKcal = {}
    for i in Ingredients:
        energyKcal[i] = data.loc[data['feed'] == i, 'energy'].iloc[0]
    return energyKcal


def calcium():
    cal = {}
    for i in Ingredients:
        cal[i] = data.loc[data['feed'] == i, 'calcium'].iloc[0]
    return cal

def sodium():
    sod = {}
    for i in Ingredients:
        sod[i] = data.loc[data['feed'] == i, 'sodium'].iloc[0]
    return sod

def minimum():
    min = {}
    for i in Ingredients:
        min[i] = data.loc[data['feed'] == i, 'min'].iloc[0]
    return min

def maximum():
    max = {}
    for i in Ingredients:
        max[i] = data.loc[data['feed'] == i, 'max'].iloc[0]
    return max

# Initialize
costs = cost()
proteinPercent = protein()
energyKcal = energy()
cal = calcium()
sod = sodium()
L = minimum()
U = maximum()

# Create the 'prob' variable to contain the problem data
prob = LpProblem("Broiler Ration Formulation", LpMinimize)

# A dictionary called 'ingredient_vars' is created to contain the referenced Variables
ingredient_vars = LpVariable.dicts("Ingr",Ingredients, lowBound = 1, upBound = 60)

for i in Ingredients:
    prob += ingredient_vars[i] >= L[i]
    prob += ingredient_vars[i] <= U[i]


# 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 five constraints are added to 'prob'
prob += lpSum([ingredient_vars[i] for i in Ingredients]) == 100, "Percentages Sum"
prob += lpSum([proteinPercent[i] * ingredient_vars[i] for i in Ingredients]) >= 19.0, "Min Protein Requirement"
prob += lpSum([proteinPercent[i] * ingredient_vars[i] for i in Ingredients]) <= 21.0, "Max Protein Requirement"
prob += lpSum([energyKcal[i] * ingredient_vars[i] for i in Ingredients]) >= 3100.0, "Min Energy Requirements"
prob += lpSum([energyKcal[i] * ingredient_vars[i] for i in Ingredients]) <= 3300.0, "Max Energy Requirements"
prob += lpSum([cal[i] * ingredient_vars[i] for i in Ingredients]) <= 1.1, "Calcium Requirements"
prob += lpSum([sod[i] * ingredient_vars[i] for i in Ingredients]) <= 0.2, "Sodium Requirements"


# The problem is solved using PuLP's choice of Solver
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 feed= ", value(prob.objective))
File feed.csv content
feed    min max cost    energy  protein calcium sodium
BONE_MEAL   0   10  0.7 1600    0   30.32   1
CORN    0   60  0.5 3350    9.4 0.05    0
.
.
.

一些输出变量是负数:

Status: Infeasible

Ingr_BONE_MEAL = 1.0

Ingr_CORN = 37.862563

Ingr_FISH_MEAL = 1.0

Ingr_LIMESTONE = -0.81506256

Ingr_PALM_OIL = 60.0

Ingr_SALT = -0.0475

Ingr_SBM = 1.0

Total Cost of Ingredients per Feed =  32.988153372

我搜索了可能的解决方案,但找不到任何解释,或者我的代码错误,但无法弄清楚到底出了什么问题。

比拉·纳西姆

添加到LpProblem实例中的第一件事始终是目标函数,如PuLP文档中所述您当前在代码中的顺序为:

for i in Ingredients:
   prob += ingredient_vars[i] >= L[i]
   prob += ingredient_vars[i] <= U[i]


# 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"

您的问题实例的目标功能是ingredient_vars[i] >= L[i]荒谬的。

正确的方法是在约束之前添加目标函数部分,如下所示:

# 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"

for i in Ingredients:
   prob += ingredient_vars[i] >= L[i]
   prob += ingredient_vars[i] <= U[i]

调试线性程序的一个好方法是使用prob.writeLP('blendingprob')写出显示实际线性程序的.lp文件方法。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

为什么当减数为零时在减法期间设置进位标志?

来自分类Dev

当填充设置为零时,为什么<ul>开箱即用?

来自分类Dev

当我将暗淡的长度设置为零时,为什么不会引起错误?

来自分类Dev

为什么当减数为零时在减法期间设置进位标志?

来自分类Dev

渲染WebGL ...属性设置为零时特定于Windows的问题?

来自分类Dev

当GLEW_ARB_framebuffer_object不为零时,为什么glFramebufferTexture为NULL指针?

来自分类Dev

针对特定网站的Rvest“ if(is_http){:参数长度为零时出错”-为什么?

来自分类Dev

当Debug设置为零时,Heroku上的CakePHP错误

来自分类Dev

仅当静态属性为零时如何设置它?

来自分类Dev

当值为零时,WebAPI不为INT返回JSON

来自分类Dev

当边距和填充为零时,为什么这些p元素之间有空格?

来自分类Dev

为什么设置后UITabBarControllerDelegate为零?

来自分类Dev

为什么LSTM的导数设置为零

来自分类Dev

如果值为零时条件解析为真

来自分类Dev

当UIView高度设置为零时,视图的子控件不会隐藏

来自分类Dev

将数组中的所有值设置为零时出错

来自分类Dev

上下文为零时,Swift Temp CoreData对象无法设置ivars

来自分类Dev

仅在设置为零时才反应渲染 zindex 样式

来自分类Dev

角度数据绑定,当值为零时返回NaN

来自分类Dev

当值为零时,WebAPI不会为INT返回JSON

来自分类Dev

赋值为零时,通过从块中返回值来分配变量

来自分类Dev

NSDate为零时,NSDateFormatter返回(空)字符串

来自分类Dev

当将函数的返回值乘以零时,为什么不短路?

来自分类Dev

当值为零时,Matlab线图与轴重叠

来自分类Dev

比较浮点数为零时的奇怪行为

来自分类Dev

当amCharts中的值为零时显示图形标签

来自分类Dev

倒计时为零时效果不佳

来自分类Dev

总和为零时如何删除pdf文件

来自分类Dev

在selectionStart突出显示为零时代码中断

Related 相关文章

  1. 1

    为什么当减数为零时在减法期间设置进位标志?

  2. 2

    当填充设置为零时,为什么<ul>开箱即用?

  3. 3

    当我将暗淡的长度设置为零时,为什么不会引起错误?

  4. 4

    为什么当减数为零时在减法期间设置进位标志?

  5. 5

    渲染WebGL ...属性设置为零时特定于Windows的问题?

  6. 6

    当GLEW_ARB_framebuffer_object不为零时,为什么glFramebufferTexture为NULL指针?

  7. 7

    针对特定网站的Rvest“ if(is_http){:参数长度为零时出错”-为什么?

  8. 8

    当Debug设置为零时,Heroku上的CakePHP错误

  9. 9

    仅当静态属性为零时如何设置它?

  10. 10

    当值为零时,WebAPI不为INT返回JSON

  11. 11

    当边距和填充为零时,为什么这些p元素之间有空格?

  12. 12

    为什么设置后UITabBarControllerDelegate为零?

  13. 13

    为什么LSTM的导数设置为零

  14. 14

    如果值为零时条件解析为真

  15. 15

    当UIView高度设置为零时,视图的子控件不会隐藏

  16. 16

    将数组中的所有值设置为零时出错

  17. 17

    上下文为零时,Swift Temp CoreData对象无法设置ivars

  18. 18

    仅在设置为零时才反应渲染 zindex 样式

  19. 19

    角度数据绑定,当值为零时返回NaN

  20. 20

    当值为零时,WebAPI不会为INT返回JSON

  21. 21

    赋值为零时,通过从块中返回值来分配变量

  22. 22

    NSDate为零时,NSDateFormatter返回(空)字符串

  23. 23

    当将函数的返回值乘以零时,为什么不短路?

  24. 24

    当值为零时,Matlab线图与轴重叠

  25. 25

    比较浮点数为零时的奇怪行为

  26. 26

    当amCharts中的值为零时显示图形标签

  27. 27

    倒计时为零时效果不佳

  28. 28

    总和为零时如何删除pdf文件

  29. 29

    在selectionStart突出显示为零时代码中断

热门标签

归档