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.

Chapter 14 — Assignment Project: How Much Stock to Order?

Open in Colab

Overview

A small shop sells one product. Every morning the owner decides how many units to order; they arrive at once, then customers come. Ordering too little loses sales and annoys customers; ordering too much ties up money and shelf space. The owner asks: what is the best ordering rule, and how much better is it than what I do now?

You will model the problem as an MDP, solve it exactly, compare it with simple rules of thumb, and then let a Q-learning agent discover a rule from experience alone, as it would have to if the demand were unknown.

The rules of the shop (per day)

ItemValue
Shelf capacity20 units (stock + order ≤ 20)
Daily demandrandom, Poisson with mean 4
Selling price10 per unit sold
Purchase cost4 per unit ordered
Delivery fee10 for any order (0 if nothing is ordered)
Holding cost0.5 per unit left on the shelf at the end of the day
Lost-sale penalty2 per customer turned away (lost goodwill)
Discount factorγ = 0.95
PartTaskPoints
AModel the shop as an MDP20
BSolve it exactly with value iteration20
CSimulate the policies; compare with rules of thumb15
DLearn from experience with Q-learning25
EReport for the owner20

Part A — The MDP (20 points)

The state is the stock in the morning (0–20), the action the number of units ordered (0 to 20 − stock).

A1 (checked). Complete one_day(stock, order, demand), returning (stock left at the end of the day, profit of the day) according to the table.

A2 (checked). Complete build_model(max_demand=30), returning P[s][a] as a list of (probability, next_state, expected_reward, False) tuples, one per distinct next state (several demands can lead to the same next state: add their probabilities, and average their profits weighted by probability). Use poisson.pmf(d, MEAN_DEMAND) for d=0,…,d = 0, \dots, max_demand − 1, and put all the remaining probability on the last value.


Part B — The Optimal Policy (20 points)

B1. Solve the MDP with value iteration (your function from the lab, adapted, is fine). Print the number of sweeps and the optimal order for every stock level.

B2. Plot the optimal order and the resulting order-up-to level (stock + order) against the morning stock. Describe the rule in one sentence a shop owner would understand.

The rule:


Part C — Simulation and Rules of Thumb (15 points)

C1 (checked). Complete simulate(policy, days, seed): start with an empty shelf, draw each day’s demand with rng.poisson(MEAN_DEMAND) from rng = np.random.default_rng(seed), apply policy(stock) and one_day, and return the average daily profit.

C2. Compare in one table: the optimal policy; “order 4 units every day (as many as fit)”; “order up to SS every day” for the best SS; the best (s,S)(s, S) rule you can find by trying every s≤8s \le 8 and s<S≤20s < S \le 20; and the optimal policy for γ=0.99\gamma = 0.99. Choose the best SS and (s,S)(s, S) on simulations with seed 0, then score every policy on 50,000 new days (seed 1): choosing and scoring on the same random days would flatter the chosen rules.

C3. The “optimal” policy of Part B is not the top of your table. Explain why that is not a contradiction.

Your answer:


Part D — Learning from Experience (25 points)

Suppose the owner does not know that demand is Poisson with mean 4. A Q-learning agent can still learn by living through simulated days (in practice, a simulator fitted to the shop’s sales history).

D1. Implement tabular Q-learning over 300,000 simulated days: α=0.1\alpha = 0.1, γ=0.95\gamma = 0.95, ε decaying linearly from 1 to 0.05 over the first 100,000 days, only valid orders (stock + order ≤ 20), demand from rng.poisson(MEAN_DEMAND) with seed 1. Every 25,000 days, evaluate the current greedy policy with simulate(..., days=5000), and plot this learning curve against the optimal profit.

D2. Compare the learnt policy with the optimal one (order for each stock level, and average daily profit over 20,000 days). Where do they differ, and why there?

Your answer:

D3. Learning by trial and error in the real shop would mean ordering random quantities for months. In two or three sentences: what would that cost, and what do companies do instead?

Your answer:


Part E — Report for the Owner (20 points)

Write 250–350 words for the shop owner:

  1. The recommended rule, in one sentence, and why ordering in batches beats ordering a little every day.

  2. The gain: average daily profit of the rule against the simple rules (your table), and what that means per year.

  3. How the rule was found: exactly from a model of demand, and again by a learning agent that did not know the model.

  4. Limits: what assumptions it rests on (demand pattern, prices, instant delivery), and when to recompute it.

Your report:


Grading Rubric

PartFull marks if…
A (20)the daily rule and the model are correct (checked)
B (20)value iteration converges; the policy is plotted and described as an (s,S)(s, S) rule
C (15)the simulator is correct (checked); a fair table including the best simple rules
D (25)Q-learning with valid actions and decaying ε; a learning curve; an explained comparison with the optimal policy; D3 answered
E (20)clear for a non-specialist; numbers match the notebook; assumptions stated