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

| 1 | #!/usr/bin/env python3 |
| 2 | """My own search inside the C4-symmetric family of 12-point configurations. |
| 3 | |
| 4 | Usage: c4search.py [SEED] [STARTS] > n12.txt |
| 5 | |
| 6 | A 12-point set invariant under the quarter turn R(x, y) = (1 - y, x) about the |
| 7 | centre is three orbits of four points each (12 = 4*3), so the whole family has |
| 8 | six free numbers: the three orbit representatives. This script optimizes those |
| 9 | six by the same softmin continuation as search.py (chain rule through the orbit |
| 10 | maps), from uniform random starts. No literature coordinates are used. |
| 11 | |
| 12 | The idea to search a rotation-restricted family is credited to the issue trail |
| 13 | (cogito-claude's symmetric routes on n = 16; the symmetric constructions in the |
| 14 | record table). The code, the parameterization and the runs are my own. |
| 15 | """ |
| 16 | |
| 17 | import itertools |
| 18 | import math |
| 19 | import random |
| 20 | import sys |
| 21 | |
| 22 | |
| 23 | def triples_of(n): |
| 24 | return list(itertools.combinations(range(n), 3)) |
| 25 | |
| 26 | |
| 27 | def expand(reps): |
| 28 | """The 12 points: each representative and its quarter-turn images.""" |
| 29 | p = [] |
| 30 | for rx, ry in reps: |
| 31 | p.append((rx, ry)) |
| 32 | p.append((1.0 - ry, rx)) |
| 33 | p.append((1.0 - rx, 1.0 - ry)) |
| 34 | p.append((ry, 1.0 - rx)) |
| 35 | return p |
| 36 | |
| 37 | |
| 38 | # d orbit point j / d (rx, ry), as linear maps: (dx/drx, dx/dry), (dy/drx, dy/dry) |
| 39 | ORBIT_JAC = ( |
| 40 | ((1.0, 0.0), (0.0, 1.0)), # (rx, ry) |
| 41 | ((0.0, -1.0), (1.0, 0.0)), # (1-ry, rx) |
| 42 | ((-1.0, 0.0), (0.0, -1.0)), # (1-rx, 1-ry) |
| 43 | ((0.0, 1.0), (-1.0, 0.0)), # (ry, 1-rx) |
| 44 | ) |
| 45 | # which representative and orbit slot each of the 12 points came from |
| 46 | SLOT = [(r, s) for r in range(3) for s in range(4)] |
| 47 | |
| 48 | |
| 49 | def softmin_step(reps, tri, beta, step): |
| 50 | """One gradient ascent step on the softmin over the C4 family; returns (S, true min A2).""" |
| 51 | p = expand(reps) |
| 52 | xs = [q[0] for q in p] |
| 53 | ys = [q[1] for q in p] |
| 54 | n = len(p) |
| 55 | gx = [0.0] * n |
| 56 | gy = [0.0] * n |
| 57 | vals = [] |
| 58 | for i, j, k in tri: |
| 59 | ax, ay, bx, by, cx, cy = xs[i], ys[i], xs[j], ys[j], xs[k], ys[k] |
| 60 | ux = bx - ax |
| 61 | uy = by - ay |
| 62 | vx = cx - ax |
| 63 | vy = cy - ay |
| 64 | cr = ux * vy - uy * vx |
| 65 | a = cr if cr >= 0.0 else -cr |
| 66 | vals.append([a, cr, i, j, k, ux, uy, vx, vy, 0.0]) |
| 67 | m = min(v[0] for v in vals) |
| 68 | z = 0.0 |
| 69 | for v in vals: |
| 70 | e = math.exp(-beta * (v[0] - m)) if beta * (v[0] - m) < 700.0 else 0.0 |
| 71 | v[9] = e |
| 72 | z += e |
| 73 | s = m - math.log(z) / beta if z > 0.0 else m |
| 74 | inv = 1.0 / z if z > 0.0 else 0.0 |
| 75 | for v in vals: |
| 76 | a, cr, i, j, k, ux, uy, vx, vy, e = v |
| 77 | if e == 0.0: |
| 78 | continue |
| 79 | w = e * inv |
| 80 | g = w if cr >= 0.0 else -w |
| 81 | gx[i] += g * (uy - vy) |
| 82 | gy[i] += g * (vx - ux) |
| 83 | gx[j] += g * vy |
| 84 | gy[j] += g * -vx |
| 85 | gx[k] += g * -uy |
| 86 | gy[k] += g * ux |
| 87 | # pull the point gradients back through the orbit maps |
| 88 | rx = [0.0] * 3 |
| 89 | ry = [0.0] * 3 |
| 90 | for t in range(n): |
| 91 | r, slot = SLOT[t] |
| 92 | j11, j12 = ORBIT_JAC[slot][0] |
| 93 | j21, j22 = ORBIT_JAC[slot][1] |
| 94 | rx[r] += j11 * gx[t] + j21 * gy[t] |
| 95 | ry[r] += j12 * gx[t] + j22 * gy[t] |
| 96 | for r in range(3): |
| 97 | nx = reps[r][0] + step * rx[r] |
| 98 | ny = reps[r][1] + step * ry[r] |
| 99 | if nx < 0.0: |
| 100 | nx = 0.0 |
| 101 | elif nx > 1.0: |
| 102 | nx = 1.0 |
| 103 | if ny < 0.0: |
| 104 | ny = 0.0 |
| 105 | elif ny > 1.0: |
| 106 | ny = 1.0 |
| 107 | reps[r] = (nx, ny) |
| 108 | return s, m / 2.0 |
| 109 | |
| 110 | |
| 111 | def smallest(p): |
| 112 | best = None |
| 113 | xs = [q[0] for q in p] |
| 114 | ys = [q[1] for q in p] |
| 115 | for i, j, k in TRI: |
| 116 | ax, ay, bx, by, cx, cy = xs[i], ys[i], xs[j], ys[j], xs[k], ys[k] |
| 117 | cr = (bx - ax) * (cy - ay) - (cx - ax) * (by - ay) |
| 118 | if cr < 0.0: |
| 119 | cr = -cr |
| 120 | if best is None or cr < best: |
| 121 | best = cr |
| 122 | return best / 2.0 |
| 123 | |
| 124 | |
| 125 | def main(): |
| 126 | global TRI |
| 127 | args = [int(a) for a in sys.argv[1:4]] |
| 128 | seed = args[0] if len(args) > 0 else 2026 |
| 129 | starts = args[1] if len(args) > 1 else 300 |
| 130 | rng = random.Random(seed) |
| 131 | TRI = triples_of(12) |
| 132 | |
| 133 | stages = [(100.0, 0.05, 60), (400.0, 0.03, 60), (1500.0, 0.015, 60), |
| 134 | (6000.0, 0.006, 50), (30000.0, 0.002, 50), (150000.0, 0.0006, 40), |
| 135 | (800000.0, 0.0002, 40)] |
| 136 | deep = [(2000000.0, 0.0001, 300), (10000000.0, 0.00003, 300), |
| 137 | (50000000.0, 0.00001, 300), (200000000.0, 0.000003, 200)] |
| 138 | pool = [] |
| 139 | for t in range(starts): |
| 140 | reps = [] |
| 141 | while len(reps) < 3: |
| 142 | rx, ry = rng.random(), rng.random() |
| 143 | if abs(rx - ry) > 0.02: # x = y collapses the orbit |
| 144 | reps.append((rx, ry)) |
| 145 | p = expand(reps) |
| 146 | if len(set(p)) < 12: |
| 147 | continue |
| 148 | best = smallest(p) |
| 149 | for beta, step, iters in stages: |
| 150 | for _ in range(iters): |
| 151 | s, m = softmin_step(reps, TRI, beta, step) |
| 152 | if m > best: |
| 153 | best = m |
| 154 | # the optimum of this family sits on the boundary: snap near-edge |
| 155 | # coordinates exactly onto it |
| 156 | reps = [(0.0 if rx < 0.002 else 1.0 if rx > 0.998 else rx, |
| 157 | 0.0 if ry < 0.002 else 1.0 if ry > 0.998 else ry) |
| 158 | for rx, ry in reps] |
| 159 | best = max(best, smallest(expand(reps))) |
| 160 | pool.append((best, reps)) |
| 161 | pool.sort(reverse=True) |
| 162 | pool = pool[:24] |
| 163 | # deep polish and the tie polish only for the leading starts |
| 164 | best_all, best_reps = None, None |
| 165 | for best0, reps0 in pool: |
| 166 | reps = reps0[:] |
| 167 | best = max(best0, smallest(expand(reps))) |
| 168 | for beta, step, iters in deep: |
| 169 | for _ in range(iters): |
| 170 | s, m = softmin_step(reps, TRI, beta, step) |
| 171 | if m > best: |
| 172 | best = m |
| 173 | # final tie polish in rep space: ascend the mean gradient of the |
| 174 | # triangles tied at the minimum (the hard-min subgradient there), |
| 175 | # with the step decaying so the iterate settles instead of circling |
| 176 | for it in range(14000): |
| 177 | stp = 2e-6 * 0.9995 ** it |
| 178 | p = expand(reps) |
| 179 | xs = [q[0] for q in p] |
| 180 | ys = [q[1] for q in p] |
| 181 | vals = [] |
| 182 | for i, j, k in TRI: |
| 183 | ax, ay, bx, by, cx, cy = xs[i], ys[i], xs[j], ys[j], xs[k], ys[k] |
| 184 | cr = (bx - ax) * (cy - ay) - (cx - ax) * (by - ay) |
| 185 | vals.append(cr if cr >= 0.0 else -cr) |
| 186 | m2 = min(vals) |
| 187 | grx = [0.0] * 3 |
| 188 | gry = [0.0] * 3 |
| 189 | cnt = 0 |
| 190 | for idx, (i, j, k) in enumerate(TRI): |
| 191 | if vals[idx] - m2 > 1e-11: |
| 192 | continue |
| 193 | cnt += 1 |
| 194 | ax, ay, bx, by, cx, cy = xs[i], ys[i], xs[j], ys[j], xs[k], ys[k] |
| 195 | cr = (bx - ax) * (cy - ay) - (cx - ax) * (by - ay) |
| 196 | g = 1.0 if cr >= 0.0 else -1.0 |
| 197 | ux, uy, vx, vy = bx - ax, by - ay, cx - ax, cy - ay |
| 198 | gx = [g * (uy - vy), g * vy, g * -uy] |
| 199 | gy = [g * (vx - ux), g * -vx, g * ux] |
| 200 | for t, pt in enumerate((i, j, k)): |
| 201 | r, slot = SLOT[pt] |
| 202 | j11, j12 = ORBIT_JAC[slot][0] |
| 203 | j21, j22 = ORBIT_JAC[slot][1] |
| 204 | grx[r] += j11 * gx[t] + j21 * gy[t] |
| 205 | gry[r] += j12 * gx[t] + j22 * gy[t] |
| 206 | w = 1.0 / cnt if cnt else 0.0 |
| 207 | reps = [(min(1.0, max(0.0, q[0] + stp * w * grx[r])), |
| 208 | min(1.0, max(0.0, q[1] + stp * w * gry[r]))) |
| 209 | for r, q in enumerate(reps)] |
| 210 | nm = smallest(expand(reps)) |
| 211 | if nm > best: |
| 212 | best = nm |
| 213 | if best_all is None or best > best_all: |
| 214 | best_all, best_reps = best, reps[:] |
| 215 | best = best_all |
| 216 | |
| 217 | p = expand(best_reps) |
| 218 | assert len(set(p)) == 12, "orbits collapsed" |
| 219 | print(f"# n = 12: smallest triangle about {best_all:.10f} " |
| 220 | f"(c4search.py {seed} {starts}: own search in the quarter-turn-symmetric family)", |
| 221 | file=sys.stderr) |
| 222 | for x, y in p: |
| 223 | print(f"{x:.17f} {y:.17f}") |
| 224 | |
| 225 | |
| 226 | if __name__ == "__main__": |
| 227 | main() |