-1

I´m replicating this example of the blending problem: https://www.coin-or.org/PuLP/CaseStudies/a_blending_problem.html

With the following data:

import pulp
from pulp import *
import pandas as pd

food = ["f1","f2","f3","f4"]
KG = [10,20,50,80]
Protein =       [18,12,16,18]
Grass = [13,14,13,16]
price_per_kg =  [15,11,10,12]

##            protein,carbohydrates,kg

df = pd.DataFrame({"tkid":food,"KG":KG,"Protein":Protein,"Grass":Grass,"value":price_per_kg})

And this is the code:

deposit =  df["tkid"].values.tolist()

factor_volumen = 1



costs =  dict((k,v) for k,v in zip(df["tkid"],df["value"]))
Protein =  dict((k,v) for k,v in zip(df["tkid"],df["Protein"]))
Grass =  dict((k,v) for k,v in zip(df["tkid"],df["Grass"]))
KG =  dict((k,v) for k,v in zip(df["tkid"],df["KG"]))

prob = LpProblem("The Whiskas Problem", LpMinimize)
deposit_vars = LpVariable.dicts("Ingr",deposit,0)
prob += lpSum([costs[i]*deposit_vars[i] for i in deposit]), "Total Cost of Ingredients per can"



prob += lpSum([deposit_vars[i] for i in deposit]) == 1.0, "PercentagesSum"
prob += lpSum([Protein[i] * deposit_vars[i] for i in deposit]) >= 17.2, "ProteinRequirement"
prob += lpSum([Grass[i] * deposit_vars[i] for i in deposit]) >= 11.8, "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))

This part is woking but I need to add one more contrain which is how many Kg I want to produce.

I have tried doing this two constrains:

## total KG produced == 14
prob += lpSum([KG[i] * deposit_vars[i] for i in deposit]) == 14, "KGRequirement"
### Can´t not use more that 8KG from deposit 1
prob += lpSum([KG[i] * deposit_vars[i] for i in deposit[0:1]]) <= 8, "KGRequirement1" 

I get this error:

Status: Infeasible
Ingr_f1 = 0.83636364
Ingr_f2 = 0.11818182
Ingr_f3 = 0.045454545
Ingr_f4 = 0.0
Total Cost of Ingredients per can =  14.30000007

But it sould be posible to use deposit 4 to satisfy this, so I think the constraint is not correct.

I realized that the percentage contraint was wrong, inted I just need to add the contraint of how much I want to produce:

prob += lpSum([KG[i] * deposit_vars[i] for i in deposit]) == 14, "KGRequirement"

And that the weigthed average of the ingredients safisfy the contion as well.

prob += lpSum([Protein[i] *KG[i] * deposit_vars[i] for i in deposit]) >= 17.2*14, "ProteinRequirement"

This are the rigth contrains now:

prob += lpSum([Protein[i] *KG[i] * deposit_vars[i] for i in deposit]) >= 17.2*14, "ProteinRequirement"
prob += lpSum([Grass[i] *KG[i] * deposit_vars[i] for i in deposit]) >= 11.8*14, "FatRequirement"
prob += lpSum([KG[i] * deposit_vars[i] for i in deposit]) == 14, "KGRequirement"
prob += lpSum([KG[i] * deposit_vars[i] for i in deposit[0:1]]) <= 8, "KGRequirement1"
Luis Ramon Ramirez Rodriguez
  • 9,591
  • 27
  • 102
  • 181
  • 1
    Could you clarify the meaning and units of your decision variables? In your objective function you multiply them by a price per kg, which would suggest `deposit_vars` are in kg, however you then multiply them by another variable which appears to be in kg in your last two constraints suggesting they are percentages... – kabdulla Feb 01 '19 at 10:56
  • @kabdulla I just edited the question – Luis Ramon Ramirez Rodriguez Feb 01 '19 at 13:23
  • 1
    OK.... so you're all sorted now? If so maybe post your own answer and accept to avoid confusion. Also consider upvoting my comment. – kabdulla Feb 01 '19 at 18:15

1 Answers1

1

I realized that the percentage contraint was wrong, inted I just need to add the contraint of how much I want to produce:

prob += lpSum([KG[i] * deposit_vars[i] for i in deposit]) == 14, "KGRequirement"

And that the weigthed average of the ingredients safisfy the contion as well.

prob += lpSum([Protein[i] *KG[i] * deposit_vars[i] for i in deposit]) >= 17.2*14, "ProteinRequirement"

This are the rigth contrains:

prob += lpSum([Protein[i] *KG[i] * deposit_vars[i] for i in deposit]) >= 17.2*14, "ProteinRequirement"
prob += lpSum([Grass[i] *KG[i] * deposit_vars[i] for i in deposit]) >= 11.8*14, "FatRequirement"
prob += lpSum([KG[i] * deposit_vars[i] for i in deposit]) == 14, "KGRequirement"
prob += lpSum([KG[i] * deposit_vars[i] for i in deposit[0:1]]) <= 8, "KGRequirement1"
Luis Ramon Ramirez Rodriguez
  • 9,591
  • 27
  • 102
  • 181