Fix #403: Add SumOfSquaresPartition model#663
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #663 +/- ##
==========================================
+ Coverage 97.19% 97.21% +0.01%
==========================================
Files 308 310 +2
Lines 40368 40595 +227
==========================================
+ Hits 39237 39463 +226
- Misses 1131 1132 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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>
Implementation SummaryChanges
Deviations from Plan
Open Questions
|
There was a problem hiding this comment.
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
SumOfSquaresPartitionmodel 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.
| #[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, | ||
| } |
| 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()) | ||
| } |
| /// 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, | ||
| } | ||
| } |
docs/paper/reductions.typ
Outdated
| #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$). |
Review Pipeline Report
Remaining issues for final review
🤖 Generated by review-pipeline |
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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