|
| 1 | +//! Reduction from Set Splitting to NAE-Satisfiability. |
| 2 | +//! |
| 3 | +//! Create one Boolean variable for each universe element. Each source subset |
| 4 | +//! becomes one NAE clause over the distinct elements it contains. A subset |
| 5 | +//! whose entries all equal a single element cannot be split, so emitting the |
| 6 | +//! clause `(x_u, x_u)` makes the target an exact mirror of the source. |
| 7 | +
|
| 8 | +use crate::models::formula::{CNFClause, NAESatisfiability}; |
| 9 | +use crate::models::set::SetSplitting; |
| 10 | +use crate::reduction; |
| 11 | +use crate::rules::traits::{ReduceTo, ReductionResult}; |
| 12 | + |
| 13 | +#[derive(Debug, Clone)] |
| 14 | +pub struct ReductionSetSplittingToNAESAT { |
| 15 | + target: NAESatisfiability, |
| 16 | + source_universe_size: usize, |
| 17 | +} |
| 18 | + |
| 19 | +impl ReductionResult for ReductionSetSplittingToNAESAT { |
| 20 | + type Source = SetSplitting; |
| 21 | + type Target = NAESatisfiability; |
| 22 | + |
| 23 | + fn target_problem(&self) -> &NAESatisfiability { |
| 24 | + &self.target |
| 25 | + } |
| 26 | + |
| 27 | + fn extract_solution( |
| 28 | + &self, |
| 29 | + target_solution: &<Self::Target as crate::traits::Problem>::Solution, |
| 30 | + ) -> crate::rules::ExtractionResult<<Self::Source as crate::traits::Problem>::Solution> { |
| 31 | + crate::rules::traits::validate_target_witness( |
| 32 | + self.target_problem(), |
| 33 | + target_solution, |
| 34 | + |value| value.0, |
| 35 | + "target witness does not satisfy the target problem", |
| 36 | + )?; |
| 37 | + |
| 38 | + Ok(target_solution[..self.source_universe_size].to_vec()) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +#[crate::aggregate_reduction(identity)] |
| 43 | +impl crate::rules::AggregateReductionResult for ReductionSetSplittingToNAESAT {} |
| 44 | + |
| 45 | +#[reduction(transform = { |
| 46 | + exact { |
| 47 | + num_vars = "universe_size", |
| 48 | + num_clauses = "num_subsets", |
| 49 | + }, |
| 50 | + upper_bound { |
| 51 | + num_clause_variables = "universe_size * num_subsets", |
| 52 | + num_literals = "2 * universe_size * num_subsets", |
| 53 | + num_literal_pairs = "2 * universe_size * num_subsets * (2 * universe_size * num_subsets + 1)", |
| 54 | + }, |
| 55 | +})] |
| 56 | +impl ReduceTo<NAESatisfiability> for SetSplitting { |
| 57 | + type Result = ReductionSetSplittingToNAESAT; |
| 58 | + |
| 59 | + fn reduce_to(&self) -> Result<Self::Result, crate::rules::ReductionError> { |
| 60 | + let universe_size = self.universe_size(); |
| 61 | + crate::rules::sat_helpers::SatVariableAllocator::new( |
| 62 | + "SetSplitting -> NAESatisfiability", |
| 63 | + universe_size, |
| 64 | + ) |
| 65 | + .map_err(<Self as ReduceTo<NAESatisfiability>>::target_construction)?; |
| 66 | + let mut clauses = Vec::with_capacity(self.num_subsets()); |
| 67 | + |
| 68 | + for subset in self.subsets() { |
| 69 | + let mut distinct: Vec<i64> = Vec::new(); |
| 70 | + for &element in subset { |
| 71 | + // SetSplitting validates element < universe_size; the allocator |
| 72 | + // above checks that every resulting SAT variable fits i64. |
| 73 | + let literal = i64::try_from(element + 1).expect("validated SAT variable"); |
| 74 | + if !distinct.contains(&literal) { |
| 75 | + distinct.push(literal); |
| 76 | + } |
| 77 | + } |
| 78 | + // Source construction rejects empty subsets, but permits repeated |
| 79 | + // occurrences of a single element. Such a subset is unsplittable. |
| 80 | + if distinct.len() == 1 { |
| 81 | + distinct.push(distinct[0]); |
| 82 | + } |
| 83 | + clauses.push(CNFClause::new(distinct)); |
| 84 | + } |
| 85 | + let target = NAESatisfiability::try_new(universe_size, clauses) |
| 86 | + .map_err(<Self as ReduceTo<NAESatisfiability>>::target_construction)?; |
| 87 | + Ok(ReductionSetSplittingToNAESAT { |
| 88 | + target, |
| 89 | + source_universe_size: universe_size, |
| 90 | + }) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +#[cfg(feature = "example-db")] |
| 95 | +pub(crate) fn canonical_rule_example_specs() -> Vec<crate::example_db::specs::RuleExampleSpec> { |
| 96 | + use crate::export::SolutionPair; |
| 97 | + |
| 98 | + vec![crate::example_db::specs::RuleExampleSpec { |
| 99 | + id: "setsplitting_to_naesatisfiability", |
| 100 | + build: || { |
| 101 | + crate::example_db::specs::rule_example_with_witness::<_, NAESatisfiability>( |
| 102 | + SetSplitting::new(4, vec![vec![0, 1], vec![1, 2, 3], vec![0, 2, 3]]), |
| 103 | + SolutionPair { |
| 104 | + source_config: serde_json::json!(vec![true, false, true, false]), |
| 105 | + target_config: serde_json::json!(vec![true, false, true, false]), |
| 106 | + }, |
| 107 | + ) |
| 108 | + }, |
| 109 | + }] |
| 110 | +} |
| 111 | + |
| 112 | +#[cfg(test)] |
| 113 | +#[path = "../unit_tests/rules/setsplitting_naesatisfiability.rs"] |
| 114 | +mod tests; |
0 commit comments