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 | """Refine a 13-point basin and bound its nearby equal-area solution.""" |
| 2 | |
| 3 | import argparse |
| 4 | import json |
| 5 | from decimal import Decimal, localcontext |
| 6 | from fractions import Fraction |
| 7 | from itertools import combinations |
| 8 | from pathlib import Path |
| 9 | |
| 10 | |
| 11 | TRIPLES = tuple(combinations(range(13), 3)) |
| 12 | ACTIVE_TOLERANCE = Decimal("1e-9") |
| 13 | RADIUS = Fraction(1, 10**42) |
| 14 | |
| 15 | |
| 16 | def signed_area(points, triple): |
| 17 | (ax, ay), (bx, by), (cx, cy) = (points[index] for index in triple) |
| 18 | return ((bx - ax) * (cy - ay) - (cx - ax) * (by - ay)) / 2 |
| 19 | |
| 20 | |
| 21 | def derivatives(points, triple): |
| 22 | (ax, ay), (bx, by), (cx, cy) = (points[index] for index in triple) |
| 23 | return ((by - cy, cx - bx), (cy - ay, ax - cx), (ay - by, bx - ax)) |
| 24 | |
| 25 | |
| 26 | def solve(matrix, rhs): |
| 27 | rows = [list(a) + list(b) for a, b in zip(matrix, rhs)] |
| 28 | size = len(rows) |
| 29 | for col in range(size): |
| 30 | pivot = max(range(col, size), key=lambda row: abs(rows[row][col])) |
| 31 | rows[col], rows[pivot] = rows[pivot], rows[col] |
| 32 | if not rows[col][col]: |
| 33 | raise ValueError("singular system of critical triangles") |
| 34 | row = [value / rows[col][col] for value in rows[col]] |
| 35 | rows[col] = row |
| 36 | for i, other in enumerate(rows): |
| 37 | if i != col and other[col]: |
| 38 | rows[i] = [a - other[col] * b for a, b in zip(other, row)] |
| 39 | return [row[size:] for row in rows] |
| 40 | |
| 41 | |
| 42 | def coordinates(template, free, state): |
| 43 | points = [list(point) for point in template] |
| 44 | for (point, axis), value in zip(free, state): |
| 45 | points[point][axis] = value |
| 46 | return points |
| 47 | |
| 48 | |
| 49 | def evaluate(template, free, active, state): |
| 50 | points = coordinates(template, free, state) |
| 51 | by_coordinate = {key: i for i, key in enumerate(free)} |
| 52 | values, jacobian = [], [] |
| 53 | for triple, sign in active: |
| 54 | row = [0] * len(state) |
| 55 | row[-1] = -1 |
| 56 | for index, (dx, dy) in zip(triple, derivatives(points, triple)): |
| 57 | for axis, deriv in enumerate((dx, dy)): |
| 58 | position = by_coordinate.get((index, axis)) |
| 59 | if position is not None: |
| 60 | row[position] = sign * deriv / 2 |
| 61 | values.append(sign * signed_area(points, triple) - state[-1]) |
| 62 | jacobian.append(row) |
| 63 | return values, jacobian |
| 64 | |
| 65 | |
| 66 | def stationarity_margins(points, free, active, norm_inv, contraction, inverse): |
| 67 | # At the certified root the inverse differs from our approximate inverse |
| 68 | # by at most this amount in the induced infinity norm (Neumann series). |
| 69 | inverse_error = norm_inv * contraction / (1 - contraction) |
| 70 | weights = [-value for value in inverse[-1]] |
| 71 | weight_margin = min(weights) - inverse_error |
| 72 | gradient = [[Fraction(0), Fraction(0)] for _ in points] |
| 73 | for (triple, sign), weight in zip(active, weights): |
| 74 | for index, (dx, dy) in zip(triple, derivatives(points, triple)): |
| 75 | gradient[index][0] += weight * sign * dx / 2 |
| 76 | gradient[index][1] += weight * sign * dy / 2 |
| 77 | |
| 78 | # Every area derivative has magnitude at most 1/2 and changes by at most |
| 79 | # RADIUS. The exact positive weights sum to one by the last column of J. |
| 80 | error = inverse_error / 2 + RADIUS |
| 81 | free = set(free) |
| 82 | margins = [] |
| 83 | for index, point in enumerate(points): |
| 84 | for axis, value in enumerate(point): |
| 85 | if (index, axis) not in free: |
| 86 | coefficient = gradient[index][axis] |
| 87 | margins.append((coefficient if value == 1 else -coefficient) - error) |
| 88 | return weight_margin, min(margins, default=Fraction(1)), norm_inv + inverse_error |
| 89 | |
| 90 | |
| 91 | def certify(template, free, active, state, inverse): |
| 92 | """Use exact rational arithmetic for the residual and contraction bounds.""" |
| 93 | template = [[Fraction(value) for value in point] for point in template] |
| 94 | state = list(map(Fraction, state)) |
| 95 | values, jacobian = evaluate(template, free, active, state) |
| 96 | columns = list(zip(*jacobian)) |
| 97 | inverse = [[Fraction(value) for value in row] for row in inverse] |
| 98 | norm_inv = max(sum(map(abs, row)) for row in inverse) |
| 99 | residual = max( |
| 100 | sum(abs(int(i == j) - sum(a * b for a, b in zip(row, column))) |
| 101 | for j, column in enumerate(columns)) |
| 102 | for i, row in enumerate(inverse) |
| 103 | ) |
| 104 | correction = max(abs(sum(a * b for a, b in zip(row, values))) for row in inverse) |
| 105 | contraction = residual + 6 * RADIUS * norm_inv |
| 106 | if contraction >= 1 or correction + contraction * RADIUS > RADIUS: |
| 107 | raise ValueError("could not certify the contraction") |
| 108 | if any(not RADIUS < value < 1 - RADIUS for value in state[:-1]): |
| 109 | raise ValueError("certificate box would extend outside the square") |
| 110 | |
| 111 | points = coordinates(template, free, state) |
| 112 | critical = {triple for triple, _ in active} |
| 113 | inactive_min = min(abs(signed_area(points, triple)) for triple in TRIPLES if triple not in critical) |
| 114 | if inactive_min - 6 * RADIUS <= state[-1] + RADIUS or state[-1] <= RADIUS: |
| 115 | raise ValueError("could not separate inactive triangles from the common minimum") |
| 116 | weight_margin, boundary_margin, inverse_bound = stationarity_margins( |
| 117 | points, free, active, norm_inv, contraction, inverse |
| 118 | ) |
| 119 | local_radius = Fraction(0) |
| 120 | if weight_margin > 0 and boundary_margin > 0: |
| 121 | direction_bound = max(1 / boundary_margin, |
| 122 | inverse_bound * (1 / weight_margin + 1 / (2 * boundary_margin))) |
| 123 | local_radius = min(1 / (6 * direction_bound), (state[-1] - RADIUS) / 6) |
| 124 | return { |
| 125 | "free_coordinates": len(free), |
| 126 | "active_triangles": len(active), |
| 127 | "radius": "1e-42", |
| 128 | "contraction_factor_bound_approx": str(float(contraction)), |
| 129 | "center_correction_approx": str(float(correction)), |
| 130 | "minimum_active_weight_bound_approx": str(float(weight_margin)), |
| 131 | "minimum_boundary_sign_margin_approx": str(float(boundary_margin)), |
| 132 | "strict_local_maximum": local_radius > 0, |
| 133 | "local_maximum_radius_approx": str(float(local_radius)), |
| 134 | "area_center": str(Decimal(state[-1].numerator) / Decimal(state[-1].denominator)), |
| 135 | "area_interval": [str(state[-1] - RADIUS), str(state[-1] + RADIUS)], |
| 136 | "exact_minimum_of_decimal_export": str(min(abs(signed_area(points, triangle)) for triangle in TRIPLES)), |
| 137 | } |
| 138 | |
| 139 | |
| 140 | def main(): |
| 141 | parser = argparse.ArgumentParser(description=__doc__) |
| 142 | parser.add_argument("input", type=Path) |
| 143 | parser.add_argument("output", type=Path) |
| 144 | args = parser.parse_args() |
| 145 | with localcontext() as ctx: |
| 146 | ctx.prec = 90 |
| 147 | template = [list(map(Decimal, line.split())) for line in args.input.read_text().splitlines() |
| 148 | if line.strip() and not line.lstrip().startswith("#")] |
| 149 | if len(template) != 13: |
| 150 | raise ValueError("expected 13 points") |
| 151 | free = [] |
| 152 | for i, point in enumerate(template): |
| 153 | for axis, value in enumerate(point): |
| 154 | if abs(value) < ACTIVE_TOLERANCE: |
| 155 | point[axis] = Decimal(0) |
| 156 | elif abs(value - 1) < ACTIVE_TOLERANCE: |
| 157 | point[axis] = Decimal(1) |
| 158 | else: |
| 159 | free.append((i, axis)) |
| 160 | areas = [signed_area(template, triple) for triple in TRIPLES] |
| 161 | lowest = min(map(abs, areas)) |
| 162 | active = [(triple, 1 if area > 0 else -1) for triple, area in zip(TRIPLES, areas) |
| 163 | if abs(area) <= lowest + ACTIVE_TOLERANCE] |
| 164 | state = [template[i][axis] for i, axis in free] + [lowest] |
| 165 | if len(active) != len(state): |
| 166 | raise ValueError(f"{len(active)} active triangles for {len(state)} unknowns") |
| 167 | for _ in range(7): |
| 168 | values, jacobian = evaluate(template, free, active, state) |
| 169 | step = solve(jacobian, [[value] for value in values]) |
| 170 | state = [value - delta[0] for value, delta in zip(state, step)] |
| 171 | state = [Decimal(f"{value:.60f}") for value in state] |
| 172 | _, jacobian = evaluate(template, free, active, state) |
| 173 | identity = [[Decimal(i == j) for j in range(len(state))] for i in range(len(state))] |
| 174 | approximate_inverse = [[f"{value:.45f}" for value in row] for row in solve(jacobian, identity)] |
| 175 | report = certify(template, free, active, state, approximate_inverse) |
| 176 | points = coordinates(template, free, state) |
| 177 | args.output.write_text("".join(f"{x} {y}\n" for x, y in points)) |
| 178 | print(json.dumps(report, indent=2)) |
| 179 | |
| 180 | |
| 181 | if __name__ == "__main__": |
| 182 | main() |