#!/usr/bin/env python3 """cost.py -- what remains: the cost of the 83# ascent, from the search logs. Parses jtwin log lines of the form R = 285 COVERABLE (a=1715 G2=1716) witness prefix 288 nodes 36435858732 (6022.4s) R = 286 REFUTED (a=1721 G2=1722) witness prefix 285 nodes ... (....s) and reports, per level n, the node count as a function of R, a log-linear fit log10(nodes) = a + b*R, and the implied growth factor per +1 in R. With that it extrapolates the remaining rungs of the 83# ascent (target R_cert+1 until the first refutation) and the rungs up to the least-squares prediction R = 306. Everything is labelled: an extrapolation is not a measurement. USAGE cost.py LOG [LOG ...] """ import math import os import re import sys sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import witness as W ROW = re.compile(r"R\s*=\s*(\d+)\s+(COVERABLE|REFUTED)\s+.*?nodes\s+(\d+)\s+\(([\d.]+)s\)") PRE = re.compile(r"witness prefix\s+(\d+)") PRED_R = 306 # from 0017's fit: A144311(23) ~ 1841 def parse(paths): rows = [] n = None for path in paths: for line in open(path, errors="replace"): m = re.search(r"^n = (\d+)", line) if m: n = int(m.group(1)) m = ROW.search(line) if m: pre = PRE.search(line) rows.append(dict(n=n, R=int(m.group(1)), ok=m.group(2) == "COVERABLE", nodes=int(m.group(3)), secs=float(m.group(4)), pre=int(pre.group(1)) if pre else None, src=path)) return rows def fit(rows): """least squares of log10(nodes) on R""" xs = [r["R"] for r in rows] ys = [math.log10(r["nodes"]) for r in rows] k = len(xs) mx, my = sum(xs) / k, sum(ys) / k sxx = sum((x - mx) ** 2 for x in xs) if sxx == 0: return None b = sum((x - mx) * (y - my) for x, y in zip(xs, ys)) / sxx return my - b * mx, b def main(): if len(sys.argv) < 2: print(__doc__) return 2 rows = parse(sys.argv[1:]) if not rows: print("no rows parsed") return 1 print("=" * 78) print("0018 COST MODEL for the 83# ascent (n = 21, primes 5..83)") print("=" * 78) print("%-4s %-6s %-10s %-8s %-15s %s" % ("n", "R", "verdict", "prefix", "nodes", "wall s")) for r in rows: print("%-4s %-6d %-10s %-8s %-15d %.1f" % (r["n"], r["R"], "COVERABLE" if r["ok"] else "REFUTED", r["pre"] if r["pre"] is not None else "-", r["nodes"], r["secs"])) print() by_n = {} for r in rows: by_n.setdefault(r["n"], []).append(r) print(" growth of the complete decision with R, per level (least squares on log10 nodes):") print(" %-5s %-6s %-14s %s" % ("n", "rows", "factor per +1 R", "note")) factors = [] for n in sorted(by_n): rs = by_n[n] f = fit(rs) if f is None: print(" %-5d %-6d %-14s 1 R value only: no slope" % (n, len(rs), "-")) continue a, b = f print(" %-5d %-6d %-14.3f log10 nodes = %.4f + %.5f R" % (n, len(rs), 10 ** b, a, b)) if len(rs) >= 3: factors.append((n, 10 ** b)) print() if factors: n_big, f_big = factors[-1] print(" largest well-sampled level: n = %d, factor %.3f per +1 R" % (n_big, f_big)) n21 = [r for r in rows if r["n"] == 21 and r["ok"]] if n21: last = max(n21, key=lambda r: r["pre"] or r["R"]) base_R = last["pre"] or last["R"] # the log may predate the witness-prefix measurement (0017's target-only # lines); the pool is the single source of truth for the certified rung pool = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "out", "seed-certificate.txt") if os.path.exists(pool): for line in open(pool): t = line.split("#")[0].split() if len(t) >= 3 and int(t[0]) == 21: n_, _f, a = int(t[0]), int(t[1]), [int(x) for x in t[2:2 + int(t[0])]] P_, C_, pre_, _b, _A, _G = W.check(n_, a) if pre_ > base_R: print(" pool corrects the rung: log says R = %d, witness prefix is %d" % (base_R, pre_)) base_R = pre_ print(" measured 83# point: witness prefix R = %d at %d nodes / %.0f s" % (base_R, last["nodes"], last["secs"])) if factors: f = factors[-1][1] print() print(" EXTRAPOLATION to the fit prediction R = %d (labelled: not a measurement):" % PRED_R) print(" remaining rungs R = %d..%d" % (base_R + 1, PRED_R)) tot = 0.0 for R in range(base_R + 1, PRED_R + 1): tot += last["nodes"] * (f ** (R - base_R)) print(" total nodes ~ %.3g (x%.1f the last measured decision)" % (tot, tot / last["nodes"])) print(" at the measured rate (%.0f s for %d nodes on 8 threads) that is ~%.0f core-hours" % (last["secs"], last["nodes"], tot / last["nodes"] * last["secs"] * 8 / 3600)) print(" plus ONE refutation at R = R_cert+1, which is the step that closes the rung") else: print(" no multi-point fit available at n = 21 yet: more rungs are needed before") print(" a cost model for this level can be fitted rather than guessed.") print() print(" NOTE. Extrapolations here are labelled; the only measured quantities are the") print(" rows above. The refutation that closes the rung is not modelled.") if __name__ == "__main__": sys.exit(main())