"""Independent exact audits of one-split scalar contraction scheduling.""" from collections import Counter from fractions import Fraction as F from itertools import permutations from pathlib import Path import json import random from preemptive_ordering import optimal_one_split_order COUNTS = Counter() def check(condition, group): COUNTS[group] += 1 if not condition: raise AssertionError(group) def independent_endpoint(centers, fragments, initial): # Direct affine-map composition, separate from production recurrence. slope, offset = F(1), F(0) for i, a in fragments: slope, offset = a * slope, a * offset + (1 - a) * centers[i] return slope * initial + offset def audit_case(centers, retentions, initial): centers, retentions, initial = list(map(F, centers)), list(map(F, retentions)), F(initial) n = len(centers) orders = list(permutations(range(n))) values = [independent_endpoint(centers, [(i, retentions[i]) for i in order], initial) for order in orders] lo, hi = min(values), max(values) unique = sorted(set(values)) targets = set(unique) targets.update((a + b) / 2 for a, b in zip(unique, unique[1:])) targets.update([lo - 1, hi + 1, (2 * lo + hi) / 3, (lo + 2 * hi) / 3]) splits = 0 for target in sorted(targets): answer = optimal_one_split_order(centers, retentions, initial, target) fs = answer["fragments"] y = independent_endpoint(centers, fs, initial) projection = min(max(target, lo), hi) check(answer["attainable_interval"] == (lo, hi), "interval_extremes") check(y == projection == answer["endpoint"] == answer["optimal_endpoint"], "exact_projection") check(answer["objective"] == (projection - target) ** 2, "optimal_objective") check(len(fs) <= n + 1, "update_budget") check(answer["adjacent_swaps_examined"] <= n * (n - 1) // 2, "swap_budget") products = [F(1)] * n occurrences = [0] * n for i, a in fs: check(0 <= i < n and 0 <= a <= 1, "valid_fragment") products[i] *= a occurrences[i] += 1 check(products == retentions, "retention_conservation") check(all(k in (1, 2) for k in occurrences), "request_coverage") check(sum(k == 2 for k in occurrences) <= 1, "one_split") if answer["split_request"] is not None: splits += 1 # Every edge of the permutation graph is exactly filled by one split. for order in orders: fs = [(i, retentions[i]) for i in order] original = independent_endpoint(centers, fs, initial) for p in range(n - 1): i, j = order[p : p + 2] swapped = list(order) swapped[p : p + 2] = [j, i] other = independent_endpoint(centers, [(k, retentions[k]) for k in swapped], initial) for lam in (F(1, 3), F(1, 2), F(2, 3)): v = 1 - lam * (1 - retentions[i]) partial = fs[:p] + [(i, retentions[i] / v), (j, retentions[j]), (i, v)] + fs[p + 2 :] got = independent_endpoint(centers, partial, initial) check(got == (1 - lam) * original + lam * other, "edge_interpolation") rng = random.Random(41071 + n) for _ in range(80): fs = [] for i, a in enumerate(retentions): v = a + (1 - a) * F(rng.randint(1, 9), 10) fs.extend([(i, a / v), (i, v)]) rng.shuffle(fs) y = independent_endpoint(centers, fs, initial) check(lo <= y <= hi, "arbitrary_fragment_bounds") return { "requests": n, "centers": list(map(str, centers)), "retentions": list(map(str, retentions)), "initial": str(initial), "unsplit_orders": len(orders), "distinct_endpoints": len(unique), "targets_audited": len(targets), "one_split_solutions": splits, "interval": [str(lo), str(hi)], } def main(): cases = [ ([], [], 0), ([2], [F(1, 3)], F(-1, 7)), ([-1, 1], [F(1, 2)] * 2, 0), ([1, -1], [0, 0], F(2, 3)), ([1, -1, 0], [0, F(2, 3), 1], F(2, 5)), ([1, 1, -1], [F(1, 2), F(1, 3), F(3, 4)], 0), ([-2, 1, 4], [F(1, 3), F(4, 5), F(2, 7)], F(-3, 11)), ([-1, F(-1, 3), F(2, 5), 1], [F(1, 2)] * 4, 0), ([F(-2, 3), F(1, 7), F(2, 5), 1], [F(1, 3), F(2, 3), F(3, 5), F(4, 7)], F(1, 9)), ([-1, F(-1, 2), F(1, 4), F(2, 3), 1], [F(2, 3)] * 5, F(-1, 5)), ([-1, F(-1, 2), F(1, 4), F(2, 3), 1], [F(1, 3), F(2, 5), F(3, 7), F(4, 9), F(5, 11)], F(1, 13)), ] results = [audit_case(*case) for case in cases] # Tightness: two unsplit updates cannot hit zero, whereas three fragments do. a = optimal_one_split_order([-1, 1], [F(1, 2)] * 2) check(a["endpoint"] == 0 and len(a["fragments"]) == 3, "tight_witness") check(set(a["attainable_interval"]) == {F(-1, 4), F(1, 4)}, "tight_witness") for bad in ([-1], [F(3, 2)]): rejected = False try: optimal_one_split_order([1], bad) except ValueError: rejected = True check(rejected, "invalid_input") payload = { "arithmetic": "exact integer and rational; deterministic fixed seed for shuffled fragments", "cases": results, "assertions": dict(sorted(COUNTS.items())), "assertions_total": sum(COUNTS.values()), "unsplit_orders_total": sum(x["unsplit_orders"] for x in results), "targets_total": sum(x["targets_audited"] for x in results), "tight_witness_fragments": [[i, str(v)] for i, v in a["fragments"]], "scope": "Finite audits support the implementation; the all-instance guarantee is the supplied mathematical proof.", } path = Path(__file__).with_name("revision3_fluid_verification_results.json") path.write_text(json.dumps(payload, indent=2) + "\n") print(json.dumps({"assertions_total": payload["assertions_total"], "unsplit_orders_total": payload["unsplit_orders_total"], "targets_total": payload["targets_total"]})) if __name__ == "__main__": main()