|
| 1 | +#[cfg(feature = "example-db")] |
| 2 | +use super::canonical_rule_example_specs; |
| 3 | +use super::ReductionVCToFVS; |
| 4 | +use crate::models::graph::{MinimumFeedbackVertexSet, MinimumVertexCover}; |
| 5 | +use crate::rules::test_helpers::assert_optimization_round_trip_from_optimization_target; |
| 6 | +use crate::rules::traits::ReductionResult; |
| 7 | +use crate::rules::ReduceTo; |
| 8 | +#[cfg(feature = "example-db")] |
| 9 | +use crate::solvers::{BruteForce, Solver}; |
| 10 | +use crate::topology::{Graph, SimpleGraph}; |
| 11 | +#[cfg(feature = "example-db")] |
| 12 | +use crate::traits::Problem; |
| 13 | + |
| 14 | +fn weighted_cycle_cover_source() -> MinimumVertexCover<SimpleGraph, i32> { |
| 15 | + MinimumVertexCover::new( |
| 16 | + SimpleGraph::new(5, vec![(0, 1), (1, 2), (2, 0), (2, 3), (3, 4)]), |
| 17 | + vec![4, 1, 3, 2, 5], |
| 18 | + ) |
| 19 | +} |
| 20 | + |
| 21 | +#[test] |
| 22 | +fn test_minimumvertexcover_to_minimumfeedbackvertexset_closed_loop() { |
| 23 | + let source = weighted_cycle_cover_source(); |
| 24 | + let reduction: ReductionVCToFVS<i32> = |
| 25 | + ReduceTo::<MinimumFeedbackVertexSet<i32>>::reduce_to(&source); |
| 26 | + |
| 27 | + assert_optimization_round_trip_from_optimization_target( |
| 28 | + &source, |
| 29 | + &reduction, |
| 30 | + "MVC -> FVS closed loop", |
| 31 | + ); |
| 32 | +} |
| 33 | + |
| 34 | +#[test] |
| 35 | +fn test_reduction_structure() { |
| 36 | + let source = weighted_cycle_cover_source(); |
| 37 | + let reduction: ReductionVCToFVS<i32> = |
| 38 | + ReduceTo::<MinimumFeedbackVertexSet<i32>>::reduce_to(&source); |
| 39 | + let target = reduction.target_problem(); |
| 40 | + |
| 41 | + assert_eq!(target.graph().num_vertices(), source.graph().num_vertices()); |
| 42 | + assert_eq!(target.num_arcs(), 2 * source.num_edges()); |
| 43 | + |
| 44 | + let mut arcs = target.graph().arcs(); |
| 45 | + arcs.sort_unstable(); |
| 46 | + |
| 47 | + assert_eq!( |
| 48 | + arcs, |
| 49 | + vec![ |
| 50 | + (0, 1), |
| 51 | + (0, 2), |
| 52 | + (1, 0), |
| 53 | + (1, 2), |
| 54 | + (2, 0), |
| 55 | + (2, 1), |
| 56 | + (2, 3), |
| 57 | + (3, 2), |
| 58 | + (3, 4), |
| 59 | + (4, 3), |
| 60 | + ] |
| 61 | + ); |
| 62 | +} |
| 63 | + |
| 64 | +#[test] |
| 65 | +fn test_weight_preservation() { |
| 66 | + let source = weighted_cycle_cover_source(); |
| 67 | + let reduction: ReductionVCToFVS<i32> = |
| 68 | + ReduceTo::<MinimumFeedbackVertexSet<i32>>::reduce_to(&source); |
| 69 | + |
| 70 | + assert_eq!(reduction.target_problem().weights(), source.weights()); |
| 71 | +} |
| 72 | + |
| 73 | +#[test] |
| 74 | +fn test_identity_solution_extraction() { |
| 75 | + let source = weighted_cycle_cover_source(); |
| 76 | + let reduction: ReductionVCToFVS<i32> = |
| 77 | + ReduceTo::<MinimumFeedbackVertexSet<i32>>::reduce_to(&source); |
| 78 | + |
| 79 | + assert_eq!( |
| 80 | + reduction.extract_solution(&[1, 0, 1, 0, 1]), |
| 81 | + vec![1, 0, 1, 0, 1] |
| 82 | + ); |
| 83 | +} |
| 84 | + |
| 85 | +#[cfg(feature = "example-db")] |
| 86 | +#[test] |
| 87 | +fn test_canonical_rule_example_spec_builds() { |
| 88 | + let example = (canonical_rule_example_specs() |
| 89 | + .into_iter() |
| 90 | + .find(|spec| spec.id == "minimumvertexcover_to_minimumfeedbackvertexset") |
| 91 | + .expect("example spec should be registered") |
| 92 | + .build)(); |
| 93 | + |
| 94 | + assert_eq!(example.source.problem, "MinimumVertexCover"); |
| 95 | + assert_eq!(example.target.problem, "MinimumFeedbackVertexSet"); |
| 96 | + assert_eq!(example.solutions.len(), 1); |
| 97 | + assert_eq!( |
| 98 | + example.solutions[0].source_config, |
| 99 | + example.solutions[0].target_config |
| 100 | + ); |
| 101 | + |
| 102 | + let source: MinimumVertexCover<SimpleGraph, i32> = |
| 103 | + serde_json::from_value(example.source.instance.clone()) |
| 104 | + .expect("source example deserializes"); |
| 105 | + let target: MinimumFeedbackVertexSet<i32> = |
| 106 | + serde_json::from_value(example.target.instance.clone()) |
| 107 | + .expect("target example deserializes"); |
| 108 | + let solution = &example.solutions[0]; |
| 109 | + |
| 110 | + let source_metric = source.evaluate(&solution.source_config); |
| 111 | + let target_metric = target.evaluate(&solution.target_config); |
| 112 | + assert!( |
| 113 | + source_metric.is_valid(), |
| 114 | + "source witness should be feasible" |
| 115 | + ); |
| 116 | + assert!( |
| 117 | + target_metric.is_valid(), |
| 118 | + "target witness should be feasible" |
| 119 | + ); |
| 120 | + |
| 121 | + let best_source = BruteForce::new() |
| 122 | + .find_best(&source) |
| 123 | + .expect("source example should have an optimum"); |
| 124 | + let best_target = BruteForce::new() |
| 125 | + .find_best(&target) |
| 126 | + .expect("target example should have an optimum"); |
| 127 | + |
| 128 | + assert_eq!(source_metric, source.evaluate(&best_source)); |
| 129 | + assert_eq!(target_metric, target.evaluate(&best_target)); |
| 130 | +} |
0 commit comments