使用纸浆进行整数编程时出错:“LpVariable”没有len()

2024-06-01 05:23:01 发布

您现在位置:Python中文网/ 问答频道 /正文

尝试求解the Blending and Mixing problem以使用python进行优化(使用gurobi和palp)。你知道吗

遗憾的是,我遇到了以下错误消息:

python Blending_problem.py 
Traceback (most recent call last):
  File "Blending_problem.py", line 24, in <module>
    LP += calcium_content   == (n_Limestone*0.38    +n_Corn*0.001   +n_Soy*0.002)   /Total_weight   #kg calcium
  File "/home/bruno/.local/lib/python2.7/site-packages/pulp/pulp.py", line 800, in __div__
    if len(other):
TypeError: object of type 'LpVariable' has no len()

有什么问题吗?代码如下:

import pulp
from gurobipy import *
LP = pulp.LpProblem('LP',pulp.LpMinimize)  

Cost=pulp.LpVariable("Cost",lowBound=0,cat=pulp.LpContinuous)

Total_weight=pulp.LpVariable("Total_weight",cat=pulp.LpInteger)

#relative amounts of nutrients
calcium_content=pulp.LpVariable("calcium_content",cat=pulp.LpContinuous,lowBound=0.008,upBound=0.012)
protein_content=pulp.LpVariable("protein_content",cat=pulp.LpContinuous,lowBound=0.22)
fiber_content=pulp.LpVariable("fiber_content",cat=pulp.LpContinuous,upBound=0.05)

#ingredient units
n_Limestone=pulp.LpVariable("n_Limestone",cat=pulp.LpInteger,lowBound=0)
n_Corn=pulp.LpVariable("n_Corn",cat=pulp.LpInteger,lowBound=0)
n_Soy=pulp.LpVariable("n_Soy",cat=pulp.LpInteger,lowBound=0)

#obj
LP += n_Limestone*10 +n_Corn*30.5 +n_Soy*90

LP += Total_weight == n_Limestone+n_Corn+n_Soy

LP += calcium_content   == (n_Limestone*0.38    +n_Corn*0.001   +n_Soy*0.002)   /Total_weight   #kg calcium
LP += protein_content   == (n_Limestone*0       +n_Corn*0.09    +n_Soy*0.5  )   /Total_weight   #kg protein
LP += fiber_content     == (n_Limestone*0       +n_Corn*0.02    +n_Soy*0.08 )   /Total_weight   #kg calcium


status = LP.solve(pulp.solvers.GUROBI(mip=True, msg=True, timeLimit=None,epgap=None))

print( 'LP status: ' + pulp.LpStatus[status] + '')

print(str(n_Limestone.value())+"kg Lime, "+str(n_Corn.value())+"kg Corn, "+str(n_Soy.value())+"kg Soy")

Tags: contentpulpcattotallpweightkgsoy
2条回答

正如kabdulla所说的,这个问题是无法解决的,因为在Lp变量上除法是不可能的。你知道吗

我可以通过删除n的整数约束得到正确的结果。。值,删除total\ weight变量并包含约束1=n\石灰石+n\玉米+n\大豆。生成的脚本如下所示:

import pulp
from gurobipy import *
LP = pulp.LpProblem('LP',pulp.LpMinimize)  

Cost=pulp.LpVariable("Cost",lowBound=0,cat=pulp.LpContinuous)


#relative amounts of nutrients
calcium_content=pulp.LpVariable("calcium_content",cat=pulp.LpContinuous,lowBound=0.008,upBound=0.012)
protein_content=pulp.LpVariable("protein_content",cat=pulp.LpContinuous,lowBound=0.22)
fiber_content=pulp.LpVariable("fiber_content",cat=pulp.LpContinuous,upBound=0.05)

#ingredient units
n_Limestone=pulp.LpVariable("n_Limestone",cat=pulp.LpContinuous,lowBound=0)
n_Corn=pulp.LpVariable("n_Corn",cat=pulp.LpContinuous,lowBound=0)
n_Soy=pulp.LpVariable("n_Soy",cat=pulp.LpContinuous,lowBound=0)
    #obj
LP += n_Limestone*10 +n_Corn*30.5 +n_Soy*90

LP += calcium_content   == (n_Limestone*0.38    +n_Corn*0.001   +n_Soy*0.002)       #kg calcium
LP += protein_content   == (n_Limestone*0       +n_Corn*0.09    +n_Soy*0.5  )       #kg protein
LP += fiber_content     == (n_Limestone*0       +n_Corn*0.02    +n_Soy*0.08 )       #kg calcium
LP += n_Limestone + n_Corn + n_Soy              == 1 


status = LP.solve(pulp.solvers.GUROBI(mip=True, msg=True, timeLimit=None,epgap=None))
print( 'LP status: ' + pulp.LpStatus[status] + '')

print(str(n_Limestone.value())+"kg Lime, "+str(n_Corn.value())+"kg Corn, "+str(n_Soy.value())+"kg Soy")

这不是一个特别有用的错误消息,但至少部分问题是实现的问题不是线性的——在“内容”约束中,您将一个问题变量除以另一个问题变量。您需要重新格式化以避免两个变量的除法/乘法。你知道吗

相关问题 更多 >