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 a rational n = 15 construction and two coordinate-wise upper bounds.""" |
| 2 | |
| 3 | from fractions import Fraction |
| 4 | from itertools import combinations |
| 5 | from pathlib import Path |
| 6 | |
| 7 | |
| 8 | TARGET = Fraction(31, 1485) |
| 9 | WEIGHTS = ( |
| 10 | { |
| 11 | (1, 7, 15): Fraction(8, 225), |
| 12 | (2, 7, 11): Fraction(31, 225), |
| 13 | (5, 9, 13): Fraction(31, 45), |
| 14 | (11, 13, 15): Fraction(31, 225), |
| 15 | }, |
| 16 | { |
| 17 | (1, 3, 10): Fraction(2, 33), |
| 18 | (4, 8, 10): Fraction(31, 33), |
| 19 | }, |
| 20 | ) |
| 21 | |
| 22 | |
| 23 | def area(triangle): |
| 24 | (ax, ay), (bx, by), (cx, cy) = triangle |
| 25 | return abs((bx - ax) * (cy - ay) - (cx - ax) * (by - ay)) / 2 |
| 26 | |
| 27 | |
| 28 | def axis_bound(points, axis, weights): |
| 29 | """Bound the minimum for a fixed other axis and fixed triangle orientations.""" |
| 30 | assert sum(weights.values()) == 1 and min(weights.values()) > 0 |
| 31 | coefficients = [Fraction(0)] * len(points) |
| 32 | for triple, weight in weights.items(): |
| 33 | positions = [points[i - 1][axis] for i in triple] |
| 34 | fixed = [points[i - 1][1 - axis] for i in triple] |
| 35 | a, b, c = fixed |
| 36 | terms = ((b - c) / 2, (c - a) / 2, (a - b) / 2) |
| 37 | value = sum(t * x for t, x in zip(terms, positions)) |
| 38 | assert abs(value) == TARGET |
| 39 | sign = 1 if value > 0 else -1 |
| 40 | for index, term in zip(triple, terms): |
| 41 | coefficients[index - 1] += weight * sign * term |
| 42 | return sum(max(0, coefficient) for coefficient in coefficients) |
| 43 | |
| 44 | |
| 45 | def main(): |
| 46 | path = Path(__file__).with_name("deletion-certificate.dat") |
| 47 | points = [tuple(map(Fraction, line.split())) for line in path.read_text().splitlines()] |
| 48 | assert len(points) == len(set(points)) == 15 |
| 49 | assert all(0 <= value <= 1 for p in points for value in p) |
| 50 | areas = list(map(area, combinations(points, 3))) |
| 51 | assert min(areas) == TARGET |
| 52 | print(f"Exact minimum {TARGET} attained by {areas.count(TARGET)} of {len(areas)} triangles.") |
| 53 | for axis, label in enumerate("xy"): |
| 54 | bound = axis_bound(points, axis, WEIGHTS[axis]) |
| 55 | assert bound == TARGET |
| 56 | print(f"Vary {label} with the other axis and orientations fixed: upper bound {bound}.") |
| 57 | |
| 58 | |
| 59 | if __name__ == "__main__": |
| 60 | main() |