|
| 1 | +use super::*; |
| 2 | +use crate::solvers::{BruteForce, ILPSolver}; |
| 3 | +use crate::traits::Problem; |
| 4 | +use crate::types::SolutionSize; |
| 5 | + |
| 6 | +#[test] |
| 7 | +fn test_reduction_creates_valid_ilp() { |
| 8 | + // 3 items with weights [3, 3, 2], capacity 5 |
| 9 | + let problem = BinPacking::new(vec![3, 3, 2], 5); |
| 10 | + let reduction: ReductionBPToILP = ReduceTo::<ILP<bool>>::reduce_to(&problem); |
| 11 | + let ilp = reduction.target_problem(); |
| 12 | + |
| 13 | + // n=3: 9 assignment vars + 3 bin vars = 12 |
| 14 | + assert_eq!(ilp.num_vars, 12, "Should have n^2 + n variables"); |
| 15 | + // 3 assignment + 3 capacity = 6 |
| 16 | + assert_eq!(ilp.constraints.len(), 6, "Should have 2n constraints"); |
| 17 | + assert_eq!(ilp.sense, ObjectiveSense::Minimize, "Should minimize"); |
| 18 | +} |
| 19 | + |
| 20 | +#[test] |
| 21 | +fn test_binpacking_to_ilp_closed_loop() { |
| 22 | + // 4 items with weights [3, 3, 2, 2], capacity 5 |
| 23 | + // Optimal: 2 bins, e.g. {3,2} and {3,2} |
| 24 | + let problem = BinPacking::new(vec![3, 3, 2, 2], 5); |
| 25 | + let reduction: ReductionBPToILP = ReduceTo::<ILP<bool>>::reduce_to(&problem); |
| 26 | + let ilp = reduction.target_problem(); |
| 27 | + |
| 28 | + let bf = BruteForce::new(); |
| 29 | + let ilp_solver = ILPSolver::new(); |
| 30 | + |
| 31 | + // Solve original with brute force |
| 32 | + let bf_solutions = bf.find_all_best(&problem); |
| 33 | + let bf_obj = problem.evaluate(&bf_solutions[0]); |
| 34 | + |
| 35 | + // Solve via ILP |
| 36 | + let ilp_solution = ilp_solver.solve(ilp).expect("ILP should be solvable"); |
| 37 | + let extracted = reduction.extract_solution(&ilp_solution); |
| 38 | + let ilp_obj = problem.evaluate(&extracted); |
| 39 | + |
| 40 | + assert_eq!(bf_obj, SolutionSize::Valid(2)); |
| 41 | + assert_eq!(ilp_obj, SolutionSize::Valid(2)); |
| 42 | +} |
| 43 | + |
| 44 | +#[test] |
| 45 | +fn test_single_item() { |
| 46 | + let problem = BinPacking::new(vec![5], 10); |
| 47 | + let reduction: ReductionBPToILP = ReduceTo::<ILP<bool>>::reduce_to(&problem); |
| 48 | + let ilp = reduction.target_problem(); |
| 49 | + |
| 50 | + assert_eq!(ilp.num_vars, 2); // 1 assignment + 1 bin var |
| 51 | + assert_eq!(ilp.constraints.len(), 2); // 1 assignment + 1 capacity |
| 52 | + |
| 53 | + let ilp_solver = ILPSolver::new(); |
| 54 | + let ilp_solution = ilp_solver.solve(ilp).expect("ILP should be solvable"); |
| 55 | + let extracted = reduction.extract_solution(&ilp_solution); |
| 56 | + |
| 57 | + assert!(problem.evaluate(&extracted).is_valid()); |
| 58 | + assert_eq!(problem.evaluate(&extracted), SolutionSize::Valid(1)); |
| 59 | +} |
| 60 | + |
| 61 | +#[test] |
| 62 | +fn test_same_weight_items() { |
| 63 | + // 4 items all weight 3, capacity 6 -> 2 items per bin -> 2 bins needed |
| 64 | + let problem = BinPacking::new(vec![3, 3, 3, 3], 6); |
| 65 | + let reduction: ReductionBPToILP = ReduceTo::<ILP<bool>>::reduce_to(&problem); |
| 66 | + let ilp = reduction.target_problem(); |
| 67 | + |
| 68 | + let ilp_solver = ILPSolver::new(); |
| 69 | + let ilp_solution = ilp_solver.solve(ilp).expect("ILP should be solvable"); |
| 70 | + let extracted = reduction.extract_solution(&ilp_solution); |
| 71 | + |
| 72 | + assert!(problem.evaluate(&extracted).is_valid()); |
| 73 | + assert_eq!(problem.evaluate(&extracted), SolutionSize::Valid(2)); |
| 74 | +} |
| 75 | + |
| 76 | +#[test] |
| 77 | +fn test_exact_fill() { |
| 78 | + // 2 items, weights [5, 5], capacity 10 -> fit in 1 bin |
| 79 | + let problem = BinPacking::new(vec![5, 5], 10); |
| 80 | + let reduction: ReductionBPToILP = ReduceTo::<ILP<bool>>::reduce_to(&problem); |
| 81 | + let ilp = reduction.target_problem(); |
| 82 | + |
| 83 | + let ilp_solver = ILPSolver::new(); |
| 84 | + let ilp_solution = ilp_solver.solve(ilp).expect("ILP should be solvable"); |
| 85 | + let extracted = reduction.extract_solution(&ilp_solution); |
| 86 | + |
| 87 | + assert!(problem.evaluate(&extracted).is_valid()); |
| 88 | + assert_eq!(problem.evaluate(&extracted), SolutionSize::Valid(1)); |
| 89 | +} |
| 90 | + |
| 91 | +#[test] |
| 92 | +fn test_solution_extraction() { |
| 93 | + let problem = BinPacking::new(vec![3, 3, 2], 5); |
| 94 | + let reduction: ReductionBPToILP = ReduceTo::<ILP<bool>>::reduce_to(&problem); |
| 95 | + |
| 96 | + // Manually construct an ILP solution: |
| 97 | + // n=3, x_{00}=1 (item 0 in bin 0), x_{11}=1 (item 1 in bin 1), x_{20}=1 (item 2 in bin 0) |
| 98 | + // y_0=1, y_1=1, y_2=0 |
| 99 | + let mut ilp_solution = vec![0usize; 12]; |
| 100 | + ilp_solution[0] = 1; // x_{0,0} = 1 |
| 101 | + ilp_solution[4] = 1; // x_{1,1} = 1 |
| 102 | + ilp_solution[6] = 1; // x_{2,0} = 1 |
| 103 | + ilp_solution[9] = 1; // y_0 = 1 |
| 104 | + ilp_solution[10] = 1; // y_1 = 1 |
| 105 | + |
| 106 | + let extracted = reduction.extract_solution(&ilp_solution); |
| 107 | + assert_eq!(extracted, vec![0, 1, 0]); |
| 108 | + assert!(problem.evaluate(&extracted).is_valid()); |
| 109 | +} |
| 110 | + |
| 111 | +#[test] |
| 112 | +fn test_ilp_structure_constraints() { |
| 113 | + // 2 items, weights [3, 4], capacity 5 |
| 114 | + let problem = BinPacking::new(vec![3, 4], 5); |
| 115 | + let reduction: ReductionBPToILP = ReduceTo::<ILP<bool>>::reduce_to(&problem); |
| 116 | + let ilp = reduction.target_problem(); |
| 117 | + |
| 118 | + // 4 assignment vars + 2 bin vars = 6 |
| 119 | + assert_eq!(ilp.num_vars, 6); |
| 120 | + // 2 assignment + 2 capacity = 4 |
| 121 | + assert_eq!(ilp.constraints.len(), 4); |
| 122 | + |
| 123 | + // Check objective: minimize y_0 + y_1 (vars at indices 4 and 5) |
| 124 | + let obj_vars: Vec<usize> = ilp.objective.iter().map(|&(v, _)| v).collect(); |
| 125 | + assert!(obj_vars.contains(&4)); |
| 126 | + assert!(obj_vars.contains(&5)); |
| 127 | + for &(_, coef) in &ilp.objective { |
| 128 | + assert!((coef - 1.0).abs() < 1e-9); |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +#[test] |
| 133 | +fn test_solve_reduced() { |
| 134 | + let problem = BinPacking::new(vec![6, 5, 5, 4, 3], 10); |
| 135 | + |
| 136 | + let ilp_solver = ILPSolver::new(); |
| 137 | + let solution = ilp_solver |
| 138 | + .solve_reduced(&problem) |
| 139 | + .expect("solve_reduced should work"); |
| 140 | + |
| 141 | + assert!(problem.evaluate(&solution).is_valid()); |
| 142 | + assert_eq!(problem.evaluate(&solution), SolutionSize::Valid(3)); |
| 143 | +} |
0 commit comments