14 package com.google.ortools.algorithms;
16 import static org.junit.jupiter.api.Assertions.assertEquals;
17 import static org.junit.jupiter.api.Assertions.assertNotNull;
19 import com.google.ortools.Loader;
20 import org.junit.jupiter.api.BeforeEach;
21 import org.junit.jupiter.api.Test;
30 private long runKnapsackSolver(
final KnapsackSolver.SolverType solverType,
final long[] profits,
31 final long[][] weights,
final long[] capacities) {
32 final KnapsackSolver solver =
new KnapsackSolver(solverType,
"test");
33 assertNotNull(solver);
34 solver.init(profits, weights, capacities);
36 return solver.solve();
39 private void solveKnapsackProblem(
final long[] profits,
final long[][] weights,
40 final long[] capacities,
final long optimalProfit) {
41 final int maxNumberOfItemsForBruteForce = 20;
42 final int maxNumberOfItemsForDivideAndConquer = 32;
43 final int maxNumberOfItemsFor64ItemsSolver = 64;
46 final long profit = runKnapsackSolver(
47 KnapsackSolver.SolverType.KNAPSACK_MULTIDIMENSION_BRANCH_AND_BOUND_SOLVER, profits,
49 assertEquals(optimalProfit, profit);
53 if (weights.length > 1) {
57 final int numOfItems = profits.length;
59 if (numOfItems <= maxNumberOfItemsForBruteForce) {
60 final long profit = runKnapsackSolver(
61 KnapsackSolver.SolverType.KNAPSACK_BRUTE_FORCE_SOLVER, profits, weights, capacities);
62 assertEquals(optimalProfit, profit);
65 if (numOfItems <= maxNumberOfItemsForDivideAndConquer) {
67 runKnapsackSolver(KnapsackSolver.SolverType.KNAPSACK_DIVIDE_AND_CONQUER_SOLVER, profits,
69 assertEquals(optimalProfit, profit);
74 runKnapsackSolver(KnapsackSolver.SolverType.KNAPSACK_DYNAMIC_PROGRAMMING_SOLVER, profits,
76 assertEquals(optimalProfit, profit);
79 if (numOfItems <= maxNumberOfItemsFor64ItemsSolver) {
80 final long profit = runKnapsackSolver(
81 KnapsackSolver.SolverType.KNAPSACK_64ITEMS_SOLVER, profits, weights, capacities);
82 assertEquals(optimalProfit, profit);
88 final long[] profits = {1, 2, 3, 4, 5, 6, 7, 8, 9};
89 final long[][] weights = {{1, 2, 3, 4, 5, 6, 7, 8, 9}};
90 final long[] capacities = {34};
91 final long optimalProfit = 34;
92 solveKnapsackProblem(profits, weights, capacities, optimalProfit);
97 final long[] profits = {1, 2, 3, 4, 5, 6, 7, 8, 9};
98 final long[][] weights = {{1, 2, 3, 4, 5, 6, 7, 8, 9}, {1, 1, 1, 1, 1, 1, 1, 1, 1}};
99 final long[] capacities = {34, 4};
100 final long optimalProfit = 30;
101 solveKnapsackProblem(profits, weights, capacities, optimalProfit);
106 final long[] profits = {360, 83, 59, 130, 431, 67, 230, 52, 93, 125, 670, 892, 600, 38, 48, 147,
107 78, 256, 63, 17, 120, 164, 432, 35, 92, 110, 22, 42, 50, 323, 514, 28, 87, 73, 78, 15, 26,
108 78, 210, 36, 85, 189, 274, 43, 33, 10, 19, 389, 276, 312};
109 final long[][] weights = {{7, 0, 30, 22, 80, 94, 11, 81, 70, 64, 59, 18, 0, 36, 3, 8, 15, 42, 9,
110 0, 42, 47, 52, 32, 26, 48, 55, 6, 29, 84, 2, 4, 18, 56, 7, 29, 93, 44, 71, 3, 86, 66, 31,
111 65, 0, 79, 20, 65, 52, 13}};
112 final long[] capacities = {850};
113 final long optimalProfit = 7534;
114 solveKnapsackProblem(profits, weights, capacities, optimalProfit);