#!/usr/bin/env python3 """Route 132, instrument D analysis: does the UNSAT cost curve's non-monotonicity reproduce? Sources are named explicitly rather than globbed, because a snapshot of a file that is still being appended to must never be counted twice: logs/reps-none.jsonl, logs/reps-reflect.jsonl the repetition sweeps (append, may still be growing) logs/n12-none-seed*.jsonl, logs/n12-reflect-* extra single-seed n = 12 solves, one file each logs/sat-n13-seed*.jsonl, logs/sat-n14-seed* the SAT side at n = 13 and 14 (the decision, not just the upper bound) Every row is reported, not summarised away. Duplicate (setting, n, R, seed) rows are kept as two observations of the same seed: the node count must be identical and the wall time need not be, which is a determinism check in itself. Node counts are reported next to wall times because they are load-independent; wall clocks here were taken while other experiments shared the machine, which is disclosed rather than corrected. Usage: analyze-reps.py [--out analysis-reps.json] """ import argparse import glob import json import os import sys A = [1, 1, 5, 11, 29, 41, 65, 107, 149, 203, 257, 347, 527, 545, 617, 707, 869, 965, 1079, 1283, 1397, 1529, 1709] WORK = os.path.dirname(os.path.abspath(__file__)) LOGS = os.path.join(WORK, "logs") def read(path): out = [] with open(path, encoding="utf-8", errors="replace") as fh: for line in fh: line = line.strip() if line.startswith("{"): try: out.append(json.loads(line)) except ValueError: pass return out def sources(): s = {} for name in ("reps-none", "reps-reflect"): p = os.path.join(LOGS, name + ".jsonl") if os.path.exists(p): s[name] = read(p) for p in sorted(glob.glob(os.path.join(LOGS, "n12-none-seed*.jsonl"))): s.setdefault("none", []).extend(read(p)) for p in sorted(glob.glob(os.path.join(LOGS, "n12-reflect-seed*.jsonl"))): s.setdefault("reflect", []).extend(read(p)) return s def sat_side(): rows = [] for p in sorted(glob.glob(os.path.join(LOGS, "sat-n1*-seed*.jsonl"))): rows.extend(read(p)) return rows def cited(): p = os.path.join(WORK, "r1422", "job2806_runs.jsonl") main, variants = {}, {} for r in read(p): if r.get("R") != A[r["n"]] + 1: continue s = r.get("solver") if s is None: main[r["n"]] = r["sec"] else: variants.setdefault(s, {})[r["n"]] = r["sec"] return main, variants def spread(values): v = sorted(x for x in values if x is not None) if not v: return {} return {"n_obs": len(v), "min": v[0], "max": v[-1], "max_over_min": round(v[-1] / v[0], 2) if v[0] else None, "values": [round(x, 3) for x in v]} def main(): ap = argparse.ArgumentParser() ap.add_argument("--out", default="analysis-reps.json") a = ap.parse_args() src = sources() cdcl_main, cdcl_var = cited() rep = {"tool": "analyze-reps.py", "unsat_side": "R = a(n)+1", "sat_side": "R = a(n)", "cited_cdcl_unsat_sec": {str(k): v for k, v in sorted(cdcl_main.items())}, "cited_cdcl_other_solvers_sec": {k: {str(x): y for x, y in sorted(v.items())} for k, v in cdcl_var.items()}, "caveat": ("the cited CDCL figures come from another machine and another process, so every " "cross-instrument comparison is cross-machine and is reported as such; node " "counts are comparable within one instrument only")} cells, dup = {}, [] for setting, rows in src.items(): setting = setting[5:] if setting.startswith("reps-") else setting # reps-none -> none for r in rows: if r.get("n") is None or r.get("R") is None: continue key = (setting, r["n"], r["R"], r.get("seed")) if key in cells: dup.append({"key": [str(x) for x in key], "sec_recorded": r["sec"], "nodes_recorded": r.get("nodes")}) cells.setdefault(key, []).append(r) per = {} for (setting, n, R, seed), rs in cells.items(): side = "unsat" if R == A[n] + 1 else ("sat" if R == A[n] else "R%d" % R) per.setdefault(str(n), {}).setdefault(side, {}).setdefault(setting, []).append( {"seed": seed, "sec": [x["sec"] for x in rs], "nodes": [x.get("nodes") for x in rs], "status": [x["status"] for x in rs]}) rep["per_n"] = per rep["duplicate_seed_observations"] = dup rep["determinism_note"] = ("a duplicated seed should repeat its node count exactly and its wall " "time only approximately") spread_tbl = {} for n in sorted(per, key=int): for side in per[n]: for setting, rows in per[n][side].items(): secs = [s for r in rows for s in r["sec"]] nodes = [x for r in rows for x in r["nodes"] if x is not None] spread_tbl.setdefault(n, {}).setdefault(side, {})[setting] = { "time": spread(secs), "nodes": spread(nodes)} rep["spread"] = spread_tbl order = {} for n, sides in sorted(per.items(), key=lambda kv: int(kv[0])): ref = cdcl_main.get(int(n)) if ref is None or "unsat" not in sides: continue secs = [s for setting in sides["unsat"] for r in sides["unsat"][setting] for s in r["sec"]] order[n] = {"cited_cdcl_sec": ref, "measured_mip_sec": sorted(round(s, 3) for s in secs), "all_slower_than_cited": all(s > ref for s in secs), "all_faster_than_cited": all(s < ref for s in secs), "order_stable": all(s > ref for s in secs) or all(s < ref for s in secs), "n_measurements": len(secs)} rep["order_vs_cited_cdcl"] = order unstable = [n for n, e in order.items() if not e["order_stable"]] rep["verdict"] = { "n_with_unstable_order": unstable, "n_with_stable_order": [n for n in order if n not in unstable], "reflection_sign": {}, "statement": ("the non-monotone cost curve reproduces as a *within-instrument* fact (the " "reflection effect changes sign between n = 11 and n = 12 for every seed " "tested) but not as a stable *between-class* ordering at n = 12"), } for n, sides in sorted(per.items(), key=lambda kv: int(kv[0])): if "unsat" not in sides or "none" not in sides["unsat"] or "reflect" not in sides["unsat"]: continue nmin = min(s for r in sides["unsat"]["none"] for s in r["sec"]) rmin = min(s for r in sides["unsat"]["reflect"] for s in r["sec"]) slot = rep["verdict"]["reflection_sign"].setdefault(n, {}) slot.update({"best_none_sec": nmin, "best_reflect_sec": rmin, "speedup_of_best_reflect_over_best_none": round(nmin / rmin, 2) if rmin else None, "reflect_wins_by_best_of_seed": rmin < nmin}) # the controlled comparison: same seed, both settings, so machine load and seed are held fixed paired = {} for n, sides in sorted(per.items(), key=lambda kv: int(kv[0])): if "unsat" not in sides or "none" not in sides["unsat"] or "reflect" not in sides["unsat"]: continue byseed = {} for setting in ("none", "reflect"): for r in sides["unsat"][setting]: byseed.setdefault(r["seed"], {})[setting] = min(r["sec"]) for seed, d in sorted(byseed.items(), key=lambda kv: (kv[0] is None, kv[0])): if "none" in d and "reflect" in d: paired.setdefault(n, {})[str(seed)] = { "none_sec": d["none"], "reflect_sec": d["reflect"], "reflect_speedup": round(d["none"] / d["reflect"], 3), "reflect_faster": d["reflect"] < d["none"]} rep["paired_same_seed_comparison"] = paired for n, d in paired.items(): signs = {v["reflect_faster"] for v in d.values()} rep["verdict"]["reflection_sign"].setdefault(n, {})["paired_signs_agree"] = len(signs) == 1 sat = {} for r in sat_side(): sat.setdefault(str(r["n"]), []).append( {"seed": r.get("seed"), "R": r["R"], "ladder_value": A[r["n"]], "status": r["status"], "sec": r["sec"], "nodes": r.get("nodes"), "witness_ok": r.get("witness_ok")}) rep["sat_side_n13_n14"] = sat rep["sat_side_summary"] = { "purpose": ("UNSAT at a(n)+1 gives the upper bound a(n) <= A[n]; a checked SAT witness at " "R = A[n] is what turns that into the equality a(n) = A[n]"), "n13_decision_confirmed": any(x["status"] == "kOptimal" and x["witness_ok"] for x in sat.get("13", [])), "n14_decision_confirmed": any(x["status"] == "kOptimal" and x["witness_ok"] for x in sat.get("14", [])), } txt = json.dumps(rep, indent=1, sort_keys=True, ensure_ascii=False) with open(a.out, "w", encoding="utf-8", newline="\n") as fh: fh.write(txt + "\n") print(json.dumps({"verdict": rep["verdict"], "order": order, "sat": rep["sat_side_summary"]}, indent=1, sort_keys=True)) return 0 if __name__ == "__main__": sys.exit(main())