Fix MultipleKnapsackSat

This commit is contained in:
Mizux Seiha
2021-12-02 09:39:12 +01:00
parent 1d48a794d4
commit 5c3d2c95b0
2 changed files with 16 additions and 12 deletions

View File

@@ -67,25 +67,27 @@ public class MultipleKnapsackSat {
// The amount packed in each bin cannot exceed its capacity.
for (int b : allBins) {
IntVar[] binWeights = new IntVar[numItems];
IntVar[] vars = new IntVar[numItems];
for (int i : allItems) {
binWeights[i] = LinearExpr.term(x[i][b], weights[i]);
vars[i] = x[i][b];
}
model.addLessOrEqual(LinearExpr.sum(binWeights), 1);
model.addLessOrEqual(LinearExpr.scalProd(vars, weights), binCapacities[b]);
}
// [END constraints]
// Objective.
// [START objective]
// Maximize total value of packed items.
IntVar[] objective = new IntVar[numItems * numBins];
IntVar[] objectiveVars = new IntVar[numItems * numBins];
int[] objectiveValues = new int[numItems * numBins];
for (int i : allItems) {
for (int b : allBins) {
int k = i * numBins + b;
objective[k] = LinearExpr.term(x[i][b], values[i]);
objectiveVars[k] = x[i][b];
objectiveValues[k] = values[i];
}
}
model.maximize(LinearExpr.sum(objective));
model.maximize(LinearExpr.scalProd(objectiveVars, objectiveValues));
// [END objective]
// [START solve]