2025-01-10 11:35:44 +01:00
|
|
|
// Copyright 2010-2025 Google LLC
|
2011-03-29 10:41:34 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
2019-05-10 22:51:10 +02:00
|
|
|
// [START program]
|
2020-05-26 09:30:42 +02:00
|
|
|
package com.google.ortools.algorithms.samples;
|
2019-05-10 22:51:10 +02:00
|
|
|
// [START import]
|
2020-09-11 03:07:18 +02:00
|
|
|
import com.google.ortools.Loader;
|
2014-06-11 22:28:04 +00:00
|
|
|
import com.google.ortools.algorithms.KnapsackSolver;
|
2019-06-04 15:50:28 -04:00
|
|
|
import java.util.ArrayList;
|
2025-07-18 08:11:03 +00:00
|
|
|
|
2019-05-10 22:51:10 +02:00
|
|
|
// [END import]
|
2011-03-29 10:41:34 +00:00
|
|
|
|
2025-07-18 08:11:03 +00:00
|
|
|
/** Sample showing how to model using the knapsack solver. */
|
2011-03-29 10:41:34 +00:00
|
|
|
public class Knapsack {
|
2020-05-26 09:30:42 +02:00
|
|
|
private Knapsack() {}
|
2011-03-29 10:41:34 +00:00
|
|
|
|
|
|
|
|
private static void solve() {
|
2019-05-10 22:51:10 +02:00
|
|
|
// [START solver]
|
2019-05-06 10:31:03 +02:00
|
|
|
KnapsackSolver solver = new KnapsackSolver(
|
|
|
|
|
KnapsackSolver.SolverType.KNAPSACK_MULTIDIMENSION_BRANCH_AND_BOUND_SOLVER, "test");
|
2019-05-10 22:51:10 +02:00
|
|
|
// [END solver]
|
|
|
|
|
|
|
|
|
|
// [START data]
|
|
|
|
|
final long[] values = {360, 83, 59, 130, 431, 67, 230, 52, 93, 125, 670, 892, 600, 38, 48, 147,
|
2019-05-06 10:31:03 +02:00
|
|
|
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};
|
|
|
|
|
|
|
|
|
|
final long[][] weights = {{7, 0, 30, 22, 80, 94, 11, 81, 70, 64, 59, 18, 0, 36, 3, 8, 15, 42, 9,
|
|
|
|
|
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}};
|
2011-03-29 10:41:34 +00:00
|
|
|
|
|
|
|
|
final long[] capacities = {850};
|
2019-05-10 22:51:10 +02:00
|
|
|
// [END data]
|
2011-03-29 10:41:34 +00:00
|
|
|
|
2019-05-10 22:51:10 +02:00
|
|
|
// [START solve]
|
|
|
|
|
solver.init(values, weights, capacities);
|
|
|
|
|
final long computedValue = solver.solve();
|
|
|
|
|
// [END solve]
|
2014-07-09 18:54:54 +00:00
|
|
|
|
2019-05-10 22:51:10 +02:00
|
|
|
// [START print_solution]
|
2020-05-26 09:30:42 +02:00
|
|
|
ArrayList<Integer> packedItems = new ArrayList<>();
|
|
|
|
|
ArrayList<Long> packedWeights = new ArrayList<>();
|
2019-06-04 15:50:28 -04:00
|
|
|
int totalWeight = 0;
|
|
|
|
|
System.out.println("Total value = " + computedValue);
|
|
|
|
|
for (int i = 0; i < values.length; i++) {
|
|
|
|
|
if (solver.bestSolutionContains(i)) {
|
|
|
|
|
packedItems.add(i);
|
|
|
|
|
packedWeights.add(weights[0][i]);
|
|
|
|
|
totalWeight = (int) (totalWeight + weights[0][i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
System.out.println("Total weight: " + totalWeight);
|
|
|
|
|
System.out.println("Packed items: " + packedItems);
|
|
|
|
|
System.out.println("Packed weights: " + packedWeights);
|
2019-05-10 22:51:10 +02:00
|
|
|
// [END print_solution]
|
2011-03-29 10:41:34 +00:00
|
|
|
}
|
|
|
|
|
|
2018-11-10 23:56:52 +01:00
|
|
|
public static void main(String[] args) throws Exception {
|
2020-09-11 03:07:18 +02:00
|
|
|
Loader.loadNativeLibraries();
|
2018-11-10 23:56:52 +01:00
|
|
|
Knapsack.solve();
|
|
|
|
|
}
|
2011-03-29 10:41:34 +00:00
|
|
|
}
|
2019-05-10 22:51:10 +02:00
|
|
|
// [END program]
|