"""Exact four-cap material packing, normalized to unit mass per cap. Input b[i][j] is the mass forbidden from pairing cap i toward cap j. The three forbidden sectors at each cap are disjoint, so every row sum is at most one. The construction returns symmetric edge flows obeying x[i,j] <= 1-max(b[i][j], b[j][i]), sum_j x[i,j] <= 1, and sum(x[i,j]**2) >= 5/9. This is an exact construction implementing the accompanying analytic proof; finite test results are not a proof for all inputs. All arithmetic uses fractions.Fraction. No numerical optimizer is used. """ from dataclasses import dataclass from fractions import Fraction EDGES = tuple((i, j) for i in range(4) for j in range(i + 1, 4)) def edge(i, j): return (i, j) if i < j else (j, i) @dataclass(frozen=True) class Packing: flows: dict owners: dict case: str def as_json(self): return { "flows": {f"{i}-{j}": str(x) for (i, j), x in self.flows.items()}, "owners": {f"{i}-{j}": v for (i, j), v in self.owners.items()}, "case": self.case, } def pack_bad_sectors(bad, owners=None): """Return an exact feasible Packing for a four-by-four bad-mass array. Optional owners chooses either endpoint of each edge attaining its maximum bad mass; it allows all tie orientations to be reproduced. Otherwise ties go to the smaller endpoint. Diagonal entries must be zero. Integers, Fraction objects, and rational strings are accepted. """ if len(bad) != 4 or any(len(row) != 4 for row in bad): raise ValueError("bad must be a four-by-four array") b = tuple(tuple(Fraction(x) for x in row) for row in bad) for i, row in enumerate(b): if row[i] != 0 or any(x < 0 for x in row) or sum(row) > 1: raise ValueError("bad rows must be nonnegative, diagonal zero, sum <= 1") capacity = {e: 1 - max(b[e[0]][e[1]], b[e[1]][e[0]]) for e in EDGES} if owners is None: own = {e: (e[0] if b[e[0]][e[1]] >= b[e[1]][e[0]] else e[1]) for e in EDGES} else: own = dict(owners) if set(own) != set(EDGES): raise ValueError("owners must specify all six unordered edges") for (i, j), v in own.items(): if v not in (i, j) or b[v][j if v == i else i] != max(b[i][j], b[j][i]): raise ValueError("an owner must attain the edge's maximum bad mass") owned = {i: [e for e in EDGES if own[e] == i] for i in range(4)} flows = {e: Fraction(0) for e in EDGES} # A source owns three edges. Their capacities sum to at least two. sources = [i for i in range(4) if len(owned[i]) == 3] if sources: i = sources[0] ordered = sorted(owned[i], key=lambda e: (-capacity[e], e)) first, second = ordered[:2] t = capacity[first] flows[first], flows[second] = t, 1 - t return Packing(flows, own, "source") # At least two vertices own two edges. Reduce each such pair to sum one. pairs = [i for i in range(4) if len(owned[i]) == 2] reduced = dict(capacity) for i in pairs: first, second = owned[i] reduced[first] = capacity[first] reduced[second] = 1 - reduced[first] if reduced[first] < Fraction(1, 3) or reduced[first] > Fraction(2, 3): flows[first], flows[second] = reduced[first], reduced[second] return Packing(flows, own, "complementary_outlier") u, v = pairs[:2] if own[edge(u, v)] != u: u, v = v, u uw = next(e for e in owned[u] if e != edge(u, v)) w = uw[0] if uw[1] == u else uw[1] vw = edge(v, w) vz = next(e for e in owned[v] if e != vw) flows[vw], flows[vz] = reduced[vw], reduced[vz] flows[uw] = min(reduced[uw], 1 - reduced[vw]) return Packing(flows, own, "balanced_pair_and_third")