|
| 1 | +//! Direct exact integer encoding of GroupingBySwapping. |
| 2 | +use crate::models::algebraic::QUBO; |
| 3 | +use crate::models::decision::Decision; |
| 4 | +use crate::models::misc::GroupingBySwapping; |
| 5 | +use crate::rules::qubo_helpers::QuboBuilder; |
| 6 | +use crate::rules::qubo_helpers::{pair_index, tournament_ranks}; |
| 7 | +use crate::rules::traits::validate_target_witness; |
| 8 | +use crate::rules::{ExtractionResult, ReduceTo, ReductionError, ReductionResult}; |
| 9 | +#[derive(Debug, Clone)] |
| 10 | +pub struct ReductionGroupingBySwappingToQUBO { |
| 11 | + target: Decision<QUBO<i64>>, |
| 12 | + source: GroupingBySwapping, |
| 13 | +} |
| 14 | +impl ReductionResult for ReductionGroupingBySwappingToQUBO { |
| 15 | + type Source = GroupingBySwapping; |
| 16 | + type Target = Decision<QUBO<i64>>; |
| 17 | + fn target_problem(&self) -> &Self::Target { |
| 18 | + &self.target |
| 19 | + } |
| 20 | + fn extract_solution( |
| 21 | + &self, |
| 22 | + bits: &Vec<bool>, |
| 23 | + ) -> ExtractionResult<<Self::Source as crate::Problem>::Solution> { |
| 24 | + validate_target_witness( |
| 25 | + &self.target, |
| 26 | + bits, |
| 27 | + |v| v.0, |
| 28 | + "QUBO does not certify a source witness", |
| 29 | + )?; |
| 30 | + let ranks = tournament_ranks(self.source.alphabet_size(), bits)?; |
| 31 | + let mut string = self.source.string().to_vec(); |
| 32 | + let mut swaps = Vec::new(); |
| 33 | + for i in 1..string.len() { |
| 34 | + let mut j = i; |
| 35 | + while j > 0 && ranks[string[j - 1]] > ranks[string[j]] { |
| 36 | + string.swap(j - 1, j); |
| 37 | + swaps.push(j - 1); |
| 38 | + j -= 1; |
| 39 | + } |
| 40 | + } |
| 41 | + if swaps.len() > self.source.budget() { |
| 42 | + return Err(crate::rules::ExtractionError::invalid( |
| 43 | + "inversion count exceeds the swap budget", |
| 44 | + )); |
| 45 | + } |
| 46 | + if !string.is_empty() { |
| 47 | + swaps.resize(self.source.budget(), string.len() - 1); |
| 48 | + } |
| 49 | + Ok(swaps) |
| 50 | + } |
| 51 | +} |
| 52 | +#[crate::aggregate_reduction(identity)] |
| 53 | +impl crate::rules::AggregateReductionResult for ReductionGroupingBySwappingToQUBO {} |
| 54 | +#[crate::reduction(transform = { exact { num_vars = "alphabet_size * (alphabet_size - 1) / 2" }, upper_bound { num_quadratic_terms = "alphabet_size * (alphabet_size - 1) * (alphabet_size - 2) / 2" } })] |
| 55 | +impl ReduceTo<Decision<QUBO<i64>>> for GroupingBySwapping { |
| 56 | + type Result = ReductionGroupingBySwappingToQUBO; |
| 57 | + fn reduce_to(&self) -> Result<Self::Result, ReductionError> { |
| 58 | + type Builder = QuboBuilder<GroupingBySwapping, Decision<QUBO<i64>>>; |
| 59 | + let n = self.alphabet_size(); |
| 60 | + let mut q = Builder::new(Builder::dimension(n, n.saturating_sub(1))? / 2); |
| 61 | + for (p, &i) in self.string().iter().enumerate() { |
| 62 | + for &j in &self.string()[p + 1..] { |
| 63 | + if i < j { |
| 64 | + q.add_constant(1)?; |
| 65 | + let v = pair_index(n, i, j); |
| 66 | + q.add(v, v, -1)?; |
| 67 | + } else if i > j { |
| 68 | + let v = pair_index(n, j, i); |
| 69 | + q.add(v, v, 1)?; |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + let max_swaps = |
| 74 | + Builder::dimension(self.string_len(), self.string_len().saturating_sub(1))? / 2; |
| 75 | + let budget = |
| 76 | + i64::try_from(self.budget().min(max_swaps)).map_err(|_| Builder::overflow())?; |
| 77 | + let max_swaps = i64::try_from(max_swaps).map_err(|_| Builder::overflow())?; |
| 78 | + q.tournament( |
| 79 | + n, |
| 80 | + &[], |
| 81 | + max_swaps.checked_add(1).ok_or_else(Builder::overflow)?, |
| 82 | + )?; |
| 83 | + let (target, constant) = q.finish()?; |
| 84 | + let bound = budget.checked_sub(constant).ok_or_else(Builder::overflow)?; |
| 85 | + Ok(ReductionGroupingBySwappingToQUBO { |
| 86 | + target: Decision::new(target, bound), |
| 87 | + source: self.clone(), |
| 88 | + }) |
| 89 | + } |
| 90 | +} |
| 91 | +#[cfg(feature = "example-db")] |
| 92 | +pub(crate) fn canonical_rule_example_specs() -> Vec<crate::example_db::specs::RuleExampleSpec> { |
| 93 | + vec![crate::example_db::specs::RuleExampleSpec { |
| 94 | + id: "groupingbyswapping_qubo", |
| 95 | + build: || { |
| 96 | + crate::example_db::specs::rule_example_with_witness::<_, Decision<QUBO<i64>>>( |
| 97 | + GroupingBySwapping::new(3, vec![0, 1, 2, 0], 2), |
| 98 | + crate::export::SolutionPair { |
| 99 | + source_config: serde_json::json!([2, 1]), |
| 100 | + target_config: serde_json::json!([true, true, true]), |
| 101 | + }, |
| 102 | + ) |
| 103 | + }, |
| 104 | + }] |
| 105 | +} |
0 commit comments