diff --git a/research/cactus-partial-query/PROOF.md b/research/cactus-partial-query/PROOF.md new file mode 100644 index 0000000000..5d0b3d21ae --- /dev/null +++ b/research/cactus-partial-query/PROOF.md @@ -0,0 +1,140 @@ +# Exact partial acquisition on a cactus + +This proof was produced by Nick Petalas. Apache-2.0. + +## Model and theorem + +Let G=(V,E) be a finite simple undirected cactus graph. Distinct simple +cycles may meet at vertices, but each edge is in at most one simple cycle. +Disconnected graphs and isolated vertices are allowed. An edge e={u,v} +offers the binary query q_e=x_u+x_v, with nonnegative acquisition price c_e. +Each source x_v is arbitrary and unknown to the decoder. It sees only +purchased replies. A purchased set S pays sum_(e in S) c_e once; +linear reconstruction of the requested targets is free in this relaxation. + +Write B for the vertex-by-edge binary incidence matrix. Let the columns of +T be any finite list of requested coefficient vectors, including zero or +dependent columns. Thus the target responses are T^t x. They are feasible +exactly when each column has even parity on every connected component. +The statement holds coordinatewise for source fields of every positive +binary row width. This is not a price for their physical realization. + +Choose any spanning forest F and any coefficient matrix R with BR=T. +For an edge e let r_e denote row e of R. Let C range over the simple +cycles and let E_br be the bridges. Then the exact minimum acquisition bill is + +```text +OPT(T) = sum_(e in E_br, r_e != 0) c_e + + sum_C [sum_(e in C) c_e - max_z sum_(e in C, r_e = z) c_e]. (1) +``` + +Here z ranges over binary rows of length equal to the number of targets. +It suffices to consider labels r_e actually appearing on C. In particular, +there is no enumeration of target superspaces or a bounded-codimension +hypothesis. The expression is independent of the chosen solution R. + +For each cycle choose a maximizing label z_C. Keep bridge e exactly when +r_e is nonzero. On C keep e exactly when r_e+z_C is nonzero. These edges +and the modified coefficient rows give an attaining reconstruction. The +constructor can choose an attaining set that is a forest. + +## Incidence solutions and the lower bound + +For any graph, the columns of a spanning forest are independent. A nonempty +edge subset of a forest has a leaf of degree one in its nontrivial support, +so its incidence sum cannot be zero. Repeated leaf elimination also represents +every vertex vector having even parity on each component. This proves the +feasibility criterion and rank B=|V|-number of components. + +Deleting one edge from each simple cycle of a cactus leaves a spanning forest. +Connectivity survives each deletion along the remainder of that cycle. No +cycle survives, because every surviving simple cycle would have been one of +the cycles just cut. Consequently the number of simple cycles is +|E|-|V|+number of components. + +The incidence vector gamma_C of a cycle is in ker B. These vectors have +disjoint nonempty edge supports, hence are independent. The dimension +calculation shows that they form a basis of ker B. Thus every solution D of +BD=T has, independently for each target column, a unique cycle-basis expansion +relative to R. In row form this says + +```text +D_e = r_e on bridges, +D_e = r_e + z_C on the edges of cycle C. (2) +``` + +There is no interaction between the choices z_C, including when two cycles +share a vertex. This is where the cactus hypothesis is used. + +If S purchases enough queries, choose one reconstruction of every target +using S. Its coefficient matrix D has zero rows outside S. Since prices are +nonnegative, + +```text +cost(S) >= sum_(e: D_e != 0) c_e. +``` + +Conversely, the support of any solution D is a feasible purchased set with +that support bill. The optimum is therefore exactly the minimum support bill +among all solutions of BD=T. By (2), a bridge's contribution is fixed, while +a cycle with shift z pays its total price minus the price of the rows equal +to z. Independent cycle minimization gives (1). An absent label saves zero; +a present label has nonnegative saved price, so one present label maximizes. +This proves the lower bound, including zero prices and an empty target list. + +## Construction, certificate and independence of choices + +Start with a spanning forest and give all nontree edges coefficient zero. +At each vertex pack its entries in the target columns into a binary row. +Process forest vertices from leaves to roots. Put the current vertex row on +its parent edge and add it to its parent's row. The residual at a root is +exactly the target parity on its connected component. All residuals vanish +if and only if T is feasible, and the resulting R satisfies BR=T. + +Construct each fundamental cycle from a nontree edge and its forest path. +In a cactus these are precisely the edge-disjoint simple cycles. Group the +rows on each cycle by equality, total the prices in every group, choose a +maximum-price group, and apply (2). The displayed reconstruction proves +attainment. At least one edge disappears on each cycle because the chosen +label appears there, so the selected support is a forest. No cycles outside +this list exist. + +The certificate records R, the cycle edge lists, each shift and saved price, +the modified rows, selected edges and bill. The checker reconstructs the +forest solution and cycles, checks every row relation, compares the chosen +saving against every present bucket, and verifies BD=T directly. Its +optimality implication is the proof above. It does not invoke the optimizer +or solve another acquisition problem. The decomposition routines are shared; +the finite subset oracle instead uses literal XOR spans and an independent +simple-cycle enumeration. + +Replacing R changes each cycle's rows by a common translation, which merely +permutes its bucket labels; bridge rows do not change. Formula (1) is therefore +independent of R. Different spanning forests have the same consequence. +No assumption about a particular target basis or its rank is required. + +## Small improvement and boundary + +Take the triangle 01,12,02, with prices 4,3,2, and request x_0+x_1. +Its cheapest whole-span basis consists of edges 12 and 02 at price five; +both answers are needed to reconstruct this target from that basis. +The exact partial-target optimum is the direct edge 01 at price four. +With both independent triangle targets requested, formula (1) instead pays +five, as it should. These numbers are enumerated by the new verifier. + +A general graph still has a cycle-space parametrization, but overlapping +cycle edges couple its shifts. Formula (1) is not asserted there. This +result does not optimize reconstruction XORs, storage, chronology, source +mutation, nonlinear observations, native frames, delivery or restoration. +A favorable acquisition bill alone supplies no complete recursive invoice. + +## Credit + +Ali Romman's [PR #5791](https://github.com/CrocSwap/integer-mult-bounds/pull/5791) +poses the partial-target boundary of whole-span binary query acquisition. +The new step here is the exact arbitrary-target cactus formula and its +certificate. Classical graphic cycle spaces, parity, linear reconstruction +and weighted grouping retain their usual credit. No literature-wide priority +for these ingredients or their general optimization setting is claimed. +Nick Petalas #5878 and #6654 are related general-pool results, not premises +or algorithms used in this proof. diff --git a/research/cactus-partial-query/README.md b/research/cactus-partial-query/README.md new file mode 100644 index 0000000000..a970545bac --- /dev/null +++ b/research/cactus-partial-query/README.md @@ -0,0 +1,159 @@ +# Exact partial-query acquisition on a cactus graph + +This contribution was produced by Nick Petalas. Apache-2.0. + +Claim type: scoped written proof with exact finite corroboration and a bounded +constructive optimizer. For binary edge queries on every finite simple cactus +graph, arbitrary target subspaces have an exact acquisition optimum. Neither +target rank nor target codimension needs to be small. No new kappa is claimed. + +Give edge e={u,v} the query x_u+x_v and nonnegative price c_e. The decoder +reconstructs requested parities only from purchased replies. Choose any matrix +R of edge coefficients reconstructing all requested targets, with row r_e. +Then + +```text +OPT = sum_(bridge e with r_e != 0) c_e + + sum_(cycle C) [sum_(e in C) c_e + - max_z sum_(e in C with r_e = z) c_e]. +``` + +On each cycle remove the maximum-price bucket of equal rows and translate +all its coefficient rows by that bucket's label. Keep the nonzero rows. +This gives an attaining forest and every reconstruction expression. +[PROOF.md](PROOF.md) proves the lower bound, construction, feasibility test +and independence from the initial coefficient solution. Its cycles may share +vertices, but must be edge-disjoint. + +For triangle queries 01,12,02 at prices 4,3,2 and target x_0+x_1, the exact +partial-target bill is four. Pruning the cheapest whole-span basis pays five. +The new result supplies a closed optimum throughout this structured pool, +rather than another bound or a search through target superspaces. + +## Reproduce and use + +From the repository root, CPython 3.12 and its standard library: + +```sh +/Users/nick/git/integer-mult-bounds/.venv/bin/python3.12 -I -W error -B research/cactus-partial-query/verify.py +/Users/nick/git/integer-mult-bounds/.venv/bin/python3.12 -I -O -W error -B research/cactus-partial-query/verify.py +/Users/nick/git/integer-mult-bounds/.venv/bin/python3.12 -B scripts/research_package.py --package research/cactus-partial-query +/Users/nick/git/integer-mult-bounds/.venv/bin/python3.12 -B scripts/check_research_commit.py --package research/cactus-partial-query --check-source-history +``` + +Substitute another CPython 3.12 path on another machine. Run the publication +preflight after committing. Both registered commands have 120-second limits. +They print `PASS_CACTUS_PARTIAL_QUERY` with a generated receipt binding the +SHA256 of all seven package files, and write nothing into the checkout. +No receipt is handwritten. Direct verification needs neither Git nor network. + +The optimizer accepts this JSON problem, where vertex bit i is coefficient i: + +```json +{"vertices":3,"edges":[[0,1],[1,2],[0,2]],"costs":[4,3,2],"targets":[3]} +``` + +Save it outside the checkout, then run: + +```sh +python3.12 -I -B research/cactus-partial-query/optimizer.py problem.json > certificate.json +python3.12 -I -B research/cactus-partial-query/optimizer.py problem.json --check certificate.json +``` + +The checker compares all cycle savings and reconstructs every target; it +does not invoke the optimizer. Its graph and base-coefficient routines are +shared with the producer. The independent exhaustive oracle shares neither +routine. + +## Exact finite evidence + +The verifier enumerates all 1,099 labelled simple graphs on one through five +vertices. A separate edge-subset simple-cycle test finds 691 cacti and rejects +408 noncacti through the optimizer. For every cactus it enumerates every +subspace of its edge-query span, producing 28,305 target bases across graphs. +Each is checked at three explicitly defined integer-price patterns, including +zero prices. All 84,915 optimum comparisons agree with a direct purchased- +subset/XOR-span oracle. The oracle enumerates 39,267 purchased subsets across +graph/price cases. Counts include repeated mathematical spaces in different +labelled graphs or price cases, as stated. + +Every optimum certificate and every binary source assignment on its graph +are checked. The 367 cycle occurrences include two cycles sharing a vertex. +Thirty-three larger fixtures include 32 triangles sharing one hub, dependent +and zero targets, and a 128-vertex forest with 128 target columns and prices +10^12. Ten semantic/certificate controls and fourteen invalid-input controls +reject. Normal and optimized Python run the identical explicit checks. +These finite checks corroborate the written theorem; they do not establish +its arbitrary-size quantifiers by extrapolation. + +## Scope, provenance and implementation bounds + +Ali Romman's [PR #5791](https://github.com/CrocSwap/integer-mult-bounds/pull/5791) +supplies the whole-span acquisition interface and partial-target question. +Its greedy theorem and scientific executable are not replayed. Classical +incidence kernels, cycle spaces, parity and weighted grouping retain credit. +The new step is formula (1) for arbitrary cactus target spaces, its attaining +reconstruction and certificate. Literature-wide priority is unclaimed. +Nick Petalas #5878 and #6654 supply related general-pool approximation and +small-codimension results; their theorems, constructions and code are not used. +Predecessor credit lines and frozen evidence are unchanged. + +Canonical base is e54523517d0f03e58352e647c403a5af3e525a57, priority revision 8. +The [advance claim](https://github.com/CrocSwap/integer-mult-bounds/discussions/1166#discussioncomment-19601550) +states this scope. SOURCES.json pins historical provenance, including #5791's +guide. It is not a runtime import. The selected-baseline binding orientation +passed, with selected #7000 at 0.00363005817; historical priority headlines do +not supersede that record. TASK.json records the changed joint-query-controller +scope and the structured next questions. The card's physical acceptance gate +is not met by this algebraic side result. + +Live overlap checked open PRs, this account's recent PRs, handoffs, active +Discussions, merged-PR searches and all-main README searches. The cached helper +covered 846 open PRs and 7,482 Discussion records with complete pagination; +its local scan covered 207 of 10,178 note paths. Targeted Git searches cover +omitted main README text, rather than all historical diffs or unpublished work. +No matching cactus acquisition result was found in that coverage. This is not +a novelty certificate. + +The theorem permits real nonnegative prices. The executable restricts prices +to integers in 0..10^12, vertices to 1..128, edges to at most 192 and target +columns to at most 128. Edges have canonical distinct endpoints; duplicates, +loops, noncacti, out-of-range values and infeasible targets reject explicitly. +The implementation is deterministic and polynomial in these input sizes, +with no enumeration of target superspaces. The certificate checker can use +quadratic work in a cycle length to compare every bucket. + +Functions stay below 60 physical lines. Loops inherit the fixed input bounds; +the exhaustive graph domain has at most ten possible edges and 1,024 subsets. +The main oracle processes at most 67 target spaces per graph, at most 64 +purchases per cactus and at most 32 source assignments per optimum. Inputs and +certificates are capped at 128 KiB; each of seven receipt files at 256 KiB. +There is no recursion, subprocess, network access or code generation in the +scientific checker. Standard-library dispatch and one fixed sibling import +have explicit targets. A local price-key callback only compares bounded rows. + +Power of Ten rule 3 deviation: CPython allocates managed integers and +containers during this bounded one-shot computation. The caps above bound +live graph, coefficient, subspace and oracle state. No fixed-allocation or +persistent-service guarantee is claimed. Explicit contracts survive -O. +Warning-strict normal and optimized execution and in-memory syntax/function- +size inspection supply the available diagnostics. No external static analyzer +or proof assistant is required or claimed. + +Acquisition prices exclude reconstruction XORs, physical delivery, source +access, chronology, dirty restoration, frames, ownership, storage, precision, +tape costs and a complete paid/productive supplier invoice. No actual native +caller, selected scientific cold replay, Lean, Docker, new kappa, selected- +frontier change or complete multiplication theorem is supplied. Independent +human and formal proof-kernel review are unclaimed. General graphs with +shared cycle edges are outside the theorem. + +## Open next questions + +- Can one actual native caller supply a cactus query pool whose complete + partial-target bill improves? First export its queries, targets and prices, + then charge reconstruction, delivery, storage and restoration. The missing + physical caller and tariffs prevent a supplier consequence. +- Can the coupled coefficient shifts be optimized for one theta graph? Start + with its three path coefficient rows. Shared edges couple the cycles, so + the independent-bucket formula supplies no answer there. diff --git a/research/cactus-partial-query/SOURCES.json b/research/cactus-partial-query/SOURCES.json new file mode 100644 index 0000000000..c4f294d56e --- /dev/null +++ b/research/cactus-partial-query/SOURCES.json @@ -0,0 +1,24 @@ +{ + "schema_version": 1, + "base_commit": "e54523517d0f03e58352e647c403a5af3e525a57", + "files": [ + { + "path": "certificates/selected-result.json", + "usage": "provenance", + "git_blob": "75d77b328ea02ded535e7f23aee5b1e55d0bb1a8", + "sha256": "154c3d30455d778fd3343b6b4851f0fa4665f0f7625b0adb3675e2bafbbf6bf1" + }, + { + "path": "docs/research/agent-task-catalog.json", + "usage": "provenance", + "git_blob": "3404fbdd5e7529a0674eef26114b5071e4586cec", + "sha256": "ead6cdc9d0af6d58e8a49744d8ec8c7c15e13d2ba93c276c434bcf9b51b93a81" + }, + { + "path": "docs/research/binary-query-basis.md", + "usage": "provenance", + "git_blob": "291bce46071c77b76b17ddfc3571df676b95c529", + "sha256": "cd60665849910830381a6a5b0c8f0d10b4bc9e54d50286eeacaf200fffd6f660" + } + ] +} diff --git a/research/cactus-partial-query/TASK.json b/research/cactus-partial-query/TASK.json new file mode 100644 index 0000000000..d70fa3fd77 --- /dev/null +++ b/research/cactus-partial-query/TASK.json @@ -0,0 +1,108 @@ +{ + "schema_version": 1, + "task_id": "joint-query-controller", + "priority_revision": 8, + "base_commit": "e54523517d0f03e58352e647c403a5af3e525a57", + "original_card": { + "id": "joint-query-controller", + "question": "Can a changed query demand or caller remove real work while satisfying native chronology and restoration?", + "starting_points": [ + "docs/research/current-priorities.md", + "docs/research/research-family-status.md", + "certificates/selected-result.json", + "docs/research/pr-intake-evidence/pr5599-completion/PROOF-REVIEW.md" + ], + "methods": [ + "Construct and independently check one source-bound instance of the stated first experiment.", + "Seek an exact counterexample or target-specific complete-budget obstruction to that same proposed change." + ], + "acceptance_gate": "A source-bound arbitrary-input identity and legal producer/consumer boundary with the complete paid and productive target invoice, including restoration, ownership, native alignment and extra charges. Price under the actual current model; historical ceiling.py assumptions must be established before use. A favorable optimistic relaxation keeps a route open but is not a completed supplier. A scoped local result remains useful; expansion requires the family-level budget gate.", + "first_step": { + "command": [ + "python3", + "-B", + "scripts/research_selected_baseline.py" + ], + "expected": "PASS_RESEARCH_BASELINE_BINDINGS_ONLY", + "inspect": "Read revision 8, the selected review and live exact-source handoffs. Then follow this card's bounded first_experiment.", + "scope": "Source-bound orientation only, not a supplier replay or theorem proof." + }, + "first_experiment": "Choose an exact caller and a new demand/interface hypothesis. Read the scoped #5617/#5631/#5649/#5747 and #5720/#5745 objections before rebuilding an excluded schedule. Compare the complete matched source/consumer protocols including all retained helper outputs. For an active-mode injection, first check #5745: its canonical delivery claim repays the eight removed centre jobs. Audit that exact claim or change its channel/feedback hypothesis; do not repeat the same free-injection search. Stop if the full favorable budget fails or the required restored output is unobservable.", + "bottleneck_link": "Judge the actual current supplier contract and complete signed target/paid bill; frozen-profile ceilings require an applicability proof.", + "allowed_variations": [ + "One exact source head, helper/cut region or quantified proposition and changed hypothesis; check live handoffs first. Different seeds are not different scopes." + ], + "scope_change_triggers": [ + "Changing source head, helper/cut domain, operation alphabet or caller boundary without recording the new scope.", + "Treating an archived proposal or local numerical pass as complete supplier admission." + ], + "overlap_terms": [ + "query demand", + "active mode", + "5720", + "5745", + "5747", + "caller" + ], + "existing_checks": [ + { + "scope": "PR #4166: author-checked installed producer/service/return/bridge connection; native-component comparison loses.", + "evidence": "research/qq16-caller-install/README.md", + "remaining": "Complete matched physical protocol bill, or changed source-compatible demand/circuit; no affordable supplier yet.", + "status": "author-checked", + "source_commit": "0b082d22a0e9a442d264ab0633e35ef26c790630", + "selected_record_sha256": "bb2eaa58647b353f88d0aec2eafb9d30df1be53589231b23e12ceef3e4bad129" + }, + { + "scope": "Selected PR5599: complete helper-subspace construction and independent caller replay; five framework assumptions remain.", + "evidence": "docs/research/pr-intake-evidence/pr5599-completion/PROOF-REVIEW.md", + "remaining": "The next experiment must identify uncovered source-specific work. Successor archive intake does not transfer this admission.", + "status": "reviewed", + "source_commit": "09dd0965711ca7eeb169057a420abab1c9052137", + "selected_record_sha256": "2a0220e866417774606e0f991e41b9c1d26b1f43b97857238cf478dbc7d330b5" + } + ] + }, + "scope_status": "changed", + "actual_scope": "Exact minimum additive acquisition cost for arbitrary binary target subspaces from simple cactus-graph edge queries, with an attaining coefficient certificate.", + "scope_change_reason": "The caller card is narrowed to a new structured query-pool theorem. No source-bound physical caller or signed supplier invoice is supplied.", + "bottleneck_link": "Partial-target acquisition can avoid paying for unused query directions. This side theorem removes superspace enumeration for one graph-structured pool; actual native delivery and reconstruction prices remain separate.", + "budget_minutes": 60, + "stop_rule": "Stop at the first proved and subset-oracle-checked cactus optimum; do not extend the graph class or install a supplier.", + "outcome": "completed", + "evidence": "PROOF.md, optimizer.py and verify.py: 84,915 exhaustive optimum comparisons over all cactus graphs on at most five labelled vertices, with certificates and literal source assignments.", + "author": "Nick Petalas", + "ai_assistance": "Independent human and formal proof-kernel review are unclaimed.", + "open_questions": [ + { + "question": "Can a named native caller supply a cactus edge-query pool whose complete partial-target bill improves?", + "why_it_matters": "The acquisition saving is useful only after all physical reconstruction and return costs are charged.", + "first_step": "Export one actual query pool, target rows and acquisition prices, then charge the attaining reconstruction, delivery and restoration.", + "blocked_by": "No physical caller or complete price model is supplied." + }, + { + "question": "Can the coefficient-shift optimization be solved for one theta graph with overlapping cycles?", + "why_it_matters": "The present independent-bucket formula relies on edge-disjoint cycles.", + "first_step": "Write the three path coefficient rows for one explicit theta graph and derive a coupled-shift optimum.", + "blocked_by": "Shared edges couple cycle choices; no such formula is proved here." + } + ], + "coordination": { + "claim_url": "https://github.com/CrocSwap/integer-mult-bounds/discussions/1166#discussioncomment-19601550", + "source": "PR #5791", + "source_author": "aliromman", + "predecessor_author": "aliromman", + "related_not_used": [ + "PR #5878", + "PR #6654" + ] + }, + "freshness": { + "upstream_commit": "e54523517d0f03e58352e647c403a5af3e525a57", + "checked_at_utc": "2026-11-13T22:44:38.648959+00:00", + "priority_revision": 8, + "selected_pr": 7000, + "selected_kappa": "0.00363005817" + }, + "claim_type": "scoped written proof with exact finite corroboration" +} diff --git a/research/cactus-partial-query/VERIFY.json b/research/cactus-partial-query/VERIFY.json new file mode 100644 index 0000000000..122959780e --- /dev/null +++ b/research/cactus-partial-query/VERIFY.json @@ -0,0 +1,34 @@ +{ + "schema_version": 1, + "status": "ready", + "python": "3.12", + "requirements": [], + "inputs": [], + "commands": [ + { + "argv": [ + "{python}", + "-I", + "-W", + "error", + "-B", + "{package}/verify.py" + ], + "timeout_seconds": 120, + "checks": "Every labelled simple graph through five vertices, independent cactus rejection, all target subspaces and three price patterns; direct purchased-subset optima, literal reconstruction, certificates, large boundaries and failure controls." + }, + { + "argv": [ + "{python}", + "-I", + "-O", + "-W", + "error", + "-B", + "{package}/verify.py" + ], + "timeout_seconds": 120, + "checks": "The identical scientific domain with optimized Python; all checks use explicit exceptions." + } + ] +} diff --git a/research/cactus-partial-query/optimizer.py b/research/cactus-partial-query/optimizer.py new file mode 100644 index 0000000000..9a6b4385af --- /dev/null +++ b/research/cactus-partial-query/optimizer.py @@ -0,0 +1,196 @@ +"""Exact acquisition for binary edge queries on a simple cactus graph. + +Produced by Nick Petalas. Apache-2.0. +""" +import argparse +import json +from pathlib import Path + + +def require(condition, message): + if not condition: + raise ValueError(message) + + +def validate(n, edges, costs, targets): + require(type(n) is int and 1 <= n <= 128, "vertex capacity: 1..128") + require(isinstance(edges, (list, tuple)) and len(edges) <= 192, + "edge capacity: at most 192") + require(isinstance(costs, (list, tuple)) and len(costs) == len(edges), + "one price per edge required") + require(isinstance(targets, (list, tuple)) and len(targets) <= 128, + "target capacity: at most 128") + seen = set() + for edge in edges: + require(isinstance(edge, (list, tuple)) and len(edge) == 2, + "edge must have two endpoints") + u, v = edge + require(type(u) is int and type(v) is int and 0 <= u < v < n, + "edges require canonical distinct endpoints") + require((u, v) not in seen, "duplicate edge") + seen.add((u, v)) + require(all(type(c) is int and 0 <= c <= 10**12 for c in costs), + "integer prices must be in 0..10^12") + require(all(type(t) is int and 0 <= t < 1 << n for t in targets), + "target outside vertex coordinates") + + +def forest(n, edges): + adjacency = [[] for _ in range(n)] + for i, (u, v) in enumerate(edges): + adjacency[u].append((v, i)) + adjacency[v].append((u, i)) + parent, parent_edge, depth = [-1] * n, [-1] * n, [0] * n + order, roots, tree = [], [], set() + for root in range(n): + if parent[root] != -1: + continue + parent[root] = root + roots.append(root) + queue = [root] + for v in queue: + order.append(v) + for w, edge in adjacency[v]: + if parent[w] == -1: + parent[w], parent_edge[w] = v, edge + depth[w] = depth[v] + 1 + tree.add(edge) + queue.append(w) + require(len(order) == n, "forest missed a vertex") + return parent, parent_edge, depth, order, roots, tree + + +def cycle_blocks(edges, parent, parent_edge, depth, tree): + cycles, used = [], set() + for edge, (u, v) in enumerate(edges): + if edge in tree: + continue + block = [edge] + for _ in range(len(parent)): + if u == v: + break + if depth[u] < depth[v]: + u, v = v, u + block.append(parent_edge[u]) + u = parent[u] + require(u == v, "tree path did not terminate") + require(len(block) >= 3 and not used.intersection(block), + "graph is not a cactus") + used.update(block) + cycles.append(tuple(sorted(block))) + return tuple(cycles), used + + +def base_rows(n, targets, edges, parent, parent_edge, order, roots): + values = [sum(((t >> v) & 1) << j for j, t in enumerate(targets)) + for v in range(n)] + rows = [0] * len(edges) + for v in reversed(order): + if parent[v] != v: + rows[parent_edge[v]] = values[v] + values[parent[v]] ^= values[v] + require(all(values[root] == 0 for root in roots), + "target has odd parity on a connected component") + return tuple(rows) + + +def bucket_choice(block, rows, costs): + buckets = {} + for edge in block: + label = rows[edge] + buckets[label] = buckets.get(label, 0) + costs[edge] + shift = min(buckets, key=lambda z: (-buckets[z], z)) + return shift, buckets[shift] + + +def optimize(n, edges, costs, targets): + validate(n, edges, costs, targets) + parent, parent_edge, depth, order, roots, tree = forest(n, edges) + cycles, cyclic_edges = cycle_blocks(edges, parent, parent_edge, depth, tree) + rows = base_rows(n, targets, edges, parent, parent_edge, order, roots) + chosen, shifts, savings = list(rows), [], [] + for block in cycles: + shift, saving = bucket_choice(block, rows, costs) + shifts.append(shift) + savings.append(saving) + for edge in block: + chosen[edge] ^= shift + selected = [i for i, row in enumerate(chosen) if row] + bridge_bill = sum(costs[i] for i, row in enumerate(rows) + if i not in cyclic_edges and row) + formula = bridge_bill + sum(sum(costs[i] for i in block) - saving + for block, saving in zip(cycles, savings)) + bill = sum(costs[i] for i in selected) + require(bill == formula, "cost decomposition failed") + result = {"cost": bill, "selected": selected, "rows": chosen, + "base_rows": list(rows), "cycles": [list(c) for c in cycles], + "shifts": shifts, "savings": savings, "bridge_bill": bridge_bill} + check(n, edges, costs, targets, result) + return result + + +def check(n, edges, costs, targets, result): + """Check a saved optimum certificate, without optimizer or price sorting.""" + validate(n, edges, costs, targets) + require(isinstance(result, dict) and set(result) == { + "cost", "selected", "rows", "base_rows", "cycles", "shifts", + "savings", "bridge_bill"}, "certificate fields") + parent, pe, depth, order, roots, tree = forest(n, edges) + cycles, cyclic_edges = cycle_blocks(edges, parent, pe, depth, tree) + require(result["cycles"] == [list(c) for c in cycles], "cycle certificate") + base = base_rows(n, targets, edges, parent, pe, order, roots) + require(result["base_rows"] == list(base), "base coefficient certificate") + rows, shifts, savings = result["rows"], result["shifts"], result["savings"] + require(isinstance(rows, list) and len(rows) == len(edges), "row count") + require(all(type(r) is int and 0 <= r < 1 << len(targets) for r in rows), + "coefficient row domain") + require(isinstance(shifts, list) and len(shifts) == len(cycles), "shift count") + require(isinstance(savings, list) and len(savings) == len(cycles), "saving count") + for k, block in enumerate(cycles): + z = shifts[k] + require(type(z) is int and 0 <= z < 1 << len(targets), "shift domain") + require(all(rows[i] == base[i] ^ z for i in block), "cycle shift") + earned = sum(costs[i] for i in block if base[i] == z) + require(type(savings[k]) is int and savings[k] == earned, "saving value") + for label in {base[i] for i in block}: + require(earned >= sum(costs[i] for i in block if base[i] == label), + "nonmaximal saving bucket") + require(all(rows[i] == base[i] for i in range(len(edges)) + if i not in cyclic_edges), "bridge coefficient") + selected = [i for i, row in enumerate(rows) if row] + require(result["selected"] == selected, "selected support") + require(type(result["cost"]) is int and result["cost"] == + sum(costs[i] for i in selected), "total bill") + bridge_bill = sum(costs[i] for i in range(len(edges)) + if i not in cyclic_edges and base[i]) + require(type(result["bridge_bill"]) is int and + result["bridge_bill"] == bridge_bill, "bridge bill") + delivered = [0] * len(targets) + for edge, (u, v) in enumerate(edges): + for j in range(len(targets)): + if rows[edge] >> j & 1: + delivered[j] ^= (1 << u) | (1 << v) + require(delivered == list(targets), "target reconstruction") + return True + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("problem", type=Path) + parser.add_argument("--check", type=Path) + args = parser.parse_args() + require(args.problem.stat().st_size <= 131072, "problem file capacity") + problem = json.loads(args.problem.read_text()) + require(isinstance(problem, dict) and set(problem) == + {"vertices", "edges", "costs", "targets"}, "problem fields") + values = [problem[k] for k in ("vertices", "edges", "costs", "targets")] + if args.check: + require(args.check.stat().st_size <= 131072, "certificate file capacity") + check(*values, json.loads(args.check.read_text())) + print("PASS_CACTUS_PARTIAL_QUERY_CERTIFICATE") + else: + print(json.dumps(optimize(*values), sort_keys=True)) + + +if __name__ == "__main__": + main() diff --git a/research/cactus-partial-query/verify.py b/research/cactus-partial-query/verify.py new file mode 100644 index 0000000000..ecf4397b6b --- /dev/null +++ b/research/cactus-partial-query/verify.py @@ -0,0 +1,221 @@ +"""Finite corroboration of the cactus partial-acquisition theorem. + +Produced by Nick Petalas. Apache-2.0. +""" +import hashlib +import itertools +import json +import sys +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parent)) +from optimizer import check, optimize + + +def insist(condition, message): + if not condition: + raise RuntimeError(message) + + +def closure(vectors): + space = {0} + for vector in vectors: + space |= {x ^ vector for x in tuple(space)} + return frozenset(space) + + +def subspaces(vectors): + ambient = closure(vectors) + spaces = {frozenset({0})} + for _ in range(len(vectors) + 1): + extended = set(spaces) + for space in spaces: + for vector in ambient - space: + extended.add(space | frozenset(x ^ vector for x in space)) + if extended == spaces: + break + spaces = extended + insist(all(s <= ambient for s in spaces), "subspace outside query span") + return sorted(spaces, key=lambda s: (len(s), tuple(sorted(s)))) + + +def basis(space): + result, generated = [], frozenset({0}) + for v in sorted(space): + if v not in generated: + result.append(v) + generated = generated | frozenset(x ^ v for x in generated) + insist(generated == space, "literal basis did not reconstruct space") + return result + + +def literal_cactus(n, edges): + memberships = [0] * len(edges) + cycles = [] + for mask in range(1, 1 << len(edges)): + degrees = [0] * n + for i, (u, v) in enumerate(edges): + if mask >> i & 1: + degrees[u] += 1 + degrees[v] += 1 + active = {i for i, degree in enumerate(degrees) if degree} + if not active or any(degrees[v] != 2 for v in active): + continue + reached = {min(active)} + for _ in range(n): + for i, (u, v) in enumerate(edges): + if mask >> i & 1 and (u in reached or v in reached): + reached.update((u, v)) + if reached != active: + continue + cycles.append(mask) + for i in range(len(edges)): + memberships[i] += mask >> i & 1 + return max(memberships, default=0) <= 1, cycles + + +def purchase_table(edges, costs): + vectors = [(1 << u) | (1 << v) for u, v in edges] + rows = [] + for mask in range(1 << len(edges)): + chosen = [i for i in range(len(edges)) if mask >> i & 1] + rows.append((closure(vectors[i] for i in chosen), + sum(costs[i] for i in chosen))) + return rows + + +def literal_delivery(n, edges, targets, result): + selected = result["selected"] + for x in range(1 << n): + answers = [((x >> u) ^ (x >> v)) & 1 for u, v in edges] + for j, target in enumerate(targets): + delivered = sum(answers[i] for i in selected + if result["rows"][i] >> j & 1) % 2 + insist(delivered == (x & target).bit_count() % 2, + "literal source/query reconstruction") + + +def exhaustive(): + counts = dict(graphs=0, cacti=0, noncacti=0, optima=0, + purchase_subsets=0, cycle_blocks=0, target_bases=0) + digest = hashlib.sha256() + for n in range(1, 6): + possible = list(itertools.combinations(range(n), 2)) + for graph in range(1 << len(possible)): + edges = [e for i, e in enumerate(possible) if graph >> i & 1] + counts["graphs"] += 1 + cactus, cycles = literal_cactus(n, edges) + if not cactus: + expect_rejection(n, edges, [1] * len(edges), []) + counts["noncacti"] += 1 + continue + counts["cacti"] += 1 + counts["cycle_blocks"] += len(cycles) + vectors = [(1 << u) | (1 << v) for u, v in edges] + spaces = subspaces(vectors) + counts["target_bases"] += len(spaces) + for prices in ([1] * len(edges), list(range(1, len(edges) + 1)), + [i % 3 for i in range(len(edges))]): + table = purchase_table(edges, prices) + counts["purchase_subsets"] += len(table) + for space in spaces: + targets = basis(space) + expected = min(c for s, c in table if space <= s) + result = optimize(n, edges, prices, targets) + insist(result["cost"] == expected, "subset oracle optimum") + insist(check(n, edges, prices, targets, result), "certificate") + literal_delivery(n, edges, targets, result) + counts["optima"] += 1 + digest.update(json.dumps([n, edges, prices, targets, result], + sort_keys=True).encode()) + counts["corpus_sha256"] = digest.hexdigest() + return counts + + +def expect_rejection(n, edges, costs, targets, result=None): + try: + if result is None: + optimize(n, edges, costs, targets) + else: + check(n, edges, costs, targets, result) + except (ValueError, TypeError, KeyError): + return + raise RuntimeError("invalid input/certificate was accepted") + + +def fixtures_and_controls(): + edges, prices, targets = [(0, 1), (1, 2), (0, 2)], [4, 3, 2], [3] + answer = optimize(3, edges, prices, targets) + insist(answer["cost"] == 4 and answer["selected"] == [0], "partial pilot") + whole = optimize(3, edges, prices, [3, 6]) + insist(whole["cost"] == 5, "whole-span comparison") + for field in ("cost", "rows", "selected", "base_rows", "cycles", + "shifts", "savings", "bridge_bill"): + corrupted = json.loads(json.dumps(answer)) + if field in ("cost", "bridge_bill"): + corrupted[field] += 1 + elif field == "cycles": + corrupted[field][0].pop() + else: + corrupted[field] = [] + expect_rejection(3, edges, prices, targets, corrupted) + bad_shift = dict(whole, rows=[1, 2, 0], shifts=[2], savings=[3]) + expect_rejection(3, edges, prices, [3, 6], bad_shift) + nonoptimal = dict(answer, rows=[0, 1, 1], shifts=[1], savings=[4], + selected=[1, 2], cost=5) + expect_rejection(3, edges, prices, targets, nonoptimal) + invalid = [(0, [], [], []), (129, [], [], []), (2, [(0, 0)], [1], []), + (2, [(1, 0)], [1], []), (2, [(0, 1)] * 2, [1, 2], []), + (2, [(0, 1)], [-1], []), (2, [(0, 1)], [True], []), + (2, [(0, 1)], [], []), (2, [(0, 1)], [1], [1]), + (3, [(0, 1)], [1], [5]), (2, [], [], [4]), + (2, [], [], [True]), (2, [], [], [0] * 129), + (2, [(0, 1)], [10**12 + 1], [])] + for args in invalid: + expect_rejection(*args) + return {"semantic_controls": 10, "input_controls": len(invalid), + "pilot_partial_cost": answer["cost"], "pilot_whole_cost": whole["cost"]} + + +def larger_fixtures(): + total, max_edges, max_targets = 0, 0, 0 + for cycles in range(1, 33): + n = 1 + 2 * cycles + edges = [] + for k in range(cycles): + a, b = 2 * k + 1, 2 * k + 2 + edges.extend([(0, a), (a, b), (0, b)]) + prices = [(i * 11) % 17 for i in range(len(edges))] + targets = [(1 << u) | (1 << v) for u, v in edges[::2]] + targets += [targets[0], 0, 0] + result = optimize(n, edges, prices, targets) + insist(check(n, edges, prices, targets, result), "multi-cycle fixture") + insist(len(result["cycles"]) == cycles, "cycle count fixture") + total += 1 + max_edges, max_targets = max(max_edges, len(edges)), max(max_targets, len(targets)) + n = 128 + edges = [(i, i + 1) for i in range(n - 1)] + targets = [(1 << i) | (1 << (i + 1)) for i in range(n - 1)] + [0] + result = optimize(n, edges, [10**12] * len(edges), targets) + insist(result["cost"] == 127 * 10**12, "maximum-domain forest") + return {"larger_fixtures": total + 1, "max_cycle_fixture_edges": max_edges, + "max_cycle_fixture_targets": max_targets, "max_domain_vertices": n} + + +def main(): + result = exhaustive() + result.update(fixtures_and_controls()) + result.update(larger_fixtures()) + names = ["README.md", "PROOF.md", "TASK.json", "SOURCES.json", "VERIFY.json", + "optimizer.py", "verify.py"] + files = {} + for name in names: + path = Path(__file__).resolve().parent / name + insist(path.stat().st_size <= 262144, "package file capacity") + files[name] = hashlib.sha256(path.read_bytes()).hexdigest() + print(json.dumps({"marker": "PASS_CACTUS_PARTIAL_QUERY", "result": result, + "package_sha256": files}, sort_keys=True)) + + +if __name__ == "__main__": + main()