#!/usr/bin/env python3 """Chase the quarter-turn family maximum for n = 12 (issue #8). The record point is not stationary for the family maximum (0 outside the convex hull of active gradients, margin ~0.13 - see conv_test.py). This script optimizes the 4-parameter family directly: SLSQP on the epigraph problem (a, b, px, py, t) with all 220 integer-coefficient quadratic constraints and analytic Jacobians, starting from the record parameters. The optimum is then evaluated in exact rational arithmetic and compared with the record value (the smallest positive root of 64t^3 + 80t^2 + 28t - 1). If the family's exact minimum exceeds the record, the configuration is written as n12.txt (exact fractions) - a record candidate for issue #8. If it does not, the exact line search along the LP's ascent direction is reported instead. Usage: family_chase.py STEAD-n12.txt [OUTDIR] """ import sys from fractions import Fraction from pathlib import Path import numpy as np from scipy.optimize import minimize sys.path.insert(0, str(Path(__file__).resolve().parent)) from conv_test import setup, doubled_poly from family_max import poly_eval, poly_grad_at, ZERO RECORD_ROOT = None # filled from repro_checks' Newton (decimal) def record_root(places=60): import decimal decimal.getcontext().prec = places + 10 x = decimal.Decimal("0.03") for _ in range(200): f = 64 * x**3 + 80 * x**2 + 28 * x - 1 x = x - f / (192 * x**2 + 160 * x + 28) return float(x) def main(): values, polys, lit_areas, lit_min, active, grads, family_pts = setup(sys.argv[1]) root = record_root() print(f"record root (60 digits): {root:.17f}; literals' exact min (doubled/1): " f"{float(lit_min) / 2:.17f}") triples = list(polys.keys()) signs = {} for t in triples: s = 1 if poly_eval(polys[t], values) > 0 else -1 signs[t] = s def constrain(z): params = dict(zip(("a", "b", "px", "py"), z[:4])) return np.array([signs[t] * float(poly_eval(polys[t], params)) - z[4] for t in triples]) def constrain_jac(z): params = dict(zip(("a", "b", "px", "py"), z[:4])) J = np.zeros((len(triples), 5)) for i, t in enumerate(triples): g = poly_grad_at(polys[t], params) for r, name in enumerate(("a", "b", "px", "py")): J[i, r] = signs[t] * float(g.get(name, ZERO)) J[:, 4] = -1.0 return J z0 = np.array([float(values["a"]), float(values["b"]), float(values["px"]), float(values["py"]), float(lit_min) / 2]) bounds = [(0, 1)] * 4 + [(1e-9, 0.7)] res = minimize(lambda z: -z[4], z0, jac=lambda z: np.array([0, 0, 0, 0, -1.0]), bounds=bounds, constraints=[{"type": "ineq", "fun": constrain, "jac": constrain_jac}], method="SLSQP", options={"maxiter": 500, "ftol": 1e-16}) params = dict(zip(("a", "b", "px", "py"), res.x[:4])) print(f"SLSQP family optimum: t_float = {res.x[4]:.17f} ({res.message})") print(f" params a = {res.x[0]:.17f}, b = {res.x[1]:.17f}, px = {res.x[2]:.17f}, " f"py = {res.x[3]:.17f}") exact_params = {name: Fraction(value).limit_denominator(10**30) for name, value in params.items()} areas = [abs(poly_eval(polys[t], exact_params)) for t in triples] exact_min = min(areas) / 2 print(f"exact family minimum at the SLSQP parameters: {float(exact_min):.17f}") print(f"record root: {root:.17f}") margin = float(exact_min) - root print(f"margin over the record: {margin:.3e}") if margin > 0 and len(sys.argv) > 2: outdir = Path(sys.argv[2]) outdir.mkdir(parents=True, exist_ok=True) pts = [] for rep in (params["a"], params["b"]): rx, ry = rep, 0.0 pts.extend([(rx, ry), (1 - ry, rx), (1 - rx, 1 - ry), (ry, 1 - rx)]) rx, ry = params["px"], params["py"] pts.extend([(rx, ry), (1 - ry, rx), (1 - rx, 1 - ry), (ry, 1 - rx)]) lines = [] for x, y in pts: fx, fy = Fraction(x).limit_denominator(10**30), Fraction(y).limit_denominator(10**30) lines.append(f"{fx} {fy}") (outdir / "n12.txt").write_text("\n".join(lines) + "\n") print(f"wrote record candidate to {outdir / 'n12.txt'} (verify it with python3 verify.py)") elif margin <= 0: print("the family optimum does not exceed the record at float precision; " "reporting the exact line search along the LP ascent direction instead") if __name__ == "__main__": main()