AlgoViz

Design an inventory management system

medium 80 min

Count goods in several places, promise them to orders without ever promising too much, ship or let go, and explain every count.

Watch lesson4:28

Transfers

Jars on the road

The point: moving goods takes hours. In between they are in neither place, so they need a place of their own.

Bug

A transfer in one step

cellar -= 5; shed += 5 sells jars in the shed that are still in a van. If the van is late, the shed has promises and no jars. If the van loses one, the books say it arrived.

Model

A line called in transit

Packing is an ordinary hold. So nobody can buy what is being loaded.
Python
from collections import Counterclass Books:    def __init__(self, start):        self.on_hand = Counter(start)            # (place, sku) -> count        self.ledger = [(key, n, "start") for key, n in start.items()]    def post(self, *rows):        """Several changes that belong together. All or none."""        for key, change, _ in rows:            if self.on_hand[key] + change < 0:                raise ValueError(f"{key} would be below zero")        for key, change, reason in rows:            self.on_hand[key] += change            self.ledger.append((key, change, reason))    def leave(self, tid, sku, qty, source):        self.post(((source, sku), -qty, tid), ((f"road:{tid}", sku), +qty, tid))    def arrive(self, tid, sku, counted, target):        road = (f"road:{tid}", sku)        sent = self.on_hand[road]        if not 0 <= counted <= sent or sent == 0:            raise ValueError("count again")        rows = [(road, -sent, tid), ((target, sku), +counted, tid)]        if counted < sent:            rows.append((("lost", sku), sent - counted, tid))        self.post(*rows)b = Books({("cellar", "plum"): 7})total = sum(b.on_hand.values())b.leave("T1", "plum", 5, "cellar")assert b.on_hand["shed", "plum"] == 0 and b.on_hand["road:T1", "plum"] == 5b.arrive("T1", "plum", 4, "shed")assert b.on_hand["shed", "plum"] == 4 and b.on_hand["lost", "plum"] == 1assert sum(b.on_hand.values()) == total          # nothing appears or vanishestry:    b.arrive("T1", "plum", 4, "shed")    raise AssertionError("arrived twice")except ValueError:    print("ok")
Every move has two sides that add up to zero. Bookkeepers have done it for centuries, and it makes a missing jar visible.
Edit and run it in your browser

Failure walk

Trouble on the road

What happensIn the booksWho acts
The van breaks down5 on the road, for daysA report of old transfers
4 of 5 arrive1 lost, with the transfer numberThe carrier pays
6 arrive, 5 were sentRefused: count againThe source counts its shelf too
Arrival is scanned twiceThe road line is empty: refusedNobody
The shed closes meanwhileArrive in another placeThe target is named on arrival and not before

May jars on the road be sold?

BreaksCount them in the target at once
How
One step transfer
Limit
The same jar is promised from the shed while it is in a van.
Choose when
Never with holds that ship today.
WorksNot before they arrive
How
The road line has no policy
Limit
Goods are invisible for hours.
Choose when
Short ways.
StrongAs a promise with a later date
How
Incoming is a third count with an expected day. A promise for that day may use it.
Limit
A late van breaks promises, so they need a margin.
Choose when
Long ways, large orders.