Heilbronn's triangle problem
Better configurations for Heilbronn’s triangle problem in the unit square, n = 11–14

| 1 | #!/usr/bin/env python3 |
| 2 | """Search for n points in the unit square with a large smallest triangle. |
| 3 | |
| 4 | Random restarts; each runs a hill climb that moves one point at a time and |
| 5 | keeps a move when the smallest area does not drop (ties broken by the second |
| 6 | smallest), with a step that shrinks when moves stop being accepted. |
| 7 | Usage: search.py N SEED RESTARTS ITERS OUT""" |
| 8 | import itertools |
| 9 | import random |
| 10 | import sys |
| 11 | |
| 12 | |
| 13 | def areas(p, tri): |
| 14 | return sorted(abs((p[b][0] - p[a][0]) * (p[c][1] - p[a][1]) - (p[c][0] - p[a][0]) * (p[b][1] - p[a][1])) / 2 |
| 15 | for a, b, c in tri) |
| 16 | |
| 17 | |
| 18 | def climb(p, tri, rng, iters): |
| 19 | s = areas(p, tri)[:2] |
| 20 | step = 0.1 |
| 21 | fails = 0 |
| 22 | for _ in range(iters): |
| 23 | i = rng.randrange(len(p)) |
| 24 | old = p[i] |
| 25 | p[i] = (min(1.0, max(0.0, old[0] + rng.gauss(0, step))), min(1.0, max(0.0, old[1] + rng.gauss(0, step)))) |
| 26 | t = areas(p, tri)[:2] |
| 27 | if t >= s: |
| 28 | s, fails = t, 0 |
| 29 | else: |
| 30 | p[i] = old |
| 31 | fails += 1 |
| 32 | if fails > 60: |
| 33 | step, fails = max(step * 0.7, 1e-7), 0 |
| 34 | return s[0] |
| 35 | |
| 36 | |
| 37 | def main(): |
| 38 | n, seed, restarts, iters, out = int(sys.argv[1]), int(sys.argv[2]), int(sys.argv[3]), int(sys.argv[4]), sys.argv[5] |
| 39 | rng = random.Random(seed) |
| 40 | tri = list(itertools.combinations(range(n), 3)) |
| 41 | best, best_p = -1, None |
| 42 | for r in range(restarts): |
| 43 | p = [(rng.random(), rng.random()) for _ in range(n)] |
| 44 | v = climb(p, tri, rng, iters) |
| 45 | if v > best: |
| 46 | best, best_p = v, list(p) |
| 47 | print(f"restart {r}: {v:.10f}", flush=True) |
| 48 | with open(out, "w") as f: |
| 49 | f.write(f"# n = {n}, search.py seed {seed}, {restarts} restarts x {iters} iterations\n") |
| 50 | f.writelines(f"{x!r} {y!r}\n" for x, y in best_p) |
| 51 | print(f"best {best:.10f}") |
| 52 | |
| 53 | |
| 54 | main() |