problemreductions/rules/
subgraphisomorphism_ilp.rs1use crate::models::algebraic::{LinearConstraint, ObjectiveSense, ILP};
11use crate::models::graph::SubgraphIsomorphism;
12use crate::reduction;
13use crate::rules::ilp_helpers::{one_hot_assignment_constraints, one_hot_decode_rows};
14use crate::rules::traits::{ReduceTo, ReductionResult};
15use crate::topology::Graph;
16
17#[derive(Debug, Clone)]
22pub struct ReductionSubIsoToILP {
23 target: ILP<bool>,
24 num_pattern_vertices: usize,
25 num_host_vertices: usize,
26}
27
28impl ReductionResult for ReductionSubIsoToILP {
29 type Source = SubgraphIsomorphism;
30 type Target = ILP<bool>;
31
32 fn target_problem(&self) -> &ILP<bool> {
33 &self.target
34 }
35
36 fn extract_solution(
38 &self,
39 target_solution: &<Self::Target as crate::traits::Problem>::Solution,
40 ) -> crate::rules::ExtractionResult<<Self::Source as crate::traits::Problem>::Solution> {
41 crate::rules::traits::validate_target_witness(
42 self.target_problem(),
43 target_solution,
44 |value| value.value.is_some(),
45 "target ILP assignment is infeasible",
46 )?;
47
48 one_hot_decode_rows(
49 target_solution,
50 self.num_pattern_vertices,
51 self.num_host_vertices,
52 0,
53 )
54 }
55}
56
57#[crate::aggregate_reduction(ilp_feasibility)]
58impl crate::rules::AggregateReductionResult for ReductionSubIsoToILP {}
59
60#[reduction(transform = upper_bound {
61 max_constraint_magnitude_bits = "2",
62 num_vars = "num_pattern_vertices * num_host_vertices",
63 num_constraints = "num_pattern_vertices + num_host_vertices + num_pattern_edges * num_host_vertices^2",
64 num_nonzeros = "(num_pattern_vertices * num_host_vertices) * (num_pattern_vertices + num_host_vertices + num_pattern_edges * num_host_vertices^2)",
65})]
66impl ReduceTo<ILP<bool>> for SubgraphIsomorphism {
67 type Result = ReductionSubIsoToILP;
68
69 fn reduce_to(&self) -> Result<Self::Result, crate::rules::ReductionError> {
70 let n_pat = self.num_pattern_vertices();
71 let n_host = self.num_host_vertices();
72 let host = self.host_graph();
73 let pattern = self.pattern_graph();
74 let pat_edges = pattern.edges();
75
76 let num_vars = n_pat * n_host;
77
78 let mut constraints = Vec::new();
79
80 constraints.extend(one_hot_assignment_constraints(n_pat, n_host, 0));
82
83 for &(v, w) in &pat_edges {
85 for u in 0..n_host {
86 for u_prime in 0..n_host {
87 if host.has_edge(u, u_prime) {
88 continue;
89 }
90 constraints.push(LinearConstraint::le(
92 vec![(v * n_host + u, 1), (w * n_host + u_prime, 1)],
93 1,
94 ));
95 }
96 }
97 }
98
99 let target = ILP::new(num_vars, constraints, vec![], ObjectiveSense::Minimize)
101 .map_err(Self::target_construction)?;
102
103 Ok(ReductionSubIsoToILP {
104 target,
105 num_pattern_vertices: n_pat,
106 num_host_vertices: n_host,
107 })
108 }
109}
110
111#[cfg(feature = "example-db")]
112pub(crate) fn canonical_rule_example_specs() -> Vec<crate::example_db::specs::RuleExampleSpec> {
113 use crate::topology::SimpleGraph;
114 vec![crate::example_db::specs::RuleExampleSpec {
115 id: "subgraphisomorphism_to_ilp",
116 build: || {
117 let host = SimpleGraph::new(4, vec![(0, 1), (1, 2), (2, 3), (3, 0)]);
119 let pattern = SimpleGraph::new(3, vec![(0, 1), (1, 2)]);
120 let source = SubgraphIsomorphism::new(host, pattern);
121 crate::example_db::specs::rule_example_via_ilp::<_, bool>(source)
122 },
123 }]
124}
125
126#[cfg(test)]
127#[path = "../unit_tests/rules/subgraphisomorphism_ilp.rs"]
128mod tests;