Skip to content

Commit

Permalink
Add details to printed solution in Knapsack.java (like those in knaps…
Browse files Browse the repository at this point in the history
…ack.cc)
  • Loading branch information
paultrow committed Jun 4, 2019
1 parent a31bbd5 commit 2a5e8fe
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions ortools/algorithms/samples/Knapsack.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// [START program]
// [START import]
import com.google.ortools.algorithms.KnapsackSolver;
import java.util.ArrayList;
// [END import]

/**
Expand Down Expand Up @@ -49,8 +50,20 @@ private static void solve() {
// [END solve]

// [START print_solution]
System.out.println("Solving knapsack with " + values.length + " items");
System.out.println("Optimal Value = " + computedValue);
ArrayList<Integer> packedItems = new ArrayList<Integer>();
ArrayList<Long> packedWeights = new ArrayList<Long>();
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);
// [END print_solution]
}

Expand Down

0 comments on commit 2a5e8fe

Please sign in to comment.