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.

Modeling Patterns: Networks, Blends and Time

Open In Colab

Modeling Patterns: Networks, Blends and Time

In Week 9 you built a first linear program, and in Week 10 you read what its constraints are worth. Real business models are larger, but most of them are assembled from a few patterns that come back again and again. This reading builds three of them for Qahwa House in PuLP: a network that moves coffee from two roasteries through a hub to six branches, a blend that mixes green beans to a taste specification, and a multiperiod plan that links the months through stock. One idea runs through all three: a balance constraint that says what comes in must go out, or stay.

After this reading you can:

  • formulate a transportation model from a table of lanes and solve it in PuLP;

  • add a transshipment node (a hub) with a flow-balance constraint and a capacity;

  • explain, with shadow prices, why the cheapest lane is not always in the cheapest plan;

  • write averages and percentages as linear constraints in a blending model;

  • link time periods with inventory-balance constraints.

Textbook: Albright & Winston, Chapter 14.

The first cell installs PuLP when you run the notebook in Colab (it takes a few seconds); on your own computer, install it once with pip install "pulp[highs]". PuLP 4 needs a solver named explicitly; the course uses HiGHS (free, open source, installed by pulp[highs]), which also reports shadow prices and reduced costs.

The decision

In January 2026 Qahwa House opens two branches, in Makkah and Madinah. Six branches now need roasted coffee every month, and it comes from two roasteries, in Riyadh and Dammam. Coffee travels either by van, straight from a roastery to a branch, or by full truck to a cross-dock in Jeddah, the hub, where vans take it on to the three western branches. A full truck costs much less per kilogram than a van, but the hub can handle at most 1,200 kg a month.

How much coffee should travel on each lane, so that every branch gets its coffee at the lowest freight cost?

Loading...
ColumnMeaning
origin, destinationthe two ends of the lane
modevan (loaded at the origin, driven to the destination) or truck (a full truck to the hub; the cost includes handling at the hub)
kmroad distance
cost_sar_per_kgfreight cost of moving one kilogram on the lane (SAR)

The lanes are the arcs of the network. The nodes have their own numbers: what each roastery can roast in a month, what each branch needs, and the hub’s capacity. We write them as dictionaries, and turn the lane table into a dictionary of costs keyed by (origin, destination).

supply 4,000 kg   demand 3,700 kg   lanes 17
<Figure size 800x440 with 1 Axes>

The roasteries can roast 4,000 kg a month, and the branches need 3,700 kg, so there is enough coffee. The question is only where each kilogram goes. With 17 lanes, there are far too many plans to try by hand.

1. The transportation pattern

Start without the hub: vans only, from a roastery straight to a branch. This is the transportation pattern, one of the oldest linear programs:

  • sources with a capacity (the roasteries) and destinations with a demand (the branches);

  • one decision variable per lane: xijx_{ij} = kg sent from source ii to destination jj, with xij≥0x_{ij} \ge 0;

  • minimise the total cost ∑ijcij xij\sum_{ij} c_{ij}\,x_{ij};

  • each source ships at most its capacity: ∑jxij≤Si\sum_j x_{ij} \le S_i;

  • each destination receives exactly its demand: ∑ixij=Dj\sum_i x_{ij} = D_j.

In PuLP, the model’s add_variable_dicts creates one variable for every key of a list, here every lane. The constraints are written with a loop over the nodes, and each one sums the variables of the lanes that leave (or enter) the node.

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 - freight 6,795 SAR per month
Loading...

The cheapest van-only plan costs 6,795 SAR a month. Riyadh roasts all it can (2,400 kg). The surprise is in the University column: the branch in Al-Kharj, 85 km from the Riyadh roastery, gets all its coffee from Dammam, 450 km away. And Madinah gets 200 kg from each roastery.

Why? Riyadh cannot serve every branch near it and in the west: that would take 3,000 kg, and it roasts 2,400. So 600 kg of those branches must come from Dammam. The useful question is not “which lane is cheapest?” but “where does it hurt least to use Dammam instead of Riyadh?”:

Al Faisaliyah -1.25 University 1.17 Madinah 1.22 Olaya 1.25 Corniche 1.28 Makkah 1.31 Name: extra SAR per kg if Dammam serves the branch, dtype: float64

Al Faisaliyah is cheaper from Dammam anyway. Among the others, University is where Dammam costs the least extra (1.17 SAR per kg), then Madinah (1.22). The linear program gives Dammam exactly those 600 kg: all 400 of University and 200 of Madinah. A van from Dammam to Al-Kharj looks wasteful lane by lane, yet it is what lets Riyadh’s coffee go where it saves the most.

2. Network flow: a hub and its balance

Now add the Jeddah hub. The hub is a transshipment node: it neither roasts nor drinks coffee, it only passes it on. Its constraint is a flow balance:

coffee into the hub=coffee out of the hub\text{coffee into the hub} = \text{coffee out of the hub}

and, because it can handle at most 1,200 kg a month, a capacity: coffee into the hub ≤1,200\le 1{,}200. Every node in a network gets a balance of this kind: at a roastery, what leaves is at most what it roasts; at a branch, what arrives is what it needs; at the hub, in equals out. That is the whole network flow pattern.

The function below builds the model for any hub capacity and any roastery capacities, so we can reuse it.

Optimal - freight 4,999 SAR per month
Loading...
<Figure size 800x440 with 1 Axes>

With the hub, freight falls to 4,999 SAR a month, 1,796 SAR less than with vans only (26% less). Three things in this plan are worth a second look:

  • The hub is full: 1,200 kg go through it (600 from each roastery), and Makkah gets its last 100 kg by van straight from Riyadh.

  • Riyadh is full (2,400 kg) and Dammam has 300 kg to spare.

  • Dammam’s coffee drives 1,350 km to Jeddah, the longest lane on the map, although Riyadh is closer to Jeddah. And Madinah gets its coffee by van from Riyadh rather than through the hub: through the hub it would cost 1.68 + 1.56 = 3.24 SAR per kg, against 2.97 direct.

Why not just use the cheapest lanes?

A natural way to plan by hand is to take the cheapest route first: sort every route from a roastery to a branch (direct, or through the hub) by its cost per kg, and send as much as you can on each in turn, until the roasteries, the hub or the branches run out. Here it is in code:

cheapest route first: 5,338 SAR   linear program: 4,999 SAR
Loading...

The rule costs 5,338 SAR a month, 339 SAR (6.8%) more than the linear program, or about 4,000 SAR a year. It starts well, but it spends Riyadh’s last 300 kg on Makkah through the hub (2.20 SAR per kg, the cheapest route left at that moment) and is then stuck: Madinah’s 400 kg must come by van from Dammam at 4.19 SAR per kg. The linear program sees the whole network at once. It sends Riyadh’s coffee to Madinah (2.97) and lets Dammam fill the hub instead (2.24 + 0.30 to Corniche). What matters is not what a lane costs, but what using it frees up elsewhere.

What the constraints are worth

Week 10’s shadow prices put a number on “frees up”. PuLP stores them in each constraint’s .pi after the solve. For a minimisation, a negative shadow price on a capacity says how much the cost would fall with one more unit of it.

supply_Riyadh -0.56 supply_Dammam 0.00 hub_capacity -0.87 Name: shadow price (SAR per kg), dtype: float64

One more kilogram of Riyadh capacity would save 0.56 SAR, and one more kilogram of hub capacity 0.87 SAR. Dammam’s capacity is worth nothing at the margin: it already has 300 kg to spare. A shadow price is only valid over a range, though. Re-solving for a range of hub capacities shows how far the 0.87 holds:

<Figure size 700x320 with 1 Axes>
0       6795.0
1200    4999.0
1300    4912.0
1500    4912.0
2100    4912.0

Without a hub (capacity 0) the freight is the van-only 6,795 SAR. Each step of hub capacity saves less than the one before, and above 1,300 kg nothing more is saved (4,912 SAR): the cheap trucks then carry everything they can usefully carry. So the shadow price of 0.87 SAR per kg holds for the next 100 kg only. If the hub’s operator offered 300 kg more capacity for 250 SAR a month, it would save 87 SAR, not 3 × 87: a bad deal.

3. The blending pattern

Qahwa House roasts one house blend, mixed from green beans of five origins. The blend must taste the same all year, so it has a specification, checked by cupping:

  • average acidity between 5.5 and 7.0, and average body at least 6.0 (both on a 0–10 scale);

  • average cup score at least 84 (on the 100-point specialty scale);

  • at least 5% Yemen Mocha, because the bag says “with Yemeni Mocha”.

Loading...

The decision variables are the proportions xix_i of each origin in one kilogram of blend, so ∑ixi=1\sum_i x_i = 1. An average is a weighted sum: the blend’s acidity is ∑iaixi\sum_i a_i x_i. So “average acidity at least 5.5” is the linear constraint ∑iaixi≥5.5\sum_i a_i x_i \ge 5.5.

When the variables are quantities (kg) rather than proportions, the average is a ratio, ∑iaixi/∑ixi≥5.5\sum_i a_i x_i / \sum_i x_i \ge 5.5, which is not linear. Multiply both sides by the total and it becomes linear again:

∑iaixi≥5.5∑ixi⟺∑i(ai−5.5) xi≥0.\sum_i a_i x_i \ge 5.5 \sum_i x_i \quad\Longleftrightarrow\quad \sum_i (a_i - 5.5)\,x_i \ge 0.

This is the one trick of the blending pattern. The function below uses this form, so it works for one kilogram and for a month’s purchase alike.

Optimal - 32.06 SAR per kg of blend
Brazil Santos 0.61 Colombia Supremo 0.00 Ethiopia Yirgacheffe 0.34 Sumatra Mandheling 0.00 Yemen Mocha 0.05 Name: share of the blend, dtype: float64
Loading...

The cheapest blend is 61% Brazil, 34% Ethiopia and 5% Yemen Mocha, at 32.06 SAR per kg. Its cup score is exactly 84 and its Yemen share exactly 5%: those two constraints are binding; acidity and body have room to spare.

Today’s recipe, 95% Colombia with 5% Yemen Mocha, also meets the specification: Colombia is the all-rounder, good on every score. Yet it is not in the cheapest blend. Brazil is cheap but too flat and too low in cup score on its own; Ethiopia is dear but has acidity and cup score to give away. Together they meet the specification for 1.89 SAR per kg less than Colombia.

Now a month’s purchase: 3,700 kg of roasted coffee needs about 4,400 kg of green beans (roasting loses about 16% of the weight), and the importer can supply only so much of each origin.

Optimal - 142,100 SAR per month (32.30 SAR per kg)
today's recipe: 149,380 SAR per month
Loading...

Ethiopia is limited to 1,200 kg a month, less than the 34% the recipe wants (1,496 kg). So the model takes all 1,200 kg and brings in Colombia (740 kg) to fill the gap. The month costs 142,100 SAR, 7,280 SAR less than today’s recipe (149,380 SAR). The same constraints, one more line each for the availability, and the answer changes: that is why we write models rather than rules of thumb.

4. The multiperiod pattern

The last pattern links time periods. During Ramadan (from mid-February to mid-March in 2026) the branches sell more coffee. The two roasteries together roast at most 4,000 kg a month in normal shifts, at 6 SAR per kg, plus up to 400 kg of overtime at 9 SAR per kg. Roasted coffee can wait in stock, at 1.2 SAR per kg per month (storage and lost freshness). The plan starts with no stock.

Jan      0
Feb    300
Mar    600
Apr      0

Month by month, March needs 600 kg of overtime and only 400 kg are possible: living from month to month, March cannot be served. The months must be planned together. The link between them is an inventory balance, the flow balance of the network pattern with time as the network:

stockt−1+roastedt−demandt=stockt\text{stock}_{t-1} + \text{roasted}_t - \text{demand}_t = \text{stock}_t

Coffee “flows” from one month to the next through the stock, exactly as it flowed through the hub.

Optimal - 99,000 SAR for the four months
Loading...
<Figure size 700x320 with 1 Axes>

The plan costs 99,000 SAR. It roasts a full 4,000 kg in January and keeps the 300 kg it does not need for February. February runs 200 kg of overtime and keeps them for March, and March runs its full 400 kg of overtime. Holding a kilogram for a month (1.2 SAR) is cheaper than the overtime premium (3 SAR), so the model roasts ahead; the overtime it cannot avoid is pushed as late as possible.

Summary

PatternVariablesThe typical constraint
Transportationkg on each lanesupply: out ≤ capacity; demand: in = need
Network flowkg on each arcbalance at every transshipment node: in = out; capacities on nodes or arcs
Blendingkg (or share) of each inputan average as a linear constraint: ∑i(ai−target) xi≥0\sum_i (a_i - \text{target})\,x_i \ge 0
Multiperiodproduction and stock in each periodinventory balance: stock before + produced − demand = stock after
  • Build the model from the data: a variable per row of the lane table, a constraint per node, written with loops.

  • The cheapest lane is not always in the cheapest plan: a lane’s real cost is what it frees up elsewhere, which the shadow prices measure.

  • A shadow price holds over a range only: re-solve to see how far.

Next week: integer and nonlinear models: yes-or-no decisions, fixed costs, set covering and portfolio risk.