problemreductions/rules/
partitionintotriangles_ilp.rs1use crate::models::algebraic::{LinearConstraint, ObjectiveSense, ILP};
13use crate::models::graph::PartitionIntoTriangles;
14use crate::reduction;
15use crate::rules::traits::{ReduceTo, ReductionResult};
16use crate::topology::{Graph, SimpleGraph};
17
18#[derive(Debug, Clone)]
25pub struct ReductionPITToILP {
26 target: ILP<bool>,
27 num_vertices: usize,
28 num_groups: usize,
29}
30
31impl ReductionResult for ReductionPITToILP {
32 type Source = PartitionIntoTriangles<SimpleGraph>;
33 type Target = ILP<bool>;
34
35 fn target_problem(&self) -> &ILP<bool> {
36 &self.target
37 }
38
39 fn extract_solution(&self, target_solution: &[usize]) -> Vec<usize> {
41 let num_groups = self.num_groups;
42 (0..self.num_vertices)
43 .map(|v| {
44 (0..num_groups)
45 .find(|&g| {
46 let idx = v * num_groups + g;
47 idx < target_solution.len() && target_solution[idx] == 1
48 })
49 .unwrap_or(0)
50 })
51 .collect()
52 }
53}
54
55#[reduction(
56 overhead = {
57 num_vars = "num_vertices^2",
58 num_constraints = "num_vertices^2 * num_vertices",
59 }
60)]
61impl ReduceTo<ILP<bool>> for PartitionIntoTriangles<SimpleGraph> {
62 type Result = ReductionPITToILP;
63
64 fn reduce_to(&self) -> Self::Result {
65 let num_vertices = self.num_vertices();
66 let q = num_vertices / 3; let num_vars = num_vertices * q;
68
69 let mut constraints = Vec::new();
70
71 for v in 0..num_vertices {
73 let terms: Vec<(usize, f64)> = (0..q).map(|g| (v * q + g, 1.0)).collect();
74 constraints.push(LinearConstraint::eq(terms, 1.0));
75 }
76
77 for g in 0..q {
79 let terms: Vec<(usize, f64)> = (0..num_vertices).map(|v| (v * q + g, 1.0)).collect();
80 constraints.push(LinearConstraint::eq(terms, 3.0));
81 }
82
83 let graph = self.graph();
86 for g in 0..q {
87 for u in 0..num_vertices {
88 for v in (u + 1)..num_vertices {
89 if !graph.has_edge(u, v) {
90 constraints.push(LinearConstraint::le(
91 vec![(u * q + g, 1.0), (v * q + g, 1.0)],
92 1.0,
93 ));
94 }
95 }
96 }
97 }
98
99 let target = ILP::new(num_vars, constraints, vec![], ObjectiveSense::Minimize);
100
101 ReductionPITToILP {
102 target,
103 num_vertices,
104 num_groups: q,
105 }
106 }
107}
108
109#[cfg(feature = "example-db")]
110pub(crate) fn canonical_rule_example_specs() -> Vec<crate::example_db::specs::RuleExampleSpec> {
111 vec![crate::example_db::specs::RuleExampleSpec {
112 id: "partitionintotriangles_to_ilp",
113 build: || {
114 let source = PartitionIntoTriangles::new(SimpleGraph::new(
116 6,
117 vec![(0, 1), (0, 2), (1, 2), (3, 4), (3, 5), (4, 5)],
118 ));
119 crate::example_db::specs::rule_example_via_ilp::<_, bool>(source)
120 },
121 }]
122}
123
124#[cfg(test)]
125#[path = "../unit_tests/rules/partitionintotriangles_ilp.rs"]
126mod tests;