Inventory 2: Feedback Through Shared State
Requires Pro (SharedState)
Problem statement
Lesson 5.1 built a store in one Recur node: the stock, the orders on their way and the reorder rule all live in one expression, and nothing outside the node can read or change the stock. That works for one store and stops working for two stores that draw on one warehouse. This lesson gives the stock a home of its own. A shared cell holds the stock; separate nodes read it, decide, and write it back. The store is a named thing, the policy is a small calculation of its own, and a loop (the stock decides the order, the order changes the stock) is allowed because every loop goes through a cell.
The store is the one of lesson 5.1, with every choice unchanged: one row is one day; demand is NegativeBinomial with r = 8 and p = 0.5 (mean 8, variance 16); the store orders Q = 40 when its position is at or below the reorder point R = 16; an order takes 2 days; unmet demand is lost; it starts with 40 units and nothing on order. Lesson 5.1 defines the four measures used here: a stockout day is a day on which some demand is not met, the fill rate is 1 minus the lost units over the units demanded, average stock is the mean end-of-day stock, and orders a year is the number of days with an order, scaled to 365.
The questions:
- Can the store and its policy be separate, named parts, and what exactly does a part see when it reads the stock?
- Does it matter where the nodes sit in the model?
- Every loop through a cell has a delay of one row. What does that do to a policy that worked in lesson 5.1?
- How do the service measures move as the reorder point goes from 8 to 28?
The model
The store lives in a SharedState node, which holds named numbers called cells. Two other nodes touch it: StateRead emits the current value of chosen cells, and StateWrite passes its input through unchanged and writes it into chosen cells. The model is samples/pro/inventory-shared-store.xml:
<dataset columns="stock, lost, demand, orderDue" seed="501">
<root node="Day"/>
<nodes>
<SharedState name="Store" cells="stock=40, due=0, lost=0, demand=0"/>
<StateRead name="ShelfForPolicy" collaborate.state="Store" cells="stock, due"/>
<Calculate name="Policy" dimension="1" expression="y[0] = if(x[0] + x[1] <= 16, 40, 0)">
<input name="ShelfForPolicy"/>
</Calculate>
<StateWrite name="PlaceOrder" collaborate.state="Store" cells="due" mode="set">
<input name="Policy"/>
</StateWrite>
<StateRead name="ShelfForSales" collaborate.state="Store" cells="stock, due"/>
<NegativeBinomial name="Demand" r="8" p="0.5"/>
<Append name="Morning">
<input name="ShelfForSales"/>
<input name="Demand"/>
</Append>
<Calculate name="Sales" dimension="3"
expression="y[0] = max(0, x[0] + x[1] - x[2]); y[1] = max(0, x[2] - x[0] - x[1]); y[2] = x[2]">
<input name="Morning"/>
</Calculate>
<StateWrite name="Close" collaborate.state="Store" cells="stock, lost, demand" mode="set">
<input name="Sales"/>
</StateWrite>
<Append name="Day">
<input name="Close"/>
<input name="PlaceOrder"/>
</Append>
</nodes>
</dataset>
The parts:
| Node | What it is |
|---|---|
Store | The cells, each with its starting value: stock (units on the shelf at the end of the day, starting at 40), due (the order that arrives at the start of the next day, 0 for none), and lost and demand (the day’s lost units and demand). Nothing reads lost and demand: they are written so that the day’s numbers go through the same door and appear in the row. |
ShelfForPolicy | A StateRead: its two output columns are the cells stock and due. The collaborate.state="Store" attribute names the store; it is not an <input>, so the reader is not part of the store’s tree. |
Policy | The reorder rule as a Calculate of its own: if stock plus due is at most 16, order 40, else 0. dimension="1" makes it a one-column output. The rule is 38 characters, and 16 and 40 stand alone; in lesson 5.1 they sat inside a 143-character expression. |
PlaceOrder | A StateWrite that writes the order into due. mode="set" replaces the cell. |
ShelfForSales, Demand, Morning, Sales | The day: read the shelf again (a StateRead can feed only one node, so a second reader is the way to read a cell in two places), draw the demand, and compute the new stock max(0, stock + due - demand), the lost units and the demand. |
Close | Writes the three results into the cells stock, lost and demand. |
Day | The row: the three values Close passes through, then the order. |
Run one year of 365 days:
grind --count 365 inventory-shared-store.xml
The first nine rows with the model’s own seed, 501, which is the seed of lesson 5.1:
| Day | Demand | Stock at end | Lost | Order (arrives tomorrow morning) |
|---|---|---|---|---|
| 1 | 14 | 26 | 0 | 0 |
| 2 | 2 | 24 | 0 | 0 |
| 3 | 4 | 20 | 0 | 0 |
| 4 | 5 | 15 | 0 | 0 |
| 5 | 10 | 5 | 0 | 40 |
| 6 | 19 | 26 | 0 | 0 |
| 7 | 10 | 16 | 0 | 0 |
| 8 | 4 | 12 | 0 | 40 |
| 9 | 4 | 48 | 0 | 0 |
The demand and the stock are the numbers of lesson 5.1’s table, day for day. The orders are there too, with one difference of bookkeeping that matters later: lesson 5.1 shows an order on the day the store places it (day 4) and again, a day later, in its “arriving tomorrow” column (day 5). This model’s order column is the second one. On day 5 the policy reads a stock of 15, finds it is at most 16 and writes an order of 40; on day 6 the order is read from due, so the stock is 5 + 40 - 19 = 26. On day 8 the same thing happens again.
How close is it to lesson 5.1? Run both models with the same seed and compare. For the 365 rows of seed 501, the stock, the lost units, the demand and the orders agree in every row; so they do for 250,000 rows of seed 77; and for 400 one-year runs (seeds 1 to 400) the stockout days and the fill rate are identical in all 400 years. Same seeds, same numbers, from two different constructions. The rest of the lesson explains why, and shows what happens when the translation is done more literally.
The year of seed 501: 17 stockout days, 99 of 3,047 units demanded lost (a fill rate of 96.75%), 73 orders placed (lesson 5.1 counted 74, because it counts the order placed on the night of day 365) and an average end-of-day stock of 20.18. The bookkeeping check of 5.1 also holds: 40 + 40 x 73 orders placed in rows 1 to 364 - (3,047 - 99) = 12 is the stock on day 365.
What a read sees
Every read of a cell returns the value as of the end of the previous row. The first row reads the starting values. Every write takes effect at the end of its own row, for the reads of the next row. That is the double-buffered row rule, and learning that rule is critical to understanding how SharedState works.
To see the reads, add one more reader, <StateRead name="Seen" collaborate.state="Store" cells="stock, due"/>, to a copy of the model and put it first under the root Append. Its two columns are what a reader sees in each row. The first six rows (seed 501):
| Row | Read: stock | Read: due | Demand | Policy writes (due) | Sales writes (stock) |
|---|---|---|---|---|---|
| 1 | 40 | 0 | 14 | 0 | 26 |
| 2 | 26 | 0 | 2 | 0 | 24 |
| 3 | 24 | 0 | 4 | 0 | 20 |
| 4 | 20 | 0 | 5 | 0 | 15 |
| 5 | 15 | 0 | 10 | 40 | 5 |
| 6 | 5 | 40 | 19 | 0 | 26 |
The read columns of each row are the write columns of the row before. In all 365 rows of that run the stock and the order that a reader sees equal the ones written in the row before; in row 1 the reader sees the starting 40 and 0. Row 6 shows the delivery: the policy of row 5 wrote 40, row 6 reads it, the sales node adds it to the shelf, and the policy of row 6, seeing a position of 5 + 40 = 45, writes no order, so due goes back to 0.
Three consequences.
Where a node sits does not matter. A read never sees a write of its own row, so no node’s result depends on which node ran first. Two checks on 100,000 rows (seed 7). Swapping the two subtrees under the root Append (PlaceOrder first, Close second) gives the same numbers, with the order column now first: the first row is 21, 0, 19, 0 before the swap and 0, 21, 0, 19 after it, and all 100,000 rows match once the columns are put back. Listing the ten nodes in the file in the reverse order gives identical rows. Lesson 3.1’s Recur and a chain of Calculate nodes have no such freedom: there, the order in which values flow is the model.
Writes have rules. A cell may have no writer, one set writer, any number of add writers, or one set writer plus any number of add writers. At the end of a row the cell becomes its set value (or its old value, if nobody set it) plus the sum of the row’s add amounts, whichever writer ran first. The second shipped sample, samples/pro/inventory-shared-rules.xml, has nothing random in it, so the rules are visible. One cell, level, starts at 100. Three nodes write it every row: Receive adds 10, Sell adds -4, and Reset sets the cell to 50 in row 5 and, in every other row, back to what it read, which changes nothing:
| Row | Seen (read) | Reset writes | Delivery (add) | Sale (add) |
|---|---|---|---|---|
| 1 | 100 | 100 | 10 | -4 |
| 2 | 106 | 106 | 10 | -4 |
| 3 | 112 | 112 | 10 | -4 |
| 4 | 118 | 118 | 10 | -4 |
| 5 | 124 | 50 | 10 | -4 |
| 6 | 56 | 56 | 10 | -4 |
| 7 | 62 | 62 | 10 | -4 |
In rows 1 to 4 the set writes back what it read and the adds make the cell grow by 6 a row. In row 5 the reader still sees 124, the end of row 4; the cell ends the row at the set value plus the adds, 50 + 10 - 4 = 56, and row 6 reads 56. Two breaches of the rules are reported before any row is produced. Two set writers on one cell: ERROR: cell 'level' of SharedState 'Store' has two set writers: 'W1' and 'W2' (one set writer plus any number of add writers is allowed). A reader asking for a cell that is not in the store: ERROR: StateRead 'Look': cell 'stok' is not in SharedState 'Store' (cells: level).
Every loop through a cell has a delay of one row. The model above is a loop: the policy reads the stock and writes an order, and the order changes the stock. Without cells, a loop of two nodes reading each other is refused (ERROR: Circular dependency detected among model node inputs: [A, B]). Through a cell it is allowed, and the price is the delay: the policy in row n never sees the demand of row n. It sees the stock at the end of row n-1. Whatever has to respond within the same row belongs in a single Calculate or in Recur.
The one-row delay and the lead time
Now the question that the double buffer raises: lesson 5.1 reviewed the stock at the end of the day, after the day’s sales, and the store here reviews it at the start of the next row. Is it the same store?
Follow one order. In lesson 5.1 the order placed at the end of day n arrives in the morning of day n + 2. Here the policy of row n + 1 reads the stock at the end of day n (the very number 5.1 used), writes the order into due, and row n + 2 reads due and adds it to the shelf in the morning. Same information, same arrival day. The delay of the loop is one of the two days of lead time, so the pipeline needs one cell, not two. That is why the store above has a single due and reproduces lesson 5.1 row for row.
A more literal translation would not do that. Lesson 5.1’s Recur kept two pipeline columns: the order arriving tomorrow and the order placed tonight. Copying them as two cells, tomorrow and after, with the policy reading the whole inventory position (stock plus both), is the natural cell-for-cell translation. It is the second sample, samples/pro/inventory-shared-cellwise.xml (10 nodes, the same demand, seed 501, R = 16, Q = 40). Its policy and its writes:
<Calculate name="Policy" dimension="1" expression="y[0] = if(x[0] + x[1] + x[2] <= 16, 40, 0)">
<input name="ShelfForPolicy"/>
</Calculate>
<StateWrite name="PlaceOrder" collaborate.state="Store" cells="after" mode="set">...</StateWrite>
<!-- Sales moves the pipeline along: new stock = max(0, stock + tomorrow - demand), tomorrow := after -->
It runs, and it is a different store. The order decided in a row is read one row later and then waits two more days, so it arrives three days after the end of the day whose stock the policy looked at, where lesson 5.1’s arrived after two. The first five days are identical (the first order is placed in row 5 in both). Then:
| Day | Demand | Store: stock | Copy: stock | Copy: lost |
|---|---|---|---|---|
| 5 | 10 | 5 | 5 | 0 |
| 6 | 19 | 26 | 0 | 14 |
| 7 | 10 | 16 | 30 | 0 |
| 11 | 14 | 24 | 0 | 2 |
| 12 | 7 | 17 | 0 | 7 |
On day 6 the store’s 40 units are on the shelf; the copy’s are still a day away, and it loses 14 units. The picture follows both for the first 60 days of seed 501.
In these 60 days the store has 1 stockout day and the copy 13. Over the whole year of seed 501 the store has 17 stockout days, a fill rate of 96.75% and 73 orders, and the copy 65 stockout days, 438 of 3,047 units lost (85.63%) and 65 orders.
One year is one draw, so use the same trick as lesson 4.3: give both stores the same demand (the same seeds) and look at the difference. Over 400 years (seeds 1 to 400) the copy has more stockout days than the store in every one of the 400 years, by 43.34 days on average (standard error 0.29), and the fill rate is 9.44 percentage points lower (standard error 0.06). The exact long-run values, from a Markov chain for each model (below):
| Measure, long run | Store (one pipeline cell) | Copy (two pipeline cells) |
|---|---|---|
| Stockout days a year | 16.11 | 59.70 |
| Fill rate | 97.614% | 88.106% |
| Orders a year | 71.26 | 64.32 |
| Average stock | 21.003 | 15.862 |
The store’s four numbers are the four numbers of lesson 5.1 (16.113, 97.614%, 71.258, 21.003). The copy holds less stock, orders less often and serves much less: 3.7 times the stockout days. Nothing in it is wrong. It is a correct model of a store that looks at its shelf a day late and then waits two days, and R = 16, which equals the mean demand of two days, protects it badly. The price of the extra day in reorder point is about one day of mean demand: the store reaches a fill rate of 97% at R = 16 (97.61%), the copy only at R = 25 (97.23%, 22.05 units of average stock against 21.00).
The rule for moving a store from Recur into cells: count the lead time from the row that reads, not from the row that writes the order. The delay of the read is a day of the lead time, so a store that takes L days from the end of the day needs L - 1 pipeline cells (one cell for the 2 days of this lesson). It also sets a floor: the shortest lead time a cell-based store can have is these 2 days. A store that must respond within the same day, with an order that arrives the next morning, cannot be built with a loop through a cell, and belongs in Recur.
Measuring service across reorder points
With the store reproducing lesson 5.1, the question is how the service moves with R. Each reorder point R from 8 to 28 (Q = 40) was run for 20 runs of 250,000 days (the first 1,000 days of each run dropped as warm-up) and set beside the exact answer of the Markov chain. No cost function yet: the measures are the ones the store reports, and lesson 5.5 puts prices on them.
| R | Stockout days a year | Fill rate | Orders a year | Average stock |
|---|---|---|---|---|
| 8 | 58.11 (58.16) | 88.937% (88.933%) | 64.92 (64.92) | 15.64 (15.64) |
| 10 | 45.68 (45.65) | 91.848% (91.851%) | 67.06 (67.05) | 16.66 (16.66) |
| 12 | 34.07 (34.03) | 94.306% (94.307%) | 68.84 (68.84) | 17.91 (17.91) |
| 14 | 24.07 (24.05) | 96.228% (96.222%) | 70.27 (70.24) | 19.36 (19.37) |
| 16 | 16.07 (16.11) | 97.621% (97.614%) | 71.25 (71.26) | 21.00 (21.00) |
| 18 | 10.31 (10.26) | 98.555% (98.561%) | 71.95 (71.95) | 22.78 (22.78) |
| 20 | 6.20 (6.23) | 99.172% (99.168%) | 72.38 (72.39) | 24.65 (24.64) |
| 22 | 3.60 (3.62) | 99.540% (99.538%) | 72.64 (72.66) | 26.57 (26.57) |
| 24 | 2.04 (2.02) | 99.751% (99.752%) | 72.83 (72.82) | 28.53 (28.53) |
| 26 | 1.07 (1.09) | 99.874% (99.871%) | 72.92 (72.91) | 30.51 (30.52) |
| 28 | 0.56 (0.57) | 99.936% (99.935%) | 72.94 (72.95) | 32.51 (32.51) |
Each cell is measured (exact). The standard errors of the measured values run from 0.006 to 0.05 days for the stockout days and from 0.0009 to 0.011 percentage points for the fill rate (the script prints every one).
The trade-off is the usual one, and the exact curve has an interesting shape. Each unit of reorder point buys less than the one before. From R = 8 to 9 the fill rate rises by 1.51 points for 0.48 units of average stock (3.1 points per unit of stock); from 12 to 13 by 1.03 points for 0.70 units (1.5 per unit); from 16 to 17 by 0.52 points for 0.87 units (0.6 per unit); from 20 to 21 by 0.21 points for 0.96 units (0.2 per unit); and from 24 to 25 by 0.07 points for 0.99 units (0.07 per unit). Past R = 20 almost every extra unit of reorder point is one more unit sitting on the shelf: the average stock grows by about 1 unit per unit of R, where at R = 8 it grew by half a unit.
The stockout days fall faster than the fill rate rises: every four units of R cut them to less than half (34.0 a year at R = 12, 16.1 at 16, 6.2 at 20, 2.0 at 24, 0.57 at 28). The orders a year rise from 64.9 to 72.95, and cannot pass 73: a year’s mean demand is 2,920 units, which is 73 orders of 40. So the fill rate of 98% needs R = 17 (98.14%, average stock 21.88), and 99% needs R = 20 (99.17%, 24.64). Whether either is worth its stock is a question of costs, and lesson 5.5 asks it. The curve of the cell-for-cell copy lies below, and to the right: the same average stock gives a lower fill rate (97% at R = 25), because every unit of its reorder point has to cover one more day.
Check: do the numbers match the math?
The exact answers come from Markov chains (lesson 3.5), one per model, written in learn/figures/sim101-22-make.py with the standard library only. For the store, the state at the end of a row is (stock, order due), and the next state depends on that and on the day’s demand, whose probabilities are the negative binomial’s. 74 states can be reached from (40, 0), the same number as in lesson 5.1, and the stationary distribution, the long-run share of days in each state, follows from repeating the step (203 steps). The chain for the copy has the state (stock, tomorrow, after) and 91 states. The two chains differ in one line each: the store orders when stock + due <= R, and the copy when stock + tomorrow + after <= R.
The store’s chain gives exactly the stationary answer of lesson 5.1’s chain: the four measures agree to within 6e-13 and the stock distributions are equal. That is the exact version of “the delay is a day of lead time”.
Long runs, 80 runs of 250,000 days each (seeds 1000 to 1079, the first 1,000 days dropped, 20 million days per model). “z” is the gap divided by the standard error:
| Measure | Store measured (se) | Store exact | z | Copy measured (se) | Copy exact | z |
|---|---|---|---|---|---|---|
| Stockout days a year | 16.085 (0.015) | 16.113 | -1.83 | 59.667 (0.027) | 59.704 | -1.38 |
| Fill rate | 97.619% (0.0028) | 97.614% | 1.65 | 88.115% (0.0058) | 88.106% | 1.70 |
| Orders a year | 71.246 (0.0074) | 71.258 | -1.66 | 64.310 (0.0055) | 64.317 | -1.28 |
| Average stock | 21.007 (0.0026) | 21.003 | 1.60 | 15.863 (0.0018) | 15.862 | 0.71 |
All eight agree to within 2 standard errors. Because the seeds are the same as in lesson 5.1, the store’s measured numbers are the same as that lesson’s, to every digit, and the batch totals of the two models (days, stockout days, lost units, demand, stock) are identical in all 80 runs. The measured stock distributions are within a total variation distance of 0.00057 (store) and 0.00058 (copy) of the exact ones; the empty shelf is the end-of-day state on 5.46% of days for the store (exact 5.46%) and on 18.27% of days for the copy (exact 18.28%). The measured demand has mean 7.9982 and variance 15.985 (exact 8 and 16).
The first year of seed 501 is a draw; over 400 one-year runs the means are 15.98 stockout days for the store (exact for a year from the starting state 16.05, z -0.33) and 59.32 for the copy (exact 59.38, z -0.22). The fill rates are 97.63% (exact 97.62%, z 0.02) and 88.18% (exact 88.17%, z 0.13).
The sweep is 168 comparisons of a measured value with its exact value (21 reorder points, 4 measures, both models; the copy’s sweep is in the figure only). The largest |z| is 2.77, 6 comparisons are beyond 2 (about 8 are expected from chance alone) and none is beyond 3. The companion page’s grid (66 policies, 20 runs of 50,000 days each) has 264 such comparisons; its largest |z| is 3.68, and 6 are beyond 3, where about 2 would be expected, with the standard errors estimated from only 20 runs. They have both signs and no pattern in the measure or the policy, but they are the less certain numbers of this lesson, and the page says how many runs each value comes from.
A hand check: every unit sold arrived on an order, so over a long time the orders times the order quantity equal the units sold. In the 80 long runs of the store, the orders times 40 come to 156,155,880 units and the units sold to 156,155,879. And the orders a year tend to 73 as R grows, because 365 days of mean demand 8 is 2,920 units, or 73 orders of 40: the exact store gives 72.95 at R = 28.
Looking ahead: the store as a library
Lesson 5.3 needs two stores that share a warehouse, each an Import of the same store library (lesson 4.1’s declare and Import). A store written in cells can be a library, with these facts, tested on a copy of this model turned into a library:
- A declared parameter replaces a whole attribute value and nothing smaller. The policy numbers R and Q therefore travel as
Constantcolumns (<Constant name="Rcol" value="$R" type="double"/>, appended to the policy’s input and read in the expression asx[2]andx[3]), becauseif(x[0] + x[1] <= $R, $Q, 0)is refused (ERROR: Calculate 'Policy' y[0]: unexpected at '$R, $Q, 0)'). The starting stock is part of thecellsstring, so the whole string is declared,cells="$cells"with<declare name="cells" value="stock=40, due=0, lost=0, demand=0"/>, and an import overrides it as a whole (cells="stock=7, due=0, lost=0, demand=0");cells="stock=$start, ..."is refused (cell 'stock' has initial value '$start', which is not a number). - Each import has its own store, and its own demand stream. Two imports of the library with
R="12" Q="30"andR="20" Q="50"run side by side and draw different demands; a node outside the library can read a cell inside one, by the prefixed name, with<StateRead name="SeeA" collaborate.state="A.Store" cells="stock"/>. - A node outside the library can also write into a cell inside it. A
StateWrite mode="add"in the parent adding -5 toA.Store’sduecell, next to the library’s ownsetwriter, turns an order of 40 into a delivery of 35 (a row with stock 24 and an order of 40 is followed by a row with stock 24 + 35 - 12 = 47; the test adds -5 every row, so a row without an order also gets -5, and a real rule would add only the shortfall): the set-then-add rule from above, across the import boundary. That is the hook for a warehouse that ships a store less than it asked for. - The library store, with its declared defaults, matches the same exact answer: 40 runs of 250,000 days (seeds 7000 to 7039) give z values of -0.31 for stockout days, 0.33 for the fill rate and 1.14 for the average stock, and -2.60 for the orders (71.233 a year against 71.258). A second, independent set of 80 runs (seeds 8000 to 8079) gives -0.81 for the average stock, 0.14, -0.10 and 0.78 for the orders: the -2.6 did not repeat.
What the model leaves out
Everything lesson 5.1 left out is still out: a fixed lead time, orders that arrive whole, a review every day, demand that is the same every day and independent from day to day, customers who are lost for good, and no costs. The new limitation is the one above: the policy always acts on the stock as it stood at the end of the previous day, and the shortest delivery is two days.
What this gives you: the store of lesson 5.1 as separate, named parts: a SharedState of cells, a StateRead that sees the end of the previous row, a policy that is a Calculate of its own (38 characters, with if and a comparison) and StateWrite nodes in set and add modes. The double-buffered row rule means that no node’s position changes any result (the swapped and the reversed model give the same rows) and that every loop has a one-row delay, which is one day of lead time: the store with one pipeline cell reproduces lesson 5.1 row for row and matches its exact Markov chain (16.085 stockout days a year measured against 16.113 exact, fill rate 97.619% against 97.614%), while the cell-for-cell copy of the two pipeline columns waits a day longer and has 59.7 stockout days instead of 16.1 at the same policy. Sweeping R from 8 to 28 shows the trade-off with exact values beside the measured ones. The companion page lets you pick the store, R and Q.
The store is now something other nodes can reach. Lesson 5.3 imports it twice, gives each copy its own demand, and has both order from one warehouse that is itself a cell: when the warehouse is short, someone has to decide who gets what, and the lesson compares the rules, and compares one warehouse against two half-sized ones.