fix, reindent, add tests

This commit is contained in:
Laurent Perron
2022-09-27 18:00:48 +02:00
parent 1a5be9484e
commit f39efd5ac8
14 changed files with 95 additions and 51 deletions

View File

@@ -27,12 +27,82 @@ public final class KnapsackSolverTest {
Loader.loadNativeLibraries();
}
@Test
public void testKnapsackSolver() {
final KnapsackSolver solver = new KnapsackSolver(
KnapsackSolver.SolverType.KNAPSACK_MULTIDIMENSION_BRANCH_AND_BOUND_SOLVER, "test");
private long runKnapsackSolver(final KnapsackSolver.SolverType solverType, final long[] profits,
final long[][] weights, final long[] capacities) {
final KnapsackSolver solver = new KnapsackSolver(solverType, "test");
assertNotNull(solver);
solver.init(profits, weights, capacities);
return solver.solve();
}
private void solveKnapsackProblem(final long[] profits, final long[][] weights,
final long[] capacities, final long optimalProfit) {
final int maxNumberOfItemsForBruteForce = 20;
final int maxNumberOfItemsForDivideAndConquer = 32;
final int maxNumberOfItemsFor64ItemsSolver = 64;
{
final long profit = runKnapsackSolver(
KnapsackSolver.SolverType.KNAPSACK_MULTIDIMENSION_BRANCH_AND_BOUND_SOLVER, profits,
weights, capacities);
assertEquals(optimalProfit, profit);
}
// Other solvers don't support multidimension models.
if (weights.length > 1) {
return;
}
final int numOfItems = profits.length;
if (numOfItems <= maxNumberOfItemsForBruteForce) {
final long profit = runKnapsackSolver(
KnapsackSolver.SolverType.KNAPSACK_BRUTE_FORCE_SOLVER, profits, weights, capacities);
assertEquals(optimalProfit, profit);
}
if (numOfItems <= maxNumberOfItemsForDivideAndConquer) {
final long profit =
runKnapsackSolver(KnapsackSolver.SolverType.KNAPSACK_DIVIDE_AND_CONQUER_SOLVER, profits,
weights, capacities);
assertEquals(optimalProfit, profit);
}
{
final long profit =
runKnapsackSolver(KnapsackSolver.SolverType.KNAPSACK_DYNAMIC_PROGRAMMING_SOLVER, profits,
weights, capacities);
assertEquals(optimalProfit, profit);
}
if (numOfItems <= maxNumberOfItemsFor64ItemsSolver) {
final long profit = runKnapsackSolver(
KnapsackSolver.SolverType.KNAPSACK_64ITEMS_SOLVER, profits, weights, capacities);
assertEquals(optimalProfit, profit);
}
}
@Test
public void testSolveOneDimension() {
final long[] profits = {1, 2, 3, 4, 5, 6, 7, 8, 9};
final long[][] weights = {{1, 2, 3, 4, 5, 6, 7, 8, 9}};
final long[] capacities = {34};
final long optimalProfit = 34;
solveKnapsackProblem(profits, weights, capacities, optimalProfit);
}
@Test
public void testSolveTwoDimensions() {
final long[] profits = {1, 2, 3, 4, 5, 6, 7, 8, 9};
final long[][] weights = {{1, 2, 3, 4, 5, 6, 7, 8, 9}, {1, 1, 1, 1, 1, 1, 1, 1, 1}};
final long[] capacities = {34, 4};
final long optimalProfit = 30;
solveKnapsackProblem(profits, weights, capacities, optimalProfit);
}
@Test
public void testSolveBigOneDimension() {
final long[] profits = {360, 83, 59, 130, 431, 67, 230, 52, 93, 125, 670, 892, 600, 38, 48, 147,
78, 256, 63, 17, 120, 164, 432, 35, 92, 110, 22, 42, 50, 323, 514, 28, 87, 73, 78, 15, 26,
78, 210, 36, 85, 189, 274, 43, 33, 10, 19, 389, 276, 312};
@@ -40,9 +110,7 @@ public final class KnapsackSolverTest {
0, 42, 47, 52, 32, 26, 48, 55, 6, 29, 84, 2, 4, 18, 56, 7, 29, 93, 44, 71, 3, 86, 66, 31,
65, 0, 79, 20, 65, 52, 13}};
final long[] capacities = {850};
solver.init(profits, weights, capacities);
final long computedProfit = solver.solve();
assertEquals(7534, computedProfit);
final long optimalProfit = 7534;
solveKnapsackProblem(profits, weights, capacities, optimalProfit);
}
}