In the previous article, we covered the basics of Linear Programming using PuLP in Python. In this follow-up article, we will delve deeper into the Pulp library and explore some advanced features and techniques for solving linear programming problems. Here is a summary of what we covered in the previous post.

- What is Linear Programming?
- What is an Optimization Problem?
- What is an Objective with respect to an optimization problem?
- What are problem variables (and their linear relationship with the objective)?
- What are constraints and how they affect the objective in terms of optimization?
- What it means to maximize or minimize an objective?
- How to build a linear programming model to find the most cost effective meals to meet an objective – to serve atleast 4 units of protiens per day?

If you haven’t yet read the article I would highly recommend to read it and then continue with this one.

### Introduction

In the previous article, we introduced the concept of linear programming and introduced a problem to find the best meal plan for a patient suffering from Anemia. We defined a single objective to maximize the content of Vitamin B12 in the patient’s meals while satisfying a few constraints, such as the minimum required daily intake of Vitamin B12, the number of meals, and the minimum calories intake.

In this follow-up article, we will take a step further and add more complexity to the problem by incorporating multiple objectives. In the healthcare industry, it is not just about maximizing the content of a single nutrient, but also about ensuring a balanced and nutritious meal plan. To achieve this, we will introduce a new objective to minimize the total fat content in the patient’s meals while still satisfying the original constraints.

By incorporating multiple objectives, we will show how to find a solution that balances the intake of Vitamin B12 and fat, and how to trade-off between conflicting objectives to find an optimal solution. In this blog post, we will demonstrate how to use the Pulp library to solve this advanced linear programming problem.

### Optimization Problem

**Objectives:**

- Maximize the content of Vitamin B12 in the patient’s meals (Objective 1)
- Minimize the total fat content in the patient’s meals (Objective 2)

**Variables:**

- Let x1, x2, x3 represent the amount of Vitamin B12 in milligrams (mg) in each of the 3 meals (breakfast, lunch, and dinner)
- Let y1, y2, y3 represent the amount of fat in grams (g) in each of the 3 meals

**Constraints:**

- The total Vitamin B12 content should not be less than 2.4 mg in all meals combined for any given day: x1 + x2 + x3 >= 2.4
- The total calories content in all meals combined should not be less than 2500 calories: c1 * y1 + c2 * y2 + c3 * y3 >= 2500, where c1, c2, and c3 represent the calorie content of each meal in calories per gram of food.
- We have to feed exactly 3 meals to the patient per day: x1 + x2 + x3 = 3
- Weights of each variable to the objectives: Let w1 and w2 represent the weights of each objective. The weight of the first objective (Maximize the content of Vitamin B12) is w1 and the weight of the second objective (Minimize the total fat content) is w2.

The problem can be formulated as a mathematical model as follows:

Maximize: w1 * (x1 + x2 + x3) + w2 * (y1 + y2 + y3)

Subject to:

x1 + x2 + x3 >= 2.4

c1 * y1 + c2 * y2 + c3 * y3 >= 2500

x1 + x2 + x3 = 3

Tabular Summary of the Problem:

Variable | Description |
---|---|

x1, x2, x3 | Amount of Vitamin B12 in milligrams in each of the 3 meals |

y1, y2, y3 | Amount of fat in grams in each of the 3 meals |

w1, w2 | Weights of each objective |

c1, c2, c3 | Calorie content of each meal in calories per gram of food |

Constraint | Description |
---|---|

x1 + x2 + x3 >= 2.4 | Total Vitamin B12 content should not be less than 2.4 mg in all meals combined |

c1 * y1 + c2 * y2 + c3 * y3 >= 2500 | Total calories content in all meals combined should not be less than 2500 calories |

x1 + x2 + x3 = 3 | Number of meals should be exactly 3 |

Objective | Description |
---|---|

Maximize: w1 * (x1 + x2 + x3) + w2 * (y1 + y2 + y3) | Maximize the content of Vitamin B12 and minimize the total fat content in the patient’s meals |

#### Solving The Problem

Let’s first look at some code that we can use to solve the problem, explanation coming below.

```
import pulp
# Define the model
model = pulp.LpProblem("Multi-Objective Meal Plan", pulp.LpMaximize)
# Define variables
x1 = pulp.LpVariable("x1", lowBound=0)
x2 = pulp.LpVariable("x2", lowBound=0)
x3 = pulp.LpVariable("x3", lowBound=0)
y1 = pulp.LpVariable("y1", lowBound=0)
y2 = pulp.LpVariable("y2", lowBound=0)
y3 = pulp.LpVariable("y3", lowBound=0)
# Define weights
w1 = 1.0
w2 = 0.5
# Define calorie content
c1 = 100
c2 = 200
c3 = 150
# Define constraints
model += x1 + x2 + x3 >= 2.4
model += c1 * y1 + c2 * y2 + c3 * y3 >= 2500
model += x1 + x2 + x3 == 3
# Define objectives
model += w1 * (x1 + x2 + x3) + w2 * (y1 + y2 + y3), "Objective"
# Solve the problem
model.solve()
# Print results
print("Vitamin B12 in breakfast: ", x1.value())
print("Vitamin B12 in lunch: ", x2.value())
print("Vitamin B12 in dinner: ", x3.value())
print("Fat in breakfast: ", y1.value())
print("Fat in lunch: ", y2.value())
print("Fat in dinner: ", y3.value())
print("Objective Value: ", pulp.value(model.objective))
```

In the above code, we first import the Pulp library and create a new `LpProblem`

object. Then, we define the variables `x1`

, `x2`

, `x3`

, `y1`

, `y2`

, and `y3`

using the `LpVariable`

class. We set the lower bound of each variable to 0 to ensure that the amount of Vitamin B12 and fat in each meal is non-negative.

Next, we define the weights `w1`

and `w2`

for the objectives and the calorie content `c1`

, `c2`

, and `c3`

of each meal. Then, we add the constraints to the problem using the `+=`

operator. Finally, we define the objectives and maximize the sum of the weights and the amounts of Vitamin B12 and fat in the patient’s meals.

We solve the problem using the `solve`

method of the `LpProblem`

class and print the results, which include the amounts of Vitamin B12 and fat in each meal and the objective value.

### How To: Steps in Solving the Problem

Step | Description |
---|---|

1 | Import the Pulp library |

2 | Create a new `LpProblem` object |

3 | Define variables using the `LpVariable` class |

4 | Define weights for the objectives |

5 | Define calorie content of each meal |

6 | Add constraints to the problem |

7 | Define the objectives and maximize the sum of the weights and the amounts of Vitamin B12 and fat in the patient’s meals |

8 | Solve the problem using the `solve` method of the `LpProblem` class |

9 | Print the results, including the amounts of Vitamin B12 and fat in each meal and the objective value |

### Summary: What Did We Learn

We learnt the following advanced concepts related to linear programming using PulP in python.

- Linear programming is a mathematical method for finding the optimal solution to an optimization problem subject to constraints.
- The Pulp library in Python provides a simple and flexible API for solving linear programming problems.
- In this article, we solved an advanced linear programming problem that incorporated multiple objectives to balance the intake of Vitamin B12 and fat in a patient’s meal plan.
- The problem was defined by specifying the variables, constraints, and objectives, and solved using the Pulp library.
- The steps to solve the problem using Pulp include importing the library, creating a new
`LpProblem`

object, defining variables, weights, calorie content, adding constraints, defining objectives, solving the problem, and printing the results.

By solving this advanced linear programming problem, we gained a deeper understanding of how to apply linear programming to real-world problems and how to use the Pulp library to find optimal solutions.

## Conclusion

In this article, we learned about advanced linear programming and how to solve a multi-objective optimization problem using the Pulp library in Python. We demonstrated how to balance the intake of Vitamin B12 and fat in a patient’s meal plan by incorporating multiple objectives into the problem and using the Pulp library to find an optimal solution.

We hope that this article has been helpful in introducing you to the concept of linear programming and multi-objective optimization, and that you now have a better understanding of how to apply these techniques to real-world problems.

In the next article, we will build on this knowledge and apply linear programming and multi-objective optimization to a finance domain problem. We will explore how to use linear programming to find the optimal investment portfolio and balance multiple objectives, such as maximizing returns and minimizing risk.

Stay tuned for the next article in this series, where we will dive deeper into the world of linear programming and multi-objective optimization!

## Leave a Reply