#!/usr/bin/env python3 """LP-iterate ascent on the quarter-turn family maximum for n = 12 (issue #8). Nonsmooth min-max ascent with exact rational evaluation at every candidate: at the current parameters, take the active set (all 220 triangle areas within a tolerance of the exact minimum), solve the LP max t s.t. grad_i . d >= t for active i, |d|_inf <= 1 (t > 0 means every active area can grow simultaneously), line-search the step along d with EXACT evaluation of the true minimum over all 220 quadratic polynomials, accept the best step, and repeat until the LP margin collapses. The result is a family stationary point (0 in the hull of its active gradients, checked exactly), and its value is compared with the record root (smallest positive root of 64t^3 + 80t^2 + 28t - 1). Usage: family_chase2.py STEAD-n12.txt [OUTDIR] """ import sys from fractions import Fraction from pathlib import Path import numpy as np from scipy.optimize import linprog sys.path.insert(0, str(Path(__file__).resolve().parent)) from conv_test import setup from family_max import poly_eval, poly_grad_at, ZERO F = Fraction def record_root_float(): import decimal decimal.getcontext().prec = 70 x = decimal.Decimal("0.03") for _ in range(200): x = x - (64 * x**3 + 80 * x**2 + 28 * x - 1) / (192 * x**2 + 160 * x + 28) return x def main(): values, polys, lit_areas, lit_min, active0, grads0, family_pts = setup(sys.argv[1]) triples = list(polys.keys()) root = record_root_float() import decimal bar = decimal.Decimal("0.0325988587") - root print(f"record root: {root:.19f}; to make the check print 0.0325988587 the family max " f"must exceed the root by {float(bar):.3e}") current = dict(values) def exact_min(params): return min(abs(poly_eval(polys[t], params)) for t in triples) def active_of(params, tol=Fraction(1, 10**25)): m = exact_min(params) act = [t for t in triples if abs(poly_eval(polys[t], params)) - m <= tol] return m, act signs = {} for t in triples: signs[t] = 1 if poly_eval(polys[t], current) > 0 else -1 m = exact_min(current) print(f"start: exact doubled min {float(m):.19f} (record literals)") history = [] for iteration in range(200): m, act = active_of(current) # LP: max t s.t. (s_i grad_i) . d >= t for active i, |d| <= 1 Gm = np.array([[float(signs[t] * grads0[t][name] if False else signs[t] * poly_grad_at(polys[t], current).get(name, ZERO)) for name in ("a", "b", "px", "py")] for t in act]) A_ub = np.hstack([-Gm, np.ones((len(act), 1))]) res = linprog(c=np.array([0.0] * 4 + [-1.0]), A_ub=A_ub, b_ub=np.zeros(len(act)), bounds=[(-1, 1)] * 4 + [(None, None)], method="highs") t_margin = res.x[4] if res.success else 0.0 if t_margin <= 1e-11: print(f"iteration {iteration}: LP margin {t_margin:.3e} -> stationary point reached") break d = res.x[:4] # exact line search over a geometric ladder of steps, deep into the gap scale best = (F(0), None, None) lam = F(1, 10**20) while lam <= F(1, 1000): trial = {name: (current[name] + lam * F(int(round(dv * 10**20)), 10**20)).limit_denominator(10**30) for name, dv in zip(("a", "b", "px", "py"), d)} tm = exact_min(trial) if tm > best[0]: best = (tm, dict(trial), lam) lam *= 4 if best[1] is None or best[0] <= m: print(f"iteration {iteration}: margin {t_margin:.3e} but no exact improvement " "along the direction; trying random perturbation of the direction") d = d + np.random.default_rng(iteration).normal(0, 0.1, 4) d = d / max(abs(d).max(), 1e-9) best = (F(0), None, None) lam = F(1, 10**20) while lam <= F(1, 1000): trial = {name: (current[name] + lam * F(int(round(dv * 10**20)), 10**20)).limit_denominator(10**30) for name, dv in zip(("a", "b", "px", "py"), d)} tm = exact_min(trial) if tm > best[0]: best = (tm, dict(trial), lam) lam *= 4 if best[1] is None or best[0] <= m: print("no improvement; stopping") break current = best[1] history.append((iteration, float(best[0]) / 2, float(best[2]))) print(f"iteration {iteration}: margin {t_margin:.3e}, exact doubled min " f"{float(best[0]):.19f} (area {float(best[0]) / 2:.19f}), step {float(best[2]):.3e}") m = exact_min(current) print(f"final exact doubled min {float(m):.19f}; area {float(m) / 2:.19f}") print(f"record root {root:.19f}") print(f"margin over the root: {float(m) / 2 - float(root):.3e}") # stationary check at the final point m, act = active_of(current) Gm = np.array([[float(signs[t] * poly_grad_at(polys[t], current).get(name, ZERO)) for name in ("a", "b", "px", "py")] for t in act]) A_eq = np.vstack([Gm.T, np.ones((1, len(act)))]) res = linprog(c=np.zeros(len(act)), A_eq=A_eq, b_eq=np.array([0.0] * 4 + [1.0]), bounds=[(0, None)] * len(act), method="highs") print(f"stationarity at the final point (0 in hull of {len(act)} active gradients): " f"{'YES' if res.success else 'NO'}") if len(sys.argv) > 2 and float(m) / 2 > float(root): outdir = Path(sys.argv[2]) outdir.mkdir(parents=True, exist_ok=True) pts = [] for rep in (current["a"], current["b"]): pts.extend([(rep, ZERO), (ONE, rep), (ONE - rep, ONE), (ZERO, ONE - rep)]) px, py = current["px"], current["py"] pts.extend([(px, py), (ONE - py, px), (ONE - px, ONE - py), (py, ONE - px)]) lines = [f"{x} {y}" for x, y in pts] (outdir / "n12.txt").write_text("\n".join(lines) + "\n") print(f"wrote the exact-fraction family point to {outdir / 'n12.txt'}") if __name__ == "__main__": main()