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 | """Verify the saved n=15 result and proof inputs from Shoal PR #17.""" |
| 2 | |
| 3 | import json |
| 4 | import subprocess |
| 5 | import sys |
| 6 | from fractions import Fraction |
| 7 | from itertools import combinations, pairwise |
| 8 | from math import sqrt |
| 9 | from pathlib import Path |
| 10 | |
| 11 | PR = "1fd9685b8c11cdb2ca4a85400023fc04efb3eb57" |
| 12 | JOB = "4f09303ea7887ed1a473f6fc12b2abdc56e21836" |
| 13 | SOURCE = "routes/hongdy22-codex-n15-search" |
| 14 | TRIPLES = tuple(combinations(range(15), 3)) |
| 15 | |
| 16 | |
| 17 | def source(name, revision=PR): |
| 18 | return subprocess.check_output(["git", "show", f"{revision}:{SOURCE}/{name}"], text=True) |
| 19 | |
| 20 | |
| 21 | def points(text): |
| 22 | result = [tuple(map(Fraction, line.split())) for line in text.splitlines() |
| 23 | if line.strip() and not line.lstrip().startswith("#")] |
| 24 | assert len(result) == len(set(result)) == 15 |
| 25 | assert all(len(point) == 2 and all(0 <= c <= 1 for c in point) for point in result) |
| 26 | return result |
| 27 | |
| 28 | |
| 29 | def signed_areas(placement): |
| 30 | areas = {} |
| 31 | for triple in TRIPLES: |
| 32 | i, j, k = triple |
| 33 | a, b, c = placement[i], placement[j], placement[k] |
| 34 | areas[triple] = sum(x * v - u * y for (x, y), (u, v) in pairwise((a, b, c, a))) / 2 |
| 35 | return areas |
| 36 | |
| 37 | |
| 38 | def gradient(placement, triple, orientation): |
| 39 | """Signed area is affine in each single coordinate.""" |
| 40 | row = [Fraction(0)] * 30 |
| 41 | i, j, k = triple |
| 42 | for index, after, before in ((i, j, k), (j, k, i), (k, i, j)): |
| 43 | next_x, next_y = placement[after] |
| 44 | prev_x, prev_y = placement[before] |
| 45 | row[2 * index] = orientation * (next_y - prev_y) / 2 |
| 46 | row[2 * index + 1] = orientation * (prev_x - next_x) / 2 |
| 47 | return row |
| 48 | |
| 49 | |
| 50 | def inverse(matrix): |
| 51 | size = len(matrix) |
| 52 | rows = [row + [Fraction(i == j) for j in range(size)] for i, row in enumerate(matrix)] |
| 53 | for column in range(size): |
| 54 | pivot = next(i for i in range(column, size) if rows[i][column]) |
| 55 | rows[column], rows[pivot] = rows[pivot], rows[column] |
| 56 | scale = rows[column][column] |
| 57 | unit_row = [value / scale for value in rows[column]] |
| 58 | rows[column] = unit_row |
| 59 | for i, row in enumerate(rows): |
| 60 | if i != column and row[column]: |
| 61 | factor = row[column] |
| 62 | rows[i] = [value - factor * unit for value, unit in zip(row, unit_row)] |
| 63 | return [row[size:] for row in rows] |
| 64 | |
| 65 | |
| 66 | def dot(left, right): |
| 67 | return sum(a * b for a, b in zip(left, right)) |
| 68 | |
| 69 | |
| 70 | def local_certificate(raw, polished, area): |
| 71 | raw_areas = signed_areas(raw) |
| 72 | minimum = min(map(abs, raw_areas.values())) |
| 73 | tight = [t for t, value in raw_areas.items() if abs(value) <= minimum + Fraction("1e-9")] |
| 74 | center = [c for point in polished for c in point] |
| 75 | free = [i for i, c in enumerate(center) if 0 < c < 1] |
| 76 | fixed = [i for i, c in enumerate(center) if c in (0, 1)] |
| 77 | radius = Fraction("1e-42") |
| 78 | assert len(tight) == len(free) + 1 == 25 |
| 79 | assert all(radius < center[i] < 1 - radius for i in free) |
| 80 | |
| 81 | areas = signed_areas(polished) |
| 82 | signs = {t: 1 if raw_areas[t] > 0 else -1 for t in tight} |
| 83 | gradients = [gradient(polished, t, signs[t]) for t in tight] |
| 84 | jacobian = [[row[i] for i in free] + [Fraction(-1)] for row in gradients] |
| 85 | approximate = inverse(jacobian) |
| 86 | columns = tuple(zip(*jacobian)) |
| 87 | assert all(dot(row, column) == (i == j) |
| 88 | for i, row in enumerate(approximate) for j, column in enumerate(columns)) |
| 89 | norm = max(sum(map(abs, row)) for row in approximate) |
| 90 | contraction = 6 * radius * norm |
| 91 | residual = [signs[t] * areas[t] - area for t in tight] |
| 92 | correction = max(abs(dot(row, residual)) for row in approximate) |
| 93 | assert contraction < 1 and correction + contraction * radius <= radius |
| 94 | gap = min(abs(value) for t, value in areas.items() if t not in signs) - area |
| 95 | assert gap > 7 * radius and area > radius |
| 96 | |
| 97 | inverse_error = norm * contraction / (1 - contraction) |
| 98 | weights = [-value for value in approximate[-1]] |
| 99 | weight_margin = min(weights) - inverse_error |
| 100 | weighted_gradient = [dot(weights, derivatives) for derivatives in zip(*gradients)] |
| 101 | boundary_margin = min(weighted_gradient[i] * (2 * center[i] - 1) for i in fixed) |
| 102 | boundary_margin -= inverse_error / 2 + radius |
| 103 | assert weight_margin > 0 and boundary_margin > 0 |
| 104 | direction_bound = max(1 / boundary_margin, (norm + inverse_error) |
| 105 | * (1 / weight_margin + 1 / (2 * boundary_margin))) |
| 106 | local_radius = min(1 / (6 * direction_bound), (area - radius) / 6) |
| 107 | assert local_radius > Fraction("3.8e-6") |
| 108 | print(f"Exact inverse norm {float(norm):.12g}; q {float(contraction):.12g}; " |
| 109 | f"center correction {float(correction):.12g}; inactive gap {float(gap):.12g}") |
| 110 | print(f"Weight margin {float(weight_margin):.12g}; boundary margin " |
| 111 | f"{float(boundary_margin):.12g}; strict-local radius {float(local_radius):.12g}") |
| 112 | |
| 113 | |
| 114 | def deletion_check(placement): |
| 115 | areas = signed_areas(placement) |
| 116 | target = Fraction(31, 1485) |
| 117 | assert min(map(abs, areas.values())) == target |
| 118 | assert sum(abs(area) == target for area in areas.values()) == 54 |
| 119 | # Convex witnesses from the PR, with vertex numbers converted to zero-based indices. |
| 120 | witnesses = [ |
| 121 | [((1, 7, 15), 8), ((2, 7, 11), 31), ((5, 9, 13), 155), ((11, 13, 15), 31)], |
| 122 | [((1, 3, 10), 2), ((4, 8, 10), 31)], |
| 123 | ] |
| 124 | for axis, witness in enumerate(witnesses): |
| 125 | rows = [] |
| 126 | for vertices, weight in witness: |
| 127 | triple = tuple(i - 1 for i in vertices) |
| 128 | assert abs(areas[triple]) == target |
| 129 | row = gradient(placement, triple, 1 if areas[triple] > 0 else -1) |
| 130 | rows.append([weight * coefficient for coefficient in row[axis::2]]) |
| 131 | upper_bound = sum(max(0, sum(column)) for column in zip(*rows)) |
| 132 | assert upper_bound / sum(weight for _, weight in witness) == target |
| 133 | print("Deletion: 54 of 455 triangles attain 31/1485; both axis bounds are 31/1485.") |
| 134 | |
| 135 | |
| 136 | def minimum_moves(placement, target): |
| 137 | areas = {t: abs(a) for t, a in signed_areas(placement).items()} |
| 138 | for kept in range(15, 0, -1): |
| 139 | count = sum(all(areas[t] >= target for t in combinations(subset, 3)) |
| 140 | for subset in combinations(range(15), kept)) |
| 141 | if count: |
| 142 | return 15 - kept, count |
| 143 | |
| 144 | |
| 145 | def main(polished_path): |
| 146 | raw = points(source("n15.txt", JOB)) |
| 147 | independent = next(track for track in json.loads(source("search-summary.json"))["tracks"] |
| 148 | if track["track"] == "independent") |
| 149 | assert raw == points("\n".join(independent["coordinates"])) |
| 150 | reference = points(source("reference15.dat")) |
| 151 | rotated = [(y, 1 - x) for x, y in raw] |
| 152 | nearest = [min(((x - u)**2 + (y - v)**2, i) for i, (u, v) in enumerate(reference)) |
| 153 | for x, y in rotated] |
| 154 | assert len({index for _, index in nearest}) == 15 |
| 155 | maximum = max(distance for distance, _ in nearest) |
| 156 | assert maximum < Fraction("1.7e-14")**2 |
| 157 | print(f"Raw job 101 agrees with its summary; rotation residual {sqrt(float(maximum)):.12g}.") |
| 158 | |
| 159 | polished = points(polished_path.read_text()) |
| 160 | assert polished == points(source("n15.txt")) |
| 161 | area = Fraction(json.loads(source("polish-report.json"))["independent"]["area_center"]) |
| 162 | local_certificate(raw, polished, area) |
| 163 | deletion = points(source("deletion-certificate.dat")) |
| 164 | deletion_check(deletion) |
| 165 | for placement, target, expected in ((reference, "0.0212105411", (6, 7)), |
| 166 | (deletion, "0.0208754209", (7, 2))): |
| 167 | moves, count = minimum_moves(placement, Fraction(target)) |
| 168 | assert (moves, count) == expected |
| 169 | print(f"Target {target}: move at least {moves} points; {count} minimum covers.") |
| 170 | |
| 171 | |
| 172 | if __name__ == "__main__": |
| 173 | main(Path(sys.argv[1]) if len(sys.argv) > 1 else Path(__file__).with_name("n15.txt")) |