problemreductions/rules/
minimumdominatingset_ilp.rs1use crate::models::algebraic::{LinearConstraint, ObjectiveSense, ILP};
10use crate::models::graph::MinimumDominatingSet;
11use crate::reduction;
12use crate::rules::traits::{ReduceTo, ReductionResult};
13use crate::topology::{Graph, SimpleGraph};
14
15#[derive(Debug, Clone)]
23pub struct ReductionDSToILP {
24 target: ILP<bool>,
25}
26
27impl ReductionResult for ReductionDSToILP {
28 type Source = MinimumDominatingSet<SimpleGraph, i32>;
29 type Target = ILP<bool>;
30
31 fn target_problem(&self) -> &ILP<bool> {
32 &self.target
33 }
34
35 fn extract_solution(&self, target_solution: &[usize]) -> Vec<usize> {
40 target_solution.to_vec()
41 }
42}
43
44#[reduction(
45 overhead = {
46 num_vars = "num_vertices",
47 num_constraints = "num_vertices",
48 }
49)]
50impl ReduceTo<ILP<bool>> for MinimumDominatingSet<SimpleGraph, i32> {
51 type Result = ReductionDSToILP;
52
53 fn reduce_to(&self) -> Self::Result {
54 let num_vars = self.graph().num_vertices();
55
56 let constraints: Vec<LinearConstraint> = (0..num_vars)
59 .map(|v| {
60 let mut terms: Vec<(usize, f64)> = vec![(v, 1.0)];
62 for neighbor in self.neighbors(v) {
63 terms.push((neighbor, 1.0));
64 }
65 LinearConstraint::ge(terms, 1.0)
66 })
67 .collect();
68
69 let objective: Vec<(usize, f64)> = self
71 .weights()
72 .iter()
73 .enumerate()
74 .map(|(i, &w)| (i, w as f64))
75 .collect();
76
77 let target = ILP::new(num_vars, constraints, objective, ObjectiveSense::Minimize);
78
79 ReductionDSToILP { target }
80 }
81}
82
83#[cfg(feature = "example-db")]
84pub(crate) fn canonical_rule_example_specs() -> Vec<crate::example_db::specs::RuleExampleSpec> {
85 vec![crate::example_db::specs::RuleExampleSpec {
86 id: "minimumdominatingset_to_ilp",
87 build: || {
88 let (n, edges) = crate::topology::small_graphs::petersen();
89 let source = MinimumDominatingSet::new(SimpleGraph::new(n, edges), vec![1i32; 10]);
90 crate::example_db::specs::rule_example_via_ilp::<_, bool>(source)
91 },
92 }]
93}
94
95#[cfg(test)]
96#[path = "../unit_tests/rules/minimumdominatingset_ilp.rs"]
97mod tests;