Technical

The mathematical model

Sets, parameters, binary decision variables, the objective function and the constraints.

Block Transfer · 3 / 7

The mathematical model is the part of most articles that readers scroll past. Seeing symbols, you feel like saying “you’d need to be a mathematician to follow this”. Do not skip this one, because there is nothing new in it. In the previous article Ali drew a pair of scales on the whiteboard: in one pan the sales to be won at the receiving store, in the other the sales to be lost at the sending store plus the cost of moving. Every symbol below stands for something on that board. All we are doing is writing the scales in a language the solver can read.

A promise: after every formula I will also write it out in plain English. If the two disagree, the formula is wrong, not the English.

The model here is not made up; it is exactly the working code in cases/block-transfer/. Every number below also comes from Lumoda’s real stock snapshot on a Monday in the middle of the season, before markdown.

Sets and indices

There are three sets. The first two are what you would guess:

M={1,…,80}storesO={1,…,1,870}options\begin{aligned} M &= \{1, \dots, 80\} && \text{stores} \\ O &= \{1, \dots, \text{1,870}\} && \text{options} \end{aligned}

MM is the physical stores open at the moment of the decision. Four of Lumoda’s 84 physical stores are closed that Monday; and the online channel does not take transfers.

The third is the most important part of this model, and the part most explanations skip:

A⊆{(i,j,o):i,j∈M,  i≠j,  o∈O}A \subseteq \{(i, j, o) : i, j \in M,\; i \neq j,\; o \in O\}

AA is the candidate set: the triples for which the question “can the block of option oo at store ii go to store jj?” is worth asking. Those not worth asking never get in.

The numbers say why this matters. The number of unconstrained triples:

80×79×1,870=11,818,40080 \times 79 \times \text{1,870} = \text{11,818,400}

Even if you look only at the 552 options on the shelves at the moment of the decision, it is 3,488,640. The real number of candidates in the reference scenario, that is with a coverCurrent stock divided by the rate of sale; it says how many weeks of sales the stock will last. floor of 6 weeks at the sending store and a minimum rate of sale of 1 unit a week at the receiving store: 5,369.

From eleven million to five thousand. What brings it down is not the model’s constraints but the set’s definition. To be a sending store there has to be stock, the set must not be broken (the core sizes on the rail), cover must clear the floor, and the rail must not be cooling. To be a receiving store the set must be brokenAn option's size set breaking up; its main measure is the net broken-size rate, the number of broken sizes divided by the number of sizes. or the stock zero, and on top of that the rate of sale must clear the threshold. A one-size option cannot have a broken set anyway (Lumoda has 211 of them); those only become candidates to receive once their rail is empty. And an option in the outlet line going to a full-price store is forbidden by the trade. The second number Ali wrote on the board, the maximum cover at the receiving store, is this set’s third gate (it sells, but the stock on hand is under the ceiling); every number in this article came out with that gate shut, when a receiving store could only be broken or out of stock.

Look once more at the “the set must not be broken” condition in the definition of a sending store. It was the trap Pelin found in the second article: a broken set stops selling, its cover heads for infinity, and a model that looks at cover alone counts that rail as the best sending store. We did not forbid this with a constraint. A broken rail never becomes a candidate to send; the solver does not even have to consider pulling stock from a broken set.

We could have written all of this into the solver as constraints. We did not, because then we would have built a model with 11,818,400 variables and waited for the solver to strike out 11,813,031 of them as “impossible anyway”. This may be the most practical sentence in the article: the most important part of a model is often the constraints you do not write. When business knowledge goes into the model not as constraints but as the definition of the set, the model gets smaller and stays readable.

Parameters

They all come from the data or from parameters.py; none of them is a magic number typed in by hand:

SymbolMeaningValue / source
sios_{io}Stock of option oo at store iiStock snapshot
viov_{io}Weekly rate of saleMean of the in-stock weeks in the last 8 weeks
fof_oThe option’s list priceProduct master
HHPlanning horizon8 weeks
ccMoving cost per unitTRY 25
RRFixed cost per routeTRY 500
KKMinimum shipment6 units
QjQ_jCapacity gap of store jjPeak stock − current stock

The word “in-stock” in the definition of viov_{io} deserves its bold. This is where Ali said in the previous article that “isn’t selling” and “can’t sell” are not the same: if you count the weeks the rail was empty in the average, you declare a product that could not sell because it was out of stock a “slow seller”. The rate is measured only on the weeks the stock was on the rail.

The peak stock in QjQ_j is the highest total stock the store actually held in the last 52 weeks. The store master has a capacity column too, but we do not use it, because at the moment of the decision 78 of the 80 stores are already over that number (median fill 1.76): the evidence of what a store can hold is not the capacity on paper but the load it has held before.

Decision variables

There are two. The first is the job itself:

xijo∈{0,1},(i,j,o)∈Ax_{ijo} \in \{0, 1\}, \quad (i,j,o) \in A

In plain English: should the block of oo at ii be moved to jj, or not? No fractions; there is no such thing as half a block. A block is not split, because what would be split is the size set.

The second looks unnecessary at first sight:

yij∈{0,1},(i,j)∈routes(A)y_{ij} \in \{0, 1\}, \quad (i,j) \in \text{routes}(A)

In plain English: is a shipment opened from ii to jj this week? Why a separate variable? Because moving stock has two kinds of cost. The per-unit part (c⋅sioc \cdot s_{io}) already scales with xx. But the cost of lifting the cartons, sending the van and having the other side open up goods-in depends not on the units but on the pair: whether you move one option or five between the same two stores, that cost is paid once. You cannot express that with xx; you need a variable that belongs to the route itself.

This is one of the subtle points of the model: without yy the model weighs every move on its own; with yy it learns to combine moves.

The objective function

First, the scales for a single candidate. Three parts:

gainijo=min⁡(sio,  vjo⋅H)⋅folossijo=min⁡(sio,  vio⋅H)⋅fotransportio=c⋅sio\begin{aligned} \text{gain}_{ijo} &= \min(s_{io},\; v_{jo} \cdot H) \cdot f_o \\ \text{loss}_{ijo} &= \min(s_{io},\; v_{io} \cdot H) \cdot f_o \\ \text{transport}_{io} &= c \cdot s_{io} \end{aligned}

In plain English, in order: we gain as much as the receiving store can sell of this block within the horizon; but it cannot sell more than the stock it is given, hence the minimum. When we take it from the sending store, we lose what it would have sold; the minimum again, for the same reason. Moving costs per unit.

The net weight is the difference between the two, less the cost:

wijo=gainijo−lossijo−transportiow_{ijo} = \text{gain}_{ijo} - \text{loss}_{ijo} - \text{transport}_{io}

And the objective:

max⁡∑(i,j,o)∈Awijo xijo  −  R∑(i,j)yij\max \sum_{(i,j,o) \in A} w_{ijo} \, x_{ijo} \;-\; R \sum_{(i,j)} y_{ij}

In plain English: add up the net benefit of the moves you choose, take off the fixed cost of every route you open, and make what is left as big as possible. The whole of the scales on the board is in this line.

Constraints

Four of them, each a direct translation of a business rule.

1. A block goes to one destination.

∑jxijo≤1∀i∈M,  o∈O\sum_{j} x_{ijo} \le 1 \qquad \forall i \in M,\; o \in O

The stock of an option at a store can be sent to one place at most. Splitting the same block in two and sharing it between two stores is outside the scope of this version; I explain why below.

2. The receiving store’s capacity is not exceeded.

∑(i,o)sio xijo≤Qj∀j∈M\sum_{(i,o)} s_{io} \, x_{ijo} \le Q_j \qquad \forall j \in M

You cannot send a store more stock than it has room for. Stockrooms and rails have their limits. This constraint does not stay on paper: at Lumoda, 12 of the 80 stores have no room at all at the moment of the decision; in the next two articles we will see how this line shapes the plan.

3. No move without an open route.

xijo≤yij∀(i,j,o)∈Ax_{ijo} \le y_{ij} \qquad \forall (i,j,o) \in A

If there is a move, a shipment has been opened between that pair. This constraint keeps yy honest: the model cannot use a route for free.

4. An open route fills a shipment.

∑osio xijo≥K yij∀(i,j)\sum_{o} s_{io} \, x_{ijo} \ge K \, y_{ij} \qquad \forall (i,j)

If you open a shipment, move at least the minimum shipment. You do not send a lorry for two units. Note that this constraint works together with the third. The third says “no move without an open route”, the fourth “an open route does not go empty”. Together they push the model to gather small moves onto the same route.

Scale: 7,730 variables, 8,965 constraints

In the reference scenario the model’s real size is:

PartCount
xx variables (candidate moves)5,369
yy variables (routes)2,361
Total binary variables7,730
Block uniqueness constraints1,155
Capacity constraints80
Route linking constraints5,369
Minimum shipment constraints2,361
Total constraints8,965

Now look at that 7,730. Since each variable can be 0 or 1, the number of possible assignments is 27,7302^{\text{7,730}}, about 102,32610^{\text{2,326}}. The number of atoms in the observable universe is reckoned at roughly 108010^{80}. So if you wanted to try a single week’s transfer decision for this chain by brute force, you would not finish even if you gave every atom in the universe its own computer.

The time the solver (CBC) takes to finish with this model: under a minute.

Put those two sentences side by side and the first thing that comes to mind is “there must be a mistake somewhere”. There isn’t. The solver does not count combinations. First it relaxes the problem: it lets the xx values be fractions between 0 and 1, and in that form the problem is a linear programme that solves quickly. The number that comes out is an upper bound on the real best solution. Then it picks a variable that came out fractional and opens two branches (“set this to zero” / “set this to one”), recomputes the upper bound in each branch, and if a branch’s upper bound is worse than the best solution in hand, it throws the whole branch away, billions of combinations, in one go. It is called branch and bound. This is the answer to the scales Ali said could not be set up a hundred and fifty thousand times: nobody sets them up; the solver strikes them out without looking.

Let me say one thing up front, because this is where we most often go wrong when reading solver output. We did not tell the solver “find the best”; we told it “stop when the gap between the plan in hand and the upper bound falls below 0.5%”. With this model, the gap where it stopped was 0.18%. So we cannot call the plan we have “the best plan”; we can call it “a plan at most 0.18% away from the best plan”. I will open up the difference between the two in the fifth article.

The plan the solver finds moves 661 options, 109,100 units in all. In the next two articles we will solve this problem two different ways, and the two ways will not arrive at the same place.

Assumptions, and where this model falls short

Let’s be honest: the tidiness above is not real life itself but a simplified version of it.

ww is not profit but the gross revenue effect. Gain and loss are worked out at list price; the cost price is not taken off. So the model answers “how much revenue does this transfer move around”, not “how much profit does it make”. Since the same product sits in both pans of a move, the margin shrinks both pans in the same proportion; the real question comes up when moves compete with one another, because at Lumoda the margin varies by product: the gross margin runs from 54.5% to 68.4%. When we measured it, setting up the scales with profit changes roughly a tenth of the greedy plan and a fifth of the solver’s plan; profit goes up by only a few tenths of a per cent (0.68% for greedy, 0.18% for the solver; the second is within the solver’s own tolerance). That is why we left the objective in revenue. Keep this in mind when reading the TRY figures in the results article: they are not profit but revenue on the move.

A block is not split. Stock taken from a store goes to one destination. Splitting it in two looks easy in the maths but raises two new problems: which sizes go to whom, and are two half sets better than one full set? That is a different kind of transfer and deserves an article of its own.

The horizon is fixed at 8 weeks. The data has both the season code and the markdown calendar; we deliberately chose a decision point in the middle of the season, before markdown, and the eight-week horizon closes before markdown starts. But the “shelf life dial” of the second article, the scales tipping towards moving as the end of the season approaches, is not in the model. In a real set-up HH varies by option and is tied to the markdown date; since the calendar is in the data, that is not missing data but the next step.

Cooling sees every arrival. Alongside warehouse shipments, the data has past store-to-store transfers too (3,049 rows). The cooling rule does not care which route the stock took to the rail; if something arrived in the last two weeks, the rail is cooling. At the moment of the decision 11,250 rails are cooling, and 21 of them got their stock from another store. That is why the rule “we already took from there last week” can be tested.

Demand is measured censored. We work out vjov_{jo} from observed sales, but a store with a broken set has higher real demand than we measure. So the store most in need of help looks a little dimmer to the model than it really is. We did not try to correct this; correcting it would mean building an estimate on an estimate and would blur what the model rests on.

Capacity is in units and measured from the past. Floor space, department and hanging capacity are not modelled separately; a store is taken to be unable to hold a load it never held last year. In your data this is probably where it will be hard: nobody keeps the difference between capacity on paper and the load a store actually holds in a table; it is the store, not the data, that decides which is right.

In the next article we will attack the problem without building this model at all, with just SQL and a ranking rule, because that is mostly how the trade does it. Then we will put the two side by side. I will tell you now: the difference is 9.1 per cent, and where it comes from is not where you would guess 🙂

By Sabri Suyunu