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 11: Logistics Network Model

Open In Colab

Lab 11: Logistics Network Model

In this lab you build the network model of Qahwa House’s coffee deliveries in PuLP, compare it with a hand planner’s rule, and then blend the house coffee at the lowest cost.

The question. In January 2026 Qahwa House opens two branches, in Makkah and Madinah. Coffee for the six branches comes from the Riyadh and Dammam roasteries, by van straight to a branch or by full truck through a cross-dock in Jeddah. How much coffee should travel on each lane, what does the best plan save, and what is the cheapest house blend?

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.

Each row of lanes is one lane of the network: origin, destination, mode (van, or truck to the hub; the truck cost includes handling at the hub), km and cost_sar_per_kg, the freight cost of one kilogram. The dictionary cost gives the same costs keyed by (origin, destination), for example cost[("Riyadh roastery", "Olaya")].

The nodes are in the dictionaries supply (what each roastery can roast in a month), demand (what each branch needs) and HUB_CAP (what the Jeddah hub can handle).

A reminder of the PuLP you need (Week 9): model.add_variable_dicts("name", keys, lowBound=0) makes one variable per key in that model; model += expression sets the objective the first time, and adds a constraint when the expression contains <=, >= or ==; pulp.lpSum(...) adds many terms; after result = model.solve(pulp.HiGHS(msg=False)), result.status.name is the solver’s verdict ("Optimal"), pulp.value(model.objective) is the optimal cost and var.varValue a variable’s value.

Part A: Vans only (the transportation pattern)

First plan without the hub: every kilogram goes by van, straight from a roastery to a branch.

A1. The van lanes

Make a list direct of the lanes (the keys of cost) that do not touch the hub.

A2. Build and solve the model

Complete the model: the objective is the total freight; each roastery ships at most its supply; each branch receives exactly its demand. Store the optimal freight in vans_only.

A3. A surprising lane

Show the plan as a table, roasteries by branches (the cell does it for you). Then store in univ_from_dammam the kilograms that the University branch, in Al-Kharj, 85 km from the Riyadh roastery, receives from Dammam.

A4. Why does a branch 85 km from Riyadh get its coffee from 450 km away? Compute, for each branch, how much more a kilogram costs from Dammam than from Riyadh, then explain in two sentences. (Hint: can Riyadh serve all the branches near it and in the west?)

Your answer here (double-click to edit).

Part B: The network with the hub

The Jeddah hub is a transshipment node: coffee goes in by truck and out by van, and none stays there. Its constraints are a flow balance (coffee into the hub = coffee out of the hub) and a capacity (coffee into the hub ≤ HUB_CAP).

B1. The hub’s constraints

Complete the function: into_hub is the sum of the shipments on the lanes whose destination is the hub, out_of_hub the sum on the lanes whose origin is the hub. Then add the two constraints. Store the optimal freight in network_cost.

B2. Read the plan

Store in hub_kg the coffee that goes through the hub, in dammam_to_hub the coffee that Dammam sends to the hub, and in saving how much less the network costs than the van-only plan of Part A (SAR per month).

B3. A hand planner’s rule

A planner without a model would fill the cheapest routes first: sort the routes from a roastery to a branch (by van, or by truck and van through the hub) by their cost per kg, and send as much as possible on each in turn. That rule gives the plan below. Compute its freight and store it in rule_cost.

B4. Compare the two plans lane by lane. In two sentences: where does the rule go wrong, and why does the best plan send Dammam’s coffee 1,350 km to Jeddah although Riyadh is closer?

Your answer here (double-click to edit).

B5. What the hub’s capacity is worth

After the solve, each constraint’s .pi is its shadow price (Week 10). Store the shadow price of the hub’s capacity in hub_price (look it up with net.get_constraint_by_name("hub_capacity")).

Part C: The house blend (the blending pattern)

Qahwa House’s house blend mixes green beans from the five origins in beans. Its specification:

  • average acidity between 5.5 and 7.0, and average body at least 6.0;

  • average cup score at least 84;

  • at least 5% Yemen Mocha.

C1. One kilogram of blend

The variables are the shares of each origin in one kilogram, so they sum to 1, and each average is a weighted sum, for example the blend’s acidity is pulp.lpSum(b.acidity[o] * x[o] for o in b.index). Complete the model and store the cost of one kilogram in blend_cost.

C2. A month’s purchase

A month of roasting needs 4,400 kg of green beans, and the importer can supply at most available_kg_month of each origin. Now the variables are kilograms, so an average is a ratio. Write it linearly: “average acidity at least 5.5” becomes pulp.lpSum((b.acidity[o] - 5.5) * kg[o] for o in b.index) >= 0, and so on for the other averages. PuLP 4 wants plain Python numbers in a comparison, and pandas stores whole numbers as NumPy integers, so write the availability as kg[o] <= int(b.available_kg_month[o]). Store the month’s cost in month_cost.

C3. Today’s recipe is 95% Colombia and 5% Yemen Mocha. What does it cost for 4,400 kg, and how much does the new purchase plan save each month? Store the saving in blend_saving.

Part D: Your recommendation

Write three sentences for the operations manager: how the coffee should travel in 2026 (with the plan’s monthly freight and what it saves compared with vans only and with the cheapest-route rule), which green beans to buy and what that saves, and one thing to check before relying on the model (an assumption or a number you would want confirmed).

Write your recommendation here (double-click to edit).

★ Extended challenges

E1. Ramadan. Demand for the four months January to April 2026 is 3,700, 4,300, 4,600 and 3,500 kg. The roasteries roast at most 4,000 kg a month in normal shifts (6 SAR per kg) plus up to 400 kg of overtime (9 SAR per kg); coffee kept in stock costs 1.2 SAR per kg per month, and there is no stock at the start. Show that planning month by month fails, then build a multiperiod model with an inventory balance (stock before + roasted − demand = stock after) and find the cheapest plan.

E2. More hub capacity. The hub’s operator offers 300 kg a month more capacity for 250 SAR a month. The shadow price from B5 says 300 × 0.87 = 261 SAR of savings. Re-solve with solve_network(hub_cap=1500): should Qahwa House accept? Why does the shadow price mislead here?

E3. More Yemeni coffee. Marketing wants 10% Yemen Mocha in the blend. What happens to the monthly purchase model of C2? What is the largest Yemen share the importer’s supply allows?

AI-use log (if you used an AI assistant)

What you asked, what you kept, what you changed.