Java Reference

Java Reference

KnapsackSolverTest.java
Go to the documentation of this file.
1 // Copyright 2010-2022 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 package com.google.ortools.algorithms;
15 
16 import static org.junit.jupiter.api.Assertions.assertEquals;
17 import static org.junit.jupiter.api.Assertions.assertNotNull;
18 
19 import com.google.ortools.Loader;
20 import org.junit.jupiter.api.BeforeEach;
21 import org.junit.jupiter.api.Test;
22 
24 public final class KnapsackSolverTest {
25  @BeforeEach
26  public void setUp() {
28  }
29 
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);
35 
36  return solver.solve();
37  }
38 
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;
44 
45  {
46  final long profit = runKnapsackSolver(
47  KnapsackSolver.SolverType.KNAPSACK_MULTIDIMENSION_BRANCH_AND_BOUND_SOLVER, profits,
48  weights, capacities);
49  assertEquals(optimalProfit, profit);
50  }
51 
52  // Other solvers don't support multidimension models.
53  if (weights.length > 1) {
54  return;
55  }
56 
57  final int numOfItems = profits.length;
58 
59  if (numOfItems <= maxNumberOfItemsForBruteForce) {
60  final long profit = runKnapsackSolver(
61  KnapsackSolver.SolverType.KNAPSACK_BRUTE_FORCE_SOLVER, profits, weights, capacities);
62  assertEquals(optimalProfit, profit);
63  }
64 
65  if (numOfItems <= maxNumberOfItemsForDivideAndConquer) {
66  final long profit =
67  runKnapsackSolver(KnapsackSolver.SolverType.KNAPSACK_DIVIDE_AND_CONQUER_SOLVER, profits,
68  weights, capacities);
69  assertEquals(optimalProfit, profit);
70  }
71 
72  {
73  final long profit =
74  runKnapsackSolver(KnapsackSolver.SolverType.KNAPSACK_DYNAMIC_PROGRAMMING_SOLVER, profits,
75  weights, capacities);
76  assertEquals(optimalProfit, profit);
77  }
78 
79  if (numOfItems <= maxNumberOfItemsFor64ItemsSolver) {
80  final long profit = runKnapsackSolver(
81  KnapsackSolver.SolverType.KNAPSACK_64ITEMS_SOLVER, profits, weights, capacities);
82  assertEquals(optimalProfit, profit);
83  }
84  }
85 
86  @Test
87  public void testSolveOneDimension() {
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);
93  }
94 
95  @Test
96  public void testSolveTwoDimensions() {
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);
102  }
103 
104  @Test
105  public void testSolveBigOneDimension() {
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);
115  }
116 }
Load native libraries needed for using ortools-java.
Definition: Loader.java:33
static synchronized void loadNativeLibraries()
Definition: Loader.java:104
Test the Knapsack solver java interface.