neoryae-aegis / verify_cap_packing.py
PureOne's picture
Publish Neoryae Aegis research artifacts v1.0.0
3b80460 verified
Raw History Blame Contribute Delete
7.25 kB
"""Independent exact checks of the constructive four-cap packing code.
Run: python verify_cap_packing.py
Writes PACKING_VERIFICATION.json beside this script. These finite tests
support reproducibility of the analytic theorem; they do not substitute
for its universal proof and make no global-optimizer claim.
"""
from collections import Counter
from fractions import Fraction as F
from itertools import combinations, product
from pathlib import Path
from random import Random
from time import perf_counter
import hashlib
import json
from cap_packing import pack_bad_sectors
# Deliberately separate from constructor constants and feasibility logic.
PAIRS = tuple(combinations(range(4), 2))
def check(b, result):
x = result.flows
assert set(x) == set(PAIRS)
assert all(isinstance(v, F) for v in x.values())
for i, j in PAIRS:
assert 0 <= x[i, j] <= 1 - b[i][j]
assert x[i, j] <= 1 - b[j][i]
for i in range(4):
assert sum(x[tuple(sorted((i, j)))] for j in range(4) if j != i) <= 1
# Exact weighted-Hall checks for material requests at this cap.
# A single allowed set has mass 1-b[i][j]; the union of two or
# three distinct allowed sets is the entire unit-mass cap.
targets = [j for j in range(4) if j != i]
for size in (1, 2, 3):
for subset in combinations(targets, size):
request = sum(x[tuple(sorted((i, j)))] for j in subset)
supply = 1 - b[i][subset[0]] if size == 1 else F(1)
assert request <= supply
energy = sum(v * v for v in x.values())
assert energy >= F(5, 9), (b, result, energy)
return energy
def solve_three(rows, rhs):
a = [[F(x) for x in row] + [F(y)] for row, y in zip(rows, rhs)]
for c in range(3):
pivot = next((r for r in range(c, 3) if a[r][c]), None)
if pivot is None:
return None
a[c], a[pivot] = a[pivot], a[c]
divisor = a[c][c]
a[c] = [v / divisor for v in a[c]]
for r in range(3):
if r != c:
factor = a[r][c]
a[r] = [v - factor * w for v, w in zip(a[r], a[c])]
return tuple(a[r][3] for r in range(3))
def sharp_star_vertex_check():
# Every vertex of {0<=x_i<=2/3, sum x_i<=1} activates three of
# its seven inequalities. Enumerate them by exact Gaussian algebra.
normals, bounds = [], []
for i in range(3):
e = [F(0)] * 3
e[i] = F(-1)
normals.append(e); bounds.append(F(0))
e = [F(0)] * 3
e[i] = F(1)
normals.append(e); bounds.append(F(2, 3))
normals.append([F(1)] * 3); bounds.append(F(1))
vertices = set()
for active in combinations(range(7), 3):
p = solve_three([normals[i] for i in active], [bounds[i] for i in active])
if p is not None and all(sum(v * w for v, w in zip(row, p)) <= bound
for row, bound in zip(normals, bounds)):
vertices.add(p)
maximum = max(sum(v * v for v in p) for p in vertices)
assert maximum == F(5, 9)
return len(vertices), maximum
def main():
start = perf_counter()
cases = Counter()
energies = []
tournament_count = 0
structured_count = 0
# Five exact row-allocation profiles for every owner tournament.
for bits in product((0, 1), repeat=6):
tournament_count += 1
owner = {e: e[bit] for e, bit in zip(PAIRS, bits)}
for profile in range(5):
b = [[F(0) for _ in range(4)] for _ in range(4)]
for i in range(4):
outgoing = [e for e in PAIRS if owner[e] == i]
k = len(outgoing)
if not k:
continue
weights = ([1] * k if profile in (0, 4) else
list(range(1, k + 1)) if profile == 1 else
list(range(k, 0, -1)) if profile == 2 else
[1] + [0] * (k - 1))
denominator = sum(weights) * (2 if profile == 4 else 1)
for e, w in zip(outgoing, weights):
j = e[0] if e[1] == i else e[1]
b[i][j] = F(w, denominator)
result = pack_bad_sectors(b, owners=owner)
energies.append(check(b, result))
cases[result.case] += 1
structured_count += 1
rng = Random(20261029)
random_count = 500
for _ in range(random_count):
b = [[F(0) for _ in range(4)] for _ in range(4)]
for i in range(4):
weights = [rng.randrange(1001) for _ in range(3)]
denominator = max(1, sum(weights) + rng.randrange(1001))
for j, w in zip((j for j in range(4) if j != i), weights):
b[i][j] = F(w, denominator)
result = pack_bad_sectors(b)
energies.append(check(b, result))
cases[result.case] += 1
# Sharp star: center bad mass 1/3 in each sector, leaves forbid a cycle.
star = [[F(0) for _ in range(4)] for _ in range(4)]
for j in (1, 2, 3):
star[0][j] = F(1, 3)
star[1][2] = star[2][3] = star[3][1] = F(1)
star_result = pack_bad_sectors(star)
star_energy = check(star, star_result)
assert star_energy == F(5, 9)
vertices, star_maximum = sharp_star_vertex_check()
energies.append(star_energy)
cases[star_result.case] += 1
count = structured_count + random_count + 1
directory = Path(__file__).resolve().parent
report = {
"status": "PASS",
"arithmetic": "Exact fractions.Fraction; no floating point in feasibility or energy checks.",
"scope": "Finite reproducibility checks of an analytic universal packing theorem; not a numerical global-optimization claim.",
"normalization": "Each cap has mass 1; pair capacity is 1-max(b_ij,b_ji).",
"target_squared_flow": "5/9",
"owner_tournaments": tournament_count,
"rational_profiles_per_tournament": 5,
"structured_arrays": structured_count,
"random_rational_arrays": random_count,
"random_seed": 20261029,
"sharp_star_arrays": 1,
"total_arrays": count,
"edge_capacity_checks": 2 * 6 * count,
"node_capacity_checks": 4 * count,
"weighted_Hall_subset_checks": 4 * 7 * count,
"construction_cases": dict(sorted(cases.items())),
"minimum_squared_flow_observed": str(min(energies)),
"sharp_star": {
"construction": star_result.as_json(),
"squared_flow": str(star_energy),
"feasible_star_polytope_vertices": vertices,
"exact_maximum_over_star_vertices": str(star_maximum),
"upper_bound_reason": "A convex squared norm attains its maximum over this bounded polytope at a vertex; all vertices were enumerated exactly.",
},
"errors": [],
"seconds": round(perf_counter() - start, 6),
"sha256": {name: hashlib.sha256((directory / name).read_bytes()).hexdigest()
for name in ("cap_packing.py", "verify_cap_packing.py")},
}
(directory / "PACKING_VERIFICATION.json").write_text(json.dumps(report, indent=2) + "\n")
print(json.dumps(report, indent=2))
if __name__ == "__main__":
main()