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.

Lab 9 Solutions: Product Mix Model in PuLP

Open In Colab

Lab 9 Solutions: Product Mix Model in PuLP

These are the worked solutions, with outputs. Try the lab yourself first.

In this lab you build the weekly production plan of Qahwa House’s central kitchen as a linear program in PuLP, and compare it with the plan the manager would make by instinct.

The question. How many batches of each product should the kitchen make this week? The manager’s instinct: as much as possible of the product with the highest margin, Yemeni Haraz.

How this lab works

  • Run the cells from top to bottom with Shift + Enter. Replace every ... with your code, then run the check cell under it.

  • Extended track: after Part D, try the ★ challenges.

  • Submit before the next lab session: File → Download → Download .ipynb.

You may use an AI assistant for code. If you do, add a short note at the end: what you asked, what you kept and what you changed.

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

Part A: The roastery model

A1. Products and capacities

Split the table: products holds the four product rows with product as the index, and capacity holds the three hours available (the capacity row, columns HOURS, as floats).

roast_h 50.0 oven_h 40.0 labour_h 160.0 Name: capacity, dtype: float64
✅ A1 four products
✅ A1 50 roaster hours, 40 oven hours, 160 staff hours

A2. The model in words

This week the oven is being serviced: the kitchen makes coffee only, House Blend (HH batches) and Yemeni Haraz (YY batches). In three short lines, write the decision variables, the objective and the constraints (with their numbers, from the table).

  • Decision variables: HH and YY, the batches of House Blend and Yemeni Haraz made this week.

  • Objective: maximise the contribution 900H+1500Y900H + 1500Y (SAR).

  • Constraints: roaster 1.0H+2.5Y≤501.0H + 2.5Y \le 50 hours; staff 2H+3Y≤1602H + 3Y \le 160 hours; demand H≤40H \le 40, Y≤16Y \le 16; and H,Y≥0H, Y \ge 0.

A3. Solve it with PuLP

Complete the model: the objective and the roaster constraint. The bounds of the variables hold the demand limits. The first solve of a session also prints two lines with HiGHS’s name and licence; you can ignore them.

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 40.0 4.0 42000.0
✅ A3 the status is Optimal
✅ A3 the roaster constraint is in the model
✅ A3 the best coffee plan earns 42,000 SAR

Part B: The picture behind the answer

B1. The corners

The feasible region of the roastery model has five corners: (0,0)(0,0), (40,0)(40,0), (40,4)(40,4), (10,16)(10,16) and (0,16)(0,16). Add a column contribution to the DataFrame corners with the contribution of each corner.

Loading...
✅ B1 five corners
✅ B1 the best corner is (40, 4), as PuLP found
✅ B1 the 'Yemeni first' corner (10, 16) earns 33,000 SAR

B2. The graph

Run the cell: it draws the feasible region, the roaster line, the two demand limits and three iso-profit lines.

<Figure size 700x520 with 1 Axes>

B3. Per roaster hour

Compute each coffee’s contribution per roaster hour and store it in a Series per_roaster_hour, indexed by product (House Blend and Yemeni Haraz only).

product House Blend 900.0 Yemeni Haraz 600.0 dtype: float64
✅ B3 House Blend: 900 SAR per roaster hour
✅ B3 Yemeni Haraz: 600 SAR per roaster hour

B4. In two sentences: why does the best coffee plan make only 4 batches of Yemeni Haraz, the product with the highest margin?

B4. The roaster is the scarce resource, and an hour of it earns 900 SAR on House Blend but only 600 SAR on Yemeni Haraz. So House Blend gets the roaster first, up to its demand of 40 batches, and the 10 hours left make 4 batches of Yemeni Haraz.

Part C: The whole kitchen

C1. The full model

The oven is back. Build the model with all four products and the three resources, reading every number from products and capacity. Complete the objective and the constraint inside the loop with pulp.lpSum. The code builds the model inside a function, build_mix, that returns the model and its variables, so you can build it again in D3.

Optimal 53900.0
✅ C1 the status is Optimal
✅ C1 three resource constraints
✅ C1 the best plan earns 53,900 SAR a week

C2. The plan and the hours

Store the plan in a Series plan (batches, indexed by product). Then build a DataFrame usage indexed by resource with three columns: used, available and slack.

House Blend     40.0
Yemeni Haraz     4.0
Date Maamoul    17.0
Saffron Cake     0.0
Loading...
✅ C2 40 House Blend, 4 Yemeni Haraz, 17 Date Maamoul, 0 Saffron Cake
✅ C2 the roaster and the staff are binding
✅ C2 the oven has 6 hours to spare

C3. The branches would buy 30 batches of Saffron Cake a week, yet the plan makes none. In two sentences, explain why, using the resource table.

C3. Staff time is binding, and both pastries need 4 staff hours a batch, so each staff hour earns 175 SAR on Date Maamoul (700 / 4) but only 140 SAR on Saffron Cake (560 / 4). The oven, where Saffron Cake would do better, has 6 hours of slack, so it does not limit the plan: every staff hour left after the coffee goes to Date Maamoul.

Part D: The manager’s plan

D1. Highest margin first

The manager’s plan fills the demand of the product with the highest margin first: 16 batches of Yemeni Haraz, then 10 of House Blend (the roaster hours left), then 20 of Date Maamoul (the oven is then full), and no Saffron Cake. Compute its weekly contribution manager_profit, and the hours it uses in a Series manager_used (indexed by resource).

47000.0
roast_h 50.0 oven_h 40.0 labour_h 148.0 dtype: float64
✅ D1 the manager's plan is feasible
✅ D1 it earns 47,000 SAR a week
✅ D1 it leaves 12 staff hours idle

D2. The gap

Store in gap how much more the LP plan earns per week, and in gap_year the same over 52 weeks.

6,900 SAR a week, 358,800 SAR a year
✅ D2 6,900 SAR a week
✅ D2 358,800 SAR a year

D3. Two promises

The branches insist on at least 30 batches of House Blend, and the manager has promised a hotel at least 10 batches of Yemeni Haraz. Build a fresh model with strict, xs = build_mix(products, capacity) (a variable belongs to one model, so we build a new one rather than copy it), add both promises as constraints on its variables xs, solve it, and store its status (a string such as "Optimal") in strict_status.

'Infeasible'
✅ D3 both promises cannot be kept

D4. In one sentence: which constraint makes the two promises impossible, and by how many hours?

D4. The roaster: 30 batches of House Blend and 10 of Yemeni Haraz need 30×1+10×2.5=5530 \times 1 + 10 \times 2.5 = 55 roaster hours, 5 more than the 50 available.

D5. Your recommendation

Write three sentences for the manager: the plan to follow this week (with numbers), what it earns compared with the “highest margin first” plan, and one assumption of the model that you would check before following it.

Recommendation. Make 40 batches of House Blend, 4 of Yemeni Haraz and 17 of Date Maamoul this week, and no Saffron Cake. This plan earns 53,900 SAR of contribution, 6,900 SAR a week (about 359,000 SAR a year) more than filling the Yemeni Haraz demand first, because it gives the scarce roaster hours to House Blend (900 SAR per roaster hour against 600) and leaves no staff hour idle. The plan assumes the contributions, the hours per batch and the demand limits are right and that a batch always takes the same time; if the demand for House Blend were overestimated, the plan would change, so the forecast should be checked first.

★ Extended challenges

E1. One more roaster hour. Re-solve the full model with 51 roaster hours instead of 50. How much does the contribution rise, and what happens to the plan?

E2. Whole batches. Pastries come in whole trays. Solve the 51-hour model again with every variable cat="Integer". Compare with E1: what is the extra roaster hour worth now?

E3. A better Saffron Cake. Raise the contribution of Saffron Cake step by step (600, 650, 700, 720 SAR) and re-solve. From which contribution does the plan start making it, and what does it stop making instead?

House Blend     40.0
Yemeni Haraz     4.4
Date Maamoul    16.7
Saffron Cake     0.0
contribution 54,290 SAR, +390 SAR

E1. One more roaster hour raises the contribution by 390 SAR, to 54,290 SAR. The extra hour makes 0.4 more batches of Yemeni Haraz (+600 SAR), which take 1.2 staff hours away from Date Maamoul (0.3 batches, −210 SAR). That 390 SAR is the roaster’s shadow price, next week’s main idea.

House Blend     40.0
Yemeni Haraz     4.0
Date Maamoul    17.0
Saffron Cake     0.0
contribution 53,900 SAR

E2. With whole batches the plan stays 40, 4, 17 and 0, and the contribution stays 53,900 SAR: one extra hour cannot make a whole extra batch of Yemeni Haraz (2.5 hours), so in whole batches it is worth nothing. Divisibility matters when a resource comes in small amounts; integer models are the topic of week 12.

600 [40.0, 4.0, 17.0, 0.0] 53,900
650 [40.0, 4.0, 17.0, 0.0] 53,900
700 [40.0, 4.0, 17.0, 0.0] 53,900
720 [40.0, 4.0, 0.0, 17.0] 54,240

E3. Below 700 SAR the plan does not change. At exactly 700 SAR a Saffron Cake batch earns as much per staff hour as Date Maamoul (175 SAR), so both plans earn 53,900 SAR and the solver may return either (HiGHS keeps Date Maamoul); above 700 SAR the plan swaps all 17 batches of Date Maamoul for 17 of Saffron Cake. Saffron Cake’s contribution must rise by 140 SAR before it enters the plan: its reduced cost, another idea of next week.