Maximizing Daily Profit of a Chocolate Factory using Python PuLP
In this article, we will discuss how to maximize the daily profit of a chocolate factory using Python PuLP, a powerful linear programming toolkit. We will cover the key concepts, subtopics, and provide detailed context on the subject.
Problem Description
Consider a chocolate factory that produces individual chocolates, with each chocolate having a specific profit value. The goal is to determine the optimal number of chocolates to produce for each type, in order to maximize the daily profit.
Introduction to Linear Programming
Linear programming is a method for optimizing a linear objective function, subject to constraints expressed as linear equations or inequalities. In our case, the objective function is to maximize the daily profit, and the constraints are determined by the factory's production capacity and raw materials available.
Getting Started with Python PuLP
To begin, we need to install PuLP using pip or conda:
# Install PuLP using pip
pip install pulp
Defining the Problem
To define our maximization problem, we need to follow these steps:
- Define the decision variables
- Define the objective function
- Define the constraints
Defining Decision Variables
The decision variables are the number of chocolates to produce for each type. We need to define a variable for each type of chocolate.
# Import PuLP
import pulp
# Define the decision variables
chocolates = pulp.LpVariable.dicts("Chocolate",
range(1, number_of_chocolates + 1),
cat="Integer",
lowBound=0)
Defining Objective Function
The objective function is to maximize the total profit from producing all chocolates:
# Define the objective function coefficients
objective_values = [profit_per_chocolate[chocolate]
for chocolate in chocolates.keys()]
# Define the objective function
prob += pulp.lpSum(objective_values[chocolate] * chocolates[chocolate]
for chocolate in chocolates)
Defining Constraints
The constraints are determined by the factory's production capacity and available raw materials. We typically express them as linear equations or inequalities.
Production Capacity Constraints
The total production capacity of the factory limits the number of chocolates produced:
# Define the production capacity constraint coefficients
capacity_coefficients = [machine_capacity[machine][chocolate]
for chocolate in chocolates.keys()
for machine in machines.keys()]
# Define the production capacity constraint lb's
capacity_lbs = [0] * len(capacity_coefficients)
# Define the production capacity constraint ub's
capacity_ubs = [machine_capacity[machine][chocolate]
for chocolate in chocolates.keys()
for machine in machines.keys()]
# Define the production capacity constraint
prob += pulp.lpSum(capacity_coefficients[index] * chocolates[chocolate]
for index, chocolate in enumerate(chocolates.keys())) \
<= sum(machine_capacity[machine].values())
Raw Materials Constraints
The raw materials available for production limited the number of chocolates that can be produced:
# Define the raw materials constraint coefficients
materials_coefficients = [raw_materials[raw_material]
[ingredient][chocolate]
for raw_material in raw_materials.keys()
for ingredient in raw_materials[raw_material].keys()
for chocolate in chocolates.keys()]
# Define the raw materials constraint lb's
materials_lbs = [0] * len(materials_coefficients)
# Define the raw materials constraint ub's
materials_ubs = [available_raw_materials
[raw_material][ingredient]
for raw_material in raw_materials.keys()
for ingredient in raw_materials[raw_material].keys()]
# Define the raw materials constraints
for raw_material, ingredient, coefficient in zip(
raw_materials.keys(),
raw_materials[raw_material].keys(),
materials_coefficients
):
prob += pulp.lpSum(coefficient * chocolates[chocolate]
for chocolate in chocolates.keys()
if ingredient in recipe[chocolate]) \
<= available_raw_materials[raw_material][ingredient]
Solving the Problem
After defining the problem, we can solve it using PuLP's solver and display the result:
# Solve the problem
prob.solve()
# Display the solution
for chocolate in chocolates:
print(f"Produce {chocolates[chocolate].varValue} "
f"chocolates of type {chocolate}")
print(f"Total Profit: {prob.objective.value()}")
- In this article, we explored how to maximize the daily profit of a chocolate factory using Python PuLP.
- We defined the key decision variables, objective function, and constraints for the problem.
- We used PuLP's solver to obtain the optimal production plan for the factory, and displayed the result.