|
| 1 | +use super::*; |
| 2 | +use crate::models::algebraic::{Comparison, ObjectiveSense}; |
| 3 | +use crate::models::graph::GraphPartitioning; |
| 4 | +use crate::solvers::{BruteForce, ILPSolver}; |
| 5 | +use crate::topology::SimpleGraph; |
| 6 | +use crate::traits::Problem; |
| 7 | +use crate::types::SolutionSize; |
| 8 | + |
| 9 | +fn canonical_instance() -> GraphPartitioning<SimpleGraph> { |
| 10 | + let graph = SimpleGraph::new( |
| 11 | + 6, |
| 12 | + vec![ |
| 13 | + (0, 1), |
| 14 | + (0, 2), |
| 15 | + (1, 2), |
| 16 | + (1, 3), |
| 17 | + (2, 3), |
| 18 | + (2, 4), |
| 19 | + (3, 4), |
| 20 | + (3, 5), |
| 21 | + (4, 5), |
| 22 | + ], |
| 23 | + ); |
| 24 | + GraphPartitioning::new(graph) |
| 25 | +} |
| 26 | + |
| 27 | +#[test] |
| 28 | +fn test_reduction_creates_valid_ilp() { |
| 29 | + let problem = canonical_instance(); |
| 30 | + let reduction: ReductionGraphPartitioningToILP = ReduceTo::<ILP<bool>>::reduce_to(&problem); |
| 31 | + let ilp = reduction.target_problem(); |
| 32 | + |
| 33 | + assert_eq!(ilp.num_vars, 15); |
| 34 | + assert_eq!(ilp.constraints.len(), 19); |
| 35 | + assert_eq!(ilp.sense, ObjectiveSense::Minimize); |
| 36 | + assert_eq!( |
| 37 | + ilp.objective, |
| 38 | + vec![ |
| 39 | + (6, 1.0), |
| 40 | + (7, 1.0), |
| 41 | + (8, 1.0), |
| 42 | + (9, 1.0), |
| 43 | + (10, 1.0), |
| 44 | + (11, 1.0), |
| 45 | + (12, 1.0), |
| 46 | + (13, 1.0), |
| 47 | + (14, 1.0), |
| 48 | + ] |
| 49 | + ); |
| 50 | +} |
| 51 | + |
| 52 | +#[test] |
| 53 | +fn test_reduction_constraint_shape() { |
| 54 | + let problem = GraphPartitioning::new(SimpleGraph::new(2, vec![(0, 1)])); |
| 55 | + let reduction: ReductionGraphPartitioningToILP = ReduceTo::<ILP<bool>>::reduce_to(&problem); |
| 56 | + let ilp = reduction.target_problem(); |
| 57 | + |
| 58 | + assert_eq!(ilp.num_vars, 3); |
| 59 | + assert_eq!(ilp.constraints.len(), 3); |
| 60 | + |
| 61 | + let balance = &ilp.constraints[0]; |
| 62 | + assert_eq!(balance.cmp, Comparison::Eq); |
| 63 | + assert_eq!(balance.terms, vec![(0, 1.0), (1, 1.0)]); |
| 64 | + assert_eq!(balance.rhs, 1.0); |
| 65 | + |
| 66 | + let first_link = &ilp.constraints[1]; |
| 67 | + assert_eq!(first_link.cmp, Comparison::Ge); |
| 68 | + assert_eq!(first_link.terms, vec![(2, 1.0), (0, -1.0), (1, 1.0)]); |
| 69 | + assert_eq!(first_link.rhs, 0.0); |
| 70 | + |
| 71 | + let second_link = &ilp.constraints[2]; |
| 72 | + assert_eq!(second_link.cmp, Comparison::Ge); |
| 73 | + assert_eq!(second_link.terms, vec![(2, 1.0), (0, 1.0), (1, -1.0)]); |
| 74 | + assert_eq!(second_link.rhs, 0.0); |
| 75 | +} |
| 76 | + |
| 77 | +#[test] |
| 78 | +fn test_graphpartitioning_to_ilp_closed_loop() { |
| 79 | + let problem = canonical_instance(); |
| 80 | + let reduction: ReductionGraphPartitioningToILP = ReduceTo::<ILP<bool>>::reduce_to(&problem); |
| 81 | + let ilp = reduction.target_problem(); |
| 82 | + |
| 83 | + let bf = BruteForce::new(); |
| 84 | + let ilp_solver = ILPSolver::new(); |
| 85 | + |
| 86 | + let bf_solutions = bf.find_all_best(&problem); |
| 87 | + let bf_obj = problem.evaluate(&bf_solutions[0]); |
| 88 | + |
| 89 | + let ilp_solution = ilp_solver.solve(ilp).expect("ILP should be solvable"); |
| 90 | + let extracted = reduction.extract_solution(&ilp_solution); |
| 91 | + let ilp_obj = problem.evaluate(&extracted); |
| 92 | + |
| 93 | + assert_eq!(bf_obj, SolutionSize::Valid(3)); |
| 94 | + assert_eq!(ilp_obj, SolutionSize::Valid(3)); |
| 95 | +} |
| 96 | + |
| 97 | +#[test] |
| 98 | +fn test_odd_vertices_reduce_to_infeasible_ilp() { |
| 99 | + let problem = GraphPartitioning::new(SimpleGraph::new(3, vec![(0, 1), (1, 2)])); |
| 100 | + let reduction: ReductionGraphPartitioningToILP = ReduceTo::<ILP<bool>>::reduce_to(&problem); |
| 101 | + let ilp = reduction.target_problem(); |
| 102 | + |
| 103 | + assert_eq!(ilp.constraints[0].cmp, Comparison::Eq); |
| 104 | + assert_eq!(ilp.constraints[0].rhs, 1.5); |
| 105 | + |
| 106 | + let solver = ILPSolver::new(); |
| 107 | + assert_eq!(solver.solve(ilp), None); |
| 108 | +} |
| 109 | + |
| 110 | +#[test] |
| 111 | +fn test_solution_extraction() { |
| 112 | + let problem = canonical_instance(); |
| 113 | + let reduction: ReductionGraphPartitioningToILP = ReduceTo::<ILP<bool>>::reduce_to(&problem); |
| 114 | + |
| 115 | + let ilp_solution = vec![0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0]; |
| 116 | + let extracted = reduction.extract_solution(&ilp_solution); |
| 117 | + |
| 118 | + assert_eq!(extracted, vec![0, 0, 0, 1, 1, 1]); |
| 119 | + assert_eq!(problem.evaluate(&extracted), SolutionSize::Valid(3)); |
| 120 | +} |
| 121 | + |
| 122 | +#[test] |
| 123 | +fn test_solve_reduced() { |
| 124 | + let problem = canonical_instance(); |
| 125 | + |
| 126 | + let ilp_solver = ILPSolver::new(); |
| 127 | + let solution = ilp_solver |
| 128 | + .solve_reduced(&problem) |
| 129 | + .expect("solve_reduced should work"); |
| 130 | + |
| 131 | + assert_eq!(problem.evaluate(&solution), SolutionSize::Valid(3)); |
| 132 | +} |
0 commit comments