尝试解决the Blending and Mixing problem以使用python进行优化(使用古罗比和果肉)。
可悲的是,我遇到了以下错误消息:
undefined
可能是什么问题?这是代码:
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()
答案 0 :(得分:0)
这不是特别有用的错误消息,但至少部分问题是实施的问题不是线性的-您在“内容”约束中将一个问题变量除以另一个变量。您需要重新制定格式以避免两个变量的除法/乘法。
答案 1 :(得分:0)
正如kabdulla所说,由于无法对Lp变量进行除法,因此该问题无法像以前那样解决。
通过除去n_ ..值的整数约束,除去total_weight变量并包括约束1 = n_Limestone + n_Corn + n_Soy,我能够获得正确的结果。生成的脚本如下所示:
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")