A linear objective function combines decision variables by multiplying each by a fixed weight and adding the results, with an optional constant. It expresses what an optimization model minimizes or maximizes. In an animal-feed mix, the objective adds each ingredient’s unit cost multiplied by its quantity. section 2.1.4
Animal-feed cost
For corn, limestone, and soybean meal quantities x1, x2, and x3, the cost objective is 30.5x1 + 10.0x2 + 90x3. Constraints separately enforce calcium, protein, fiber, and conservation requirements. PuLP solves the model and produces a mixture of 65% corn, 3% limestone, and 32% soybean meal, costing $0.4916 per kilogram. section 2.1.4
Table 2.1 Animal feed mix problem (view table figure)
| Ingredients |
Nutritive content and price of ingredients |
|||
|---|---|---|---|---|
| Calcium (kg/kg) |
Protein (kg/kg) |
Fiber (kg/kg) |
Unit cost (cents/kg) |
|
| Corn |
0.001 |
0.09 |
0.02 |
30.5 |
| Limestone |
0.38 |
0.0 |
0.0 |
10.0 |
| Soybean meal |
0.002 |
0.50 |
0.08 |
90.0 |
Compare ingredients by nutrient content and unit cost before constructing the linear objective. section 2.1.4
Listing 2.6 Solving a linear programming problem using PuLP
from pulp import *
model = LpProblem("Animal_Feed_Mix_Problem", LpMinimize) #1
x1 = LpVariable('Corn', lowBound = 0, upBound = 1, cat='Continous') #2
x2 = LpVariable('Limestone', lowBound = 0, upBound = 1, cat='Continous') #2
x3 = LpVariable('Soybean meal', lowBound = 0, upBound = 1, cat='Continous') #2
model += 30.5*x1 + 10.0*x2 + 90*x3, 'Cost' #3
model +=0.008 <= 0.001*x1 + 0.38*x2 + 0.002*x3 <= 0.012, 'Calcium limits' #4
model += 0.09*x1 + 0.5*x3 >=0.22, 'Minimum protein' #4
model += 0.02*x1 + 0.08*x3 <=0.05, 'Maximum fiber' #4
model += x1+x2+x3 == 1, 'Conservation' #4
model.solve() #5
for v in model.variables(): #6
print(v.name, '=', round(v.varValue,2)*100, '%') #6
#6
print('Total cost of the mixture per kg = ', #6
round(value(model.objective)/100, 2), '$') #6
#1 Create a linear programming model.
#2 Define three variables that represent the percentages of corn, limestone, and soybean meal in the mixture.
#3 Define the total cost as theobjective function to be minimized.
#4 Add the constraints.
#5 Solve the problem using PuLP’s choice of solver.
#6 Print the results (the optimal percentages of the ingredients and the cost of the mixture per kg)
Inspect a complete PuLP implementation that declares variables, defines the objective, adds constraints, and solves the model. section 2.1.4
