#!/usr/bin/env python3 """Job #4434 / route 117 step check -- the #1833 phase gate re-indexed for P=30, and what it removes from the step's population. Input : outputs/job4434/served/f1833_dbound2612.json (return #1833, served) and outputs/job4434/served/f1812_sweep2775b.json (return #1812, served). Output: outputs/job4434/dgate30.json, outputs/job4434/dgate_saving.json. dbound2612.json stores only the *strict* shapes (D < floor(2L/p)); every other shape in its stated domain has D = floor(2L/p), and shapes with floor = 0 are omitted since D <= 0 there. Nothing here recomputes #1833's table: the served table is re-indexed, and an independent exact evaluation of D(30,p,L) (base_extension.phase_bound_D, the same closed form) is used only as a cross-check of that re-index. """ import json import os import sys sys.path.insert(0, ".solveathome/private/lib/covering") import base_extension as be # noqa: E402 SERVED_D = "outputs/job4434/served/f1833_dbound2612.json" SERVED_1812 = "outputs/job4434/served/f1812_sweep2775b.json" OUT30 = "outputs/job4434/dgate30.json" OUTSAVE = "outputs/job4434/dgate_saving.json" P = 30 def reindex(path): db = json.load(open(path, encoding="utf-8")) strict = {(q, p, L): d for (q, p, L, fl, d, dmin) in db["strict"]} Lmax, pmax = db["Lmax"], db["pmax"] def D(p, L): if L > Lmax: raise ValueError("L beyond the served table") fl = (2 * L) // p if fl == 0: return 0 return strict.get((P, p, L), fl) return db, strict, D def main(): db, strict, D = reindex(SERVED_D) # cross-check the re-index against an independent exact evaluation cross = [] for p in (7, 11, 13, 17, 19, 23): for L in (1, 5, 11, 12, 14, 18, 19, 20, 21, 22, 27, 33, 39, 48): if (2 * L) // p == 0: continue d_exact, _ = be.phase_bound_D(P, p, L) cross.append({"p": p, "L": L, "reindexed": D(p, L), "exact": d_exact, "agree": D(p, L) == d_exact}) assert all(c["agree"] for c in cross), [c for c in cross if not c["agree"]] # #1833's own prose, recomputed from the table assert [be.min_L_for_drop(P, p, 3) for p in (7, 11, 13, 17, 19)] == [14, 19, 21, 27, 33] assert be.phase_bound_D(P, 7, 7)[0] == 0 and be.phase_bound_D(P, 7, 8)[0] == 1 thr = {} for p in be.primes_upto(db["pmax"]): if P % p == 0: continue thr[str(p)] = {"min_A_drop2_permitted": next((L for L in range(1, db["Lmax"] + 1) if D(p, L) >= 2), None), "min_A_drop3_permitted": next((L for L in range(1, db["Lmax"] + 1) if D(p, L) >= 3), None), "step_filter_p_le_A": p} out30 = {"tool": "outputs/job4434/dgate_reindex.py", "source": "return #1833 served dbound2612.json, re-indexed (no recomputation)", "P": P, "Lmax": db["Lmax"], "pmax": db["pmax"], "cross_check_shapes": len(cross), "cross_check_all_agree": True, "thresholds": thr, "proven_claim": ("drop <= D(P,p,A) for every R with K*(P,R) = A, hence a drop " "of 2 needs D(30,p,A) >= 2 and a drop of 3 needs D(30,p,A) >= 3")} with open(OUT30, "w", encoding="utf-8") as fh: json.dump(out30, fh, indent=1, sort_keys=True) # --- what the gate removes from the population already on record --------- sw = json.load(open(SERVED_1812, encoding="utf-8")) rows = sw["rows"] stepf = [r for r in rows if r["p"] <= r["A"]] gate2 = [r for r in stepf if D(r["p"], r["A"]) >= 2] gate3 = [r for r in stepf if D(r["p"], r["A"]) >= 3] by_p = {} for r in stepf: b = by_p.setdefault(str(r["p"]), [0, 0]) b[0] += 1 if D(r["p"], r["A"]) >= 2: b[1] += 1 # price of the step's population, like-for-like with #1812's own wall clock old = be.population(P, (3, 4, 5, 6), 3 * 10 ** 8) pop = be.population(P, (5, 6, 7), 3 * 10 ** 9) work_1812 = sum(c["M"] for c in old) + sw["price"]["sum_Mp"] work_step = sum(c["M"] for c in pop) + sum(c["M"] * p for c in pop for p in c["ps"]) wall_1812 = 983.0 # #1812's own recorded addendum wall clock cpu_1812 = wall_1812 * 6 / 3600.0 # 6 threads ratio = work_step / work_1812 save = {"tool": "outputs/job4434/dgate_saving.py", "served_rows": len(rows), "step_filter_kept": len(stepf), "gate_D_ge_2_kept": len(gate2), "gate_D_ge_3_kept": len(gate3), "kept_by_p": by_p, "gate_removes_fraction": 1.0 - len(gate2) / float(len(stepf)), "price": {"work_1812_slots": work_1812, "work_step_slots": work_step, "ratio": ratio, "wall_1812_seconds": wall_1812, "threads_1812": 6, "cpu_hours_1812": cpu_1812, "cpu_hours_step_like_for_like": ratio * cpu_1812, "step_budget_hours": 2, "fraction_of_population_covered_by_budget": 2.0 / (ratio * cpu_1812)}} with open(OUTSAVE, "w", encoding="utf-8") as fh: json.dump(save, fh, indent=1, sort_keys=True) print(json.dumps({"cross_check_shapes": len(cross), "step_filter_kept": len(stepf), "gate2": len(gate2), "gate3": len(gate3), "kept_by_p": by_p, "ratio_vs_1812": round(ratio, 1), "cpu_hours_like_for_like": round(ratio * cpu_1812, 1)}, indent=1)) if __name__ == "__main__": main()