#!/usr/bin/env python3 """Search for n points in the unit square with a large smallest triangle. Usage: search.py N [STARTS] [SEED] [WORKERS] > n.txt Each start: a random configuration, a short simulated annealing on the smallest triangle (floating point), then a sequential linear programme (SLP), then HOPS rounds of basin hopping (move 1-3 points, SLP again, keep if better) that maximises t subject to the linearised signed areas of all near-minimal triangles staying >= t, inside a shrinking trust region and the square. The best start is printed with 17 significant digits; verify.py judges it exactly. Needs numpy and scipy. """ import itertools import sys from multiprocessing import Pool import numpy as np from scipy.optimize import linprog def triples(n): return np.array(list(itertools.combinations(range(n), 3))) def signed(p, T): a, b, c = p[T[:, 0]], p[T[:, 1]], p[T[:, 2]] return 0.5 * ((b[:, 0] - a[:, 0]) * (c[:, 1] - a[:, 1]) - (c[:, 0] - a[:, 0]) * (b[:, 1] - a[:, 1])) def smallest(p, T): return np.abs(signed(p, T)).min() def anneal(p, T, rng, steps=20000): n = len(p) now = best = smallest(p, T) kept = p.copy() for k in range(steps): heat = 1e-3 ** (1 + k / steps) step = 0.2 * 0.005 ** (k / steps) i = rng.integers(n) old = p[i].copy() p[i] = np.clip(old + rng.normal(0, step, 2), 0, 1) new = smallest(p, T) if new >= now or rng.random() < np.exp((new - now) / heat): now = new if new > best: best, kept = new, p.copy() else: p[i] = old return kept def grad(p, T): """d(signed area)/d(coordinates) for every triple: rows over triples, columns x0,y0,x1,y1,...""" n = len(p) a, b, c = p[T[:, 0]], p[T[:, 1]], p[T[:, 2]] G = np.zeros((len(T), 2 * n)) r = np.arange(len(T)) # 2S = (bx-ax)(cy-ay) - (cx-ax)(by-ay) G[r, 2 * T[:, 0]] += 0.5 * (-(c[:, 1] - a[:, 1]) + (b[:, 1] - a[:, 1])) G[r, 2 * T[:, 0] + 1] += 0.5 * (-(b[:, 0] - a[:, 0]) + (c[:, 0] - a[:, 0])) G[r, 2 * T[:, 1]] += 0.5 * (c[:, 1] - a[:, 1]) G[r, 2 * T[:, 1] + 1] += 0.5 * (-(c[:, 0] - a[:, 0])) G[r, 2 * T[:, 2]] += 0.5 * (-(b[:, 1] - a[:, 1])) G[r, 2 * T[:, 2] + 1] += 0.5 * (b[:, 0] - a[:, 0]) return G def slp(p, T, iters=400, radius=0.02): n = len(p) best = smallest(p, T) for _ in range(iters): s = signed(p, T) sg = np.sign(s) sg[sg == 0] = 1 m = np.abs(s).min() act = np.abs(s) < m + 4 * radius # triangles that can become minimal within the trust region G = grad(p, T)[act] * sg[act, None] # variables: dx (2n), t ; maximise t: minimise -t # -(|s| + G dx) + t <= 0 A = np.hstack([-G, np.ones((act.sum(), 1))]) bnd = np.abs(s[act]) x = p.reshape(-1) bounds = [(max(-radius, -xi), min(radius, 1 - xi)) for xi in x] + [(None, None)] cost = np.zeros(2 * n + 1) cost[-1] = -1 res = linprog(cost, A_ub=A, b_ub=bnd, bounds=bounds, method="highs") if res.status != 0: radius /= 2 continue q = np.clip(p + res.x[:-1].reshape(n, 2), 0, 1) v = smallest(q, T) if v > best: p, best = q, v radius = min(radius * 1.5, 0.05) else: radius /= 2 if radius < 1e-13: break return p, best HOPS = 40 def one(args): n, seed = args rng = np.random.default_rng(seed) T = triples(n) p = anneal(rng.random((n, 2)), T, rng, steps=5000) p, v = slp(p, T) for _ in range(HOPS): # basin hopping: move a few points, optimise again, keep it if better q = p.copy() k = rng.integers(1, 4) for i in rng.choice(n, k, replace=False): q[i] = np.clip(q[i] + rng.normal(0, 0.08, 2), 0, 1) q, w = slp(q, T, iters=150) if w > v: p, v = q, w return v, seed, p if __name__ == "__main__": if len(sys.argv) < 2: sys.exit(__doc__.strip()) n = int(sys.argv[1]) starts = int(sys.argv[2]) if len(sys.argv) > 2 else 64 seed = int(sys.argv[3]) if len(sys.argv) > 3 else 0 workers = int(sys.argv[4]) if len(sys.argv) > 4 else 8 with Pool(workers) as pool: runs = pool.map(one, [(n, seed + k) for k in range(starts)]) runs.sort(key=lambda r: -r[0]) for v, s, _ in runs[:5]: print(f"# start seed {s}: {v:.10f}", file=sys.stderr) v, s, p = runs[0] print(f"# n = {n}: smallest triangle about {v:.10f}, best of {starts} starts (seeds {seed}..{seed + starts - 1}), " f"found by search.py {n} {starts} {seed}; best seed {s}") for x, y in p: print(f"{float(x)!r} {float(y)!r}")