| | @@ -0,0 +1,232 @@ |
| 1 | +"""Verify the joint difference-packing certificates using exact rational arithmetic.""" |
| 2 | + |
| 3 | +import csv |
| 4 | +import sys |
| 5 | +from collections import Counter, defaultdict |
| 6 | +from fractions import Fraction |
| 7 | +from functools import cache |
| 8 | +from itertools import combinations, product |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | +HERE = Path(__file__).resolve().parent |
| 12 | +sys.path.insert(0, str(HERE.parent / "upper-fibre-dichotomy")) |
| 13 | +sys.path.insert(0, str(HERE.parent / "upper-conditional")) |
| 14 | +from blocks import directions, dot, is_sidon, multiply, positive_definite, weights |
| 15 | +from margins import DELETION, solve, sum_matrix |
| 16 | + |
| 17 | +EXCEPTION = ((0, 3), (1, 4, 5), (2,), (6,), (7,)) |
| 18 | +REPLACEMENT = (11, 7, 12, 13, 10) |
| 19 | +CLIQUE = ("AB", "AC", "BA", "BB", "BD", "BE", "CB") |
| 20 | +WORST = ((0, 4), (1, 5), (2,), (3,), (6,), (7,)) |
| 21 | +HALF_UNIT = Fraction(1, 2 * 10**10) |
| 22 | + |
| 23 | + |
| 24 | +def pattern_name(pattern): |
| 25 | + return "|".join("".join(map(str, part)) for part in pattern) |
| 26 | + |
| 27 | + |
| 28 | +def shift_types(pattern, starts=tuple(range(8))): |
| 29 | + """Encode signed differences of block starts. Equal masks are interchangeable.""" |
| 30 | + radius = max(starts) - min(starts) |
| 31 | + return { |
| 32 | + chr(65 + i) + chr(65 + j): sum(1 << (s + radius) |
| 33 | + for s in {starts[a] - starts[b] for a in p for b in q}) |
| 34 | + for i, p in enumerate(pattern) for j, q in enumerate(pattern) |
| 35 | + } |
| 36 | + |
| 37 | + |
| 38 | +def demands(shifts, vector): |
| 39 | + counts = Counter() |
| 40 | + for (a, b), mask in shifts.items(): |
| 41 | + counts[mask] += vector[ord(a) - 65] * vector[ord(b) - 65] |
| 42 | + return counts |
| 43 | + |
| 44 | + |
| 45 | +def shift_load(counts): |
| 46 | + return max(sum(n for mask, n in counts.items() if mask & (1 << s)) for s in range(15)) |
| 47 | + |
| 48 | + |
| 49 | +def read_certificates(): |
| 50 | + records = defaultdict(lambda: defaultdict(list)) |
| 51 | + with (HERE / "certificates.csv").open(newline="") as f: |
| 52 | + for row in csv.DictReader(f): |
| 53 | + mass = Fraction(row["weight"]) |
| 54 | + assert mass > 0 |
| 55 | + records[row["partition"]][row["certificate"]].append((mass, row["type_pairs"].split())) |
| 56 | + return records |
| 57 | + |
| 58 | + |
| 59 | +def packing_cover(shifts, terms): |
| 60 | + """Total mass and marginal cover of a list of disjoint packings.""" |
| 61 | + total = Fraction(0) |
| 62 | + cover = Counter() |
| 63 | + for weight, pairs in terms: |
| 64 | + used = 0 |
| 65 | + for pair in pairs: |
| 66 | + mask = shifts[pair] |
| 67 | + assert used & mask == 0, pairs |
| 68 | + used |= mask |
| 69 | + cover[mask] += weight |
| 70 | + total += weight |
| 71 | + assert total > 0 |
| 72 | + return total, cover |
| 73 | + |
| 74 | + |
| 75 | +def max_packed_weight(prices): |
| 76 | + """Maximum total price of pairwise disjoint masks.""" |
| 77 | + containing = { |
| 78 | + 1 << j: [(mask, value) for mask, value in prices.items() if mask & (1 << j)] |
| 79 | + for j in range(15) |
| 80 | + } |
| 81 | + |
| 82 | + @cache |
| 83 | + def best(available): |
| 84 | + if not available: |
| 85 | + return 0 |
| 86 | + bit = available & -available |
| 87 | + value = best(available ^ bit) |
| 88 | + for mask, cost in containing[bit]: |
| 89 | + if available & mask == mask: |
| 90 | + value = max(value, cost + best(available ^ mask)) |
| 91 | + return value |
| 92 | + |
| 93 | + return best((1 << 15) - 1) |
| 94 | + |
| 95 | + |
| 96 | +def optimum_bounds(pattern, shifts, vector, packing, functional): |
| 97 | + w = weights(pattern, DELETION) |
| 98 | + demand = demands(shifts, vector) |
| 99 | + total, cover = packing_cover(shifts, packing) |
| 100 | + # At this scale, dropping some uses of each mask gives the exact marginals. |
| 101 | + capacity = max(total * n / cover[mask] for mask, n in demand.items()) |
| 102 | + lower = dot(w, vector)**2 / capacity |
| 103 | + for s in range(15): |
| 104 | + assert dot(vector, multiply(sum_matrix(pattern, s), vector)) <= 2 * capacity |
| 105 | + |
| 106 | + prices = Counter() |
| 107 | + for weight, pairs in functional: |
| 108 | + for pair in pairs: |
| 109 | + mask = shifts[pair] |
| 110 | + assert mask not in prices and weight.denominator == 1 |
| 111 | + prices[mask] = weight.numerator |
| 112 | + budget = max_packed_weight(prices) |
| 113 | + assert budget > 0 |
| 114 | + names = [chr(65 + i) for i in range(len(pattern))] |
| 115 | + matrix = [[Fraction(prices[shifts[a+b]] + prices[shifts[b+a]], 2 * budget) |
| 116 | + for b in names] for a in names] |
| 117 | + assert positive_definite(matrix) |
| 118 | + dual_vector = solve(matrix, w) |
| 119 | + assert multiply(matrix, dual_vector) == w |
| 120 | + return lower, dot(w, dual_vector) |
| 121 | + |
| 122 | + |
| 123 | +def verify_profiles(): |
| 124 | + published = directions() |
| 125 | + records = read_certificates() |
| 126 | + with (HERE / "optima.csv").open(newline="") as f: |
| 127 | + rows = list(csv.DictReader(f)) |
| 128 | + by_name = {row["partition"]: row for row in rows} |
| 129 | + assert len(rows) == len(by_name) == len(published) == 34 |
| 130 | + assert set(records) == set(by_name) == {pattern_name(p) for p in published} |
| 131 | + excluded = [] |
| 132 | + print(f'{"Partition":22} {"Published profile":18} Maximum w·α') |
| 133 | + for pattern, old in published.items(): |
| 134 | + name = pattern_name(pattern) |
| 135 | + shifts = shift_types(pattern) |
| 136 | + terms = records[name] |
| 137 | + expected = {"published", "lower", "upper"} |
| 138 | + if pattern == EXCEPTION: |
| 139 | + expected.add("replacement") |
| 140 | + expected.update(f"rounded-{r}" for r in range(1, 7)) |
| 141 | + assert set(terms) == expected |
| 142 | + w = weights(pattern, DELETION) |
| 143 | + old_demand = demands(shifts, old) |
| 144 | + load = shift_load(old_demand) |
| 145 | + assert 4 * dot(w, old)**2 > 9 * load |
| 146 | + total, cover = packing_cover(shifts, terms["published"]) |
| 147 | + assert cover == old_demand |
| 148 | + if pattern == EXCEPTION: |
| 149 | + clique = [shifts[pair] for pair in CLIQUE] |
| 150 | + assert len(set(clique)) == 7 and all(a & b for a, b in combinations(clique, 2)) |
| 151 | + assert sum(old_demand[k] for k in clique) == total == 106 > load == 100 |
| 152 | + excluded.append(name) |
| 153 | + total, cover = packing_cover(shifts, terms["replacement"]) |
| 154 | + assert cover == demands(shifts, REPLACEMENT) |
| 155 | + assert total == shift_load(cover) == 583 |
| 156 | + assert 4 * dot(w, REPLACEMENT)**2 > 9 * total |
| 157 | + assert max(dot(REPLACEMENT, multiply(sum_matrix(pattern, s), REPLACEMENT)) |
| 158 | + for s in range(15)) == 724 <= 2 * total |
| 159 | + for r in range(1, 7): |
| 160 | + starts = tuple(16*b + (b*r+7)//8 for b in range(8)) |
| 161 | + rounded = shift_types(pattern, starts) |
| 162 | + mass, cover = packing_cover(rounded, terms[f"rounded-{r}"]) |
| 163 | + assert mass == load and cover == demands(rounded, old) |
| 164 | + verdict = "excluded" |
| 165 | + else: |
| 166 | + assert total == load |
| 167 | + verdict = "not excluded" |
| 168 | + |
| 169 | + row = by_name[name] |
| 170 | + v = tuple(map(int, row["direction"].split())) |
| 171 | + assert len(v) == len(pattern) and min(v) > 0 |
| 172 | + lower, upper = optimum_bounds(pattern, shifts, v, terms["lower"], terms["upper"]) |
| 173 | + rounded = Fraction(row["rounded_optimum"]) |
| 174 | + assert (rounded - HALF_UNIT)**2 < lower <= upper < (rounded + HALF_UNIT)**2, name |
| 175 | + assert lower > Fraction(9, 4) |
| 176 | + if pattern == WORST: |
| 177 | + assert lower == upper == Fraction(57975781, 25000000) |
| 178 | + print(f'{name:22} {verdict:18} {row["rounded_optimum"]}') |
| 179 | + assert excluded == [pattern_name(EXCEPTION)] |
| 180 | + print("Published profiles feasible: 33/34. Partition optima above 3/2: 34/34.") |
| 181 | + print("Rounded starts: the exceptional published profile fits for M mod 8 = 1 through 6.") |
| 182 | + |
| 183 | + |
| 184 | +def verify_small_systems(): |
| 185 | + """Compare the charging proof to all four-fibre systems in three columns.""" |
| 186 | + valid = rejected = conflicts = 0 |
| 187 | + for pattern in (EXCEPTION, WORST): |
| 188 | + length = 3 |
| 189 | + shifts = shift_types(pattern) |
| 190 | + clique = {shifts[pair] for pair in CLIQUE} if pattern == EXCEPTION else { |
| 191 | + mask for mask in shifts.values() if mask & (1 << 7) |
| 192 | + } |
| 193 | + options = [(chr(65+i), x, tuple(x+b*length for b in part)) |
| 194 | + for i, part in enumerate(pattern) for x in range(length)] |
| 195 | + for family in combinations(options, 4): |
| 196 | + if any(not is_sidon(section) for section in product(*(f[2] for f in family))): |
| 197 | + rejected += 1 |
| 198 | + continue |
| 199 | + valid += 1 |
| 200 | + pairs = defaultdict(list) |
| 201 | + for i, (a, x, _) in enumerate(family): |
| 202 | + for j, (b, y, _) in enumerate(family): |
| 203 | + if i != j: |
| 204 | + pairs[x-y].append((i, j, shifts[a+b])) |
| 205 | + shared = Counter() |
| 206 | + ref = [f[2][0] for f in family] |
| 207 | + for group in pairs.values(): |
| 208 | + for (i, j, m), (k, h, t) in combinations(group, 2): |
| 209 | + if not m & t: |
| 210 | + continue |
| 211 | + assert {i, j} & {k, h} |
| 212 | + delta = ref[i] - ref[j] - ref[k] + ref[h] |
| 213 | + assert delta % length == 0 and abs(delta // length) <= 14 |
| 214 | + shared[delta // length] += 1 |
| 215 | + assert all(count <= 6 * len(family) for count in shared.values()) |
| 216 | + conflicts += shared.total() |
| 217 | + diag = sum(shifts[a+a] in clique for a, _, _ in family) |
| 218 | + for u in (1, 2, 3): |
| 219 | + via_pairs = u * diag + sum(max(u-abs(g), 0) * sum(m in clique for _, _, m in group) |
| 220 | + for g, group in pairs.items()) |
| 221 | + via_windows = 0 |
| 222 | + for z in range(1-u, length): |
| 223 | + inside = [a for a, x, _ in family if z <= x < z+u] |
| 224 | + via_windows += sum(shifts[a+b] in clique for a in inside for b in inside) |
| 225 | + assert via_pairs == via_windows <= u*u + u*(diag + shared.total()) |
| 226 | + assert valid and rejected and conflicts |
| 227 | + print(f"Four-fibre systems: {valid} valid, {rejected} rejected, {conflicts} shared-fibre conflicts.") |
| 228 | + |
| 229 | + |
| 230 | +if __name__ == "__main__": |
| 231 | + verify_profiles() |
| 232 | + verify_small_systems() |