Skip to content

Fix #202: [Rule] Partition to Knapsack#744

Merged
GiggleLiu merged 4 commits intomainfrom
issue-202
Mar 22, 2026
Merged

Fix #202: [Rule] Partition to Knapsack#744
GiggleLiu merged 4 commits intomainfrom
issue-202

Conversation

@GiggleLiu
Copy link
Copy Markdown
Contributor

Summary

Add the implementation plan for the Partition -> Knapsack reduction pipeline.

Fixes #202

@GiggleLiu
Copy link
Copy Markdown
Contributor Author

Implementation Summary

Changes

  • Added src/rules/partition_knapsack.rs implementing the Partition -> Knapsack reduction with checked u64 -> i64 conversion, identity solution extraction, and a canonical rule example.
  • Registered the new rule in src/rules/mod.rs.
  • Added src/unit_tests/rules/partition_knapsack.rs covering satisfiable closed-loop behavior, target-structure checks, an odd-total unsatisfiable case, and overflow handling.
  • Added a Partition -> Knapsack reduction-rule entry and worked example in docs/paper/reductions.typ driven from the exported canonical example data.

Deviations from Plan

  • None.

Open Questions

  • None.

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 22, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.58%. Comparing base (669f090) to head (689b133).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #744   +/-   ##
=======================================
  Coverage   97.58%   97.58%           
=======================================
  Files         415      417    +2     
  Lines       51632    51699   +67     
=======================================
+ Hits        50386    50453   +67     
  Misses       1246     1246           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@GiggleLiu
Copy link
Copy Markdown
Contributor Author

Agentic Review Report

Structural Check

Structural Review: rule partition_knapsack

Structural Completeness

# Check Status
1 Rule file exists PASS — src/rules/partition_knapsack.rs is present.
2 #[reduction(...)] macro present PASS — #[reduction(overhead = { num_items = "num_elements" })] is present on the rule impl.
3 ReductionResult impl present PASS — ReductionPartitionToKnapsack implements ReductionResult.
4 ReduceTo impl present PASS — impl ReduceTo<Knapsack> for Partition is present.
5 #[cfg(test)] + #[path = ...] test link PASS — the rule file links ../unit_tests/rules/partition_knapsack.rs.
6 Test file exists PASS — src/unit_tests/rules/partition_knapsack.rs is present.
7 Closed-loop test present PASS — test_partition_to_knapsack_closed_loop exercises the round-trip.
8 Registered in rules/mod.rs PASS — the module and canonical example spec are both registered.
9 Canonical rule example registered PASS — partition_knapsack::canonical_rule_example_specs() is included in the aggregated rule example registry consumed by src/example_db/rule_builders.rs.
10 Example-db lookup tests exist PASS — src/unit_tests/example_db.rs covers build_rule_db / find_rule_example, and the suite passes with this rule included.
11 Paper reduction-rule entry PASS — docs/paper/reductions.typ includes a reduction-rule("Partition", "Knapsack", ...) entry with a worked example.
12 Blacklisted autogenerated files committed PASS — none of the prohibited generated artifacts are part of the PR diff.

Build Status

  • make test: PASS
  • make clippy: PASS
  • make paper: PASS

Semantic Review

  • extract_solution correctness: OK — the source and target both use one binary variable per element/item, so the identity mapping is the correct inverse.
  • Overhead accuracy: OK — reduce_to() creates exactly one knapsack item per partition element, so num_items = num_elements is accurate.
  • Example quality: OK — the canonical example is tutorial-style, exports both source and target data, and the witness saturates the target capacity.
  • Paper quality: OK — the proof sketch correctly states the optimization bridge: the knapsack optimum equals S/2 iff the partition instance is satisfiable.
  • Mathematical correctness: OK — manual tracing and CLI checks on both the canonical satisfiable instance and an odd-total unsatisfiable instance matched the claimed semantics.

Issue Compliance

# Check Status
1 Source/target match issue OK — implemented as Partition -> Knapsack.
2 Reduction algorithm matches OK — weights and values both copy the source sizes, and capacity is floor(total_sum / 2).
3 Solution extraction matches OK — the same binary vector is returned, matching the issue’s 1:1 representation note.
4 Correctness preserved OK — satisfiable instances solve to knapsack value S/2; the odd-total unsatisfiable case remains unsatisfied after extraction.
5 Overhead expressions match OK — the implemented overhead is num_items = num_elements, as specified in the issue.
6 Example matches OK — the paper/example-db instance is the worked 3,1,1,2,2,1 example from the issue context.

Summary

  • 12/12 structural checks passed.
  • 6/6 issue compliance checks passed.
  • No structural or semantic issues found.

Quality Check

Quality Review

Design Principles

  • DRY: OK — the rule is intentionally minimal and does not duplicate non-trivial logic beyond the required checked cast helper in src/rules/partition_knapsack.rs.
  • KISS: OK — the implementation mirrors the textbook reduction directly, with no unnecessary abstraction or control-flow complexity.
  • HC/LC: OK — responsibility is cleanly split between the rule, its tests, and the paper entry.

Test Quality

  • Naive test detection: OK
    • The suite checks round-trip correctness, target structure, an unsatisfiable odd-total case, and numeric bound handling.
    • The CLI/user-path check additionally confirmed the reduction on both the canonical satisfiable example and a custom odd-total unsatisfiable example.

Issues

Critical (Must Fix)

  • None.

Important (Should Fix)

  • None.

Minor (Nice to Have)

  • None.

Summary

  • No quality issues found in the PR diff.

Agentic Feature Tests

Feature Test Report: problem-reductions

Project type: CLI + library
Feature tested: Partition -> Knapsack
Profile: ephemeral read-only review run
Use Case: A downstream user wants to discover the new reduction from the catalog, generate the canonical example, reduce it to Knapsack, and solve the resulting bundle from the CLI.
Expected Outcome: The rule is discoverable from pred list / pred show, canonical examples can be created for both source and target sides, and solving the reduced bundle maps back to a correct Partition witness.
Verdict: pass
Critical Issues: 0

Summary

Feature Discoverable Setup Works Expected Outcome Met Doc Quality
Partition -> Knapsack yes yes yes yes good

Per-Feature Details

Partition -> Knapsack

  • What I tried:
    • target/debug/pred list --rules and confirmed Partition -> Knapsack appears.
    • target/debug/pred show Partition and target/debug/pred show Knapsack and confirmed the outgoing/incoming reduction metadata.
    • target/debug/pred create --example Partition and solved the canonical source instance with brute force.
    • target/debug/pred create --example Partition --to Knapsack for the source-side rule example.
    • target/debug/pred create --example Partition --to Knapsack --example-side target for the target-side canonical knapsack instance.
    • target/debug/pred reduce ... --to Knapsack followed by target/debug/pred solve ... --solver brute-force.
    • An additional odd-total custom instance (sizes = [2,4,5]) through the same reduce/solve flow.
  • Discoverability: Good. The new rule is visible in pred list --rules, and pred show Partition documents the outgoing reduction clearly.
  • Setup: Good. Using the locally built CLI binary in the review worktree required no undocumented steps.
  • Functionality: Good. The canonical example reduced to a knapsack instance with matching weights/values and capacity 5, and solving the bundle returned a valid partition witness. The odd-total instance returned evaluation: false on the source side while still exposing the target optimum, which is the intended decision-to-optimization behavior.
  • Expected vs Actual Outcome: Matched.
  • Blocked steps: None.
  • Friction points: None specific to this feature.
  • Doc suggestions: None required from this review run.

Issues Found

  • None.

Suggestions

  • None.

Generated by review-pipeline

@GiggleLiu GiggleLiu merged commit ca7b5b6 into main Mar 22, 2026
5 checks passed
@GiggleLiu GiggleLiu deleted the issue-202 branch April 12, 2026 00:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Rule] Partition to Knapsack

1 participant