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 | """Reinsert several points by clipping away the forbidden strips.""" |
| 2 | |
| 3 | from itertools import combinations |
| 4 | |
| 5 | import numpy as np |
| 6 | |
| 7 | from search import N, TRIPLES, neighbor, signed_area |
| 8 | |
| 9 | |
| 10 | SQUARE = ((0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0)) |
| 11 | TRIPLE_INDICES = np.array(TRIPLES) |
| 12 | CANDIDATE_MASKS = [ |
| 13 | np.array([sum(1 << node for node in members) for members in combinations(range(N), size)]) |
| 14 | for size in range(3, N + 1) |
| 15 | ] |
| 16 | |
| 17 | |
| 18 | def minimum_cover(triples, rng): |
| 19 | """Choose a smallest set of at least three points meeting every triple.""" |
| 20 | edges = np.bitwise_or.reduce(1 << triples, axis=1) |
| 21 | for masks in CANDIDATE_MASKS: |
| 22 | valid = masks[np.all((masks[:, None] & edges) != 0, axis=1)] |
| 23 | if len(valid): |
| 24 | mask = int(rng.choice(valid)) |
| 25 | return rng.permutation([i for i in range(N) if mask & (1 << i)]) |
| 26 | raise ValueError("no cover of the tight triangles") |
| 27 | |
| 28 | |
| 29 | def pair_lines(points): |
| 30 | lines = [] |
| 31 | for (ax, ay), (bx, by) in combinations(points, 2): |
| 32 | nx, ny = (ay - by) / 2, (bx - ax) / 2 |
| 33 | lines.append((nx, ny, nx * ax + ny * ay)) |
| 34 | return sorted(lines, key=lambda line: line[0] ** 2 + line[1] ** 2) |
| 35 | |
| 36 | |
| 37 | def clip_half_plane(vertices, distances): |
| 38 | """Clip a convex polygon using signed distances to the clipping line.""" |
| 39 | output = [] |
| 40 | previous = vertices[-1] |
| 41 | old_distance = distances[-1] |
| 42 | for point, distance in zip(vertices, distances): |
| 43 | if (old_distance >= 0) != (distance >= 0): |
| 44 | scale = old_distance / (old_distance - distance) |
| 45 | output.append(tuple(a + scale * (b - a) for a, b in zip(previous, point))) |
| 46 | if distance >= 0: |
| 47 | output.append(point) |
| 48 | previous, old_distance = point, distance |
| 49 | return output |
| 50 | |
| 51 | |
| 52 | def feasible_cells(lines, target): |
| 53 | """Return cells of the square where every fixed-pair area is at least target.""" |
| 54 | cells = [SQUARE] |
| 55 | for nx, ny, offset in lines: |
| 56 | kept = [] |
| 57 | for vertices in cells: |
| 58 | values = [nx * x + ny * y - offset for x, y in vertices] |
| 59 | high, low = max(values), min(values) |
| 60 | if low >= target or high <= -target: |
| 61 | kept.append(vertices) |
| 62 | continue |
| 63 | if high > target: |
| 64 | kept.append(clip_half_plane(vertices, [value - target for value in values])) |
| 65 | if low < -target: |
| 66 | kept.append(clip_half_plane(vertices, [-value - target for value in values])) |
| 67 | if not kept: |
| 68 | return [] |
| 69 | cells = kept |
| 70 | return cells |
| 71 | |
| 72 | |
| 73 | def insertion(fixed, previous, quality, rng, ratios): |
| 74 | lines = pair_lines(fixed.tolist()) |
| 75 | nx, ny, offsets = np.array(lines).T |
| 76 | for ratio in ratios: |
| 77 | target = max(0.003, ratio * quality) |
| 78 | cells = feasible_cells(lines, target) |
| 79 | centers = np.array([np.mean(cell, axis=0) for cell in cells]) |
| 80 | if not len(centers): |
| 81 | continue |
| 82 | distances = np.linalg.norm(centers[:, None, :] - previous, axis=2).min(axis=1) |
| 83 | far_enough = distances > 0.035 |
| 84 | if far_enough.any(): |
| 85 | centers = centers[far_enough] |
| 86 | elif ratio != ratios[-1]: |
| 87 | continue |
| 88 | areas = np.abs(centers[:, 0, None] * nx + centers[:, 1, None] * ny - offsets) |
| 89 | best = np.argsort(areas.min(axis=1))[-5:] |
| 90 | return centers[rng.choice(best)] |
| 91 | return None |
| 92 | |
| 93 | |
| 94 | def strip_neighbor(points, rng, step): |
| 95 | # Interleave the cheap single-point and Gaussian moves as a control. |
| 96 | if step % 3 == 0 and step % 13 != 0: |
| 97 | return neighbor(points, rng, step) |
| 98 | |
| 99 | areas = np.abs(signed_area(points)) |
| 100 | quality = areas.min() |
| 101 | tight = TRIPLE_INDICES[areas <= quality + 0.0008] |
| 102 | weights = 1 + np.bincount(tight.ravel(), minlength=N) |
| 103 | probabilities = weights / weights.sum() if step % 2 else None |
| 104 | cover_move = step % 13 == 0 |
| 105 | if cover_move: |
| 106 | removed = minimum_cover(TRIPLE_INDICES[areas <= quality + 1e-8], rng) |
| 107 | else: |
| 108 | count = 3 if step % 5 == 0 else 2 |
| 109 | removed = rng.choice(N, size=count, replace=False, p=probabilities) |
| 110 | fixed = np.delete(points, removed, axis=0) |
| 111 | new = points.copy() |
| 112 | ratios = (0.93, 0.78, 0.62, 0.48, 0.33) if cover_move else (0.93, 0.78, 0.62) |
| 113 | for index in removed: |
| 114 | candidate = insertion(fixed, points[removed], quality, rng, ratios) |
| 115 | if candidate is None: |
| 116 | return neighbor(points, rng, step) |
| 117 | new[index] = candidate |
| 118 | fixed = np.vstack((fixed, candidate)) |
| 119 | return np.clip(new, 0, 1) |