Get startedGet started for free

The diet problem revisited

You are reviewing the financials for a farmer who asked you to revisit the diet of his pigs and cut on the cost if possible. The current cost minimization diet is based on the vet's recommendation for at least 17% protein, 2% fat, 7lb food following specifications

Food Cost ($/lb) Protein (%) Fat (%)
corn 0.11 10 2.5
soybean 0.28 40 1

You have the information that the 7 lb was a rounded figure and could decrease to 6.6 lb. You are asked to see how changing the weight or fat constraints one at a time affects the minimum cost. You will solve the original problem as is and examine the slack and shadow price.

pulp has been imported for you and model has been defined as well as the variables C and S for corn and soybean.

This exercise is part of the course

Introduction to Optimization in Python

View Course

Exercise instructions

  • Print the slack of the Weight constraint.
  • Check if the shadow price of the Weight constraint is greater than 0.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

model.constraints['Weight'] = C + S >= 7  

model.solve() 
print(f"Status: {LpStatus[model.status]}\n") 

# Print the slack of the weight constraint
print("The slack of the Weight constraint is {}", 
      ____.constraints['Weight'].____)

# Check if the shadow price is greater than 0
if ____.constraints['Weight'].____ > 0:
	print('Tightening the constraint will increase minimum cost')
Edit and Run Code