#!/usr/bin/env python3 """ Job #1328 / route 25: the permutation control is streamable, so it scales past x = 29. #587's next_step says of the arrangement control at x = 31: "Re-run the permutation control at x = 31 if the budget allows, 3 draws; if not, say so rather than reporting m* alone." Read literally that needs the materialised gap word: 6,226,553,025 gaps, uint16 (max gap 348 > 255) = 11.6 GiB, and a Fisher-Yates over it is random access. Against a 16 GB / 4 cpu-h assignment cap that is at best marginal, and #587 treated it as the reason the control might not happen. It is not needed. Every gap of the twin tile is EVEN and at most Ghat, so the gap multiset is a histogram over at most Ghat/2 bins (33 distinct values at x = 23; Ghat/2 = 174 is the ceiling at x = 31). A uniform random permutation of a multiset can therefore be emitted BLOCKWISE: for a block of B slots, draw the block's composition from the multivariate hypergeometric on the remaining counts, shuffle inside the block, and carry the running window. Memory is O(B + #distinct gaps + m), independent of D. This script validates that claim where it can be checked: at x = 23 and x = 19 it runs (a) the direct shuffle #587 used and (b) the streaming shuffle, and compares both against #587's published lambda_shuffled. """ import sys, time, argparse import numpy as np import ladder1328 as L PUBLISHED_SHUFFLED = {13: 2.022, 17: 1.846, 19: 1.691, 23: 1.575, 29: 1.565} PUBLISHED_REAL = {13: 2.758, 17: 2.547, 19: 2.562, 23: 2.475, 29: 2.336} def gap_word(x): primes, P, C = L.level(x) pos = L.tile(primes, P) ext = np.concatenate((pos, [pos[0] + P])) g = (ext[1:] - ext[:-1]).astype(np.int64) assert g.size == C and int(g.sum()) == P return g, P, C def maxsum_of(gaps, m): """max sum of m consecutive entries of a cyclic gap word.""" ext = np.concatenate((gaps, gaps[:m])) cs = np.concatenate(([0], np.cumsum(ext))) return int((cs[m:] - cs[:-m])[:gaps.size].max()) def mstar_from(gaps, budget, mmax=64): best = 0 for m in range(1, mmax): if maxsum_of(gaps, m) < budget: best = m else: break return best def streaming_perm_maxsums(counts, values, total, mmax, rng, block=1 << 20): """ maxsum_m for EVERY m in 1..mmax over ONE uniform random permutation of the multiset {values: counts}, emitted blockwise. Memory O(block + len(values) + mmax); the permuted word is never materialised. One permutation per call: all m are read off the same draw, which is what m* needs. """ remaining = counts.copy() left = int(total) carry = np.empty(0, dtype=np.int64) head = None # first mmax gaps, to close the cycle at the end best = np.zeros(mmax + 1, dtype=np.int64) while left > 0: b = int(min(block, left)) comp = rng.multivariate_hypergeometric(remaining, b) remaining -= comp left -= b blk = np.repeat(values, comp) rng.shuffle(blk) arr = np.concatenate((carry, blk)) if carry.size else blk if head is None: head = arr[:mmax].copy() if arr.size > mmax: cs = np.concatenate(([0], np.cumsum(arr))) for m in range(1, mmax + 1): s = cs[m:] - cs[:-m] if s.size: v = int(s.max()) if v > best[m]: best[m] = v carry = arr[-mmax:] else: carry = arr wrap = np.concatenate((carry, head)) cs = np.concatenate(([0], np.cumsum(wrap))) for m in range(1, mmax + 1): s = cs[m:] - cs[:-m] if s.size: v = int(s.max()) if v > best[m]: best[m] = v return best def mstar_streaming(counts, values, total, budget, rng, mmax=40): best = streaming_perm_maxsums(counts, values, total, mmax, rng) out = 0 for m in range(1, mmax + 1): if best[m] < budget: out = m else: break return out def main(): ap = argparse.ArgumentParser() ap.add_argument("--x", type=int, default=23) ap.add_argument("--draws", type=int, default=5) ap.add_argument("--seed", type=int, default=1328) a = ap.parse_args() g, P, C = gap_word(a.x) ghat = int(g.max()) gbar = P / C budget = 4 * ghat values, counts = np.unique(g, return_counts=True) lam_real = mstar_from(g, budget) * gbar / ghat print(f"# x = {a.x} D = {C} gbar = {gbar:.4f} Ghat = {ghat} 4Ghat = {budget}") print(f"# gap alphabet: {values.size} distinct values, all even = " f"{bool((values % 2 == 0).all())}, max {int(values.max())}") print(f"# multiset as a histogram: {values.size} bins vs a materialised word of {C} gaps") print(f"# lambda_real = {lam_real:.4f} (#587: {PUBLISHED_REAL.get(a.x)})") rng = np.random.default_rng(a.seed) t0 = time.time() direct = [] for _ in range(a.draws): sh = rng.permutation(g) direct.append(mstar_from(sh, budget) * gbar / ghat) t_direct = time.time() - t0 rng2 = np.random.default_rng(a.seed) t0 = time.time() stream = [] for _ in range(a.draws): stream.append(mstar_streaming(counts.copy(), values, C, budget, rng2) * gbar / ghat) t_stream = time.time() - t0 d, s = np.array(direct), np.array(stream) # timing to stderr: stdout must reproduce byte for byte print(f"direct {t_direct:.1f}s, streaming {t_stream:.1f}s", file=sys.stderr) print(f"# lambda_shuffled, DIRECT : mean {d.mean():.4f} sd {d.std(ddof=1):.4f} " f"{np.round(d,4).tolist()}") print(f"# lambda_shuffled, STREAMING: mean {s.mean():.4f} sd {s.std(ddof=1):.4f} " f"{np.round(s,4).tolist()}") print(f"# #587 published lambda_shuffled at x = {a.x}: {PUBLISHED_SHUFFLED.get(a.x)}") print(f"# separation (real - shuffled): direct {lam_real-d.mean():+.4f}, " f"streaming {lam_real-s.mean():+.4f}") if __name__ == "__main__": main()