|
| 1 | +use super::*; |
| 2 | +use crate::models::algebraic::{Comparison, ObjectiveSense, ILP}; |
| 3 | +use crate::rules::test_helpers::assert_optimization_round_trip_from_optimization_target; |
| 4 | +use crate::solvers::ILPSolver; |
| 5 | + |
| 6 | +#[test] |
| 7 | +fn test_knapsack_to_ilp_closed_loop() { |
| 8 | + let knapsack = Knapsack::new(vec![1, 3, 4, 5], vec![1, 4, 5, 7], 7); |
| 9 | + let reduction = ReduceTo::<ILP<bool>>::reduce_to(&knapsack); |
| 10 | + |
| 11 | + assert_optimization_round_trip_from_optimization_target( |
| 12 | + &knapsack, |
| 13 | + &reduction, |
| 14 | + "Knapsack->ILP closed loop", |
| 15 | + ); |
| 16 | + |
| 17 | + let ilp_solution = ILPSolver::new() |
| 18 | + .solve(reduction.target_problem()) |
| 19 | + .expect("ILP should be solvable"); |
| 20 | + let extracted = reduction.extract_solution(&ilp_solution); |
| 21 | + assert_eq!(extracted, vec![0, 1, 1, 0]); |
| 22 | +} |
| 23 | + |
| 24 | +#[test] |
| 25 | +fn test_knapsack_to_ilp_structure() { |
| 26 | + let knapsack = Knapsack::new(vec![1, 3, 4, 5], vec![1, 4, 5, 7], 7); |
| 27 | + let reduction = ReduceTo::<ILP<bool>>::reduce_to(&knapsack); |
| 28 | + let ilp = reduction.target_problem(); |
| 29 | + |
| 30 | + assert_eq!(ilp.num_vars(), 4); |
| 31 | + assert_eq!(ilp.num_constraints(), 1); |
| 32 | + assert_eq!(ilp.sense, ObjectiveSense::Maximize); |
| 33 | + assert_eq!(ilp.objective, vec![(0, 1.0), (1, 4.0), (2, 5.0), (3, 7.0)]); |
| 34 | + |
| 35 | + let constraint = &ilp.constraints[0]; |
| 36 | + assert_eq!(constraint.cmp, Comparison::Le); |
| 37 | + assert_eq!(constraint.rhs, 7.0); |
| 38 | + assert_eq!( |
| 39 | + constraint.terms, |
| 40 | + vec![(0, 1.0), (1, 3.0), (2, 4.0), (3, 5.0)] |
| 41 | + ); |
| 42 | +} |
| 43 | + |
| 44 | +#[test] |
| 45 | +fn test_knapsack_to_ilp_zero_capacity() { |
| 46 | + let knapsack = Knapsack::new(vec![2, 3], vec![5, 7], 0); |
| 47 | + let reduction = ReduceTo::<ILP<bool>>::reduce_to(&knapsack); |
| 48 | + |
| 49 | + let ilp_solution = ILPSolver::new() |
| 50 | + .solve(reduction.target_problem()) |
| 51 | + .expect("zero-capacity ILP should still be solvable"); |
| 52 | + let extracted = reduction.extract_solution(&ilp_solution); |
| 53 | + assert_eq!(extracted, vec![0, 0]); |
| 54 | +} |
| 55 | + |
| 56 | +#[test] |
| 57 | +fn test_knapsack_to_ilp_empty_instance() { |
| 58 | + let knapsack = Knapsack::new(vec![], vec![], 0); |
| 59 | + let reduction = ReduceTo::<ILP<bool>>::reduce_to(&knapsack); |
| 60 | + let ilp = reduction.target_problem(); |
| 61 | + |
| 62 | + assert_eq!(ilp.num_vars(), 0); |
| 63 | + assert_eq!(ilp.num_constraints(), 1); |
| 64 | + assert_eq!(ilp.constraints[0].cmp, Comparison::Le); |
| 65 | + assert_eq!(ilp.constraints[0].rhs, 0.0); |
| 66 | + assert!(ilp.constraints[0].terms.is_empty()); |
| 67 | + assert!(ilp.objective.is_empty()); |
| 68 | + |
| 69 | + let ilp_solution = ILPSolver::new() |
| 70 | + .solve(ilp) |
| 71 | + .expect("empty Knapsack ILP should still be solvable"); |
| 72 | + let extracted = reduction.extract_solution(&ilp_solution); |
| 73 | + assert_eq!(extracted, Vec::<usize>::new()); |
| 74 | +} |
| 75 | + |
| 76 | +#[cfg(feature = "example-db")] |
| 77 | +#[test] |
| 78 | +fn test_knapsack_to_ilp_canonical_example_spec() { |
| 79 | + let spec = canonical_rule_example_specs() |
| 80 | + .into_iter() |
| 81 | + .find(|spec| spec.id == "knapsack_to_ilp") |
| 82 | + .expect("missing canonical Knapsack -> ILP example spec"); |
| 83 | + let example = (spec.build)(); |
| 84 | + |
| 85 | + assert_eq!(example.source.problem, "Knapsack"); |
| 86 | + assert_eq!(example.target.problem, "ILP"); |
| 87 | + assert_eq!(example.source.instance["capacity"], 7); |
| 88 | + assert_eq!(example.target.instance["num_vars"], 4); |
| 89 | + assert_eq!(example.target.instance["constraints"].as_array().unwrap().len(), 1); |
| 90 | + assert_eq!( |
| 91 | + example.solutions, |
| 92 | + vec![crate::export::SolutionPair { |
| 93 | + source_config: vec![0, 1, 1, 0], |
| 94 | + target_config: vec![0, 1, 1, 0], |
| 95 | + }] |
| 96 | + ); |
| 97 | +} |
0 commit comments