"""Exact finite allocation example, not a queueing or hardware benchmark.""" from collections import Counter from fractions import Fraction from hashlib import sha256 from itertools import product import json from pathlib import Path import sys POLICIES = ("random", "two-current", "two-frozen", "two-frozen-low-id") LABELS = ("One random choice", "Two choices, current counts", "Two choices, frozen counts", "Frozen counts, lowest-ID tie") def distribution(servers, jobs, policy): if type(servers) is not int or not 1 <= servers <= 5: raise ValueError("servers must be an integer from 1 to 5") if type(jobs) is not int or not 0 <= jobs <= 6: raise ValueError("jobs must be an integer from 0 to 6") if policy not in POLICIES: raise ValueError("unknown policy") choices = 1 if policy == "random" else 2 states = Counter({(0,) * servers: 1}) for _ in range(jobs): following = Counter() for state, weight in states.items(): for sample in product(range(servers), repeat=choices): if policy in ("random", "two-frozen"): destination = sample[0] elif policy == "two-frozen-low-id": destination = min(sample) else: destination = min(sample, key=lambda i: state[i]) updated = list(state) updated[destination] += 1 following[tuple(updated)] += weight states = following return states, servers ** (choices * jobs) def fraction(value): return {"numerator": value.numerator, "denominator": value.denominator, "exact": str(value), "decimal": float(value)} def results(): rows = [] for policy, label in zip(POLICIES, LABELS): states, total = distribution(3, 4, policy) maxima = Counter() for state, weight in states.items(): maxima[max(state)] += weight rows.append({ "policy": policy, "label": label, "elementary_sequences": total, "expected_max_jobs": fraction(Fraction(sum(k * v for k, v in maxima.items()), total)), "probability_max_at_least_3": fraction(Fraction(sum(v for k, v in maxima.items() if k >= 3), total)), "max_distribution": [{"maximum_jobs": k, "sequence_count": v, "probability": fraction(Fraction(v, total))} for k, v in sorted(maxima.items())], "states": [{"jobs": list(k), "sequence_count": v} for k, v in sorted(states.items())], }) return { "schema_version": 1, "generated_for": "2026-11-20", "evidence_type": "exact finite synthetic allocation, not empirical measurement", "servers": 3, "arrivals": 4, "initial_jobs": [0, 0, 0], "departures": 0, "sampling": "independent uniform ordered draws with replacement", "tie_break": "first sampled candidate except explicit lowest-ID control", "feedback": "current updates after every allocation; frozen remains [0,0,0]", "units": "jobs per server, not milliseconds", "uncertainty": "complete finite enumeration; no sampling confidence interval", "cases": rows, } def artifacts(): root = Path(__file__).resolve().parent data = json.dumps(results(), indent=2) + "\n" names = ("model.py", "test_model.py", "README.md") hashes = {name: sha256((root / name).read_bytes()).hexdigest() for name in names} hashes["results.json"] = sha256(data.encode()).hexdigest() manifest = "".join(hashes[name] + " " + name + "\n" for name in sorted(hashes)) return {"results.json": data, "SHA256SUMS": manifest} if __name__ == "__main__": if sys.argv[1:] not in ([], ["--check"]): raise SystemExit("usage: python3 model.py [--check]") for name, content in artifacts().items(): path = Path(__file__).resolve().parent / name if "--check" in sys.argv: if not path.is_file() or path.read_text() != content: raise SystemExit("stale artifact: " + name) else: path.write_text(content) print("Exact allocation artifacts verified" if "--check" in sys.argv else "Exact allocation artifacts generated")