|
| 1 | +use super::*; |
| 2 | +use crate::models::algebraic::{ClosestVectorProblem, VarBounds}; |
| 3 | +use crate::rules::test_helpers::assert_satisfaction_round_trip_from_optimization_target; |
| 4 | +use crate::solvers::{BruteForce, Solver}; |
| 5 | +use crate::traits::Problem; |
| 6 | +use crate::types::SolutionSize; |
| 7 | +use std::collections::HashSet; |
| 8 | + |
| 9 | +#[test] |
| 10 | +fn test_subsetsum_to_closestvectorproblem_closed_loop() { |
| 11 | + let source = SubsetSum::new(vec![3u32, 7, 1, 8], 11u32); |
| 12 | + let reduction = ReduceTo::<ClosestVectorProblem<i32>>::reduce_to(&source); |
| 13 | + let target = reduction.target_problem(); |
| 14 | + |
| 15 | + assert_eq!(target.num_basis_vectors(), 4); |
| 16 | + assert_eq!(target.ambient_dimension(), 5); |
| 17 | + assert_eq!(target.bounds(), &[VarBounds::binary(); 4]); |
| 18 | + |
| 19 | + assert_satisfaction_round_trip_from_optimization_target( |
| 20 | + &source, |
| 21 | + &reduction, |
| 22 | + "SubsetSum -> ClosestVectorProblem closed loop", |
| 23 | + ); |
| 24 | +} |
| 25 | + |
| 26 | +#[test] |
| 27 | +fn test_subsetsum_to_closestvectorproblem_structure() { |
| 28 | + let source = SubsetSum::new(vec![3u32, 7, 1, 8], 11u32); |
| 29 | + let reduction = ReduceTo::<ClosestVectorProblem<i32>>::reduce_to(&source); |
| 30 | + let target = reduction.target_problem(); |
| 31 | + |
| 32 | + assert_eq!(target.basis()[0], vec![1, 0, 0, 0, 3]); |
| 33 | + assert_eq!(target.basis()[1], vec![0, 1, 0, 0, 7]); |
| 34 | + assert_eq!(target.basis()[2], vec![0, 0, 1, 0, 1]); |
| 35 | + assert_eq!(target.basis()[3], vec![0, 0, 0, 1, 8]); |
| 36 | + assert_eq!(target.target(), &[0.5, 0.5, 0.5, 0.5, 11.0]); |
| 37 | +} |
| 38 | + |
| 39 | +#[test] |
| 40 | +fn test_subsetsum_to_closestvectorproblem_issue_example_minimizers() { |
| 41 | + let source = SubsetSum::new(vec![3u32, 7, 1, 8], 11u32); |
| 42 | + let reduction = ReduceTo::<ClosestVectorProblem<i32>>::reduce_to(&source); |
| 43 | + let target = reduction.target_problem(); |
| 44 | + let solutions: HashSet<Vec<usize>> = BruteForce::new() |
| 45 | + .find_all_best(target) |
| 46 | + .into_iter() |
| 47 | + .collect(); |
| 48 | + |
| 49 | + let expected: HashSet<Vec<usize>> = [vec![1, 0, 0, 1], vec![1, 1, 1, 0]].into_iter().collect(); |
| 50 | + assert_eq!(solutions, expected); |
| 51 | + |
| 52 | + for solution in &solutions { |
| 53 | + assert_eq!(target.evaluate(solution), SolutionSize::Valid(1.0)); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +#[test] |
| 58 | +fn test_subsetsum_to_closestvectorproblem_unsatisfiable_instance() { |
| 59 | + let source = SubsetSum::new(vec![2u32, 4, 6], 5u32); |
| 60 | + let reduction = ReduceTo::<ClosestVectorProblem<i32>>::reduce_to(&source); |
| 61 | + let target = reduction.target_problem(); |
| 62 | + let best = BruteForce::new() |
| 63 | + .find_best(target) |
| 64 | + .expect("unsatisfiable instance should still have a best CVP assignment"); |
| 65 | + |
| 66 | + match target.evaluate(&best) { |
| 67 | + SolutionSize::Valid(value) => assert!(value > (source.num_elements() as f64).sqrt() / 2.0), |
| 68 | + SolutionSize::Invalid => panic!("CVP solution should be valid"), |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +#[test] |
| 73 | +#[should_panic( |
| 74 | + expected = "SubsetSum -> ClosestVectorProblem requires all sizes and target to fit in i32" |
| 75 | +)] |
| 76 | +fn test_subsetsum_to_closestvectorproblem_panics_on_large_coefficients() { |
| 77 | + let source = SubsetSum::new(vec![(i32::MAX as u64) + 1], 1u64); |
| 78 | + let _ = ReduceTo::<ClosestVectorProblem<i32>>::reduce_to(&source); |
| 79 | +} |
0 commit comments