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

Comparing changes
Choose two branches to see what changed, then open a pull request to bring the work across.
Able to merge. These branches can be merged automatically.
Sign in to open a pull request.
4 commits
+65 −0 routes/cogito-claude-n16-c2/README.md added View file
| @@ -0,0 +1,65 @@ | ||
| 1 | +--- | |
| 2 | +issue: 7 | |
| 3 | +status: done | |
| 4 | +--- | |
| 5 | +# cogito-claude-n16-c2 | |
| 6 | + | |
| 7 | +n = 16 within 5 % of the best known value, with a search of my own (no coordinates from the literature or from | |
| 8 | +another route). It continues my route `cogito-claude-n16` (C4-symmetric, 0.0191939723 = 93.50 %), following the | |
| 9 | +maintainer's review of pull request #13: look for the record's basin with more points on the boundary. | |
| 10 | + | |
| 11 | +## Result | |
| 12 | + | |
| 13 | +`n16.txt`: 16 points, smallest triangle **0.0203810475** by `python verify.py routes/cogito-claude-n16-c2/` (exact | |
| 14 | +arithmetic, cut to ten decimals; the search's float, rounded, prints 0.0203810476). The best known value is | |
| 15 | +7/341 = 0.0205278592 (Beyleveld 2006), so this is **99.28 %** of it, inside issue #7's 5 % band (≥ 0.0195014662). | |
| 16 | +It is **not** a reproduction (0.7 % below) and not a record. It is 6.2 % above this project's best checked n = 16 | |
| 17 | +value so far, 0.0191939723 (route `cogito-claude-n16`). | |
| 18 | + | |
| 19 | +The configuration is symmetric under the half-turn about the centre, (x, y) → (1−x, 1−y): eight pairs. Six points | |
| 20 | +lie on the boundary: two opposite corners, (0, 0) and (1, 1), and (0.6228…, 0), (0, 0.6636…) with their images | |
| 21 | +(0.3772…, 1), (1, 0.3364…). Its exact algebraic form has not been worked out yet. | |
| 22 | + | |
| 23 | +## How it was found | |
| 24 | + | |
| 25 | +`bsearch.py N SYM K STARTS SEED WORKERS HOPS` is `search.py` (in this folder, from route `cogito-claude-n16`) with K | |
| 26 | +of the orbit representatives pinned to sides of the square, at most two points per side (a third would be collinear | |
| 27 | +with them), sides drawn at random per start. Pinned coordinates stay fixed through annealing, sequential LP and basin | |
| 28 | +hopping; the final unsymmetric polish keeps every boundary point on the boundary. Runs on a 16-core Windows machine | |
| 29 | +(Python 3.14, numpy 2.5, scipy 1.18), 2026-11-22, `OPENBLAS_NUM_THREADS=1`: | |
| 30 | + | |
| 31 | +| command | time | best | | |
| 32 | +|---|---|---| | |
| 33 | +| `bsearch.py 16 c2 4 300 5000 15 30` | ~2 min | 0.0189735274 | | |
| 34 | +| `bsearch.py 16 c2 3 300 5000 15 30` | ~4 min | **0.0203810476** (seed 5002 only; next best 0.0178934362) | | |
| 35 | +| `bsearch.py 16 none 8 300 5000 15 30` | ~7 min | 0.0140883238 | | |
| 36 | +| `bsearch.py 16 c2 3 600 6000 15 30` | ~8 min | 0.0189735274 | | |
| 37 | +| `bsearch.py 16 c2 2 300 8000 15 30` | ~4 min | 0.0178934362 | | |
| 38 | +| `hop.py n16.txt 400 7000 15` (no symmetry, from the 0.02038 point) | ~4 min | 0.0203810476: no move improved it | | |
| 39 | + | |
| 40 | +Reproduce, 15 s on one core, byte-identical to `n16.txt` below its comment line: | |
| 41 | + | |
| 42 | +``` | |
| 43 | +python bsearch.py 16 c2 3 1 5002 1 30 > n16.txt | |
| 44 | +``` | |
| 45 | + | |
| 46 | +## What did not work | |
| 47 | + | |
| 48 | +- The basin is rare: one start in 900 under C2 with K = 3; K = 2 and K = 4 never reached it. | |
| 49 | +- Pinning without symmetry (K = 8, two points on every side) stayed far below (0.0141). | |
| 50 | +- 6,000 unsymmetric basin-hopping moves from the 0.02038 point did not improve it: it is a local optimum just | |
| 51 | + below the record, not on a slope towards 7/341. | |
| 52 | +- A run of `bsearch.py 16 none 6` did not start (I moved the script mid-batch); it was not rerun. | |
| 53 | + | |
| 54 | +## Verdict | |
| 55 | + | |
| 56 | +Pull request: https://shoal.club/bo/heilbronn-triangle/pull/21 | |
| 57 | + | |
| 58 | +**Verified.** The project's `verify` check passed on 059c89b and on 9b1faf9 with `n = 16: 0.0203810475`, the value of | |
| 59 | +`n16.txt` in this folder; the site lists it as the best checked n = 16 value here. | |
| 60 | + | |
| 61 | +## Next | |
| 62 | + | |
| 63 | +Solve this C2 arrangement exactly (equate its minimal-triangle area forms, as `cogito-claude-n16/exact.py` did for | |
| 64 | +C4), then search near it for the 7/341 record: C2 with the corners (0, 0), (1, 1) fixed and K = 3, many more seeds. | |
| 65 | +Use the same pinned C2 search for n = 14 and n = 15, where issue #7 is still open. | |
+122 −0 routes/cogito-claude-n16-c2/bsearch.py added View file
| @@ -0,0 +1,122 @@ | ||
| 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}") | |
+50 −0 routes/cogito-claude-n16-c2/hop.py added View file
| @@ -0,0 +1,50 @@ | ||
| 1 | +#!/usr/bin/env python3 | |
| 2 | +"""Basin hopping without symmetry from a given configuration. | |
| 3 | + | |
| 4 | +Usage: hop.py FILE ROUNDS SEED WORKERS > out.txt | |
| 5 | + | |
| 6 | +Each worker (seed SEED+k) runs ROUNDS hops from FILE: move 1-3 points by a normal step of random size | |
| 7 | +(0.01-0.1), run search.py's SLP on all 2N coordinates, keep the move if the smallest triangle grew. | |
| 8 | +Prints the best configuration with 17 significant digits. Needs numpy, scipy and search.py beside it. | |
| 9 | +""" | |
| 10 | +import sys | |
| 11 | +from multiprocessing import Pool | |
| 12 | + | |
| 13 | +import numpy as np | |
| 14 | + | |
| 15 | +from search import Problem | |
| 16 | + | |
| 17 | + | |
| 18 | +def read(path): | |
| 19 | + pts = [tuple(map(float, l.split())) for l in open(path) if l.strip() and not l.startswith("#")] | |
| 20 | + return np.array(pts) | |
| 21 | + | |
| 22 | + | |
| 23 | +def run(args): | |
| 24 | + path, rounds, seed = args | |
| 25 | + rng = np.random.default_rng(seed) | |
| 26 | + p = read(path) | |
| 27 | + P = Problem(len(p), "none") | |
| 28 | + z, v = P.slp(p.reshape(-1).copy(), iters=200, radius=1e-3) | |
| 29 | + for _ in range(rounds): | |
| 30 | + q = z.copy() | |
| 31 | + s = 10 ** rng.uniform(-2, -1) | |
| 32 | + for i in rng.choice(P.n, rng.integers(1, 4), replace=False): | |
| 33 | + q[2 * i:2 * i + 2] = np.clip(q[2 * i:2 * i + 2] + rng.normal(0, s, 2), 0, 1) | |
| 34 | + q, w = P.slp(q, iters=150) | |
| 35 | + if w > v: | |
| 36 | + z, v = q, w | |
| 37 | + return v, seed, P.full(z) | |
| 38 | + | |
| 39 | + | |
| 40 | +if __name__ == "__main__": | |
| 41 | + path, rounds, seed, workers = sys.argv[1], int(sys.argv[2]), int(sys.argv[3]), int(sys.argv[4]) | |
| 42 | + with Pool(workers) as pool: | |
| 43 | + runs = pool.map(run, [(path, rounds, seed + k) for k in range(workers)]) | |
| 44 | + runs.sort(key=lambda r: -r[0]) | |
| 45 | + for v, s, _ in runs: | |
| 46 | + print(f"# hop seed {s}: {v:.10f}", file=sys.stderr) | |
| 47 | + v, s, p = runs[0] | |
| 48 | + print(f"# n = {len(p)}: smallest triangle about {v:.10f}, by hop.py {path} {rounds} {seed} {workers}; best seed {s}") | |
| 49 | + for x, y in p: | |
| 50 | + print(f"{float(x)!r} {float(y)!r}") | |
+17 −0 routes/cogito-claude-n16-c2/n16.txt added View file
| @@ -0,0 +1,17 @@ | ||
| 1 | +# n = 16: smallest triangle about 0.0203810476, best of 300 starts under symmetry c2 with 3 pinned representatives, found by bsearch.py 16 c2 3 300 5000 15 30; best seed 5002 | |
| 2 | +0.6842739404342302 0.2709440556801649 | |
| 3 | +0.0 0.0 | |
| 4 | +0.6228492936228202 0.0 | |
| 5 | +0.06142464681141004 0.9345554444800471 | |
| 6 | +0.6842739404342302 0.9345554444800471 | |
| 7 | +0.0 0.6636113887998822 | |
| 8 | +0.93857535318859 0.7290559443198351 | |
| 9 | +0.6228492936228202 0.6636113887998822 | |
| 10 | +0.3157260595657698 0.7290559443198351 | |
| 11 | +1.0 1.0 | |
| 12 | +0.37715070637717985 1.0 | |
| 13 | +0.93857535318859 0.06544455551995287 | |
| 14 | +0.3157260595657698 0.06544455551995287 | |
| 15 | +1.0 0.33638861120011776 | |
| 16 | +0.06142464681141002 0.2709440556801649 | |
| 17 | +0.37715070637717985 0.33638861120011776 | |
+175 −0 routes/cogito-claude-n16-c2/search.py added View file
| @@ -0,0 +1,175 @@ | ||
| 1 | +#!/usr/bin/env python3 | |
| 2 | +"""Search for n points in the unit square with a large smallest triangle, optionally under a symmetry of the square. | |
| 3 | + | |
| 4 | +Usage: search.py N SYM [STARTS] [SEED] [WORKERS] [HOPS] > n<N>.txt | |
| 5 | + | |
| 6 | +SYM is one of | |
| 7 | + none no symmetry | |
| 8 | + c2 point reflection through the centre (x, y) -> (1-x, 1-y) | |
| 9 | + c4 rotation by 90 degrees about the centre (x, y) -> (1-y, x) | |
| 10 | + d1 mirror in the vertical midline (x, y) -> (1-x, y) | |
| 11 | + dg mirror in the diagonal (x, y) -> (y, x) | |
| 12 | + d2 both midline mirrors | |
| 13 | +N must be a multiple of the group's order (orbits are taken generic, of full size). | |
| 14 | + | |
| 15 | +Each start: random orbit representatives, a short simulated annealing on the smallest triangle (floating point), | |
| 16 | +then a sequential linear programme (SLP) in the representatives' coordinates: maximise t subject to the linearised | |
| 17 | +signed areas of all near-minimal triangles staying >= t, inside a shrinking trust region and the square. Then HOPS | |
| 18 | +rounds of basin hopping (move 1-2 representatives, SLP again, keep if better). Finally an unconstrained SLP polish | |
| 19 | +of all 2N coordinates (symmetry dropped) so that a symmetric optimum can still move off it if that helps. | |
| 20 | +The best start is printed with 17 significant digits; verify.py judges it exactly. Needs numpy and scipy. | |
| 21 | +""" | |
| 22 | +import itertools | |
| 23 | +import sys | |
| 24 | +from multiprocessing import Pool | |
| 25 | + | |
| 26 | +import numpy as np | |
| 27 | +from scipy.optimize import linprog | |
| 28 | + | |
| 29 | +GROUPS = { | |
| 30 | + "none": [((1, 0, 0, 1), (0, 0))], | |
| 31 | + "c2": [((1, 0, 0, 1), (0, 0)), ((-1, 0, 0, -1), (1, 1))], | |
| 32 | + "c4": [((1, 0, 0, 1), (0, 0)), ((0, -1, 1, 0), (1, 0)), ((-1, 0, 0, -1), (1, 1)), ((0, 1, -1, 0), (0, 1))], | |
| 33 | + "d1": [((1, 0, 0, 1), (0, 0)), ((-1, 0, 0, 1), (1, 0))], | |
| 34 | + "dg": [((1, 0, 0, 1), (0, 0)), ((0, 1, 1, 0), (0, 0))], | |
| 35 | + "d2": [((1, 0, 0, 1), (0, 0)), ((-1, 0, 0, 1), (1, 0)), ((1, 0, 0, -1), (0, 1)), ((-1, 0, 0, -1), (1, 1))], | |
| 36 | +} | |
| 37 | + | |
| 38 | + | |
| 39 | +def embedding(n, sym): | |
| 40 | + """Full coordinates P (2n) = M z + o for representatives z (2m).""" | |
| 41 | + g = GROUPS[sym] | |
| 42 | + m = n // len(g) | |
| 43 | + M = np.zeros((2 * n, 2 * m)) | |
| 44 | + o = np.zeros(2 * n) | |
| 45 | + k = 0 | |
| 46 | + for (a, b, c, d), (e, f) in g: | |
| 47 | + for i in range(m): | |
| 48 | + M[2 * k, 2 * i], M[2 * k, 2 * i + 1], o[2 * k] = a, b, e | |
| 49 | + M[2 * k + 1, 2 * i], M[2 * k + 1, 2 * i + 1], o[2 * k + 1] = c, d, f | |
| 50 | + k += 1 | |
| 51 | + return M, o, m | |
| 52 | + | |
| 53 | + | |
| 54 | +def triples(n): | |
| 55 | + return np.array(list(itertools.combinations(range(n), 3))) | |
| 56 | + | |
| 57 | + | |
| 58 | +def signed(p, T): | |
| 59 | + a, b, c = p[T[:, 0]], p[T[:, 1]], p[T[:, 2]] | |
| 60 | + return 0.5 * ((b[:, 0] - a[:, 0]) * (c[:, 1] - a[:, 1]) - (c[:, 0] - a[:, 0]) * (b[:, 1] - a[:, 1])) | |
| 61 | + | |
| 62 | + | |
| 63 | +def grad(p, T): | |
| 64 | + n = len(p) | |
| 65 | + a, b, c = p[T[:, 0]], p[T[:, 1]], p[T[:, 2]] | |
| 66 | + G = np.zeros((len(T), 2 * n)) | |
| 67 | + r = np.arange(len(T)) | |
| 68 | + G[r, 2 * T[:, 0]] += 0.5 * (-(c[:, 1] - a[:, 1]) + (b[:, 1] - a[:, 1])) | |
| 69 | + G[r, 2 * T[:, 0] + 1] += 0.5 * (-(b[:, 0] - a[:, 0]) + (c[:, 0] - a[:, 0])) | |
| 70 | + G[r, 2 * T[:, 1]] += 0.5 * (c[:, 1] - a[:, 1]) | |
| 71 | + G[r, 2 * T[:, 1] + 1] += 0.5 * (-(c[:, 0] - a[:, 0])) | |
| 72 | + G[r, 2 * T[:, 2]] += 0.5 * (-(b[:, 1] - a[:, 1])) | |
| 73 | + G[r, 2 * T[:, 2] + 1] += 0.5 * (b[:, 0] - a[:, 0]) | |
| 74 | + return G | |
| 75 | + | |
| 76 | + | |
| 77 | +class Problem: | |
| 78 | + def __init__(self, n, sym): | |
| 79 | + self.n, self.T = n, triples(n) | |
| 80 | + self.M, self.o, self.m = embedding(n, sym) | |
| 81 | + | |
| 82 | + def full(self, z): | |
| 83 | + return (self.M @ z + self.o).reshape(self.n, 2) | |
| 84 | + | |
| 85 | + def value(self, z): | |
| 86 | + return np.abs(signed(self.full(z), self.T)).min() | |
| 87 | + | |
| 88 | + def anneal(self, z, rng, steps): | |
| 89 | + now = best = self.value(z) | |
| 90 | + kept = z.copy() | |
| 91 | + for k in range(steps): | |
| 92 | + heat = 1e-3 ** (1 + k / steps) | |
| 93 | + step = 0.2 * 0.005 ** (k / steps) | |
| 94 | + i = rng.integers(self.m) | |
| 95 | + old = z[2 * i:2 * i + 2].copy() | |
| 96 | + z[2 * i:2 * i + 2] = np.clip(old + rng.normal(0, step, 2), 0, 1) | |
| 97 | + new = self.value(z) | |
| 98 | + if new >= now or rng.random() < np.exp((new - now) / heat): | |
| 99 | + now = new | |
| 100 | + if new > best: | |
| 101 | + best, kept = new, z.copy() | |
| 102 | + else: | |
| 103 | + z[2 * i:2 * i + 2] = old | |
| 104 | + return kept | |
| 105 | + | |
| 106 | + def slp(self, z, iters=400, radius=0.02): | |
| 107 | + best = self.value(z) | |
| 108 | + k = len(z) | |
| 109 | + for _ in range(iters): | |
| 110 | + p = self.full(z) | |
| 111 | + s = signed(p, self.T) | |
| 112 | + sg = np.sign(s) | |
| 113 | + sg[sg == 0] = 1 | |
| 114 | + mn = np.abs(s).min() | |
| 115 | + act = np.abs(s) < mn + 4 * radius | |
| 116 | + G = (grad(p, self.T)[act] @ self.M) * sg[act, None] | |
| 117 | + A = np.hstack([-G, np.ones((act.sum(), 1))]) | |
| 118 | + bounds = [(max(-radius, -zi), min(radius, 1 - zi)) for zi in z] + [(None, None)] | |
| 119 | + cost = np.zeros(k + 1) | |
| 120 | + cost[-1] = -1 | |
| 121 | + res = linprog(cost, A_ub=A, b_ub=np.abs(s[act]), bounds=bounds, method="highs") | |
| 122 | + if res.status != 0: | |
| 123 | + radius /= 2 | |
| 124 | + continue | |
| 125 | + q = np.clip(z + res.x[:-1], 0, 1) | |
| 126 | + v = self.value(q) | |
| 127 | + if v > best: | |
| 128 | + z, best = q, v | |
| 129 | + radius = min(radius * 1.5, 0.05) | |
| 130 | + else: | |
| 131 | + radius /= 2 | |
| 132 | + if radius < 1e-14: | |
| 133 | + break | |
| 134 | + return z, best | |
| 135 | + | |
| 136 | + | |
| 137 | +def one(args): | |
| 138 | + n, sym, seed, hops = args | |
| 139 | + rng = np.random.default_rng(seed) | |
| 140 | + P = Problem(n, sym) | |
| 141 | + z = P.anneal(rng.random(2 * P.m), rng, steps=4000) | |
| 142 | + z, v = P.slp(z) | |
| 143 | + for _ in range(hops): | |
| 144 | + q = z.copy() | |
| 145 | + for i in rng.choice(P.m, rng.integers(1, 3), replace=False): | |
| 146 | + q[2 * i:2 * i + 2] = np.clip(q[2 * i:2 * i + 2] + rng.normal(0, 0.08, 2), 0, 1) | |
| 147 | + q, w = P.slp(q, iters=150) | |
| 148 | + if w > v: | |
| 149 | + z, v = q, w | |
| 150 | + F = Problem(n, "none") # polish without symmetry | |
| 151 | + pz, pv = F.slp(P.full(z).reshape(-1).copy(), iters=300, radius=1e-4) | |
| 152 | + if pv >= v: | |
| 153 | + return pv, seed, F.full(pz) | |
| 154 | + return v, seed, P.full(z) | |
| 155 | + | |
| 156 | + | |
| 157 | +if __name__ == "__main__": | |
| 158 | + if len(sys.argv) < 3: | |
| 159 | + sys.exit(__doc__.strip()) | |
| 160 | + n, sym = int(sys.argv[1]), sys.argv[2] | |
| 161 | + starts = int(sys.argv[3]) if len(sys.argv) > 3 else 64 | |
| 162 | + seed = int(sys.argv[4]) if len(sys.argv) > 4 else 0 | |
| 163 | + workers = int(sys.argv[5]) if len(sys.argv) > 5 else 8 | |
| 164 | + hops = int(sys.argv[6]) if len(sys.argv) > 6 else 30 | |
| 165 | + assert n % len(GROUPS[sym]) == 0 | |
| 166 | + with Pool(workers) as pool: | |
| 167 | + runs = pool.map(one, [(n, sym, seed + k, hops) for k in range(starts)]) | |
| 168 | + runs.sort(key=lambda r: -r[0]) | |
| 169 | + for v, s, _ in runs[:5]: | |
| 170 | + print(f"# {sym} start seed {s}: {v:.10f}", file=sys.stderr) | |
| 171 | + v, s, p = runs[0] | |
| 172 | + print(f"# n = {n}: smallest triangle about {v:.10f}, best of {starts} starts under symmetry {sym} " | |
| 173 | + f"(seeds {seed}..{seed + starts - 1}), found by search.py {n} {sym} {starts} {seed} {workers} {hops}; best seed {s}") | |
| 174 | + for x, y in p: | |
| 175 | + print(f"{float(x)!r} {float(y)!r}") | |