Skip to content

Fix #403: Add SumOfSquaresPartition model#663

Open
GiggleLiu wants to merge 6 commits intomainfrom
issue-403-sum-of-squares-partition
Open

Fix #403: Add SumOfSquaresPartition model#663
GiggleLiu wants to merge 6 commits intomainfrom
issue-403-sum-of-squares-partition

Conversation

@GiggleLiu
Copy link
Contributor

Summary

Add the SumOfSquaresPartition satisfaction problem (Garey & Johnson SP19, A3). Given a finite set of positive integers, K groups, and bound J, determine whether the set can be partitioned into K groups such that the sum of squared group sums is at most J.

Fixes #403

@codecov
Copy link

codecov bot commented Mar 16, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.95%. Comparing base (d922f80) to head (acb12fd).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #663      +/-   ##
==========================================
+ Coverage   96.94%   96.95%   +0.01%     
==========================================
  Files         273      275       +2     
  Lines       36505    36735     +230     
==========================================
+ Hits        35388    35618     +230     
  Misses       1117     1117              

☔ 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 and others added 2 commits March 16, 2026 18:35
Add the Sum of Squares Partition satisfaction problem (Garey & Johnson SP19).
Given positive integers, K groups, and bound J, determine whether the set can
be partitioned into K groups with sum of squared group sums <= J.

- Model: src/models/misc/sum_of_squares_partition.rs
- Tests: 16 unit tests including paper example, edge cases, serialization
- CLI: pred create SumOfSquaresPartition --sizes --num-groups --bound
- Paper: problem-def entry in reductions.typ
- Registry: declare_variants!, ProblemSchemaEntry, trait_consistency
- Example DB: canonical model example with fixture regeneration

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

Implementation Summary

Changes

  • src/models/misc/sum_of_squares_partition.rs — New model: struct, Problem trait (satisfaction), SatisfactionProblem, declare_variants!, ProblemSchemaEntry, canonical example spec
  • src/unit_tests/models/misc/sum_of_squares_partition.rs — 16 unit tests: creation, evaluation (satisfying/unsatisfying), brute force, serialization, paper example, edge cases, panics
  • src/models/misc/mod.rs — Register module and re-export
  • src/models/mod.rs — Re-export SumOfSquaresPartition
  • src/lib.rs — Add to prelude
  • src/unit_tests/trait_consistency.rs — Add check_problem_trait entry
  • src/example_db/fixtures/examples.json — Regenerated with 33 model examples (was 32)
  • problemreductions-cli/src/cli.rs — Add --num-groups flag, help table entry
  • problemreductions-cli/src/commands/create.rs — Add SumOfSquaresPartition match arm, import, hint, all_data_flags_empty
  • docs/paper/reductions.typ — display-name entry + problem-def with formal definition, background, example
  • docs/src/reductions/problem_schemas.json — Regenerated with new schema
  • docs/src/reductions/reduction_graph.json — Updated

Deviations from Plan

  • None

Open Questions

  • None

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 satisfaction problem model, SumOfSquaresPartition, to the misc models set, and wires it into the library’s exports, CLI instance creation, example database, and generated docs so it can be created/solved like existing problems.

Changes:

  • Introduces SumOfSquaresPartition model with schema registration, variants declaration, and example-db spec.
  • Adds unit tests and trait-consistency coverage for the new model.
  • Integrates the model into CLI pred create, example fixtures, and generated documentation artifacts.

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/models/misc/sum_of_squares_partition.rs New model implementation, schema registration, variant declaration, and example-db spec.
src/models/misc/mod.rs Registers and re-exports the new misc model; adds example-db spec hook.
src/models/mod.rs Re-exports SumOfSquaresPartition from the models root.
src/lib.rs Adds SumOfSquaresPartition to the crate prelude exports.
src/unit_tests/models/misc/sum_of_squares_partition.rs New unit test suite covering construction, evaluation, brute-force solving, and serialization.
src/unit_tests/trait_consistency.rs Adds trait-consistency check for the new model’s dims().
problemreductions-cli/src/cli.rs Adds --num-groups flag to pred create arguments and help text.
problemreductions-cli/src/commands/create.rs Adds pred create SumOfSquaresPartition ... wiring and example command string.
src/example_db/fixtures/examples.json Adds a canonical example instance and satisfying configurations for the example DB.
docs/src/reductions/problem_schemas.json Regenerates schemas JSON to include the new model.
docs/src/reductions/reduction_graph.json Regenerates reduction graph JSON to include the new variant node.
docs/paper/reductions.typ Adds the paper definition block for SumOfSquaresPartition.

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

Comment on lines +55 to +63
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SumOfSquaresPartition {
/// Positive integer sizes for each element.
sizes: Vec<i64>,
/// Number of groups K.
num_groups: usize,
/// Upper bound J on the sum of squared group sums.
bound: i64,
}
Comment on lines +113 to +125
pub fn sum_of_squares(&self, config: &[usize]) -> Option<i64> {
if config.len() != self.sizes.len() {
return None;
}
let mut group_sums = vec![0i64; self.num_groups];
for (i, &g) in config.iter().enumerate() {
if g >= self.num_groups {
return None;
}
group_sums[g] += self.sizes[i];
}
Some(group_sums.iter().map(|&s| s * s).sum())
}
Comment on lines +70 to +87
/// Panics if any size is not positive (must be > 0), if `num_groups` is 0,
/// or if `num_groups` exceeds the number of elements.
pub fn new(sizes: Vec<i64>, num_groups: usize, bound: i64) -> Self {
assert!(
sizes.iter().all(|&s| s > 0),
"All sizes must be positive (> 0)"
);
assert!(num_groups > 0, "Number of groups must be positive");
assert!(
num_groups <= sizes.len(),
"Number of groups must not exceed number of elements"
);
Self {
sizes,
num_groups,
bound,
}
}
#problem-def("SumOfSquaresPartition")[
Given a finite set $A = {a_0, dots, a_(n-1)}$ with sizes $s(a_i) in ZZ^+$, a positive integer $K lt.eq |A|$ (number of groups), and a positive integer $J$ (bound), determine whether $A$ can be partitioned into $K$ disjoint sets $A_1, dots, A_K$ such that $sum_(i=1)^K (sum_(a in A_i) s(a))^2 lt.eq J$.
][
Problem SP19 in Garey and Johnson @garey1979. NP-complete in the strong sense, so no pseudo-polynomial time algorithm exists unless $P = "NP"$. For fixed $K$, a dynamic-programming algorithm runs in $O(n S^(K-1))$ pseudo-polynomial time, where $S = sum s(a)$. The problem remains NP-complete when the exponent 2 is replaced by any fixed rational $alpha > 1$. #footnote[No algorithm improving on brute-force $O(K^n)$ enumeration is known for the general case.] The squared objective penalizes imbalanced partitions, connecting it to variance minimization, load balancing, and $k$-means clustering. Sum of Squares Partition generalizes Partition ($K = 2$, $J = S^2 slash 2$).
@GiggleLiu
Copy link
Contributor Author

Review Pipeline Report

Check Result
Copilot comments 4 fixed
Issue/human comments 3 checked, 0 fixed
Structural review passed
CI pending (no runs attached yet for f93bae2f)
Agentic test passed
Needs human decision none
Board Review pool → Under review

Remaining issues for final review

  • GitHub has not attached any CI runs to head commit f93bae2fbb4a9b4a894854d9843e357300490526 yet. This was not auto-fixable from the worktree, and GraphQL polling hit the authenticated rate limit before any checks appeared. Recommended maintainer action: re-check CI once runs attach, or manually requeue if GitHub continues to leave the commit without checks.

🤖 Generated by review-pipeline

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] SumOfSquaresPartition

2 participants