|
7 | 7 |
|
8 | 8 | use crate::registry::{FieldInfo, ProblemSchemaEntry, VariantDimension}; |
9 | 9 | use crate::traits::Problem; |
10 | | -use crate::variant::{KValue, K2, K3, KN}; |
| 10 | +use crate::variant::{KValue, K2, K3}; |
11 | 11 | use serde::{de::Error as _, Deserialize, Deserializer, Serialize}; |
12 | 12 |
|
13 | 13 | use super::{sat::validate_cnf_literals, CNFClause}; |
@@ -53,7 +53,7 @@ inventory::submit! { |
53 | 53 | name: "KSatisfiability", |
54 | 54 | display_name: "K-Satisfiability", |
55 | 55 | aliases: &["KSAT"], |
56 | | - dimensions: &[VariantDimension::new("k", "KN", &["KN", "K2", "K3"])], |
| 56 | + dimensions: &[VariantDimension::new("k", "K3", &["K3", "K2"])], |
57 | 57 | category: crate::registry::ProblemCategory::Formula, |
58 | 58 | module_path: module_path!(), |
59 | 59 | description: "SAT with exactly k literals per clause", |
@@ -124,63 +124,24 @@ impl<K: KValue> KSatisfiability<K> { |
124 | 124 | /// Create a new K-SAT problem. |
125 | 125 | /// |
126 | 126 | /// # Panics |
127 | | - /// Panics if any clause does not have exactly K literals (when K is a |
128 | | - /// concrete value like K2, K3). When K is KN (arbitrary), no clause-length |
129 | | - /// validation is performed. |
| 127 | + /// Panics if any clause does not have exactly K literals. |
130 | 128 | pub fn new(num_vars: usize, clauses: Vec<CNFClause>) -> Self { |
131 | 129 | Self::try_new(num_vars, clauses).unwrap_or_else(|message| panic!("{message}")) |
132 | 130 | } |
133 | 131 |
|
134 | | - /// Create a K-SAT problem after validating its clauses. |
| 132 | + /// Create a K-SAT problem after validating that every clause has exactly |
| 133 | + /// K literals. |
135 | 134 | pub fn try_new( |
136 | 135 | num_vars: usize, |
137 | 136 | clauses: Vec<CNFClause>, |
138 | 137 | ) -> Result<Self, crate::registry::ConstructionError> { |
139 | 138 | validate_cnf_literals(num_vars, &clauses)?; |
140 | | - if let Some(k) = K::K { |
141 | | - for (i, clause) in clauses.iter().enumerate() { |
142 | | - if clause.len() != k { |
143 | | - return Err( |
144 | | - format!("Clause {i} has {} literals, expected {k}", clause.len()).into(), |
145 | | - ); |
146 | | - } |
147 | | - } |
148 | | - } |
149 | | - Ok(Self { |
150 | | - num_vars, |
151 | | - clauses, |
152 | | - _phantom: std::marker::PhantomData, |
153 | | - }) |
154 | | - } |
155 | | - |
156 | | - /// Create a new K-SAT problem allowing clauses with fewer than K literals. |
157 | | - /// |
158 | | - /// This is useful when the reduction algorithm produces clauses with |
159 | | - /// fewer literals (e.g., when allow_less is true in the Julia implementation). |
160 | | - /// |
161 | | - /// # Panics |
162 | | - /// Panics if any clause has more than K literals (when K is a concrete |
163 | | - /// value like K2, K3). When K is KN (arbitrary), no clause-length |
164 | | - /// validation is performed. |
165 | | - pub fn new_allow_less(num_vars: usize, clauses: Vec<CNFClause>) -> Self { |
166 | | - Self::try_new_allow_less(num_vars, clauses).unwrap_or_else(|message| panic!("{message}")) |
167 | | - } |
168 | | - |
169 | | - /// Create a K-SAT problem with shorter clauses after validation. |
170 | | - pub fn try_new_allow_less( |
171 | | - num_vars: usize, |
172 | | - clauses: Vec<CNFClause>, |
173 | | - ) -> Result<Self, crate::registry::ConstructionError> { |
174 | | - validate_cnf_literals(num_vars, &clauses)?; |
175 | | - if let Some(k) = K::K { |
176 | | - for (i, clause) in clauses.iter().enumerate() { |
177 | | - if clause.len() > k { |
178 | | - return Err(format!( |
179 | | - "Clause {i} has {} literals, expected at most {k}", |
180 | | - clause.len() |
181 | | - ) |
182 | | - .into()); |
183 | | - } |
| 139 | + let k = K::K.ok_or("KSatisfiability requires a fixed clause width K")?; |
| 140 | + for (i, clause) in clauses.iter().enumerate() { |
| 141 | + if clause.len() != k { |
| 142 | + return Err( |
| 143 | + format!("Clause {i} has {} literals, expected {k}", clause.len()).into(), |
| 144 | + ); |
184 | 145 | } |
185 | 146 | } |
186 | 147 | Ok(Self { |
@@ -273,13 +234,11 @@ impl<K: KValue> crate::solvers::BruteForceProblem for KSatisfiability<K> { |
273 | 234 | } |
274 | 235 |
|
275 | 236 | crate::declare_variants! { |
276 | | - default KSatisfiability<KN> => "2^num_vars", |
| 237 | + default KSatisfiability<K3> => "1.307^num_vars" aliases ["3SAT"], |
277 | 238 | KSatisfiability<K2> => "num_vars + num_clauses" aliases ["2SAT"], |
278 | | - KSatisfiability<K3> => "1.307^num_vars" aliases ["3SAT"], |
279 | 239 | } |
280 | 240 |
|
281 | 241 | crate::register_brute_force! { |
282 | | - KSatisfiability<KN> decode |_, indices: Vec<usize>| crate::config::config_to_bits(&indices), |
283 | 242 | KSatisfiability<K2> decode |_, indices: Vec<usize>| crate::config::config_to_bits(&indices), |
284 | 243 | KSatisfiability<K3> decode |_, indices: Vec<usize>| crate::config::config_to_bits(&indices), |
285 | 244 | } |
|
0 commit comments