# Linear Programming in Python using Pulp – Part 2

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:

#### 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.

### 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!

## One response to “Linear Programming in Python using Pulp – Part 2”

1. […] the previous article, we introduced the concepts of linear programming and multi-objective optimization and showed how […]

Other posts