DotNet Reference

DotNet Reference

Knapsack.cs
Go to the documentation of this file.
1 // Copyright 2010-2018 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 // [START program]
15 // [START import]
16 using System;
18 // [END import]
19 
20 public class Knapsack
21 {
22  static void Main()
23  {
24  // [START solver]
25  KnapsackSolver solver = new KnapsackSolver(
26  KnapsackSolver.SolverType.KNAPSACK_MULTIDIMENSION_BRANCH_AND_BOUND_SOLVER,
27  "KnapsackExample");
28  // [END solver]
29 
30  // [START data]
31  long[] values = { 360, 83, 59, 130, 431, 67, 230, 52, 93,
32  125, 670, 892, 600, 38, 48, 147, 78, 256,
33  63, 17, 120, 164, 432, 35, 92, 110, 22,
34  42, 50, 323, 514, 28, 87, 73, 78, 15,
35  26, 78, 210, 36, 85, 189, 274, 43, 33,
36  10, 19, 389, 276, 312 };
37 
38  long[,] weights = { { 7, 0, 30, 22, 80, 94, 11, 81, 70,
39  64, 59, 18, 0, 36, 3, 8, 15, 42,
40  9, 0, 42, 47, 52, 32, 26, 48, 55,
41  6, 29, 84, 2, 4, 18, 56, 7, 29,
42  93, 44, 71, 3, 86, 66, 31, 65, 0,
43  79, 20, 65, 52, 13 } };
44 
45  long[] capacities = { 850 };
46  // [END data]
47 
48  // [START solve]
49  solver.Init(values, weights, capacities);
50  long computedValue = solver.Solve();
51  // [END solve]
52 
53  // [START print_solution]
54  Console.WriteLine("Optimal Value = " + computedValue);
55  // [END print_solution]
56  }
57 }
58 // [END program]
SolverType
long Solve()
Definition: Knapsack.cs:21
void Init(long[] profits, long[,] weights, long[] capacities)