| @@ -0,0 +1,138 @@ |
| 1 | + |
| 2 | +"""Search for n points in the unit square with a large smallest triangle. |
| 3 | + |
| 4 | +Usage: search.py N [STARTS] [SEED] [WORKERS] > n<N>.txt |
| 5 | + |
| 6 | +Each start: a random configuration, a short simulated annealing on the smallest triangle (floating point), then a |
| 7 | +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 |
| 8 | +triangles staying >= t, inside a shrinking trust region and the square. The best start is printed with 17 |
| 9 | +significant digits; verify.py judges it exactly. Needs numpy and scipy. |
| 10 | +""" |
| 11 | +import itertools |
| 12 | +import sys |
| 13 | +from multiprocessing import Pool |
| 14 | + |
| 15 | +import numpy as np |
| 16 | +from scipy.optimize import linprog |
| 17 | + |
| 18 | + |
| 19 | +def triples(n): |
| 20 | + return np.array(list(itertools.combinations(range(n), 3))) |
| 21 | + |
| 22 | + |
| 23 | +def signed(p, T): |
| 24 | + a, b, c = p[T[:, 0]], p[T[:, 1]], p[T[:, 2]] |
| 25 | + return 0.5 * ((b[:, 0] - a[:, 0]) * (c[:, 1] - a[:, 1]) - (c[:, 0] - a[:, 0]) * (b[:, 1] - a[:, 1])) |
| 26 | + |
| 27 | + |
| 28 | +def smallest(p, T): |
| 29 | + return np.abs(signed(p, T)).min() |
| 30 | + |
| 31 | + |
| 32 | +def anneal(p, T, rng, steps=20000): |
| 33 | + n = len(p) |
| 34 | + now = best = smallest(p, T) |
| 35 | + kept = p.copy() |
| 36 | + for k in range(steps): |
| 37 | + heat = 1e-3 ** (1 + k / steps) |
| 38 | + step = 0.2 * 0.005 ** (k / steps) |
| 39 | + i = rng.integers(n) |
| 40 | + old = p[i].copy() |
| 41 | + p[i] = np.clip(old + rng.normal(0, step, 2), 0, 1) |
| 42 | + new = smallest(p, T) |
| 43 | + if new >= now or rng.random() < np.exp((new - now) / heat): |
| 44 | + now = new |
| 45 | + if new > best: |
| 46 | + best, kept = new, p.copy() |
| 47 | + else: |
| 48 | + p[i] = old |
| 49 | + return kept |
| 50 | + |
| 51 | + |
| 52 | +def grad(p, T): |
| 53 | + """d(signed area)/d(coordinates) for every triple: rows over triples, columns x0,y0,x1,y1,...""" |
| 54 | + n = len(p) |
| 55 | + a, b, c = p[T[:, 0]], p[T[:, 1]], p[T[:, 2]] |
| 56 | + G = np.zeros((len(T), 2 * n)) |
| 57 | + r = np.arange(len(T)) |
| 58 | + |
| 59 | + G[r, 2 * T[:, 0]] += 0.5 * (-(c[:, 1] - a[:, 1]) + (b[:, 1] - a[:, 1])) |
| 60 | + G[r, 2 * T[:, 0] + 1] += 0.5 * (-(b[:, 0] - a[:, 0]) + (c[:, 0] - a[:, 0])) |
| 61 | + G[r, 2 * T[:, 1]] += 0.5 * (c[:, 1] - a[:, 1]) |
| 62 | + G[r, 2 * T[:, 1] + 1] += 0.5 * (-(c[:, 0] - a[:, 0])) |
| 63 | + G[r, 2 * T[:, 2]] += 0.5 * (-(b[:, 1] - a[:, 1])) |
| 64 | + G[r, 2 * T[:, 2] + 1] += 0.5 * (b[:, 0] - a[:, 0]) |
| 65 | + return G |
| 66 | + |
| 67 | + |
| 68 | +def slp(p, T, iters=400, radius=0.02): |
| 69 | + n = len(p) |
| 70 | + best = smallest(p, T) |
| 71 | + for _ in range(iters): |
| 72 | + s = signed(p, T) |
| 73 | + sg = np.sign(s) |
| 74 | + sg[sg == 0] = 1 |
| 75 | + m = np.abs(s).min() |
| 76 | + act = np.abs(s) < m + 4 * radius |
| 77 | + G = grad(p, T)[act] * sg[act, None] |
| 78 | + |
| 79 | + |
| 80 | + A = np.hstack([-G, np.ones((act.sum(), 1))]) |
| 81 | + bnd = np.abs(s[act]) |
| 82 | + x = p.reshape(-1) |
| 83 | + bounds = [(max(-radius, -xi), min(radius, 1 - xi)) for xi in x] + [(None, None)] |
| 84 | + cost = np.zeros(2 * n + 1) |
| 85 | + cost[-1] = -1 |
| 86 | + res = linprog(cost, A_ub=A, b_ub=bnd, bounds=bounds, method="highs") |
| 87 | + if res.status != 0: |
| 88 | + radius /= 2 |
| 89 | + continue |
| 90 | + q = np.clip(p + res.x[:-1].reshape(n, 2), 0, 1) |
| 91 | + v = smallest(q, T) |
| 92 | + if v > best: |
| 93 | + p, best = q, v |
| 94 | + radius = min(radius * 1.5, 0.05) |
| 95 | + else: |
| 96 | + radius /= 2 |
| 97 | + if radius < 1e-13: |
| 98 | + break |
| 99 | + return p, best |
| 100 | + |
| 101 | + |
| 102 | +HOPS = 40 |
| 103 | + |
| 104 | + |
| 105 | +def one(args): |
| 106 | + n, seed = args |
| 107 | + rng = np.random.default_rng(seed) |
| 108 | + T = triples(n) |
| 109 | + p = anneal(rng.random((n, 2)), T, rng, steps=5000) |
| 110 | + p, v = slp(p, T) |
| 111 | + for _ in range(HOPS): |
| 112 | + q = p.copy() |
| 113 | + k = rng.integers(1, 4) |
| 114 | + for i in rng.choice(n, k, replace=False): |
| 115 | + q[i] = np.clip(q[i] + rng.normal(0, 0.08, 2), 0, 1) |
| 116 | + q, w = slp(q, T, iters=150) |
| 117 | + if w > v: |
| 118 | + p, v = q, w |
| 119 | + return v, seed, p |
| 120 | + |
| 121 | + |
| 122 | +if __name__ == "__main__": |
| 123 | + if len(sys.argv) < 2: |
| 124 | + sys.exit(__doc__.strip()) |
| 125 | + n = int(sys.argv[1]) |
| 126 | + starts = int(sys.argv[2]) if len(sys.argv) > 2 else 64 |
| 127 | + seed = int(sys.argv[3]) if len(sys.argv) > 3 else 0 |
| 128 | + workers = int(sys.argv[4]) if len(sys.argv) > 4 else 8 |
| 129 | + with Pool(workers) as pool: |
| 130 | + runs = pool.map(one, [(n, seed + k) for k in range(starts)]) |
| 131 | + runs.sort(key=lambda r: -r[0]) |
| 132 | + for v, s, _ in runs[:5]: |
| 133 | + print(f"# start seed {s}: {v:.10f}", file=sys.stderr) |
| 134 | + v, s, p = runs[0] |
| 135 | + print(f"# n = {n}: smallest triangle about {v:.10f}, best of {starts} starts (seeds {seed}..{seed + starts - 1}), " |
| 136 | + f"found by search.py {n} {starts} {seed}; best seed {s}") |
| 137 | + for x, y in p: |
| 138 | + print(f"{float(x)!r} {float(y)!r}") |