Skip to main content

problemreductions/rules/
ilp_bool_ilp_i64.rs

1//! Natural embedding of binary ILP into bounded integer ILP.
2//!
3//! The stored `[0, 1]` bounds, constraints, and objective carry over unchanged.
4//!
5//! This same-name variant reduction preserves the witness representation.
6
7use crate::models::algebraic::Bounded;
8use crate::models::algebraic::ILP;
9use crate::reduction;
10use crate::rules::traits::{ReduceTo, ReductionResult};
11
12#[derive(Debug, Clone)]
13pub struct ReductionBinaryILPToIntILP {
14    target: ILP<i64, i64, Bounded>,
15}
16
17impl ReductionResult for ReductionBinaryILPToIntILP {
18    type Source = ILP<bool>;
19    type Target = ILP<i64, i64, Bounded>;
20
21    fn target_problem(&self) -> &ILP<i64, i64, Bounded> {
22        &self.target
23    }
24
25    fn extract_solution(
26        &self,
27        target_solution: &<Self::Target as crate::traits::Problem>::Solution,
28    ) -> crate::rules::ExtractionResult<<Self::Source as crate::traits::Problem>::Solution> {
29        crate::rules::traits::validate_target_solution(self.target_problem(), target_solution)?;
30
31        Ok(target_solution.to_vec())
32    }
33}
34
35#[reduction(
36    transform = exact {
37        max_constraint_magnitude_bits = "max_constraint_magnitude_bits",
38        num_vars = "num_vars",
39        num_constraints = "num_constraints",
40        num_nonzeros = "num_nonzeros",
41    },)]
42impl ReduceTo<ILP<i64, i64, Bounded>> for ILP<bool> {
43    type Result = ReductionBinaryILPToIntILP;
44
45    fn reduce_to(&self) -> Result<Self::Result, crate::rules::ReductionError> {
46        Ok(ReductionBinaryILPToIntILP {
47            target: ILP::<i64, i64, Bounded>::with_variables(
48                self.variables().to_vec(),
49                self.constraints().to_vec(),
50                self.objective().to_vec(),
51                self.sense(),
52            )
53            .map_err(<Self as ReduceTo<ILP<i64, i64, Bounded>>>::target_construction)?,
54        })
55    }
56}
57
58#[cfg(test)]
59#[path = "../unit_tests/rules/ilp_bool_ilp_i64.rs"]
60mod tests;