"""Exact deterministic sampling counterexample; not a Poisson simulation.""" from fractions import Fraction import json from pathlib import Path import sys def trace(arrivals, service, horizon): """Integer ticks, initially empty FIFO, completions before tied arrivals.""" if type(service) is not int or service <= 0: raise ValueError("service must be a positive integer") if type(horizon) is not int or horizon <= 0: raise ValueError("horizon must be a positive integer") if not isinstance(arrivals, (list, tuple)) or not arrivals: raise ValueError("nonempty arrival sequence required") if any(type(t) is not int or t < 0 or t >= horizon for t in arrivals): raise ValueError("arrivals must be integer ticks inside [0, horizon)") if any(a >= b for a, b in zip(arrivals, arrivals[1:])): raise ValueError("arrivals must be strictly increasing") end, rows = 0, [] for arrival in arrivals: start = max(arrival, end) busy = end > arrival end = start + service rows.append(dict(arrival=arrival, start=start, end=end, busy_before=busy)) if end > horizon: raise ValueError("horizon must include every completion") busy_ticks = sum(row["end"] - row["start"] for row in rows) seen = sum(row["busy_before"] for row in rows) return dict(requests=len(rows), busy_ticks=busy_ticks, horizon_ticks=horizon, time_busy_fraction=str(Fraction(busy_ticks, horizon)), arrivals_seeing_busy=seen, arrival_busy_fraction=str(Fraction(seen, len(rows))), rows=rows) def result(): cases = [ ("periodic", "One every 2 ms", list(range(0, 200, 20))), ("pairs", "Pairs 0.5 ms apart, every 4 ms", [40 * pair + offset for pair in range(5) for offset in (0, 5)]), ("burst", "Ten arrivals 0.1 ms apart", list(range(10))), ] return dict(schema_version=1, evidence="constructed deterministic example", tick_ms="1/10", service_ticks=10, window="[0, 20) ms", convention="Initially empty FIFO; completions precede arrivals at equal timestamps.", cases=[dict(id=key, label=label, **trace(arrivals, 10, 200)) for key, label, arrivals in cases]) def render(): return json.dumps(result(), indent=2) + "\n" if __name__ == "__main__": path = Path(__file__).with_name("results.json") if "--check" in sys.argv: if path.read_text() != render(): raise SystemExit("Result drift") print("Exact sampling results match") else: path.write_text(render())