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 10: What-If App over a Linear Program

Open In Colab

Lab 10: What-If App over a Linear Program

In this lab you take the weekly plan of Qahwa House’s central kitchen (week 9) and ask what it is worth to change it: one more hour, a lower price, an offer from a supplier. Then you package the model in a Streamlit app the manager can play with.

The question. Four offers are on the manager’s desk for next month: a second roaster shift, staff overtime, hours on a neighbour’s oven and a House Blend campaign. Which should the kitchen take? Valued with the shadow prices, the overtime looks like the best of the four. Is it?

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, plus your whatif_app.py.

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.

products holds the four products (contribution per batch, hours of roaster, oven and staff per batch, max_batches the branches can sell); capacity the 50 roaster, 40 oven and 160 staff hours of a week. Each offer adds amount to a capacity (roast_h, oven_h, labour_h) or to the demand limit of House Blend, for weekly_cost_sar a week.

Part A: Shadow prices and reduced costs

A1. The model as a function

solve_mix builds and solves week 9’s model and returns the plan with its sensitivity information. Complete the two gaps: the shadow price of each resource constraint is c[r].pi, and the reduced cost of each variable is x[p].dj.

A2. Check a shadow price by re-solving

A shadow price is a promise: one more hour adds this much to the best profit. Check two of them. Store in gain_roaster the profit with 51 roaster hours minus the base profit, and in gain_oven the same with 41 oven hours. (Copy capacity first: more = capacity.copy().)

A3. In two sentences: why is the oven’s shadow price 0, and what does that tell you about the oven rental offer before you solve anything?

Your answer here (double-click to edit).

Part B: How far does a shadow price hold?

B1. Sweep the staff hours

Solve the model for every number of staff hours from 150 to 200. Store the results in a DataFrame staff_sweep, indexed by the hours, with a column profit. Then add a column value of the last hour: the profit minus the profit with one hour less (.diff()).

B2. See it

Run the cell: it draws the value of each extra staff hour.

B3. The overtime offer

The overtime offer adds 24 staff hours for 130 SAR an hour (3,120 SAR a week). Store in naive_gain the gain you would expect from the shadow price (24 × the staff’s shadow price), and in overtime_gain the real gain: the profit with 184 staff hours minus the base profit (use staff_sweep).

B4. How many overtime hours?

Overtime can be bought by the hour. For each number of hours from 0 to 24, the net gain is the profit gain minus 130 SAR per hour. Store the best number of hours in best_hours and its net gain in best_net.

B5. In two or three sentences: why does “24 × 175” overstate the value of the overtime? What happens in the kitchen at 172 staff hours? (Hint: look at the plan with 172 and with 173 staff hours.)

Your answer here (double-click to edit).

Part C: Reduced costs

C1. Price out the products

Value the hours each product uses at the shadow prices: hours_value is a Series (one value per product) equal to the product’s hours times the shadow prices, summed over the three resources (products[HOURS] @ prices). Then reduced = contribution − hours_value.

C2. The branches’ proposal

The branches propose to cut the price of Saffron Cake by 10%, from 960 to 864 SAR a batch (its variable cost stays 400 SAR), so that they can sell 40 batches a week instead of 30. Make a copy cut of products with the new contribution and demand limit, solve it, and store the result in after_cut.

C3. In two sentences: use the reduced cost to explain the result of C2, and say from what price per batch Saffron Cake would be worth making.

Your answer here (double-click to edit).

Part D: The what-if app

D1. Fill in the app

The next cell writes a Streamlit app to whatif_app.py. The manager sets the hours of each resource and ticks the offers; the app re-solves the model and shows the plan, the shadow prices and the net gain of the offers: the profit with the offers, minus the profit without them, minus their cost. Fill in the five gaps marked ..., run the cell, then run the test cell under it.

D2. See it in a browser

Colab cannot show a Streamlit page, so open it in GitHub Codespaces (free with GitHub Education) or on your own computer:

  1. Put whatif_app.py in a folder, open a terminal there and run pip install streamlit pandas "pulp[highs]>=4.3,<5".

  2. Run streamlit run whatif_app.py; a browser tab opens at http://localhost:8501. (Away from the course repository, the app downloads the data from the course website.)

  3. To share it, push the file and a requirements.txt (streamlit, pandas, pulp[highs]>=4.3,<5) to a GitHub repository and deploy it on Streamlit Community Cloud (share.streamlit.io).

The course’s finished version, with each offer valued alone, the contributions and demand limits, the reduced costs and a sweep chart of any resource, is week10/app/app.py in the course repository.

D3. Your recommendation

Use your app: tick the offers one at a time, then in combinations. Then write three sentences for the manager: which offers to take next month (with the net gain in SAR a week), why the overtime on its own is a weaker deal than its shadow price suggests, and one number in the model you would check before signing.

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

★ Extended challenges

E1. Every combination. With 4 offers there are 16 combinations. Solve the model for each (itertools.product([False, True], repeat=4)) and rank them by net gain. Which combination is best, and which two offers are worth more together than apart?

E2. Rent the kitchen. Write and solve the dual LP: choose a price for each hour of the three resources and for each batch of demand of each product, to minimise the total value of the kitchen’s hours and demand limits, such that for every product the hours and the demand it uses are worth at least its contribution. Compare the optimal value and the prices with Part A.

E3. Whole batches. Solve the model for the best combination of E1 with every variable cat="Integer". Does the recommendation change?

E4. A better app. Add to your app a table with each offer valued alone (shadow price × amount, re-solved gain, net), as in section 5 of the reading.

Your answer here (double-click to edit).

Your answer here (double-click to edit).

Your answer here (double-click to edit).

Your answer here (double-click to edit).

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

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