problemreductions/models/misc/
subset_sum.rs1use crate::registry::{FieldInfo, ProblemSchemaEntry};
11use crate::traits::Problem;
12use num_bigint::{BigUint, ToBigUint};
13use num_traits::Zero;
14use serde::{Deserialize, Serialize};
15
16inventory::submit! {
17 ProblemSchemaEntry {
18 name: "SubsetSum",
19 display_name: "Subset Sum",
20 aliases: &[],
21 dimensions: &[],
22 module_path: module_path!(),
23 description: "Find a subset of positive integers that sums to exactly a target value",
24 fields: &[
25 FieldInfo { name: "sizes", type_name: "Vec<BigUint>", description: "Positive integer sizes s(a) for each element" },
26 FieldInfo { name: "target", type_name: "BigUint", description: "Target sum B" },
27 ],
28 }
29}
30
31#[derive(Debug, Clone, Serialize, Deserialize)]
53pub struct SubsetSum {
54 #[serde(with = "super::biguint_serde::decimal_biguint_vec")]
55 sizes: Vec<BigUint>,
56 #[serde(with = "super::biguint_serde::decimal_biguint")]
57 target: BigUint,
58}
59
60impl SubsetSum {
61 pub fn new<S, T>(sizes: Vec<S>, target: T) -> Self
67 where
68 S: ToBigUint,
69 T: ToBigUint,
70 {
71 let sizes: Vec<BigUint> = sizes
72 .into_iter()
73 .map(|s| s.to_biguint().expect("All sizes must be positive (> 0)"))
74 .collect();
75 assert!(
76 sizes.iter().all(|s| !s.is_zero()),
77 "All sizes must be positive (> 0)"
78 );
79 let target = target
80 .to_biguint()
81 .expect("SubsetSum target must be nonnegative");
82 Self { sizes, target }
83 }
84
85 pub(crate) fn new_unchecked(sizes: Vec<BigUint>, target: BigUint) -> Self {
90 Self { sizes, target }
91 }
92
93 pub fn sizes(&self) -> &[BigUint] {
95 &self.sizes
96 }
97
98 pub fn target(&self) -> &BigUint {
100 &self.target
101 }
102
103 pub fn num_elements(&self) -> usize {
105 self.sizes.len()
106 }
107}
108
109impl Problem for SubsetSum {
110 const NAME: &'static str = "SubsetSum";
111 type Value = crate::types::Or;
112
113 fn variant() -> Vec<(&'static str, &'static str)> {
114 crate::variant_params![]
115 }
116
117 fn dims(&self) -> Vec<usize> {
118 vec![2; self.num_elements()]
119 }
120
121 fn evaluate(&self, config: &[usize]) -> crate::types::Or {
122 crate::types::Or({
123 if config.len() != self.num_elements() {
124 return crate::types::Or(false);
125 }
126 if config.iter().any(|&v| v >= 2) {
127 return crate::types::Or(false);
128 }
129 let mut total = BigUint::zero();
130 for (i, &x) in config.iter().enumerate() {
131 if x == 1 {
132 total += &self.sizes[i];
133 }
134 }
135 total == self.target
136 })
137 }
138}
139
140crate::declare_variants! {
141 default SubsetSum => "2^(num_elements / 2)",
142}
143
144#[cfg(feature = "example-db")]
145pub(crate) fn canonical_model_example_specs() -> Vec<crate::example_db::specs::ModelExampleSpec> {
146 vec![crate::example_db::specs::ModelExampleSpec {
148 id: "subset_sum",
149 instance: Box::new(SubsetSum::new(vec![3u32, 7, 1, 8, 2, 4], 11u32)),
150 optimal_config: vec![1, 0, 0, 1, 0, 0],
151 optimal_value: serde_json::json!(true),
152 }]
153}
154
155#[cfg(test)]
156#[path = "../../unit_tests/models/misc/subset_sum.rs"]
157mod tests;