Skip to content

Commit 732aeea

Browse files
jgFagescprudhom
authored andcommitted
fix div propagators
1 parent 6eb98b0 commit 732aeea

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ NEXT MILESTONE
1212
#### Constraints & LCG
1313
- Add table reformulation for pow constraint when LCG is on
1414
- Reduce runtime for building table reformulations by excluding the result variable from the tuple generation
15+
- Fix integer overflow in PropDivXYZLight
16+
- Fix bounded domains management with LCG in PropDivXYZ
1517

1618
### Deprecated API (to be removed in next release):
1719

solver/src/main/java/org/chocosolver/solver/constraints/ternary/PropDivXYZ.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -358,14 +358,14 @@ private boolean updateAbsZ() throws ContradictionException {
358358
protected boolean sameSign(IntVar a, IntVar b, Reason r) throws ContradictionException {
359359
boolean res = false;
360360
if (b.getLB() >= 0) {
361-
res = a.updateLowerBound(0, this,
361+
int minValue = b.getLB() > 0 ? 1 : 0;
362+
res = a.updateLowerBound(minValue, this,
362363
lcg() ? Reason.gather(r, b.getMinLit()) : Reason.undef());
363-
}
364-
if (b.getUB() <= 0) {
365-
res |= a.updateUpperBound(0, this,
364+
} else if (b.getUB() <= 0) {
365+
int maxValue = b.getUB() < 0 ? -1 : 0;
366+
res |= a.updateUpperBound(maxValue, this,
366367
lcg() ? Reason.gather(r, b.getMaxLit()) : Reason.undef());
367-
}
368-
if (!b.contains(0)) {
368+
} else if (!b.contains(0)) {
369369
res |= a.removeValue(0, this,
370370
lcg() ? Reason.gather(r, b.getLit(0, LR_EQ)) : Reason.undef());
371371
}

solver/src/main/java/org/chocosolver/solver/constraints/ternary/PropDivXYZLight.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.chocosolver.solver.variables.IntVar;
2020
import org.chocosolver.solver.variables.events.IntEventType;
2121
import org.chocosolver.util.ESat;
22+
import org.chocosolver.util.tools.MathUtils;
2223

2324
/**
2425
* Z = ceil(X/Y)
@@ -95,31 +96,30 @@ public void propagate(int evtmask) throws ContradictionException {
9596
int z_max = Z.getUB();
9697

9798
// z >= ceil(x.min / y.max)
98-
hasChanged = Z.updateLowerBound((x_min + y_max - 1) / y_max, this,
99+
hasChanged = Z.updateLowerBound(MathUtils.safeAdd(x_min, y_max - 1) / y_max, this,
99100
lcg() ? Reason.r(X.getMinLit(), Y.getMaxLit()) : Reason.undef());
100101
// z <= ceil(x.max / y.min)
101-
hasChanged |= Z.updateUpperBound((x_max + y_min - 1) / y_min, this,
102+
hasChanged |= Z.updateUpperBound(MathUtils.safeAdd(x_max, y_min - 1) / y_min, this,
102103
lcg() ? Reason.r(X.getMaxLit(), Y.getMinLit()) : Reason.undef());
103104

104105
// x >= y.min * (z.min - 1) + 1
105-
hasChanged |= X.updateLowerBound(y_min * (z_min - 1) + 1, this,
106+
hasChanged |= X.updateLowerBound(MathUtils.safeMultiply(y_min, (z_min - 1)) + 1, this,
106107
lcg() ? Reason.r(Y.getMinLit(), Z.getMinLit()) : Reason.undef());
107108
// x <= y.max * z.max
108-
hasChanged |= X.updateUpperBound(y_max * z_max, this,
109+
hasChanged |= X.updateUpperBound(MathUtils.safeMultiply(y_max, z_max), this,
109110
lcg() ? Reason.r(Y.getMaxLit(), Z.getMaxLit()) : Reason.undef());
110111

111112
// y >= ceil(x.min / z.max)
112113
if (z_max >= 1) {
113-
hasChanged |= Y.updateLowerBound((x_min + z_max - 1) / z_max, this,
114+
hasChanged |= Y.updateLowerBound(MathUtils.safeAdd(x_min, z_max - 1) / z_max, this,
114115
lcg() ? Reason.r(X.getMinLit(), Z.getMaxLit()) : Reason.undef());
115116
}
116117

117118
// y <= ceil(x.max / z.min-1) - 1
118119
if (z_min >= 2) {
119-
hasChanged |= Y.updateUpperBound((x_max + z_min - 2) / (z_min - 1) - 1, this,
120+
hasChanged |= Y.updateUpperBound(MathUtils.safeAdd(x_max, z_min - 2) / (z_min - 1) - 1, this,
120121
lcg() ? Reason.r(X.getMaxLit(), Z.getMinLit()) : Reason.undef());
121122
}
122-
123123
} while (hasChanged);
124124
}
125125

0 commit comments

Comments
 (0)