Skip to content

Fix #116: Add Knapsack to QUBO reduction#172

Open
zazabap wants to merge 14 commits intoCodingThrust:mainfrom
zazabap:issue-116-knapsack-to-qubo
Open

Fix #116: Add Knapsack to QUBO reduction#172
zazabap wants to merge 14 commits intoCodingThrust:mainfrom
zazabap:issue-116-knapsack-to-qubo

Conversation

@zazabap
Copy link
Collaborator

@zazabap zazabap commented Mar 4, 2026

Summary

  • Adds implementation plan for Knapsack → QUBO reduction rule
  • Encodes capacity constraint as equality using binary slack variables (⌊log₂C⌋+1 bits)
  • QUBO objective: −∑v_i·x_i + P·(constraint)² with P > ∑v_i

Closes #116

zazabap and others added 13 commits March 4, 2026 19:06
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Re-export Knapsack from models/mod.rs (structural review finding)
- Add tests: zero capacity, single item, greedy-not-optimal adversarial case

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@codecov
Copy link

codecov bot commented Mar 6, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.91%. Comparing base (a88812b) to head (eacb62b).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #172      +/-   ##
==========================================
+ Coverage   96.88%   96.91%   +0.02%     
==========================================
  Files         200      204       +4     
  Lines       27537    27768     +231     
==========================================
+ Hits        26680    26911     +231     
  Misses        857      857              

☔ 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.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new Knapsack problem model and wires in a Knapsack → QUBO reduction (via binary slack variables + quadratic penalty), including tests, CLI support, examples, and documentation/exports.

Changes:

  • Introduces Knapsack as a first-class problem model (schema-registered, exported in prelude, unit-tested).
  • Implements and registers the Knapsack → QUBO reduction rule with closed-loop brute-force tests and an example exporter.
  • Updates CLI alias/dispatch plus regenerates reduction graph + schemas and adds paper documentation for Knapsack and the new rule.

Reviewed changes

Copilot reviewed 19 out of 19 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
tests/suites/examples.rs Registers the new reduction_knapsack_to_qubo example in the examples test suite.
src/unit_tests/rules/knapsack_qubo.rs Adds closed-loop tests validating the reduction against brute-force optimal solutions.
src/unit_tests/models/misc/knapsack.rs Adds unit tests for Knapsack model behavior and serialization.
src/rules/mod.rs Registers the new knapsack_qubo rule module.
src/rules/knapsack_qubo.rs Implements Knapsack → QUBO reduction and solution extraction.
src/models/mod.rs Re-exports Knapsack from models.
src/models/misc/mod.rs Adds knapsack module and re-export in misc category.
src/models/misc/knapsack.rs Implements the Knapsack model, schema registration, evaluation logic, and variant declaration.
src/lib.rs Exposes Knapsack via the crate prelude.
problemreductions-cli/src/problem_name.rs Adds KS / knapsack alias resolution.
problemreductions-cli/src/dispatch.rs Adds load/serialize dispatch for Knapsack.
problemreductions-cli/src/commands/graph.rs Removes stray whitespace (no functional change).
examples/reduction_knapsack_to_qubo.rs Adds an example program exporting the reduction instance + result JSON.
docs/src/reductions/reduction_graph.json Regenerates reduction graph to include Knapsack and Knapsack → QUBO.
docs/src/reductions/problem_schemas.json Regenerates problem schemas to include Knapsack.
docs/plans/2026-03-04-knapsack-to-qubo.md Adds implementation plan document for the reduction.
docs/plans/2026-03-04-knapsack-model.md Adds implementation plan document for the model.
docs/paper/references.bib Adds Knapsack-related bibliography entries.
docs/paper/reductions.typ Adds Knapsack problem definition and the Knapsack → QUBO reduction write-up + example.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Collaborator Author

@zazabap zazabap left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mistakes found.

- Delete plan files per user request
- Remove KS alias per user request (keep only lowercase "knapsack")
- Fix num_slack_bits() to use integer bit-length instead of float log2
  (avoids precision loss for large i64 capacities above 2^53)
- Fix 1i64 << j to 1u64 << j in QUBO slack coefficients
  (avoids negative coefficient from signed overflow)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@zazabap zazabap self-assigned this Mar 9, 2026
@GiggleLiu
Copy link
Contributor

GiggleLiu commented Mar 10, 2026

I have merged #171 , please rebased on the main branch. We'd better not simultaneouly work on models and rules to avoid diverged history.

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] Knapsack to QUBO

3 participants