Maximize Chocolate Profit with PuLP: A Step-by-Step Linear Programming Guide
This tutorial demonstrates how to model and solve a chocolate‑production profit maximization problem using Python's PuLP library, covering decision‑variable definition, objective formulation, constraints, and result extraction in a clear step‑by‑step guide for beginners.
1 Example (Chocolate Production)
Assume a chocolate factory produces two types of chocolate, A and B, each requiring milk and chocolate. Each unit of A needs 1 unit of milk and 3 units of chocolate; each unit of B needs 1 unit of milk and 2 units of chocolate.
The kitchen has 5 units of milk and 12 units of chocolate. Selling price: A = 6 rupees per unit, B = 5 rupees per unit.
Goal: maximize profit by deciding how many units of A and B to produce.
1.1 Decision Variables
A: total units of product A
B: total units of product B
Profit: total profit
1.2 Objective Function
The total profit is 6·A + 5·B. The objective function is:
1.3 Constraints
Milk constraint: A + B ≤ 5
Chocolate constraint: 3·A + 2·B ≤ 12
2 Solving with PuLP
PuLP is an open‑source Python library for linear, integer, and mixed‑integer programming. Install it via: pip install pulp Import the library:
import pulp as lp2.1 Initialize Model
model = lp.LpProblem(name='cho', sense=lp.LpMaximize)2.2 Define Decision Variables
x = lp.LpVariable('A', lowBound=0, upBound=4, cat='Integer')
y = lp.LpVariable('B', lowBound=0, upBound=5, cat='Integer')2.3 Define Objective Function
objective = 6*x + 5*y
model += objective, 'Maximize_Profit'2.4 Add Constraints
model += x + y <= 5, 'milk_constraint'
model += 3*x + 2*y <= 12, 'cho_constraint'2.5 Solve
model.solve()The solver returns status 1, indicating success.
2.6 Retrieve Results
print('Objective value:', lp.value(model.objective))
print('Variable x:', lp.value(x))
print('Variable y:', lp.value(y))Output shows objective value 49.0, x = 4.0, y = 5.0.
3 Full Code
import pulp as lp
model = lp.LpProblem(name='cho', sense=lp.LpMaximize)
x = lp.LpVariable('A', lowBound=0, upBound=4, cat='Integer')
y = lp.LpVariable('B', lowBound=0, upBound=5, cat='Integer')
objective = 6*x + 5*y
model += objective, 'Maximize_Profit'
model.solve()
print(model)
print('Objective value:', lp.value(model.objective))
print('Variable x:', lp.value(x))
print('Variable y:', lp.value(y))Reference: https://blog.csdn.net/youcans/article/details/116371416
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Model Perspective
Insights, knowledge, and enjoyment from a mathematical modeling researcher and educator. Hosted by Haihua Wang, a modeling instructor and author of "Clever Use of Chat for Mathematical Modeling", "Modeling: The Mathematics of Thinking", "Mathematical Modeling Practice: A Hands‑On Guide to Competitions", and co‑author of "Mathematical Modeling: Teaching Design and Cases".
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
