problemreductions/rules/maximumcontactmapoverlap_ilp.rs
1//! Reduction from MaximumContactMapOverlap to ILP (Integer Linear Programming).
2//!
3//! Binary mapping variables `x_(i,j)` indicate that residue `i in V_1` is
4//! aligned to residue `j in V_2`. Row and column inequalities encode a partial
5//! injective alignment. Order-preservation is enforced by forbidding crossings
6//! and equal-image matches: for every `i < k in V_1` and every `j >= l in V_2`,
7//! we add `x_(i,j) + x_(k,l) <= 1`. For every pair of contacts
8//! `({i,k} in E_1, {j,l} in E_2)` with `i < k` and `j < l` we introduce a
9//! binary `y_(i,k,j,l)` linked by `y <= x_(i,j)` and `y <= x_(k,l)`. The ILP
10//! objective is `max sum y_(i,k,j,l)`, which equals the number of preserved
11//! contacts under the alignment.
12//!
13//! This is a direct ILP rendering of the polyhedral formulation studied by
14//! Andonov, Malod-Dognin, and Yanev (J. Comput. Biol., 2011) and by
15//! Xie and Sahinidis (J. Comput. Biol., 2007).
16
17use crate::models::algebraic::{LinearConstraint, ObjectiveSense, ILP};
18use crate::models::graph::MaximumContactMapOverlap;
19use crate::reduction;
20use crate::rules::traits::{ReduceTo, ReductionResult};
21
22/// Result of reducing MaximumContactMapOverlap to ILP.
23///
24/// Variable layout (all binary):
25/// - `x_(i,j)` at index `i * n2 + j` for `i in V_1`, `j in V_2`
26/// - `y_(i,k,j,l)` for each contact pair from `E_1 x E_2` (with `i < k` and
27/// `j < l` enforced by the contact canonicalization), indexed sequentially
28/// after the `x` block in the order they are enumerated by the constructor.
29#[derive(Debug, Clone)]
30pub struct ReductionCMOToILP {
31 target: ILP<bool>,
32 num_vertices_1: usize,
33 num_vertices_2: usize,
34}
35
36impl ReductionResult for ReductionCMOToILP {
37 type Source = MaximumContactMapOverlap;
38 type Target = ILP<bool>;
39
40 fn target_problem(&self) -> &ILP<bool> {
41 &self.target
42 }
43
44 /// Extract the CMO configuration from the ILP assignment.
45 ///
46 /// For each source residue `i in V_1`, find the unique `j` with
47 /// `x_(i,j) = 1` and encode it as `j + 1` (CMO's `bot` is `0`); if no
48 /// `x_(i,*)` is selected, the residue is left unmatched (`0`).
49 fn extract_solution(&self, target_solution: &[usize]) -> Vec<usize> {
50 let n1 = self.num_vertices_1;
51 let n2 = self.num_vertices_2;
52 (0..n1)
53 .map(|i| {
54 (0..n2)
55 .find(|&j| target_solution[i * n2 + j] == 1)
56 .map(|j| j + 1)
57 .unwrap_or(0)
58 })
59 .collect()
60 }
61}
62
63#[reduction(
64 overhead = {
65 num_vars = "num_vertices_1 * num_vertices_2 + num_contacts_1 * num_contacts_2",
66 num_constraints = "num_vertices_1 + num_vertices_2 + num_vertices_1 * (num_vertices_1 - 1) / 2 * num_vertices_2 * (num_vertices_2 + 1) / 2 + 2 * num_contacts_1 * num_contacts_2",
67 }
68)]
69impl ReduceTo<ILP<bool>> for MaximumContactMapOverlap {
70 type Result = ReductionCMOToILP;
71
72 fn reduce_to(&self) -> Self::Result {
73 let n1 = self.num_vertices_1();
74 let n2 = self.num_vertices_2();
75 let contacts_1 = self.contacts_1();
76 let contacts_2 = self.contacts_2();
77
78 let num_x = n1 * n2;
79 let x_idx = |i: usize, j: usize| -> usize { i * n2 + j };
80
81 // y-variables: one per (contact in E_1) x (contact in E_2). The
82 // canonicalized contacts already satisfy i < k and j < l.
83 let num_y = contacts_1.len() * contacts_2.len();
84 let num_vars = num_x + num_y;
85 let y_idx = |seq: usize| -> usize { num_x + seq };
86
87 let mut constraints: Vec<LinearConstraint> = Vec::new();
88
89 // Row constraints: each residue of G_1 maps to at most one residue of G_2.
90 for i in 0..n1 {
91 let terms: Vec<(usize, f64)> = (0..n2).map(|j| (x_idx(i, j), 1.0)).collect();
92 constraints.push(LinearConstraint::le(terms, 1.0));
93 }
94
95 // Column constraints: each residue of G_2 receives at most one residue of G_1.
96 for j in 0..n2 {
97 let terms: Vec<(usize, f64)> = (0..n1).map(|i| (x_idx(i, j), 1.0)).collect();
98 constraints.push(LinearConstraint::le(terms, 1.0));
99 }
100
101 // Order-preservation: for i < k in V_1 and j >= l in V_2,
102 // forbid x_(i,j) + x_(k,l) <= 1. This rules out crossings (j > l)
103 // as well as equal-image matches (j == l).
104 for i in 0..n1 {
105 for k in (i + 1)..n1 {
106 for j in 0..n2 {
107 for l in 0..=j {
108 constraints.push(LinearConstraint::le(
109 vec![(x_idx(i, j), 1.0), (x_idx(k, l), 1.0)],
110 1.0,
111 ));
112 }
113 }
114 }
115 }
116
117 // Linking constraints for every contact pair: y_(i,k,j,l) <= x_(i,j) and
118 // y_(i,k,j,l) <= x_(k,l). Because each y has positive objective
119 // coefficient and there is no negative coupling, an optimum sets y to 1
120 // exactly when both endpoint-match variables are selected.
121 let mut seq = 0usize;
122 for &(i, k) in contacts_1 {
123 for &(j, l) in contacts_2 {
124 let yv = y_idx(seq);
125 constraints.push(LinearConstraint::le(
126 vec![(yv, 1.0), (x_idx(i, j), -1.0)],
127 0.0,
128 ));
129 constraints.push(LinearConstraint::le(
130 vec![(yv, 1.0), (x_idx(k, l), -1.0)],
131 0.0,
132 ));
133 seq += 1;
134 }
135 }
136
137 // Objective: maximize the number of preserved contacts.
138 let objective: Vec<(usize, f64)> = (0..num_y).map(|s| (y_idx(s), 1.0)).collect();
139
140 let target = ILP::new(num_vars, constraints, objective, ObjectiveSense::Maximize);
141
142 ReductionCMOToILP {
143 target,
144 num_vertices_1: n1,
145 num_vertices_2: n2,
146 }
147 }
148}
149
150#[cfg(feature = "example-db")]
151pub(crate) fn canonical_rule_example_specs() -> Vec<crate::example_db::specs::RuleExampleSpec> {
152 vec![crate::example_db::specs::RuleExampleSpec {
153 id: "maximumcontactmapoverlap_to_ilp",
154 build: || {
155 // Canonical instance from issue #1043:
156 // G_1: n_1 = 4, E_1 = {{0,2}, {1,3}}
157 // G_2: n_2 = 5, E_2 = {{0,3}, {1,4}, {0,2}}
158 // Optimal CMO alignment 0->0, 1->1, 2->3, 3->4 preserves 2 contacts.
159 let source = MaximumContactMapOverlap::new(
160 4,
161 vec![(0, 2), (1, 3)],
162 5,
163 vec![(0, 3), (1, 4), (0, 2)],
164 );
165 crate::example_db::specs::rule_example_via_ilp::<_, bool>(source)
166 },
167 }]
168}
169
170#[cfg(test)]
171#[path = "../unit_tests/rules/maximumcontactmapoverlap_ilp.rs"]
172mod tests;