Raw Materials Optimization for Food Manufacturing with Python

Use linear programming to create an optimal recipe for a cheap meal bar that meets specific nutritional requirements

Raw Materials Optimization for Food Manufacturing with Python

Use linear programming to create an optimal recipe for a cheap meal bar that meets specific nutritional requirements

Article originally published on Medium.

Scenario

As an R&D manager of a large food manufacturer, you need to design a recipe for a new cheap and healthy meal bar.

Meal bars must function as a meal rather than just a snack to fill in the cracks.

Photo by Sproutified on Unsplash

This is a Linear Programming problem statement with objectives and constraints.

In this article, we will see how to build a model to design this optimal recipe.

I. Problem Statement


Scenario

7 ingredients are available

  • Meat: Chicken, Beef, Mutton
  • Non-Meat: Rice, Corn, Wheat bran, Peanuts

These ingredients have different nutrition facts (in grams) per gram

Nutrition fact for each ingredient — (Image by Author)

They have also different costs ($/gram)

Cost per gram for each ingredient — (Image by Author)

Objective

Minimize the total cost per bar (Weight: 120g).

Constraints

Minimal values for protein and fibre

  • Protein: 20g
  • Fibre: 6g

Maximum values for fat, salt and sugar

  • Fat: 22g
  • Salt: 3g
  • Sugar: 30g

Results

  • Scenario 1: initial scenario
Cost per Bar = 7.91 $
Qty_Beef = 48.56 g
Qty_Chicken = 0.0 g
Qty_Corn = 0.0 g
Qty_Mutton = 0.0 g
Qty_Peanuts = 34.09 g
Qty_Rice = 0.0 g
Qty_Wheat_bran = 37.36 g

You only need to put beef, peanuts and wheat bran for a total cost of 7.91$ per bar (120 g).

  • Scenario 2: reduce the protein level to 12 g
Cost per Bar = 0.78 $
Status: Optimal
Qty_Beef = 0.0 g
Qty_Chicken = 0.0 g
Qty_Corn = 0.0 g
Qty_Mutton = 0.0 g
Qty_Peanuts = 43.15 g
Qty_Rice = 55.19 g
Qty_Wheat_bran = 21.66 g

Your bar is now purely vegan and quite cheap.

  • Scenario 3: reduce the sugar to 20 g
Cost per Bar = 10.32 $
Status: Optimal
Qty_Beef = 65.32 g
Qty_Chicken = 0.0 g
Qty_Corn = 0.0 g
Qty_Mutton = 0.0 g
Qty_Peanuts = 30.96 g
Qty_Rice = 0.0 g
Qty_Wheat_bran = 23.72 g

To ensure a low level of sugar, you need to use more meat. Therefore you will see your cost per bar increasing.

🔗
You can find the full code in this Github repository: Link.

II. Build your model

PuLP is a modelling framework for Linear (LP) and Integer Programming (IP) problems written in Python maintained by COIN-OR Foundation (Computational Infrastructure for Operations Research).

1. Import Parameters

You can also find these datasets in my GitHub repository.

Nutrition facts

Ingredients Costs

2. Declare your variables, parameters and model

  • LpMinimize: your objective is to minimize the cost of your bar
  • lowBound =0: you cannot have a negative value of ingredient quantity

3. Define the objective and add constraints

4. Solve your model and analyze the results

III. Next Steps

This solution is not perfect, you can easily improve it.

  • What if your customers want corn?

You can add corn to this recipe by adding a constraint of a minimum quantity of corn.

  • Do we have infinite possibilities?

Let us try to change the quantity from 120 g to 100 g, what is the result?

Cost per Bar = 11.08 $

Status: Infeasible
Qty_Beef = 71.27 g
Qty_Chicken = 0.0 g
Qty_Corn = 0.0 g
Qty_Mutton = 0.0 g
Qty_Peanuts = 30.26 g
Qty_Rice = 0.0 g
Qty_Wheat_bran = -1.53 g

For this combination of constraints, there is no optimal solution for a bar of 100g.

To get the minimum quantity, start by listing the constraints (nutrition facts) and then try to find the minimum quantity that is feasible.

References

[1] Computational Infrastructure for Operations Research, Optimization with PuLP (Documentation), Link