# Bit-width allocation

> Given a storage limit and predicted losses at several precisions, choose one bit width per layer. A one-bit upgrade to a large layer can consume enough storage for two smaller upgrades. Compare exhaustive search, greedy upgrades, and exact dynamic programming on the same tables.

Given a storage limit and predicted losses at several precisions, choose one bit width per layer. A one-bit upgrade to a large layer can consume enough storage for two smaller upgrades. Compare exhaustive search, greedy upgrades, and exact dynamic programming on the same tables.

## Result

> At nine layers, dynamic programming matches exhaustive loss in 0.0714 ms against 3.6329 ms

## Modules

* [`knotkit/prng`](/modules/prng/): Generates seeded pseudorandom numbers and unbiased integers for reproducible simulations and tests
* [`knotkit/errors`](/modules/errors/): Provides the base error classes every Knotkit package throws from, discriminated by package, category, and cause

## Figures

* 48 layers, 2064 bits available. Dynamic programming finds the minimum predicted loss of 9568 using 2064 bits.
* Greedy uses 2063 bits for loss 9583. Mean solve time: 560 µs greedy, 5.56 ms exact (M1 Pro, Bun 1.4.4).
* On the three-layer example at 31 bits, greedy chooses widths \[3, 2, 2] for loss 12. The optimum is \[2, 3, 3] for loss 9.

Hover or focus a chart for exact values. Drawn by [`demo.ts`](https://github.com/jkomyno/knotkit/blob/main/problems/bit-width-allocation/demo.ts) at build time, from the fixtures the benchmark uses; it reproduces the README figure.

## Reproduce

```bash
git clone https://github.com/jkomyno/knotkit.git && cd knotkit && bun install
bun run demo:problem:bit-width-allocation
```

[`problems/bit-width-allocation/README.md`](https://github.com/jkomyno/knotkit/blob/main/problems/bit-width-allocation/README.md) states the fixture, the seed, and the result table.
