problemreductions/rules/
minimumfeedbackvertexset_ilp.rs1use crate::models::algebraic::{LinearConstraint, ObjectiveSense, ILP};
10use crate::models::graph::MinimumFeedbackVertexSet;
11use crate::reduction;
12use crate::rules::traits::{ReduceTo, ReductionResult};
13
14#[derive(Debug, Clone)]
23pub struct ReductionMFVSToILP {
24 target: ILP<i64>,
25 num_vertices: usize,
27}
28
29impl ReductionResult for ReductionMFVSToILP {
30 type Source = MinimumFeedbackVertexSet<i64>;
31 type Target = ILP<i64>;
32
33 fn target_problem(&self) -> &ILP<i64> {
34 &self.target
35 }
36
37 fn extract_solution(
42 &self,
43 target_solution: &<Self::Target as crate::traits::Problem>::Solution,
44 ) -> crate::rules::ExtractionResult<<Self::Source as crate::traits::Problem>::Solution> {
45 crate::rules::traits::validate_target_solution(self.target_problem(), target_solution)?;
46
47 Ok(target_solution[..self.num_vertices]
48 .iter()
49 .map(|&value| value == 1)
50 .collect())
51 }
52}
53
54#[reduction(
55 transform = exact {
56 num_vars = "2 * num_vertices",
57 num_constraints = "num_arcs + 2 * num_vertices",
58 },
59 unavailable = {
60 num_nonzeros = "the exact target parameter is not represented by this reduction's symbolic transform",
61 }
62)]
63impl ReduceTo<ILP<i64>> for MinimumFeedbackVertexSet<i64> {
64 type Result = ReductionMFVSToILP;
65
66 fn reduce_to(&self) -> Result<Self::Result, crate::rules::ReductionError> {
67 let n = self.graph().num_vertices();
68 let arcs = self.graph().arcs();
69 let num_vars = 2 * n;
70
71 let mut constraints = Vec::new();
76 let n_i64 = <Self as ReduceTo<ILP<i64>>>::exact_i64(n, "encoding the topological order")?;
77
78 for i in 0..n {
80 constraints.push(LinearConstraint::le(vec![(i, 1)], 1));
81 }
82
83 for i in 0..n {
85 constraints.push(LinearConstraint::le(vec![(n + i, 1)], n_i64 - 1));
86 }
87
88 for &(u, v) in &arcs {
92 let terms = vec![
93 (n + v, 1), (n + u, -1), (u, n_i64), (v, n_i64), ];
98 constraints.push(LinearConstraint::ge(terms, 1));
99 }
100
101 let objective: Vec<(usize, i64)> = self
103 .weights()
104 .iter()
105 .enumerate()
106 .map(|(vertex, &weight)| (vertex, weight))
107 .collect();
108
109 let target = ILP::new(num_vars, constraints, objective, ObjectiveSense::Minimize)
110 .map_err(<Self as ReduceTo<ILP<i64>>>::target_construction)?;
111
112 Ok(ReductionMFVSToILP {
113 target,
114 num_vertices: n,
115 })
116 }
117}
118
119#[cfg(feature = "example-db")]
120pub(crate) fn canonical_rule_example_specs() -> Vec<crate::example_db::specs::RuleExampleSpec> {
121 use crate::topology::DirectedGraph;
122
123 vec![crate::example_db::specs::RuleExampleSpec {
124 id: "minimumfeedbackvertexset_to_ilp",
125 build: || {
126 let graph = DirectedGraph::new(3, vec![(0, 1), (1, 2), (2, 0)]);
128 let source = MinimumFeedbackVertexSet::new(graph, vec![1i64; 3]);
129 crate::example_db::specs::rule_example_via_ilp::<_, i64>(source)
130 },
131 }]
132}
133
134#[cfg(test)]
135#[path = "../unit_tests/rules/minimumfeedbackvertexset_ilp.rs"]
136mod tests;