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