1

Trying to solve the Blending and Mixing problem to exercise optimization with python (using gurobi and pulp).

Sadly I ran into the following error message:

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()

What could be wrong? This is the code:

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

2 Answers 2

1

As stated by kabdulla, the problem is not solvable like it was posted since division on Lp variables is not possible.

I was able to get the correct results by removing the Integer constraints of the n_.. values, removing the total_weight variable and including the constraint 1 = n_Limestone+n_Corn+n_Soy. The resulting script looks as follows:

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")
Sign up to request clarification or add additional context in comments.

Comments

0

That's not a particularly helpful error message, but at least part of your problem is that the problem as implemented is not linear - you are dividing one problem variable by another in the 'content' constraints. You'll need to reformulate to avoid this division/multiplication of two variables.

1 Comment

Yes its not helpful at all. I appreciate your input, it got me on the track to solving the problem :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.