#!/usr/bin/env python3 """Search for n points in the unit square with a large smallest triangle, optionally under a symmetry of the square. Usage: search.py N SYM [STARTS] [SEED] [WORKERS] [HOPS] > n.txt SYM is one of none no symmetry c2 point reflection through the centre (x, y) -> (1-x, 1-y) c4 rotation by 90 degrees about the centre (x, y) -> (1-y, x) d1 mirror in the vertical midline (x, y) -> (1-x, y) dg mirror in the diagonal (x, y) -> (y, x) d2 both midline mirrors N must be a multiple of the group's order (orbits are taken generic, of full size). Each start: random orbit representatives, a short simulated annealing on the smallest triangle (floating point), then a sequential linear programme (SLP) in the representatives' coordinates: maximise t subject to the linearised signed areas of all near-minimal triangles staying >= t, inside a shrinking trust region and the square. Then HOPS rounds of basin hopping (move 1-2 representatives, SLP again, keep if better). Finally an unconstrained SLP polish of all 2N coordinates (symmetry dropped) so that a symmetric optimum can still move off it if that helps. 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 GROUPS = { "none": [((1, 0, 0, 1), (0, 0))], "c2": [((1, 0, 0, 1), (0, 0)), ((-1, 0, 0, -1), (1, 1))], "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))], "d1": [((1, 0, 0, 1), (0, 0)), ((-1, 0, 0, 1), (1, 0))], "dg": [((1, 0, 0, 1), (0, 0)), ((0, 1, 1, 0), (0, 0))], "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))], } def embedding(n, sym): """Full coordinates P (2n) = M z + o for representatives z (2m).""" g = GROUPS[sym] m = n // len(g) M = np.zeros((2 * n, 2 * m)) o = np.zeros(2 * n) k = 0 for (a, b, c, d), (e, f) in g: for i in range(m): M[2 * k, 2 * i], M[2 * k, 2 * i + 1], o[2 * k] = a, b, e M[2 * k + 1, 2 * i], M[2 * k + 1, 2 * i + 1], o[2 * k + 1] = c, d, f k += 1 return M, o, m 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 grad(p, T): 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)) 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 class Problem: def __init__(self, n, sym): self.n, self.T = n, triples(n) self.M, self.o, self.m = embedding(n, sym) def full(self, z): return (self.M @ z + self.o).reshape(self.n, 2) def value(self, z): return np.abs(signed(self.full(z), self.T)).min() def anneal(self, z, rng, steps): now = best = self.value(z) kept = z.copy() for k in range(steps): heat = 1e-3 ** (1 + k / steps) step = 0.2 * 0.005 ** (k / steps) i = rng.integers(self.m) old = z[2 * i:2 * i + 2].copy() z[2 * i:2 * i + 2] = np.clip(old + rng.normal(0, step, 2), 0, 1) new = self.value(z) if new >= now or rng.random() < np.exp((new - now) / heat): now = new if new > best: best, kept = new, z.copy() else: z[2 * i:2 * i + 2] = old return kept def slp(self, z, iters=400, radius=0.02): best = self.value(z) k = len(z) for _ in range(iters): p = self.full(z) s = signed(p, self.T) sg = np.sign(s) sg[sg == 0] = 1 mn = np.abs(s).min() act = np.abs(s) < mn + 4 * radius G = (grad(p, self.T)[act] @ self.M) * sg[act, None] A = np.hstack([-G, np.ones((act.sum(), 1))]) bounds = [(max(-radius, -zi), min(radius, 1 - zi)) for zi in z] + [(None, None)] cost = np.zeros(k + 1) cost[-1] = -1 res = linprog(cost, A_ub=A, b_ub=np.abs(s[act]), bounds=bounds, method="highs") if res.status != 0: radius /= 2 continue q = np.clip(z + res.x[:-1], 0, 1) v = self.value(q) if v > best: z, best = q, v radius = min(radius * 1.5, 0.05) else: radius /= 2 if radius < 1e-14: break return z, best def one(args): n, sym, seed, hops = args rng = np.random.default_rng(seed) P = Problem(n, sym) z = P.anneal(rng.random(2 * P.m), rng, steps=4000) z, v = P.slp(z) for _ in range(hops): q = z.copy() for i in rng.choice(P.m, rng.integers(1, 3), replace=False): q[2 * i:2 * i + 2] = np.clip(q[2 * i:2 * i + 2] + rng.normal(0, 0.08, 2), 0, 1) q, w = P.slp(q, iters=150) if w > v: z, v = q, w F = Problem(n, "none") # polish without symmetry pz, pv = F.slp(P.full(z).reshape(-1).copy(), iters=300, radius=1e-4) if pv >= v: return pv, seed, F.full(pz) return v, seed, P.full(z) if __name__ == "__main__": if len(sys.argv) < 3: sys.exit(__doc__.strip()) n, sym = int(sys.argv[1]), sys.argv[2] starts = int(sys.argv[3]) if len(sys.argv) > 3 else 64 seed = int(sys.argv[4]) if len(sys.argv) > 4 else 0 workers = int(sys.argv[5]) if len(sys.argv) > 5 else 8 hops = int(sys.argv[6]) if len(sys.argv) > 6 else 30 assert n % len(GROUPS[sym]) == 0 with Pool(workers) as pool: runs = pool.map(one, [(n, sym, seed + k, hops) for k in range(starts)]) runs.sort(key=lambda r: -r[0]) for v, s, _ in runs[:5]: print(f"# {sym} 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 under symmetry {sym} " f"(seeds {seed}..{seed + starts - 1}), found by search.py {n} {sym} {starts} {seed} {workers} {hops}; best seed {s}") for x, y in p: print(f"{float(x)!r} {float(y)!r}")