Skip to content

Commit

Permalink
Fix bin packing initial propagation (#1113)
Browse files Browse the repository at this point in the history
* #1112 : fix bin packing initial propagation

* move code initialising P and sumP from constructor to fullPropagation in PropBinPacking

* clear R and P sets before full propagation in PropBinPacking
  • Loading branch information
ArthurGodet authored Nov 12, 2024
1 parent 758489f commit c78d3d9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,20 +127,8 @@ public PropBinPacking(IntVar[] itemBin, int[] itemSize, IntVar[] binLoad, int of
for(int j = 0; j<nbAvailableBins; j++) {
P[j] = SetFactory.makeStoredSet(SetType.BITSET, 0, itemBin[0].getModel());
R[j] = SetFactory.makeStoredSet(SetType.BITSET, 0, itemBin[0].getModel());
int pj = 0;
int rj = 0;
for(int i = 0; i<nbItems; i++) {
if(itemBin[i].contains(j+offset)) {
P[j].add(i);
pj += itemSize[i];
if(itemBin[i].isInstantiated()) {
R[j].add(i);
rj += itemSize[i];
}
}
}
sumR[j] = itemBin[0].getModel().getEnvironment().makeInt(rj);
sumP[j] = itemBin[0].getModel().getEnvironment().makeInt(pj);
sumR[j] = itemBin[0].getModel().getEnvironment().makeInt(0);
sumP[j] = itemBin[0].getModel().getEnvironment().makeInt(0);
}

binsToProcess = new BitSet(nbAvailableBins);
Expand Down Expand Up @@ -345,9 +333,26 @@ public void propagate(int idxVarInProp, int mask) throws ContradictionException
forcePropagate(PropagatorEventType.CUSTOM_PROPAGATION);
}

private void initializeBinsDataStructure() {
for(int j = 0; j<nbAvailableBins; j++) {
R[j].clear();
P[j].clear();
sumR[j].set(0);
int pj = 0;
for (int i = 0; i < nbItems; i++) {
if (itemBin[i].contains(j + offset)) {
P[j].add(i);
pj += itemSize[i];
}
}
sumP[j].set(pj);
}
}

@Override
public void propagate(int evtmask) throws ContradictionException {
if(PropagatorEventType.isFullPropagation(evtmask)) {
initializeBinsDataStructure();
for(int i = 0; i<itemBin.length; i++) { // Pack All
itemBin[i].updateBounds(offset, nbAvailableBins+offset-1, this);
if(itemBin[i].isInstantiated()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@

import org.chocosolver.solver.Cause;
import org.chocosolver.solver.Model;
import org.chocosolver.solver.Solver;
import org.chocosolver.solver.constraints.Constraint;
import org.chocosolver.solver.constraints.ConstraintsName;
import org.chocosolver.solver.constraints.nary.binPacking.PropBinPacking;
import org.chocosolver.solver.exception.ContradictionException;
import org.chocosolver.solver.search.strategy.Search;
import org.chocosolver.solver.variables.BoolVar;
import org.chocosolver.solver.variables.IntVar;
import org.chocosolver.solver.variables.impl.BitsetIntVarImpl;
import org.chocosolver.solver.variables.impl.BoolVarImpl;
import org.chocosolver.util.ESat;
import org.chocosolver.util.tools.ArrayUtils;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
Expand Down Expand Up @@ -157,4 +161,26 @@ public void testFixedLoadBackPropag2() {
model.getSolver().findAllSolutions();
Assert.assertEquals(model.getSolver().getSolutionCount(), 16);
}

@Test(groups = "1s", timeOut = 60000)
public void testBug1() {
Model model = new Model();
int[] itemSize = new int[]{2, 9};
IntVar[] itemBin = new IntVar[2];
itemBin[0] = new BitsetIntVarImpl("X_0", new int[]{0, 1}, model);
itemBin[1] = new BitsetIntVarImpl("X_1", new int[]{0}, model);
IntVar[] binLoad = new IntVar[2];
binLoad[0] = new BoolVarImpl("X_6", model);
binLoad[1] = new BitsetIntVarImpl("X_8", new int[]{0, 9, 11}, model);
Constraint c = model.binPacking(itemBin, itemSize, binLoad, 0);
c.post();
Assert.assertEquals(c.isSatisfied(), ESat.FALSE);
Solver solver = model.getSolver();
try {
solver.propagate();
Assert.fail();
} catch (ContradictionException e) {
// should fail
}
}
}

0 comments on commit c78d3d9

Please sign in to comment.