Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
KnapsackSolverTests.cs
Go to the documentation of this file.
1// Copyright 2010-2024 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
14using System;
15using Xunit;
17
19{
21{
22 private long RunKnapsackSolver(KnapsackSolver.SolverType solverType, long[] profits, long[,] weights,
23 long[] capacities)
24 {
25 KnapsackSolver solver = new KnapsackSolver(solverType, "test");
26 Assert.NotNull(solver);
27 solver.Init(profits, weights, capacities);
28
29 return solver.Solve();
30 }
31
32 private void SolveKnapsackProblem(long[] profits, long[,] weights, long[] capacities, long optimalProfit)
33 {
34 int maxNumberOfItemsForBruteForce = 20;
35 int maxNumberOfItemsForDivideAndConquer = 32;
36 int maxNumberOfItemsFor64ItemsSolver = 64;
37
38 {
39 long profit = RunKnapsackSolver(KnapsackSolver.SolverType.KNAPSACK_MULTIDIMENSION_BRANCH_AND_BOUND_SOLVER,
40 profits, weights, capacities);
41 Assert.Equal(optimalProfit, profit);
42 }
43
44 // Other solvers don't support multidimension models.
45 if (weights.Length > 1)
46 {
47 return;
48 }
49
50 int numOfItems = profits.Length;
51
52 if (numOfItems <= maxNumberOfItemsForBruteForce)
53 {
54 long profit =
55 RunKnapsackSolver(KnapsackSolver.SolverType.KNAPSACK_BRUTE_FORCE_SOLVER, profits, weights, capacities);
56 Assert.Equal(optimalProfit, profit);
57 }
58
59 if (numOfItems <= maxNumberOfItemsForDivideAndConquer)
60 {
61 long profit = RunKnapsackSolver(KnapsackSolver.SolverType.KNAPSACK_DIVIDE_AND_CONQUER_SOLVER, profits,
62 weights, capacities);
63 Assert.Equal(optimalProfit, profit);
64 }
65
66 {
67 long profit = RunKnapsackSolver(KnapsackSolver.SolverType.KNAPSACK_DYNAMIC_PROGRAMMING_SOLVER, profits,
68 weights, capacities);
69 Assert.Equal(optimalProfit, profit);
70 }
71
72 if (numOfItems <= maxNumberOfItemsFor64ItemsSolver)
73 {
74 long profit =
75 RunKnapsackSolver(KnapsackSolver.SolverType.KNAPSACK_64ITEMS_SOLVER, profits, weights, capacities);
76 Assert.Equal(optimalProfit, profit);
77 }
78 }
79
80 [Fact]
81 public void SolveOneDimension()
82 {
83 long[] profits = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
84 long[,] weights = { { 1, 2, 3, 4, 5, 6, 7, 8, 9 } };
85 long[] capacities = { 34 };
86 long optimalProfit = 34;
87 SolveKnapsackProblem(profits, weights, capacities, optimalProfit);
88 }
89
90 [Fact]
91 public void SolveTwoDimensions()
92 {
93 long[] profits = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
94 long[,] weights = { { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, { 1, 1, 1, 1, 1, 1, 1, 1, 1 } };
95 long[] capacities = { 34, 4 };
96 long optimalProfit = 30;
97 SolveKnapsackProblem(profits, weights, capacities, optimalProfit);
98 }
99
100 [Fact]
102 {
103 long[] profits = { 360, 83, 59, 130, 431, 67, 230, 52, 93, 125, 670, 892, 600, 38, 48, 147, 78,
104 256, 63, 17, 120, 164, 432, 35, 92, 110, 22, 42, 50, 323, 514, 28, 87, 73,
105 78, 15, 26, 78, 210, 36, 85, 189, 274, 43, 33, 10, 19, 389, 276, 312 };
106 long[,] weights = { { 7, 0, 30, 22, 80, 94, 11, 81, 70, 64, 59, 18, 0, 36, 3, 8, 15,
107 42, 9, 0, 42, 47, 52, 32, 26, 48, 55, 6, 29, 84, 2, 4, 18, 56,
108 7, 29, 93, 44, 71, 3, 86, 66, 31, 65, 0, 79, 20, 65, 52, 13 } };
109 long[] capacities = { 850 };
110 long optimalProfit = 7534;
111 SolveKnapsackProblem(profits, weights, capacities, optimalProfit);
112 }
113}
114} // namespace Google.OrTools.Tests
void Init(long[] profits, long[,] weights, long[] capacities)