problemreductions/rules/
minimumweightdecoding_ilp.rs1use crate::models::algebraic::MinimumWeightDecoding;
19use crate::models::algebraic::{LinearConstraint, ObjectiveSense, ILP};
20use crate::reduction;
21use crate::rules::traits::{ReduceTo, ReductionResult};
22
23#[derive(Debug, Clone)]
29pub struct ReductionMinimumWeightDecodingToILP {
30 target: ILP<i32>,
31 num_cols: usize,
32}
33
34impl ReductionResult for ReductionMinimumWeightDecodingToILP {
35 type Source = MinimumWeightDecoding;
36 type Target = ILP<i32>;
37
38 fn target_problem(&self) -> &ILP<i32> {
39 &self.target
40 }
41
42 fn extract_solution(&self, target_solution: &[usize]) -> Vec<usize> {
44 target_solution[..self.num_cols].to_vec()
45 }
46}
47
48#[reduction(
49 overhead = {
50 num_vars = "num_cols + num_rows",
51 num_constraints = "num_rows + num_cols",
52 }
53)]
54impl ReduceTo<ILP<i32>> for MinimumWeightDecoding {
55 type Result = ReductionMinimumWeightDecodingToILP;
56
57 fn reduce_to(&self) -> Self::Result {
58 let m = self.num_cols();
59 let n = self.num_rows();
60 let num_vars = m + n;
61
62 let x = |j: usize| j; let k = |i: usize| m + i; let mut constraints = Vec::new();
66
67 for i in 0..n {
69 let mut terms: Vec<(usize, f64)> = Vec::new();
70 for j in 0..m {
71 if self.matrix()[i][j] {
72 terms.push((x(j), 1.0));
73 }
74 }
75 terms.push((k(i), -2.0));
76 let rhs = if self.target()[i] { 1.0 } else { 0.0 };
77 constraints.push(LinearConstraint::eq(terms, rhs));
78 }
79
80 for j in 0..m {
82 constraints.push(LinearConstraint::le(vec![(x(j), 1.0)], 1.0));
83 }
84
85 let objective: Vec<(usize, f64)> = (0..m).map(|j| (x(j), 1.0)).collect();
87
88 ReductionMinimumWeightDecodingToILP {
89 target: ILP::new(num_vars, constraints, objective, ObjectiveSense::Minimize),
90 num_cols: m,
91 }
92 }
93}
94
95#[cfg(feature = "example-db")]
96pub(crate) fn canonical_rule_example_specs() -> Vec<crate::example_db::specs::RuleExampleSpec> {
97 vec![crate::example_db::specs::RuleExampleSpec {
98 id: "minimumweightdecoding_to_ilp",
99 build: || {
100 let source = MinimumWeightDecoding::new(
101 vec![
102 vec![true, false, true, true],
103 vec![false, true, true, false],
104 vec![true, true, false, true],
105 ],
106 vec![true, true, false],
107 );
108 crate::example_db::specs::rule_example_via_ilp::<_, i32>(source)
109 },
110 }]
111}
112
113#[cfg(test)]
114#[path = "../unit_tests/rules/minimumweightdecoding_ilp.rs"]
115mod tests;