Skip to content

Commit 8ab56f2

Browse files
committed
Integrate checked matrix-cover objective bounds
2 parents 3571ec1 + 52bd171 commit 8ab56f2

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

‎src/rules/maxcut_minimummatrixcover.rs‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,30 @@ impl ReduceTo<MinimumMatrixCover> for MaxCut<SimpleGraph, i64> {
7575
fn reduce_to(&self) -> Result<Self::Result, crate::rules::ReductionError> {
7676
let n = self.graph().num_vertices();
7777
let edges = self.edges();
78+
let overflow = || {
79+
crate::rules::ReductionError::integer_overflow::<Self, MinimumMatrixCover>(
80+
"bounding the matrix-cover objective",
81+
)
82+
};
83+
// All entries are nonnegative. Their sum is attained by equal signs and
84+
// bounds the absolute value of every signed evaluation prefix. Checking
85+
// this sum is therefore necessary and sufficient for i64 evaluation.
86+
let mut matrix_sum = 0_i64;
87+
for &(_, _, weight) in &edges {
88+
let link_sum = if weight < 0 {
89+
weight
90+
.checked_neg()
91+
.and_then(|magnitude| magnitude.checked_mul(2))
92+
.and_then(|sum| sum.checked_add(1))
93+
} else {
94+
Some(weight)
95+
}
96+
.ok_or_else(overflow)?;
97+
matrix_sum = link_sum
98+
.checked_mul(2)
99+
.and_then(|sum| matrix_sum.checked_add(sum))
100+
.ok_or_else(overflow)?;
101+
}
78102
let size = n + edges.iter().filter(|&&(_, _, w)| w < 0).count();
79103
let mut matrix: Vec<Vec<i64>> = vec![vec![0i64; size]; size];
80104
let mut link = |a: usize, b: usize, weight: i64| {

‎src/unit_tests/rules/maxcut_minimummatrixcover.rs‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,3 +286,53 @@ fn test_minimum_negative_weight_overflows() {
286286
crate::rules::ReductionError::IntegerOverflow { .. }
287287
));
288288
}
289+
290+
#[test]
291+
fn test_unrepresentable_matrix_objective_is_rejected() {
292+
for (n, edges, weights) in [
293+
(2, vec![(0, 1)], vec![-3_000_000_000_000_000_000]),
294+
(2, vec![(0, 1)], vec![-((i64::MAX - 2) / 4 + 1)]),
295+
(2, vec![(0, 1)], vec![i64::MAX / 2 + 1]),
296+
(3, vec![(0, 1), (1, 2)], vec![-2_000_000_000_000_000_000; 2]),
297+
(
298+
3,
299+
vec![(0, 1), (1, 2)],
300+
vec![-1_000_000_000_000_000_000, 3_000_000_000_000_000_000],
301+
),
302+
] {
303+
let source = MaxCut::<SimpleGraph, i64>::new(SimpleGraph::new(n, edges), weights);
304+
// Every source configuration is evaluable, including its optimum.
305+
let optimum = BruteForce::new().solve(&source).unwrap().unwrap();
306+
assert!(source.evaluate(&optimum).unwrap().0.is_some());
307+
assert!(matches!(
308+
ReduceTo::<MinimumMatrixCover>::reduce_to(&source),
309+
Err(crate::rules::ReductionError::IntegerOverflow { .. })
310+
));
311+
}
312+
}
313+
314+
#[test]
315+
fn test_representable_matrix_objective_boundaries() {
316+
for (weight, expected) in [(-((i64::MAX - 2) / 4), 0), (i64::MAX / 2, i64::MAX / 2)] {
317+
let source =
318+
MaxCut::<SimpleGraph, i64>::new(SimpleGraph::new(2, vec![(0, 1)]), vec![weight]);
319+
let reduction = ReduceTo::<MinimumMatrixCover>::reduce_to(&source).unwrap();
320+
assert_optimization_round_trip_from_optimization_target(
321+
&source,
322+
&reduction,
323+
"MaxCut at the matrix objective boundary",
324+
);
325+
let target = reduction.target_problem();
326+
assert_eq!(
327+
target.evaluate(&vec![false; target.num_rows()]).unwrap(),
328+
Min(Some(i64::MAX - 1))
329+
);
330+
let target_optimum = BruteForce::new().solve(target).unwrap().unwrap();
331+
assert_eq!(
332+
target.evaluate(&target_optimum).unwrap(),
333+
Min(Some(-(i64::MAX - 1)))
334+
);
335+
let extracted = reduction.extract_solution(&target_optimum).unwrap();
336+
assert_eq!(source.evaluate(&extracted).unwrap(), Max(Some(expected)));
337+
}
338+
}

0 commit comments

Comments
 (0)