Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Optimization Modeling: The Best Product Mix

Open In Colab

Optimization Modeling: The Best Product Mix

Until now the course has described data and predicted outcomes. This week we prescribe: we let the computer search through every possible plan and return the best one. The tool is the linear program (LP), the workhorse of operations in airlines, refineries, factories and supply chains.

Qahwa House’s central kitchen in Al-Kharj roasts the coffee and bakes the pastries for all four branches. Every week the manager decides how many batches of four products to make. The roaster, the oven and the staff have only so many hours, and the branches can only sell so much. Which plan earns the most?

The manager’s instinct is to make as much as possible of the product with the highest margin. We will see why that instinct leaves money on the table.

After this reading you can:

  • name the three parts of an optimization model: decision variables, objective and constraints;

  • write a small product-mix model in words and in algebra;

  • draw the feasible region of a two-variable LP and find its best corner with iso-profit lines;

  • solve an LP with PuLP and read the status, the plan, the objective and the slack of each constraint;

  • build a larger model from a table with lpSum and loops;

  • recognise an infeasible and an unbounded model, and state the assumptions of a linear model.

Textbook: Albright & Winston, Business Analytics: Data Analysis and Decision Making, Chapter 13.

Setup

The first cell installs PuLP, with the HiGHS solver, when you are in Colab; on your own computer, install it once with pip install "pulp[highs]>=4.3,<5".

Loading...
ColumnMeaning
product, kind, batchwhat is made, and what one batch is
price_sarwhat the branches pay the kitchen for one batch (SAR)
variable_cost_sarbeans, flour, dates, saffron and packaging for one batch (SAR)
contribution_sarprice minus variable cost: what one more batch adds to the week’s profit
roast_h, oven_h, labour_hhours of the roaster, the oven and the kitchen staff one batch uses
max_batchesthe most batches the branches can sell in a week

The last row, capacity, holds the hours available each week: 50 roaster hours, 40 oven hours and 160 staff hours (four people, 40 hours each).

1. Three ingredients

Every optimization model has three parts. Write them in words before you write any code.

PartQuestionThe kitchen
Decision variablesWhat do we choose?the number of batches of each product this week
ObjectiveWhat do we maximise or minimise?the week’s total contribution (SAR)
ConstraintsWhat limits the choice?roaster, oven and staff hours; what the branches can sell; no negative batches

A plan that meets every constraint is feasible. The best feasible plan is optimal. Regression (weeks 6 and 7) gave us numbers to expect; an LP gives us the numbers to choose.

We split the table into the products and the capacities:

roast_h 50.0 oven_h 40.0 labour_h 160.0 Name: capacity, dtype: float64

2. Two products: the roastery alone

Start small. Suppose that this week the oven is being serviced, so the kitchen makes coffee only. Two decisions:

  • HH = batches of House Blend, YY = batches of Yemeni Haraz.

max⁡900 H+1500 Ycontribution (SAR)s.t.1.0 H+2.5 Y≤50roaster hours2.0 H+3.0 Y≤160staff hoursH≤40,Y≤16what the branches can sellH, Y≥0\begin{aligned} \max\quad & 900\,H + 1500\,Y && \text{contribution (SAR)}\\ \text{s.t.}\quad & 1.0\,H + 2.5\,Y \le 50 && \text{roaster hours}\\ & 2.0\,H + 3.0\,Y \le 160 && \text{staff hours}\\ & H \le 40,\quad Y \le 16 && \text{what the branches can sell}\\ & H,\ Y \ge 0 \end{aligned}

Every expression is linear: each batch adds the same contribution and uses the same hours, whatever the others do.

With two variables we can draw every feasible plan. Each constraint keeps the points on one side of a line; the points that satisfy all of them form the feasible region. The staff constraint never binds here: even 40 and 16 batches would need only 2×40+3×16=1282 \times 40 + 3 \times 16 = 128 of the 160 hours, so its line lies outside the picture.

<Figure size 700x520 with 1 Axes>

The dashed lines are iso-profit lines: every plan on one line earns the same contribution. Moving the line up and to the right raises the profit. The best plan is the last feasible point the line touches as it moves outward, here the corner (H,Y)=(40,4)(H, Y) = (40, 4).

This is the key fact of linear programming: if an LP has an optimal plan, then one of the corners of the feasible region is optimal. A two-variable model can be solved by checking the corners:

Loading...

The best corner earns 42,000 SAR: 40 batches of House Blend and 4 of Yemeni Haraz. The manager’s instinct, Yemeni Haraz first because it earns 1,500 SAR a batch, fills the demand of 16 batches, which takes 40 of the 50 roaster hours and leaves room for only 10 batches of House Blend: 33,000 SAR, 9,000 less.

Why? The roaster is the scarce resource, so what matters is the contribution per roaster hour, not per batch:

product House Blend 900.0 Yemeni Haraz 600.0 Name: SAR per roaster hour, dtype: float64

An hour of roaster time spent on House Blend earns 900 SAR; on Yemeni Haraz, only 600. So House Blend gets the roaster first, up to its demand of 40 batches (40 hours), and the 10 hours left go to Yemeni Haraz: 4 batches.

3. Solving it with PuLP

Drawing works for two variables. For more, we hand the model to a solver. PuLP is a Python library in which you write the model almost as you would on paper; it then calls a solver to find the optimum. PuLP 4 needs a solver named explicitly; the course uses HiGHS (free, open source, installed by pulp[highs]), and it also reports shadow prices and reduced costs, which we use next week.

  1. Create the problem with pulp.LpProblem(name, pulp.LpMaximize).

  2. Create one variable per decision with problem.add_variable(name, lowBound=..., upBound=...). A variable belongs to the problem that created it.

  3. Add the objective with +=, then each constraint with += and a name.

  4. Call problem.solve(pulp.HiGHS(msg=False)). It returns a result: read its status first, then the variable values and the objective.

Running HiGHS 1.16.0 (git hash: f3f113d): Copyright (c) 2026 under MIT licence terms
Includes third-party software components, see THIRD_PARTY_NOTICES.md for full details
Optimal
House Blend: 40.0  Yemeni Haraz: 4.0  contribution: 42000.0

The status is Optimal, and PuLP finds the corner we found on the graph: 40 and 4, for 42,000 SAR. The first solve of a session also prints two lines with HiGHS’s name and licence; you can ignore them.

Always read the status first. A number printed after an Infeasible status is meaningless.

Next, the constraints. A constraint is binding when the plan uses all of its resource (its slack is zero); otherwise it has slack, spare capacity:

Loading...

The roaster is binding: all 50 hours are used. Staff time is not: 68 of the 160 hours are free. Only the binding constraints shape the optimum. If the roaster had more hours, the profit would rise; extra staff hours would add nothing this week. How much one more roaster hour is worth is the subject of next week.

4. The whole kitchen: four products, three resources

Now the oven is back. The full weekly model has four decision variables, one per product, and three resource constraints. In general form, with xpx_p the batches of product pp, cpc_p its contribution, arpa_{rp} the hours of resource rr one batch uses, brb_r the hours available and upu_p the demand:

max⁡∑pcp xps.t.∑parp xp≤br for each resource r,0≤xp≤up.\max \sum_p c_p\,x_p \quad\text{s.t.}\quad \sum_p a_{rp}\,x_p \le b_r \text{ for each resource } r, \qquad 0 \le x_p \le u_p .

We no longer type the numbers into the model: we read them from the table. pulp.lpSum adds up a list of terms, and a loop writes one constraint per resource. The same code would solve a model with 400 products. We write the steps as a function, build_mix, that returns the model and its variables, so that we can build variants of it later (section 6).

Optimal · 53,900 SAR a week
House Blend     40.0
Yemeni Haraz     4.0
Date Maamoul    17.0
Saffron Cake     0.0 

Loading...

The best weekly plan: 40 batches of House Blend, 4 of Yemeni Haraz, 17 of Date Maamoul and no Saffron Cake, for 53,900 SAR a week.

Read it with the resource table. The roaster (50 of 50 hours) and the staff (160 of 160) are binding; the oven has 6 hours to spare. So the plan follows the two scarce resources:

  • House Blend earns the most per roaster hour, so it fills its demand (40 batches, 40 roaster hours, 80 staff hours);

  • the 10 roaster hours left make 4 batches of Yemeni Haraz (12 staff hours);

  • the 68 staff hours left go to pastries. Both pastries need 4 staff hours a batch, and Date Maamoul earns 700 SAR against Saffron Cake’s 560, so all 68 hours make 17 batches of Date Maamoul, which need 34 of the 40 oven hours.

Saffron Cake is not made at all, although the branches would sell 30 batches: its staff hours earn more as Date Maamoul.

5. The intuitive plan

How good is the instinct “highest margin first”? The function below makes as much as possible of the product with the highest contribution per batch, then the next one with the hours left, and so on.

Loading...
intuitive plan: 47,000 SAR · LP plan: 53,900 SAR · gap: 6,900 SAR a week

Loading...

The intuitive plan earns 47,000 SAR a week, 6,900 SAR less than the LP: about 359,000 SAR over a year of 52 weeks, from the same kitchen and staff. It fills the 16 batches of Yemeni Haraz first, which eats 40 roaster hours and leaves room for only 10 batches of House Blend. It then fills the oven with Date Maamoul (20 batches), and leaves 12 staff hours idle.

The ratios explain it. Each product’s contribution per hour of each resource:

Loading...

Ranked per batch, Yemeni Haraz wins. Per roaster hour, House Blend wins. Per staff hour, Yemeni Haraz is ahead of House Blend again, and per oven hour Saffron Cake beats Date Maamoul. Each ranking gives a different plan, and the right one depends on which resources are scarce, which we only know once the plan is chosen. A simple ranking works when a single resource binds; with two or more, the LP weighs them all at once. That is what a solver is for.

6. When a model breaks

Two things can go wrong before any optimum exists.

Infeasible: no plan meets every constraint. Suppose the branches insist on at least 30 batches of House Blend and the manager promises a hotel at least 10 batches of Yemeni Haraz. Those two alone need 30+2.5×10=5530 + 2.5 \times 10 = 55 roaster hours, and there are 50. We build a fresh model with build_mix (a variable belongs to one model, so we do not copy the old one) and add the two promises:

'Infeasible'

An infeasible model is a message to the manager, not a bug: the promises cannot all be kept. Either find 5 more roaster hours or renegotiate one of the two commitments.

Unbounded: the objective can grow without limit. This happens when a constraint is missing. Keep only the roaster constraint and forget the demand limits: pastries do not use the roaster, so nothing stops the model from baking an infinite number of them.

'Unbounded'

No real business has unlimited profit. An unbounded model always means that something real (a capacity, a demand, a budget) was left out.

7. What “linear” assumes

An LP is only as good as four assumptions. Check them before you trust a plan.

AssumptionMeaningIn the kitchen
Proportionalitytwice the batches, twice the contribution and twice the hoursno volume discounts on green beans, no set-up time per run
Additivitythe totals are sums over the productsroasting House Blend does not slow down Yemeni Haraz
Divisibilityvariables may take fractional values4.4 batches makes sense for beans (kg), less so for trays; whole numbers come in week 12
Certaintyevery number in the table is knowndemand and prices are estimates; next week we ask how much they can change before the plan does

When an assumption fails badly, the model is changed (integer variables, fixed costs, nonlinear terms; weeks 11 and 12) or tested (sensitivity analysis, week 10; simulation, week 14).

Check your understanding

  1. Name the decision variables, the objective and the constraints of the kitchen model in one sentence each.

  2. Why is the optimal plan of a two-variable LP at a corner of the feasible region?

  3. The roaster has no slack and the oven has 6 hours of slack. Which of the two would you try to expand first, and why?

  4. Why does “highest contribution per batch first” fail here, and when would a ranking by contribution per hour be enough?

  5. A model returns the status Unbounded. What do you check first?

Summary

  • An optimization model has decision variables, an objective and constraints; write them in words first.

  • The feasible region of an LP is bounded by straight lines; if there is an optimum, a corner is optimal, and iso-profit lines show which.

  • In PuLP: LpProblem, variables with bounds from add_variable, the objective and constraints added with +=, lpSum and loops to build from a table, then solve(pulp.HiGHS(msg=False)) and read the status first.

  • Binding constraints have zero slack and shape the plan; the others do not matter at the optimum.

  • The best plan (53,900 SAR) beats the “highest margin first” plan (47,000 SAR) because two resources are scarce at once, and only the LP weighs them together.

  • Infeasible means the promises conflict; unbounded means a real limit is missing.

Next week: how much is one more roaster hour worth, and how far can a price move before the plan changes? Sensitivity analysis and duality.