#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Route 21, job #1306: the p=43 -> r=47 slot model and the exact one-step margin oracle. Independent re-implementation from the definitions stated in return #575 and #567 (no producer bytes were read or regenerated). Everything is exact: weights are Fractions. Model (as published in #575): slot x : x = 5 mod 6, and x != 0, -2 (mod q) for every prime 5 <= q <= 43 (the first N = 20 such x at or after the declared window start) Q_43 (old) : primes q with 43 < q <= 86 = 47 53 59 61 67 71 73 79 83 Q_47 (new) : primes q with 47 < q <= 94 = 53 59 61 67 71 73 79 83 89 89 is the sole entering owner, 47 the sole leaving owner K_t(c) : {x : x = c or c-2 (mod t)}, the class pair of owner t's phase c C_q : max_c w(K_q(c)) (owner capacity, before deletion) E_c : slots in K_47(c) (the deleted set of the leaving owner) C'_q(c) : max_cc w(K_q(cc) \\ E_c) (capacity after that deletion) delta : 1 - sum_{q in Q_43} C_q (W = 1 normalised) one-step margin (identical to #567's identity, see report): margin(c) = 1 - w(E_c) - sum_{q in Q_47} C'_q(c) = delta + (C_47 - w(E_c)) + sum_{q in Q_43\\47} (C_q - C'_q(c)) - C'_89(c) """ from fractions import Fraction as F W = F(1) # normalised total weight N_SLOTS = 20 BAND = 43 # leaving-owner band p PRIMES = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97] Q_OLD = [q for q in PRIMES if BAND < q <= 2 * BAND] # [47..83], paper's Q_43 Q_NEW = [q for q in PRIMES if (BAND + 4) < q <= 2 * (BAND + 4)] # [53..89], paper's Q_47 LEAVING, ENTERING = Q_OLD[0], Q_NEW[-1] # 47, 89 ADM_Q = [q for q in PRIMES if 5 <= q <= BAND] # admissibility moduli def slots(window_start, n=N_SLOTS): """First n twin-admissible starts x >= window_start: x = 5 mod 6, x != 0, -2 mod q (5<=q<=43).""" out, x = [], window_start + ((5 - window_start) % 6) while len(out) < n: if all(x % q and (x + 2) % q for q in ADM_Q): out.append(x) x += 6 return out def class_pairs(xs, t): """phase c -> tuple of slot indices in K_t(c) (only non-empty phases are kept).""" pairs = {} for i, x in enumerate(xs): for c in {x % t, (x + 2) % t}: pairs.setdefault(c, []).append(i) return {c: tuple(v) for c, v in pairs.items() if v} class Instance: """One window: slots, its class pairs, its deletion phases and the exact margin oracle.""" def __init__(self, window_start, n=N_SLOTS): self.window_start = window_start self.xs = slots(window_start, n) self.N = len(self.xs) self.span = max(self.xs) - self.xs[0] # L, the interval the route quotes self.pairs_old = {q: class_pairs(self.xs, q) for q in Q_OLD} self.pairs_new = {q: class_pairs(self.xs, q) for q in Q_NEW} self.phases = sorted(range(LEAVING)) # the 47 deletion phases of owner 47 self.deleted = [tuple(self.pairs_old[LEAVING].get(c, ())) for c in self.phases] # ---------------------------------------------------------------- capacities def capacities(self, w): """C_q for the old owners, as exact Fractions.""" return {q: max([sum(w[i] for i in p) for p in self.pairs_old[q].values()] or [F(0)]) for q in Q_OLD} def delta(self, w): C = self.capacities(w) return W - sum(C.values()), C def post_capacity(self, q, c, w): """C'_q(c) = max_cc w(K_q(cc) \\ E_c) and the binding phase cc.""" dead = set(self.deleted[c]) best, arg = F(0), None for cc, p in self.pairs_new[q].items(): v = sum(w[i] for i in p if i not in dead) if v > best: best, arg = v, cc return best, arg def margin_profile(self, w): """Exact margin at every one of the 47 phases, plus the binding pair of each owner.""" prof = [] for c in self.phases: dead = set(self.deleted[c]) we = sum(w[i] for i in dead) tot, binds = F(0), {} for q in Q_NEW: v, arg = self.post_capacity(q, c, w) tot += v binds[q] = arg prof.append({"phase": c, "margin": W - we - tot, "deleted_weight": we, "enter_cost": self.post_capacity(ENTERING, c, w)[0], "bind": binds}) return prof def oracle(self, w): """min_c margin(c) exactly, with the row that attains it (for cutting planes).""" prof = self.margin_profile(w) arg = min(prof, key=lambda r: (r["margin"], r["phase"])) return arg["margin"], arg, prof def witness_575(): """#575's published weight vector, denominator 291, on window start 1160000.""" num = [5, 7, 22, 16, 16, 20, 18, 22, 7, 24, 8, 8, 12, 24, 12, 9, 12, 17, 14, 18] return [F(k, 291) for k in num] def uniform(n=N_SLOTS): return [F(1, n)] * n def check_575(): """Reproduce every number #575 published for its positive witness.""" inst = Instance(1160000) w = witness_575() out = {} out["slots_match"] = inst.xs == [1160021, 1160039, 1160057, 1160099, 1160147, 1160219, 1160249, 1160321, 1160351, 1160447, 1160459, 1160477, 1160519, 1160561, 1160567, 1160657, 1160681, 1160711, 1160771, 1160837] out["span_L"] = inst.span d, C = inst.delta(w) out["delta"] = str(d) out["C_q"] = {str(q): str(C[q]) for q in Q_OLD} out["sum_C"] = str(sum(C.values())) prof = inst.margin_profile(w) pos = [r for r in prof if r["margin"] > 0] neg = [r for r in prof if r["margin"] < 0] out["phases"] = len(prof) out["positive_phases"] = len(pos) out["positive_values"] = sorted({str(r["margin"]) for r in pos}) out["min_margin"] = str(min(r["margin"] for r in prof)) out["max_margin"] = str(max(r["margin"] for r in prof)) out["C47"] = str(inst.capacities(w)[LEAVING]) ec = [r["enter_cost"] for r in prof] out["enter_cost_min"] = str(min(ec)) out["enter_cost_max"] = str(max(ec)) out["neg_count"] = len(neg) # identity check: the margin computed two ways must agree at every phase ident = True for r in prof: c = r["phase"] dead = set(inst.deleted[c]) we = sum(w[i] for i in dead) C2 = inst.capacities(w) rhs = (d + (C2[LEAVING] - we) + sum(C2[q] - inst.post_capacity(q, c, w)[0] for q in Q_OLD if q != LEAVING) - inst.post_capacity(ENTERING, c, w)[0]) ident &= (rhs == r["margin"]) out["identity_holds_all_phases"] = ident # uniform weights on the same window du, _ = inst.delta(uniform()) out["delta_uniform"] = str(du) out["uniform_margin_min"] = str(min(r["margin"] for r in inst.margin_profile(uniform()))) return out if __name__ == "__main__": import json, sys r = check_575() print(json.dumps(r, indent=1)) ok = (r["slots_match"] and r["delta"] == "2/97" and r["sum_C"] == "95/97" and r["positive_phases"] == 18 and r["neg_count"] == 29 and r["min_margin"] == "-10/97" and r["positive_values"] == ["1/97"] and r["C47"] == "11/97" and r["enter_cost_min"] == "28/291" and r["enter_cost_max"] == "12/97" and r["identity_holds_all_phases"]) print("REPLICATES_575:", ok) sys.exit(0 if ok else 1)