problemreductions/rules/
maximumdomaticnumber_ilp.rs1use crate::models::algebraic::{LinearConstraint, ObjectiveSense, ILP};
12use crate::models::graph::MaximumDomaticNumber;
13use crate::reduction;
14use crate::rules::traits::{ReduceTo, ReductionResult};
15use crate::topology::{Graph, SimpleGraph};
16
17#[derive(Debug, Clone)]
23pub struct ReductionDomaticNumberToILP {
24 target: ILP<bool>,
25 n: usize,
26}
27
28impl ReductionResult for ReductionDomaticNumberToILP {
29 type Source = MaximumDomaticNumber<SimpleGraph>;
30 type Target = ILP<bool>;
31
32 fn target_problem(&self) -> &ILP<bool> {
33 &self.target
34 }
35
36 fn extract_solution(
40 &self,
41 target_solution: &<Self::Target as crate::traits::Problem>::Solution,
42 ) -> crate::rules::ExtractionResult<<Self::Source as crate::traits::Problem>::Solution> {
43 crate::rules::traits::validate_target_solution(self.target_problem(), target_solution)?;
44
45 Ok({
46 let n = self.n;
47 let mut config = vec![0; n];
48 for v in 0..n {
49 for i in 0..n {
50 if target_solution[v * n + i] == 1 {
51 config[v] = i;
52 break;
53 }
54 }
55 }
56 config
57 })
58 }
59}
60
61#[reduction(transform = {
62 exact {
63 max_constraint_magnitude_bits = "1",
64 num_vars = "num_vertices * num_vertices + num_vertices",
65 num_constraints = "num_vertices + num_vertices * num_vertices + num_vertices * num_vertices",
66 },
67 upper_bound {
68 num_nonzeros = "(num_vertices * num_vertices + num_vertices) * (num_vertices + num_vertices * num_vertices + num_vertices * num_vertices)",
69 },
70})]
71impl ReduceTo<ILP<bool>> for MaximumDomaticNumber<SimpleGraph> {
72 type Result = ReductionDomaticNumberToILP;
73
74 fn reduce_to(&self) -> Result<Self::Result, crate::rules::ReductionError> {
75 let n = self.graph().num_vertices();
76 let num_vars = n * n + n;
77 let mut constraints = Vec::new();
78
79 for v in 0..n {
81 let terms: Vec<(usize, i64)> = (0..n).map(|i| (v * n + i, 1)).collect();
82 constraints.push(LinearConstraint::eq(terms, 1));
83 }
84
85 for v in 0..n {
88 let mut neighborhood = self.graph().neighbors(v);
89 neighborhood.push(v);
90 neighborhood.sort_unstable();
91 neighborhood.dedup();
92 for i in 0..n {
93 let mut terms: Vec<(usize, i64)> =
94 neighborhood.iter().map(|&u| (u * n + i, 1)).collect();
95 terms.push((n * n + i, -1));
97 constraints.push(LinearConstraint::ge(terms, 0));
98 }
99 }
100
101 for v in 0..n {
105 for i in 0..n {
106 constraints.push(LinearConstraint::le(
107 vec![(v * n + i, 1), (n * n + i, -1)],
108 0,
109 ));
110 }
111 }
112
113 let objective: Vec<(usize, i64)> = (0..n).map(|i| (n * n + i, 1)).collect();
115
116 let target = ILP::new(num_vars, constraints, objective, ObjectiveSense::Maximize)
117 .map_err(Self::target_construction)?;
118
119 Ok(ReductionDomaticNumberToILP { target, n })
120 }
121}
122
123#[cfg(feature = "example-db")]
124pub(crate) fn canonical_rule_example_specs() -> Vec<crate::example_db::specs::RuleExampleSpec> {
125 vec![crate::example_db::specs::RuleExampleSpec {
126 id: "maximumdomaticnumber_to_ilp",
127 build: || {
128 let source = MaximumDomaticNumber::new(SimpleGraph::new(3, vec![(0, 1), (1, 2)]));
130 crate::example_db::specs::rule_example_via_ilp::<_, bool>(source)
131 },
132 }]
133}
134
135#[cfg(test)]
136#[path = "../unit_tests/rules/maximumdomaticnumber_ilp.rs"]
137mod tests;