Skip to content

Fix #491: Add ExactCoverBy3Sets model#603

Merged
GiggleLiu merged 11 commits intomainfrom
issue-491-exact-cover-by-3-sets
Mar 15, 2026
Merged

Fix #491: Add ExactCoverBy3Sets model#603
GiggleLiu merged 11 commits intomainfrom
issue-491-exact-cover-by-3-sets

Conversation

@zazabap
Copy link
Copy Markdown
Collaborator

@zazabap zazabap commented Mar 12, 2026

Summary

Add the Exact Cover by 3-Sets (X3C) problem as a satisfaction problem model. X3C is a classical NP-complete problem from Garey & Johnson (A3 SP2): given a universe X with |X| = 3q elements and a collection C of 3-element subsets, determine whether C contains a subcollection of q disjoint triples covering every element exactly once.

Fixes #491

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 12, 2026

Codecov Report

❌ Patch coverage is 99.47368% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 96.87%. Comparing base (e56b61f) to head (6ecb8c2).
⚠️ Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
src/models/set/exact_cover_by_3_sets.rs 98.80% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #603      +/-   ##
==========================================
+ Coverage   96.86%   96.87%   +0.01%     
==========================================
  Files         264      266       +2     
  Lines       35196    35384     +188     
==========================================
+ Hits        34091    34277     +186     
- Misses       1105     1107       +2     

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

@zazabap
Copy link
Copy Markdown
Collaborator Author

zazabap commented Mar 12, 2026

Implementation Summary

Changes

  • src/models/set/exact_cover_by_3_sets.rs — New ExactCoverBy3Sets satisfaction problem model with Metric = bool
  • src/models/set/mod.rs — Module registration
  • src/models/mod.rs — Re-export
  • src/lib.rs — Prelude export
  • src/unit_tests/models/set/exact_cover_by_3_sets.rs — 11 unit tests (creation, evaluation, solver, serialization, edge cases, panics)
  • problemreductions-cli/src/dispatch.rs — CLI load/serialize dispatch
  • problemreductions-cli/src/problem_name.rs — Alias "x3c" and "X3C"
  • problemreductions-cli/src/commands/create.rs — CLI pred create X3C support with 3-element validation
  • problemreductions-cli/src/cli.rs — Help table entry
  • docs/paper/reductions.typ — Problem definition entry with display name
  • docs/src/reductions/problem_schemas.json — Regenerated schemas

Deviations from Plan

  • None

Open Questions

  • None

Copy link
Copy Markdown

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 the Exact Cover by 3-Sets (X3C) problem as a new set-based satisfaction model, wiring it into the library exports, CLI (alias + create/serialize/deserialize), and documentation so it can be created/loaded and appears in generated schema/docs.

Changes:

  • Introduce ExactCoverBy3Sets model (schema registration, evaluation logic, variant declaration) plus unit tests.
  • Expose the model via models + prelude, and add CLI support (alias X3C, dispatch load/serialize, and pred create handling).
  • Update docs outputs (problem_schemas.json) and the paper problem definitions to include X3C.

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/models/set/exact_cover_by_3_sets.rs New X3C satisfaction model + schema registration + tests hook
src/models/set/mod.rs Registers and re-exports X3C from the set module
src/models/mod.rs Re-exports X3C at the models module level
src/lib.rs Exposes X3C through the crate prelude
src/unit_tests/models/set/exact_cover_by_3_sets.rs Adds unit tests for construction/evaluate/solver/serde/panics
problemreductions-cli/src/problem_name.rs Adds X3C alias + resolution mapping
problemreductions-cli/src/dispatch.rs Adds load/serialize dispatch arms for X3C
problemreductions-cli/src/commands/create.rs Adds pred create ExactCoverBy3Sets / pred create X3C support
problemreductions-cli/src/cli.rs Documents CLI flags for X3C
docs/src/reductions/problem_schemas.json Adds schema entry for X3C (generated output)
docs/paper/reductions.typ Adds X3C to the paper’s problem-name map and definition section

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

pub struct ExactCoverBy3Sets {
/// Size of the universe (elements are 0..universe_size, must be divisible by 3).
universe_size: usize,
/// Collection of 3-element subsets, each represented as a sorted triple of elements.
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

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

Field doc says each subset is represented as a "sorted triple", but new() does not sort or validate ordering. Either sort/canonicalize each triple on construction (and ideally document whether duplicates are allowed), or update the comment to avoid implying an invariant that isn't enforced.

Suggested change
/// Collection of 3-element subsets, each represented as a sorted triple of elements.
/// Collection of 3-element subsets, each represented as a triple of elements.
/// Note: `new` does not sort or otherwise canonicalize these triples, nor
/// does it enforce uniqueness of elements within a triple.

Copilot uses AI. Check for mistakes.
Comment on lines +74 to +84
for (i, subset) in subsets.iter().enumerate() {
for &elem in subset {
assert!(
elem < universe_size,
"Subset {} contains element {} which is outside universe of size {}",
i,
elem,
universe_size
);
}
}
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

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

ExactCoverBy3Sets::new verifies elements are within the universe, but it does not validate that each subset actually contains 3 distinct elements. Since the model/schema describe "3-element subsets", accepting triples like [0, 0, 1] makes the instance invalid and can lead to confusing always-UNSAT behavior; consider asserting all three entries differ (and/or normalizing/sorting) during construction.

Copilot uses AI. Check for mistakes.
Comment on lines +376 to +401
// ExactCoverBy3Sets
"ExactCoverBy3Sets" => {
let universe = args.universe.ok_or_else(|| {
anyhow::anyhow!(
"ExactCoverBy3Sets requires --universe and --sets\n\n\
Usage: pred create X3C --universe 6 --sets \"0,1,2;3,4,5\""
)
})?;
let sets = parse_sets(args)?;
// Validate each set has exactly 3 elements
for (i, set) in sets.iter().enumerate() {
if set.len() != 3 {
bail!(
"Subset {} has {} elements, but X3C requires exactly 3 elements per subset",
i,
set.len()
);
}
}
let subsets: Vec<[usize; 3]> = sets.into_iter().map(|s| [s[0], s[1], s[2]]).collect();
(
ser(problemreductions::models::set::ExactCoverBy3Sets::new(
universe, subsets,
))?,
resolved_variant.clone(),
)
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

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

This CLI path calls ExactCoverBy3Sets::new(...), which assert!s on invalid inputs (universe not divisible by 3, or subset elements out of range). As written, pred create X3C can panic and crash the CLI; add explicit validation here (e.g., universe % 3 == 0 and elem < universe) and return a structured bail! error instead of relying on library assertions.

Copilot uses AI. Check for mistakes.
GiggleLiu and others added 3 commits March 13, 2026 14:25
- Sort each triple on construction to enforce the "sorted triple" invariant
- Validate that each subset contains 3 distinct elements
- Add CLI validation before calling new() to avoid panics

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@GiggleLiu
Copy link
Copy Markdown
Contributor

Review Pipeline Report

Check Result
Merge with main conflicts resolved (5 files)
Copilot comments 3 fixed (distinct element validation, triple sorting, CLI panic prevention)
Issue/human comments 1 checked, 0 new fixes needed
CI green
Agentic test passed (all functionality works, minor doc gaps noted)
Board review-agentic → In Review (⚠️ board move needs project scope)

🤖 Generated by review-pipeline

…-by-3-sets

# Conflicts:
#	.claude/CLAUDE.md
#	docs/paper/reductions.typ
#	problemreductions-cli/src/dispatch.rs
#	problemreductions-cli/src/problem_name.rs
#	src/lib.rs
#	src/models/mod.rs
@GiggleLiu
Copy link
Copy Markdown
Contributor

Review Pipeline Report

Check Result
Copilot comments 3 fixed
Issue/human comments checked, 0 fixed
Structural review passed after fixes
CI green
Agentic test passed
Board Review pool → Under review → Final review

Agentic test note: the X3C CLI flow worked end-to-end (list, show, create, inspect, evaluate, duplicate-input rejection`). Minor friction remains because README/CLI docs do not yet include a concrete X3C example.

🤖 Generated by review-pipeline

GiggleLiu and others added 3 commits March 15, 2026 16:16
Remove 3 duplicate display-name entries (OptimalLinearArrangement, RuralPostman,
LongestCommonSubsequence) introduced by merge conflict in reductions.typ. Add
missing should_panic test for duplicate-element validation in X3C constructor.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
… helper

Add load-model-example() Typst function (mirrors load-example() for rules)
that loads canonical model examples from generated models.json. Rewrite the
ExactCoverBy3Sets problem-def example to derive all concrete values from JSON
instead of hand-writing them.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
- Migrate 28 problem-def example sections in reductions.typ to use
  load-model-example() instead of hand-written data. All concrete values
  now derived from models.json via Typst expressions.
- Add load-model-example() helper (mirrors load-example() for rules)
- Add concrete X3C example to CLI help text and docs/src/cli.md
- 10 problems without models.json entries remain hand-written:
  GraphPartitioning, OptimalLinearArrangement, BinPacking, Knapsack,
  RuralPostman, SubgraphIsomorphism, LongestCommonSubsequence, SubsetSum,
  MinimumFeedbackArcSet, FlowShopScheduling

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@GiggleLiu GiggleLiu merged commit e9f6077 into main Mar 15, 2026
5 checks passed
@GiggleLiu GiggleLiu mentioned this pull request Mar 15, 2026
@GiggleLiu GiggleLiu deleted the issue-491-exact-cover-by-3-sets branch April 12, 2026 00:53
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.

[Model] ExactCoverBy3Sets(x3c)

3 participants