Operations 7 min read

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.

Model Perspective
Model Perspective
Model Perspective
Maximize Chocolate Profit with PuLP: A Step-by-Step Linear Programming Guide

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:

<code>pip install pulp</code>

Import the library:

<code>import pulp as lp</code>

2.1 Initialize Model

<code>model = lp.LpProblem(name='cho', sense=lp.LpMaximize)</code>

2.2 Define Decision Variables

<code>x = lp.LpVariable('A', lowBound=0, upBound=4, cat='Integer')
y = lp.LpVariable('B', lowBound=0, upBound=5, cat='Integer')</code>

2.3 Define Objective Function

<code>objective = 6*x + 5*y
model += objective, 'Maximize_Profit'</code>

2.4 Add Constraints

<code>model += x + y <= 5, 'milk_constraint'
model += 3*x + 2*y <= 12, 'cho_constraint'</code>

2.5 Solve

<code>model.solve()</code>

The solver returns status 1, indicating success.

2.6 Retrieve Results

<code>print('Objective value:', lp.value(model.objective))
print('Variable x:', lp.value(x))
print('Variable y:', lp.value(y))</code>

Output shows objective value 49.0, x = 4.0, y = 5.0.

3 Full Code

<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))
</code>

Reference: https://blog.csdn.net/youcans/article/details/116371416

optimizationPythonoperations researchLinear ProgrammingPuLP
Model Perspective
Written by

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

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.