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 | """search.py with points pinned to the boundary of the square. |
| 3 | |
| 4 | Usage: bsearch.py N SYM K STARTS SEED WORKERS HOPS > n<N>.txt |
| 5 | |
| 6 | Like search.py, but among the orbit representatives K are pinned to sides of the square (their other coordinate |
| 7 | stays free): at most 2 per side (a third would make three collinear points), sides drawn at random. SYM is none or c2; |
| 8 | under c2 a representative on x = 0 (y = 0) has its image on x = 1 (y = 1), so K <= 4 there, K <= 8 without symmetry. Pinned coordinates never move in the annealing, the SLP or the hops; the final unsymmetric polish |
| 9 | keeps every point that lies on the boundary on it. Needs numpy, scipy and search.py beside it. |
| 10 | """ |
| 11 | import sys |
| 12 | from multiprocessing import Pool |
| 13 | |
| 14 | import numpy as np |
| 15 | |
| 16 | from search import GROUPS, Problem |
| 17 | |
| 18 | |
| 19 | class Pinned(Problem): |
| 20 | def __init__(self, n, sym, pin): |
| 21 | super().__init__(n, sym) |
| 22 | self.pin = pin # boolean mask over the 2m representative coordinates |
| 23 | |
| 24 | def anneal(self, z, rng, steps): |
| 25 | now = best = self.value(z) |
| 26 | kept = z.copy() |
| 27 | for k in range(steps): |
| 28 | heat = 1e-3 ** (1 + k / steps) |
| 29 | step = 0.2 * 0.005 ** (k / steps) |
| 30 | i = rng.integers(self.m) |
| 31 | old = z[2 * i:2 * i + 2].copy() |
| 32 | move = rng.normal(0, step, 2) * ~self.pin[2 * i:2 * i + 2] |
| 33 | z[2 * i:2 * i + 2] = np.clip(old + move, 0, 1) |
| 34 | new = self.value(z) |
| 35 | if new >= now or rng.random() < np.exp((new - now) / heat): |
| 36 | now = new |
| 37 | if new > best: |
| 38 | best, kept = new, z.copy() |
| 39 | else: |
| 40 | z[2 * i:2 * i + 2] = old |
| 41 | return kept |
| 42 | |
| 43 | def slp(self, z, iters=400, radius=0.02): |
| 44 | from scipy.optimize import linprog |
| 45 | from search import grad, signed |
| 46 | best = self.value(z) |
| 47 | k = len(z) |
| 48 | for _ in range(iters): |
| 49 | p = self.full(z) |
| 50 | s = signed(p, self.T) |
| 51 | sg = np.sign(s) |
| 52 | sg[sg == 0] = 1 |
| 53 | mn = np.abs(s).min() |
| 54 | act = np.abs(s) < mn + 4 * radius |
| 55 | G = (grad(p, self.T)[act] @ self.M) * sg[act, None] |
| 56 | A = np.hstack([-G, np.ones((act.sum(), 1))]) |
| 57 | bounds = [(0, 0) if f else (max(-radius, -zi), min(radius, 1 - zi)) for zi, f in zip(z, self.pin)] |
| 58 | cost = np.zeros(k + 1) |
| 59 | cost[-1] = -1 |
| 60 | res = linprog(cost, A_ub=A, b_ub=np.abs(s[act]), bounds=bounds + [(None, None)], method="highs") |
| 61 | if res.status != 0: |
| 62 | radius /= 2 |
| 63 | continue |
| 64 | q = np.clip(z + res.x[:-1], 0, 1) |
| 65 | v = self.value(q) |
| 66 | if v > best: |
| 67 | z, best = q, v |
| 68 | radius = min(radius * 1.5, 0.05) |
| 69 | else: |
| 70 | radius /= 2 |
| 71 | if radius < 1e-14: |
| 72 | break |
| 73 | return z, best |
| 74 | |
| 75 | |
| 76 | def one(args): |
| 77 | n, sym, K, seed, hops = args |
| 78 | rng = np.random.default_rng(seed) |
| 79 | m = n // len(GROUPS[sym]) |
| 80 | z = rng.random(2 * m) |
| 81 | pin = np.zeros(2 * m, bool) |
| 82 | # sides as (coordinate, value): a side holds at most 2 points (a third would be collinear), so the K pinned |
| 83 | # representatives and their images are spread evenly: under c2 a representative on x = 0 has its image on x = 1. |
| 84 | sides = [(0, 0), (1, 0), (0, 1), (1, 1)] if sym == "none" else [(0, 0), (1, 0)] |
| 85 | slots = [sd for sd in sides for _ in range(2)] |
| 86 | for i, j in zip(rng.choice(m, K, replace=False), rng.permutation(len(slots))[:K]): |
| 87 | c, val = slots[j] |
| 88 | pin[2 * i + c] = True |
| 89 | z[2 * i + c] = val |
| 90 | P = Pinned(n, sym, pin) |
| 91 | z = P.anneal(z, rng, steps=4000) |
| 92 | z, v = P.slp(z) |
| 93 | for _ in range(hops): |
| 94 | q = z.copy() |
| 95 | for i in rng.choice(m, rng.integers(1, 3), replace=False): |
| 96 | q[2 * i:2 * i + 2] = np.clip(q[2 * i:2 * i + 2] + rng.normal(0, 0.08, 2) * ~pin[2 * i:2 * i + 2], 0, 1) |
| 97 | q, w = P.slp(q, iters=150) |
| 98 | if w > v: |
| 99 | z, v = q, w |
| 100 | full = P.full(z).reshape(-1) |
| 101 | F = Pinned(n, "none", (full == 0) | (full == 1)) |
| 102 | pz, pv = F.slp(full.copy(), iters=300, radius=1e-4) |
| 103 | if pv >= v: |
| 104 | return pv, seed, F.full(pz) |
| 105 | return v, seed, P.full(z) |
| 106 | |
| 107 | |
| 108 | if __name__ == "__main__": |
| 109 | if len(sys.argv) < 8: |
| 110 | sys.exit(__doc__.strip()) |
| 111 | n, sym, K = int(sys.argv[1]), sys.argv[2], int(sys.argv[3]) |
| 112 | starts, seed, workers, hops = map(int, sys.argv[4:8]) |
| 113 | with Pool(workers) as pool: |
| 114 | runs = pool.map(one, [(n, sym, K, seed + k, hops) for k in range(starts)]) |
| 115 | runs.sort(key=lambda r: -r[0]) |
| 116 | for v, s, _ in runs[:5]: |
| 117 | print(f"# {sym} K={K} start seed {s}: {v:.10f}", file=sys.stderr) |
| 118 | v, s, p = runs[0] |
| 119 | print(f"# n = {n}: smallest triangle about {v:.10f}, best of {starts} starts under symmetry {sym} with {K} " |
| 120 | f"pinned representatives, found by bsearch.py {n} {sym} {K} {starts} {seed} {workers} {hops}; best seed {s}") |
| 121 | for x, y in p: |
| 122 | print(f"{float(x)!r} {float(y)!r}") |