Functional Weave
Code in Rust

manufacturing.batch-size@1.0.0

README.md

2,196 bytes · view raw

# manufacturing.batch-size

The economic batch quantity (EBQ, also called the economic production quantity
or EPQ): the batch size at which yearly set-up cost equals yearly holding cost
when stock is made in-house at a finite rate rather than delivered all at once:

    EBQ = sqrt( 2 * D * S / (H * (1 - D / P)) )
        = sqrt( 2 * D * S * P / (H * (P - D)) )

`D` is annual demand, `P` the annual production rate (what the process would
make in a year of running), `S` the set-up cost of one batch and `H` the cost of
holding one unit for a year.

**Why not just EOQ.** Because stock is used while the batch is still being
made, the peak stock is `Q * (1 - D/P)`, not `Q`, so the batch is larger than
the economic order quantity by `sqrt(P / (P - D))`. With demand 1,000, rate
2,000, set-up £5 and holding 50p, EOQ (`inventory.eoq`) says 141; the EBQ is
200. As `P` grows the EBQ falls back to the EOQ.

**Whole units, rounded exactly.** As in `inventory.eoq`, the root is never taken
in floating point. The floor comes from `math.integer-sqrt` applied to
`floor(2DSP / (H(P - D)))`, and the rounding decision compares whole numbers,
so an exact half (sqrt(6.25) = 2.5) goes by `mode`, not by the language:

- `down`: the floor; `up`: the floor plus one unless the ratio is a perfect square
- `half-up`: plus one when `(2n + 1)^2 * H(P - D) <= 8DSP`
- `half-even`: as half-up, but an exact half goes to the even neighbour

**Limits.** `8 * D * S * P` must stay within 2^63 - 1 and the ratio under the
root within 2^53 - 1 (the range `math.integer-sqrt` takes); both are far beyond
real plants. Money is `Money` in minor units, set-up and holding in the same
currency; the currencies cancel, so the answer is a plain unit count.

Zero demand gives 0. The production rate must be greater than demand (at or
below it the process can never build stock and the formula has no answer),
holding cost must be positive, and neither demand nor set-up cost may be
negative.

Sources: E. W. Taft, "The most economical production lot", The Iron Age 101,
1918; any operations-management text, e.g. Slack, Brandon-Jones and Johnston,
*Operations Management*, chapter on inventory planning (the EBQ model).