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 | """LP-iterate ascent on the quarter-turn family maximum for n = 12 (issue #8). |
| 3 | |
| 4 | Nonsmooth min-max ascent with exact rational evaluation at every candidate: |
| 5 | |
| 6 | at the current parameters, take the active set (all 220 triangle areas within a |
| 7 | tolerance of the exact minimum), solve the LP max t s.t. grad_i . d >= t for |
| 8 | active i, |d|_inf <= 1 (t > 0 means every active area can grow simultaneously), |
| 9 | line-search the step along d with EXACT evaluation of the true minimum over all |
| 10 | 220 quadratic polynomials, accept the best step, and repeat until the LP margin |
| 11 | collapses. The result is a family stationary point (0 in the hull of its active |
| 12 | gradients, checked exactly), and its value is compared with the record root |
| 13 | (smallest positive root of 64t^3 + 80t^2 + 28t - 1). |
| 14 | |
| 15 | Usage: family_chase2.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 linprog |
| 24 | |
| 25 | sys.path.insert(0, str(Path(__file__).resolve().parent)) |
| 26 | from conv_test import setup |
| 27 | from family_max import poly_eval, poly_grad_at, ZERO |
| 28 | |
| 29 | F = Fraction |
| 30 | |
| 31 | |
| 32 | def record_root_float(): |
| 33 | import decimal |
| 34 | decimal.getcontext().prec = 70 |
| 35 | x = decimal.Decimal("0.03") |
| 36 | for _ in range(200): |
| 37 | x = x - (64 * x**3 + 80 * x**2 + 28 * x - 1) / (192 * x**2 + 160 * x + 28) |
| 38 | return x |
| 39 | |
| 40 | |
| 41 | def main(): |
| 42 | values, polys, lit_areas, lit_min, active0, grads0, family_pts = setup(sys.argv[1]) |
| 43 | triples = list(polys.keys()) |
| 44 | root = record_root_float() |
| 45 | import decimal |
| 46 | bar = decimal.Decimal("0.0325988587") - root |
| 47 | print(f"record root: {root:.19f}; to make the check print 0.0325988587 the family max " |
| 48 | f"must exceed the root by {float(bar):.3e}") |
| 49 | |
| 50 | current = dict(values) |
| 51 | |
| 52 | def exact_min(params): |
| 53 | return min(abs(poly_eval(polys[t], params)) for t in triples) |
| 54 | |
| 55 | def active_of(params, tol=Fraction(1, 10**25)): |
| 56 | m = exact_min(params) |
| 57 | act = [t for t in triples |
| 58 | if abs(poly_eval(polys[t], params)) - m <= tol] |
| 59 | return m, act |
| 60 | |
| 61 | signs = {} |
| 62 | for t in triples: |
| 63 | signs[t] = 1 if poly_eval(polys[t], current) > 0 else -1 |
| 64 | |
| 65 | m = exact_min(current) |
| 66 | print(f"start: exact doubled min {float(m):.19f} (record literals)") |
| 67 | history = [] |
| 68 | for iteration in range(200): |
| 69 | m, act = active_of(current) |
| 70 | # LP: max t s.t. (s_i grad_i) . d >= t for active i, |d| <= 1 |
| 71 | Gm = np.array([[float(signs[t] * grads0[t][name] if False else |
| 72 | signs[t] * poly_grad_at(polys[t], current).get(name, ZERO)) |
| 73 | for name in ("a", "b", "px", "py")] for t in act]) |
| 74 | A_ub = np.hstack([-Gm, np.ones((len(act), 1))]) |
| 75 | res = linprog(c=np.array([0.0] * 4 + [-1.0]), A_ub=A_ub, b_ub=np.zeros(len(act)), |
| 76 | bounds=[(-1, 1)] * 4 + [(None, None)], method="highs") |
| 77 | t_margin = res.x[4] if res.success else 0.0 |
| 78 | if t_margin <= 1e-11: |
| 79 | print(f"iteration {iteration}: LP margin {t_margin:.3e} -> stationary point reached") |
| 80 | break |
| 81 | d = res.x[:4] |
| 82 | # exact line search over a geometric ladder of steps, deep into the gap scale |
| 83 | best = (F(0), None, None) |
| 84 | lam = F(1, 10**20) |
| 85 | while lam <= F(1, 1000): |
| 86 | trial = {name: (current[name] + lam * F(int(round(dv * 10**20)), 10**20)).limit_denominator(10**30) |
| 87 | for name, dv in zip(("a", "b", "px", "py"), d)} |
| 88 | tm = exact_min(trial) |
| 89 | if tm > best[0]: |
| 90 | best = (tm, dict(trial), lam) |
| 91 | lam *= 4 |
| 92 | if best[1] is None or best[0] <= m: |
| 93 | print(f"iteration {iteration}: margin {t_margin:.3e} but no exact improvement " |
| 94 | "along the direction; trying random perturbation of the direction") |
| 95 | d = d + np.random.default_rng(iteration).normal(0, 0.1, 4) |
| 96 | d = d / max(abs(d).max(), 1e-9) |
| 97 | best = (F(0), None, None) |
| 98 | lam = F(1, 10**20) |
| 99 | while lam <= F(1, 1000): |
| 100 | trial = {name: (current[name] + lam * F(int(round(dv * 10**20)), 10**20)).limit_denominator(10**30) |
| 101 | for name, dv in zip(("a", "b", "px", "py"), d)} |
| 102 | tm = exact_min(trial) |
| 103 | if tm > best[0]: |
| 104 | best = (tm, dict(trial), lam) |
| 105 | lam *= 4 |
| 106 | if best[1] is None or best[0] <= m: |
| 107 | print("no improvement; stopping") |
| 108 | break |
| 109 | current = best[1] |
| 110 | history.append((iteration, float(best[0]) / 2, float(best[2]))) |
| 111 | print(f"iteration {iteration}: margin {t_margin:.3e}, exact doubled min " |
| 112 | f"{float(best[0]):.19f} (area {float(best[0]) / 2:.19f}), step {float(best[2]):.3e}") |
| 113 | |
| 114 | m = exact_min(current) |
| 115 | print(f"final exact doubled min {float(m):.19f}; area {float(m) / 2:.19f}") |
| 116 | print(f"record root {root:.19f}") |
| 117 | print(f"margin over the root: {float(m) / 2 - float(root):.3e}") |
| 118 | |
| 119 | # stationary check at the final point |
| 120 | m, act = active_of(current) |
| 121 | Gm = np.array([[float(signs[t] * poly_grad_at(polys[t], current).get(name, ZERO)) |
| 122 | for name in ("a", "b", "px", "py")] for t in act]) |
| 123 | A_eq = np.vstack([Gm.T, np.ones((1, len(act)))]) |
| 124 | res = linprog(c=np.zeros(len(act)), A_eq=A_eq, b_eq=np.array([0.0] * 4 + [1.0]), |
| 125 | bounds=[(0, None)] * len(act), method="highs") |
| 126 | print(f"stationarity at the final point (0 in hull of {len(act)} active gradients): " |
| 127 | f"{'YES' if res.success else 'NO'}") |
| 128 | |
| 129 | if len(sys.argv) > 2 and float(m) / 2 > float(root): |
| 130 | outdir = Path(sys.argv[2]) |
| 131 | outdir.mkdir(parents=True, exist_ok=True) |
| 132 | pts = [] |
| 133 | for rep in (current["a"], current["b"]): |
| 134 | pts.extend([(rep, ZERO), (ONE, rep), (ONE - rep, ONE), (ZERO, ONE - rep)]) |
| 135 | px, py = current["px"], current["py"] |
| 136 | pts.extend([(px, py), (ONE - py, px), (ONE - px, ONE - py), (py, ONE - px)]) |
| 137 | lines = [f"{x} {y}" for x, y in pts] |
| 138 | (outdir / "n12.txt").write_text("\n".join(lines) + "\n") |
| 139 | print(f"wrote the exact-fraction family point to {outdir / 'n12.txt'}") |
| 140 | |
| 141 | |
| 142 | if __name__ == "__main__": |
| 143 | main() |