"""Exact-model GI/M/1 evaluation with rational certificates, not a simulation.""" import argparse from decimal import Decimal, localcontext from fractions import Fraction as F import hashlib import json from pathlib import Path ROOT = Path(__file__).resolve().parent LAWS = { "exponential": ("Exponential", None), "two_point_80": ("Two-point 80/20", ((F(4, 5), F(1, 2)), (F(1, 5), F(3)))), "two_point_90": ("Two-point 90/10", ((F(9, 10), F(2, 3)), (F(1, 10), F(4)))), "deterministic": ("Constant intervals", ((F(1), F(1)),)), } LOADS = ("0.2", "0.5", "0.8", "0.95") def rational(value): if isinstance(value, bool) or not isinstance(value, (str, int, F)): raise ValueError("Use finite exact strings, integers or Fractions") return F(value) def dec(value): value = F(value) return Decimal(value.numerator) / Decimal(value.denominator) def exp_bounds(x, tolerance=F(1, 10**45)): """Taylor odd/even partial sums enclose exp(-x), using exact rationals.""" x = rational(x) if x < 0 or tolerance <= 0: raise ValueError("Require nonnegative x and positive tolerance") if not x: return F(1), F(1) term = total = F(1) for n in range(1, 2001): term *= -x / n total += term if n % 2 and n > x and abs(term) < tolerance: return total, total - term raise ArithmeticError("Taylor certificate budget exceeded") def transform(law, z): if law == "exponential": return 1 / (1 + z) return sum(dec(p) * (-dec(a) * z).exp() for p, a in LAWS[law][1]) def transform_bounds(law, z): if law == "exponential": exact = 1 / (1 + z) return exact, exact lower = upper = F(0) for p, a in LAWS[law][1]: lo, hi = exp_bounds(a * z) lower += p * lo upper += p * hi return lower, upper def certified_root(approximate, bounds): """Use Decimal to find candidates; accept only exact sign certificates.""" with localcontext() as ctx: ctx.prec = 80 lo, hi = Decimal(0), Decimal("0.5") for _ in range(100): if approximate(hi) < 0: break hi = (1 + hi) / 2 else: raise ArithmeticError("No interior upper bracket") for _ in range(100): mid = (lo + hi) / 2 if approximate(mid) > 0: lo = mid else: hi = mid if hi - lo < Decimal("1e-24"): break # Expansion also handles an exactly representable root hit. a = max(F(0), F(lo) - F(1, 10**23)) b = min(F(1), F(hi) + F(1, 10**23)) fa, fb = bounds(a), bounds(b) if not (0 <= a < b < 1 and fa[0] > 0 and fb[1] < 0): raise ArithmeticError("Exact signs did not certify the candidate") return a, b, fa[0], fb[1] def solve(law, rho, mean_service_ms="1"): rho, service = rational(rho), rational(mean_service_ms) if law not in LAWS or not F(1, 100) <= rho <= F(999, 1000) or service <= 0: raise ValueError("Known law, 0.01 <= rho <= 0.999, positive service required") def approximate(r): return transform(law, (1 - r) / dec(rho)) - r def bounds(r): lo, hi = transform_bounds(law, (1 - r) / rho) return lo - r, hi - r lo, hi, f_lo, f_hi = certified_root(approximate, bounds) wait_lo = service * lo / (1 - lo) wait_hi = service * hi / (1 - hi) scv = F(0) if law == "deterministic" else F(1) vut = (scv + 1) * rho * service / (2 * (1 - rho)) wait = (wait_lo + wait_hi) / 2 with localcontext() as ctx: ctx.prec = 80 fmt = lambda v: format(dec(v), ".6f").replace("-0.000000", "0.000000") return { "law": law, "label": LAWS[law][0], "rho": str(rho), "mean_interarrival_ms": str(service / rho), "mean_service_ms": str(service), "arrival_scv": str(scv), "kingman_wait_ms": str(vut), "root_bracket": [str(lo), str(hi)], "residual_lower_at_left": str(F(f_lo * 10**50 // 1, 10**50)), "residual_upper_at_right": str(-F(-f_hi * 10**50 // 1, 10**50)), "wait_ms_bracket": [str(wait_lo), str(wait_hi)], "wait_bracket_width_ms": str(wait_hi - wait_lo), "display_wait_ms": fmt(wait), "display_kingman_ms": fmt(vut), "display_error_percent": fmt(100 * (vut / wait - 1)), } def results(): return { "schema_version": 1, "generated_date": "2026-11-19", "kind": "analytic_model_evaluation_not_measurement_or_simulation", "model_sha256": hashlib.sha256(Path(__file__).read_bytes()).hexdigest(), "units": {"waiting": "ms", "rho": "dimensionless", "scv": "dimensionless"}, "error_definition": "100 * (Kingman / exact-model mean waiting - 1)", "certificate": "Exact rational Taylor bounds certify root signs; monotone r/(1-r) maps root brackets to waiting brackets.", "rows": [solve(law, rho) for rho in LOADS for law in LAWS], } def main(): parser = argparse.ArgumentParser() parser.add_argument("--check", action="store_true") args = parser.parse_args() payload = json.dumps(results(), indent=2) + "\n" path = ROOT / "results.json" if args.check: if not path.exists() or path.read_text() != payload: raise SystemExit("Missing or stale results") print("16 exact-model rows and rational certificates verified") else: path.write_text(payload) if __name__ == "__main__": main()