| @@ -0,0 +1,175 @@ |
| 1 | + |
| 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") |
| 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}") |