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, i64>;
29 type Target = ILP<bool>;
30
31 fn target_problem(&self) -> &ILP<bool> {
32 &self.target
33 }
34
35 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(target_solution.iter().map(|&value| value == 1).collect())
46 }
47}
48
49#[reduction(
50 transform = exact {
51 num_vars = "num_vertices",
52 num_constraints = "num_vertices",
53 },
54 unavailable = {
55 num_nonzeros = "the exact target parameter is not represented by this reduction's symbolic transform",
56 }
57)]
58impl ReduceTo<ILP<bool>> for MinimumDominatingSet<SimpleGraph, i64> {
59 type Result = ReductionDSToILP;
60
61 fn reduce_to(&self) -> Result<Self::Result, crate::rules::ReductionError> {
62 let num_vars = self.graph().num_vertices();
63
64 let constraints: Vec<LinearConstraint> = (0..num_vars)
67 .map(|v| {
68 let mut terms: Vec<(usize, i64)> = vec![(v, 1)];
70 for neighbor in self.neighbors(v) {
71 terms.push((neighbor, 1));
72 }
73 LinearConstraint::ge(terms, 1)
74 })
75 .collect();
76
77 let objective: Vec<(usize, i64)> = self
79 .weights()
80 .iter()
81 .enumerate()
82 .map(|(vertex, &weight)| (vertex, weight))
83 .collect();
84
85 let target = ILP::new(num_vars, constraints, objective, ObjectiveSense::Minimize)
86 .map_err(Self::target_construction)?;
87
88 Ok(ReductionDSToILP { target })
89 }
90}
91
92#[cfg(feature = "example-db")]
93pub(crate) fn canonical_rule_example_specs() -> Vec<crate::example_db::specs::RuleExampleSpec> {
94 vec![crate::example_db::specs::RuleExampleSpec {
95 id: "minimumdominatingset_to_ilp",
96 build: || {
97 let (n, edges) = crate::topology::small_graphs::petersen();
98 let source = MinimumDominatingSet::new(SimpleGraph::new(n, edges), vec![1i64; 10]);
99 crate::example_db::specs::rule_example_via_ilp::<_, bool>(source)
100 },
101 }]
102}
103
104#[cfg(test)]
105#[path = "../unit_tests/rules/minimumdominatingset_ilp.rs"]
106mod tests;