Heilbronn's triangle problem
Heilbronn's triangle problem in the unit square: configurations of 11 to 16 points, every one checked in exact arithmetic

| 1 | #!/usr/bin/env python3 |
| 2 | """Chase the quarter-turn family maximum for n = 12 (issue #8). |
| 3 | |
| 4 | The record point is not stationary for the family maximum (0 outside the convex |
| 5 | hull of active gradients, margin ~0.13 - see conv_test.py). This script optimizes |
| 6 | the 4-parameter family directly: SLSQP on the epigraph problem (a, b, px, py, t) |
| 7 | with all 220 integer-coefficient quadratic constraints and analytic Jacobians, |
| 8 | starting from the record parameters. The optimum is then evaluated in exact |
| 9 | rational arithmetic and compared with the record value (the smallest positive |
| 10 | root of 64t^3 + 80t^2 + 28t - 1). If the family's exact minimum exceeds the |
| 11 | record, the configuration is written as n12.txt (exact fractions) - a record |
| 12 | candidate for issue #8. If it does not, the exact line search along the LP's |
| 13 | ascent direction is reported instead. |
| 14 | |
| 15 | Usage: family_chase.py STEAD-n12.txt [OUTDIR] |
| 16 | """ |
| 17 | |
| 18 | import sys |
| 19 | from fractions import Fraction |
| 20 | from pathlib import Path |
| 21 | |
| 22 | import numpy as np |
| 23 | from scipy.optimize import minimize |
| 24 | |
| 25 | sys.path.insert(0, str(Path(__file__).resolve().parent)) |
| 26 | from conv_test import setup, doubled_poly |
| 27 | from family_max import poly_eval, poly_grad_at, ZERO |
| 28 | |
| 29 | RECORD_ROOT = None # filled from repro_checks' Newton (decimal) |
| 30 | |
| 31 | |
| 32 | def record_root(places=60): |
| 33 | import decimal |
| 34 | decimal.getcontext().prec = places + 10 |
| 35 | x = decimal.Decimal("0.03") |
| 36 | for _ in range(200): |
| 37 | f = 64 * x**3 + 80 * x**2 + 28 * x - 1 |
| 38 | x = x - f / (192 * x**2 + 160 * x + 28) |
| 39 | return float(x) |
| 40 | |
| 41 | |
| 42 | def main(): |
| 43 | values, polys, lit_areas, lit_min, active, grads, family_pts = setup(sys.argv[1]) |
| 44 | root = record_root() |
| 45 | print(f"record root (60 digits): {root:.17f}; literals' exact min (doubled/1): " |
| 46 | f"{float(lit_min) / 2:.17f}") |
| 47 | |
| 48 | triples = list(polys.keys()) |
| 49 | signs = {} |
| 50 | for t in triples: |
| 51 | s = 1 if poly_eval(polys[t], values) > 0 else -1 |
| 52 | signs[t] = s |
| 53 | |
| 54 | def constrain(z): |
| 55 | params = dict(zip(("a", "b", "px", "py"), z[:4])) |
| 56 | return np.array([signs[t] * float(poly_eval(polys[t], params)) - z[4] for t in triples]) |
| 57 | |
| 58 | def constrain_jac(z): |
| 59 | params = dict(zip(("a", "b", "px", "py"), z[:4])) |
| 60 | J = np.zeros((len(triples), 5)) |
| 61 | for i, t in enumerate(triples): |
| 62 | g = poly_grad_at(polys[t], params) |
| 63 | for r, name in enumerate(("a", "b", "px", "py")): |
| 64 | J[i, r] = signs[t] * float(g.get(name, ZERO)) |
| 65 | J[:, 4] = -1.0 |
| 66 | return J |
| 67 | |
| 68 | z0 = np.array([float(values["a"]), float(values["b"]), float(values["px"]), |
| 69 | float(values["py"]), float(lit_min) / 2]) |
| 70 | bounds = [(0, 1)] * 4 + [(1e-9, 0.7)] |
| 71 | res = minimize(lambda z: -z[4], z0, jac=lambda z: np.array([0, 0, 0, 0, -1.0]), |
| 72 | bounds=bounds, |
| 73 | constraints=[{"type": "ineq", "fun": constrain, "jac": constrain_jac}], |
| 74 | method="SLSQP", options={"maxiter": 500, "ftol": 1e-16}) |
| 75 | params = dict(zip(("a", "b", "px", "py"), res.x[:4])) |
| 76 | print(f"SLSQP family optimum: t_float = {res.x[4]:.17f} ({res.message})") |
| 77 | print(f" params a = {res.x[0]:.17f}, b = {res.x[1]:.17f}, px = {res.x[2]:.17f}, " |
| 78 | f"py = {res.x[3]:.17f}") |
| 79 | |
| 80 | exact_params = {name: Fraction(value).limit_denominator(10**30) |
| 81 | for name, value in params.items()} |
| 82 | areas = [abs(poly_eval(polys[t], exact_params)) for t in triples] |
| 83 | exact_min = min(areas) / 2 |
| 84 | print(f"exact family minimum at the SLSQP parameters: {float(exact_min):.17f}") |
| 85 | print(f"record root: {root:.17f}") |
| 86 | margin = float(exact_min) - root |
| 87 | print(f"margin over the record: {margin:.3e}") |
| 88 | |
| 89 | if margin > 0 and len(sys.argv) > 2: |
| 90 | outdir = Path(sys.argv[2]) |
| 91 | outdir.mkdir(parents=True, exist_ok=True) |
| 92 | pts = [] |
| 93 | for rep in (params["a"], params["b"]): |
| 94 | rx, ry = rep, 0.0 |
| 95 | pts.extend([(rx, ry), (1 - ry, rx), (1 - rx, 1 - ry), (ry, 1 - rx)]) |
| 96 | rx, ry = params["px"], params["py"] |
| 97 | pts.extend([(rx, ry), (1 - ry, rx), (1 - rx, 1 - ry), (ry, 1 - rx)]) |
| 98 | lines = [] |
| 99 | for x, y in pts: |
| 100 | fx, fy = Fraction(x).limit_denominator(10**30), Fraction(y).limit_denominator(10**30) |
| 101 | lines.append(f"{fx} {fy}") |
| 102 | (outdir / "n12.txt").write_text("\n".join(lines) + "\n") |
| 103 | print(f"wrote record candidate to {outdir / 'n12.txt'} (verify it with python3 verify.py)") |
| 104 | elif margin <= 0: |
| 105 | print("the family optimum does not exceed the record at float precision; " |
| 106 | "reporting the exact line search along the LP ascent direction instead") |
| 107 | |
| 108 | |
| 109 | if __name__ == "__main__": |
| 110 | main() |