From 7ce8e1e1706496ea54ac13dc424293ec2b70f9d9 Mon Sep 17 00:00:00 2001 From: cprudhom Date: Wed, 22 Jan 2020 11:51:52 +0100 Subject: [PATCH 1/2] Reduce code of PropMaxBC and PropMinBC --- .../solver/constraints/ternary/PropMaxBC.java | 99 +------------------ .../solver/constraints/ternary/PropMinBC.java | 94 ------------------ 2 files changed, 4 insertions(+), 189 deletions(-) diff --git a/solver/src/main/java/org/chocosolver/solver/constraints/ternary/PropMaxBC.java b/solver/src/main/java/org/chocosolver/solver/constraints/ternary/PropMaxBC.java index 53797eb3e9..4ad206da98 100644 --- a/solver/src/main/java/org/chocosolver/solver/constraints/ternary/PropMaxBC.java +++ b/solver/src/main/java/org/chocosolver/solver/constraints/ternary/PropMaxBC.java @@ -12,10 +12,13 @@ import org.chocosolver.solver.constraints.Propagator; import org.chocosolver.solver.constraints.PropagatorPriority; import org.chocosolver.solver.exception.ContradictionException; -import org.chocosolver.solver.exception.SolverException; +import org.chocosolver.solver.learn.ExplanationForSignedClause; +import org.chocosolver.solver.learn.Implications; import org.chocosolver.solver.variables.IntVar; import org.chocosolver.solver.variables.events.IntEventType; import org.chocosolver.util.ESat; +import org.chocosolver.util.objects.ValueSortedMap; +import org.chocosolver.util.objects.setDataStructures.iterable.IntIterableRangeSet; /** * X = MAX(Y,Z) @@ -43,100 +46,6 @@ public int getPropagationConditions(int vIdx) { @Override public void propagate(int evtmask) throws ContradictionException { - filter(); - } - - private void filter() throws ContradictionException { - int c = 0; - c += (vars[0].isInstantiated() ? 1 : 0); - c += (vars[1].isInstantiated() ? 2 : 0); - c += (vars[2].isInstantiated() ? 4 : 0); - switch (c) { - case 7: // everything is instantiated - case 6:// Z and Y are instantiated - vars[0].instantiateTo(Math.max(vars[1].getValue(), vars[2].getValue()), this); - break; - case 5: // X and Z are instantiated - { - int best = vars[0].getValue(); - int val2 = vars[2].getValue(); - if (best > val2) { - vars[1].instantiateTo(best, this); - } else if (best < val2) { - fails(); // TODO: could be more precise, for explanation purpose - } else { // X = Z - vars[1].updateUpperBound(best, this); - } - } - break; - case 4: // Z is instantiated - { - int val = vars[2].getValue(); - if (val > vars[1].getUB()) { // => X = Z - if (vars[0].instantiateTo(val, this)) { - setPassive(); - } - } else { - _filter(); - } - } - break; - case 3:// X and Y are instantiated - { - int best = vars[0].getValue(); - int val1 = vars[1].getValue(); - if (best > val1) { - vars[2].instantiateTo(best, this); - } else if (best < val1) { - fails(); // TODO: could be more precise, for explanation purpose - } else { // X = Y - vars[2].updateUpperBound(best, this); - } - } - break; - case 2: // Y is instantiated - { - int val = vars[1].getValue(); - if (val > vars[2].getUB()) { // => X = Y - if (vars[0].instantiateTo(val, this)) { - setPassive(); - } - } else { // val in Z - _filter(); - } - } - break; - case 1: // X is instantiated - { - int best = vars[0].getValue(); - if (!vars[1].contains(best) && !vars[2].contains(best)) { - fails(); // TODO: could be more precise, for explanation purpose - } - if (vars[1].getUB() < best) { - if (vars[2].instantiateTo(best, this)) { - setPassive(); - } - } else if (vars[2].getUB() < best) { - if (vars[1].instantiateTo(best, this)) { - setPassive(); - } - } else { - if (vars[1].updateUpperBound(best, this) | vars[2].updateUpperBound(best, this)) { - filter(); // to ensure idempotency for "free" - } - } - } - - break; - case 0: // otherwise - _filter(); - break; - default: - throw new SolverException("Unexpected mask " + c); - } - } - - private void _filter() throws ContradictionException { boolean change; do { change = vars[0].updateLowerBound(Math.max(vars[1].getLB(), vars[2].getLB()), this); diff --git a/solver/src/main/java/org/chocosolver/solver/constraints/ternary/PropMinBC.java b/solver/src/main/java/org/chocosolver/solver/constraints/ternary/PropMinBC.java index e42fcb599e..92d56ec7fa 100644 --- a/solver/src/main/java/org/chocosolver/solver/constraints/ternary/PropMinBC.java +++ b/solver/src/main/java/org/chocosolver/solver/constraints/ternary/PropMinBC.java @@ -12,7 +12,6 @@ import org.chocosolver.solver.constraints.Propagator; import org.chocosolver.solver.constraints.PropagatorPriority; import org.chocosolver.solver.exception.ContradictionException; -import org.chocosolver.solver.exception.SolverException; import org.chocosolver.solver.variables.IntVar; import org.chocosolver.solver.variables.events.IntEventType; import org.chocosolver.util.ESat; @@ -43,99 +42,6 @@ public int getPropagationConditions(int vIdx) { @Override public void propagate(int evtmask) throws ContradictionException { - filter(); - } - - private void filter() throws ContradictionException { - int c = 0; - c += (vars[0].isInstantiated() ? 1 : 0); - c += (vars[1].isInstantiated() ? 2 : 0); - c += (vars[2].isInstantiated() ? 4 : 0); - switch (c) { - case 7: // everything is instantiated - case 6:// Z and Y are instantiated - vars[0].instantiateTo(Math.min(vars[1].getValue(), vars[2].getValue()), this); - break; - case 5: // X and Z are instantiated - { - int best = vars[0].getValue(); - int val2 = vars[2].getValue(); - if (best < val2) { - vars[1].instantiateTo(best, this); - } else if (best > val2) { - fails(); // TODO: could be more precise, for explanation purpose - } else { // X = Z - vars[1].updateLowerBound(best, this); - } - } - break; - case 4: // Z is instantiated - { - int val = vars[2].getValue(); - if (val < vars[1].getLB()) { // => X = Z - if(vars[0].instantiateTo(val, this)) { - setPassive(); - } - } else { - _filter(); - } - } - break; - case 3:// X and Y are instantiated - { - int best = vars[0].getValue(); - int val1 = vars[1].getValue(); - if (best < val1) { - vars[2].instantiateTo(best, this); - } else if (best > val1) { - fails(); // TODO: could be more precise, for explanation purpose - } else { // X = Y - vars[2].updateLowerBound(best, this); - } - } - break; - case 2: // Y is instantiated - { - int val = vars[1].getValue(); - if (val < vars[2].getLB()) { // => X = Y - if(vars[0].instantiateTo(val, this)) { - setPassive(); - } - } else { // val in Z - _filter(); - } - } - break; - case 1: // X is instantiated - { - int best = vars[0].getValue(); - if (!vars[1].contains(best) && !vars[2].contains(best)) { - fails(); // TODO: could be more precise, for explanation purpose - } - if (vars[1].getLB() > best) { - if(vars[2].instantiateTo(best, this)) { - setPassive(); - } - } else if (vars[2].getLB() > best) { - if(vars[1].instantiateTo(best, this)) { - setPassive(); - } - } else { - if (vars[1].updateLowerBound(best, this) | vars[2].updateLowerBound(best, this)) { - filter(); // to ensure idempotency for "free" - } - } - } - - break; - case 0: // otherwise - _filter(); - break; - default: throw new SolverException("Unexpected mask "+c); - } - } - - private void _filter() throws ContradictionException { boolean change; do { change = vars[0].updateLowerBound(Math.min(vars[1].getLB(), vars[2].getLB()), this); From f864f03006f00f55219a47fd056d06531a185266 Mon Sep 17 00:00:00 2001 From: cprudhom Date: Wed, 22 Jan 2020 11:51:52 +0100 Subject: [PATCH 2/2] Reduce code of PropMaxBC and PropMinBC + deal with entailment --- .../chocosolver/solver/constraints/ternary/PropMaxBC.java | 6 ++++++ .../chocosolver/solver/constraints/ternary/PropMinBC.java | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/solver/src/main/java/org/chocosolver/solver/constraints/ternary/PropMaxBC.java b/solver/src/main/java/org/chocosolver/solver/constraints/ternary/PropMaxBC.java index 4ad206da98..e72138e6f2 100644 --- a/solver/src/main/java/org/chocosolver/solver/constraints/ternary/PropMaxBC.java +++ b/solver/src/main/java/org/chocosolver/solver/constraints/ternary/PropMaxBC.java @@ -59,6 +59,12 @@ public void propagate(int evtmask) throws ContradictionException { change |= vars[2].updateLowerBound(vars[0].getLB(), this); } } while (change); + if(vars[0].isInstantiated()){ + int bst = vars[0].getValue(); + if(vars[1].isInstantiatedTo(bst) || vars[2].isInstantiatedTo(bst)){ + setPassive(); + } + } } @Override diff --git a/solver/src/main/java/org/chocosolver/solver/constraints/ternary/PropMinBC.java b/solver/src/main/java/org/chocosolver/solver/constraints/ternary/PropMinBC.java index 92d56ec7fa..628b535513 100644 --- a/solver/src/main/java/org/chocosolver/solver/constraints/ternary/PropMinBC.java +++ b/solver/src/main/java/org/chocosolver/solver/constraints/ternary/PropMinBC.java @@ -55,6 +55,12 @@ public void propagate(int evtmask) throws ContradictionException { change |= vars[2].updateUpperBound(vars[0].getUB(), this); } } while (change); + if(vars[0].isInstantiated()){ + int bst = vars[0].getValue(); + if(vars[1].isInstantiatedTo(bst) || vars[2].isInstantiatedTo(bst)){ + setPassive(); + } + } } @Override