Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cpru min max #665

Merged
merged 3 commits into from
Jan 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand All @@ -150,6 +59,12 @@ private void _filter() 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -149,6 +55,12 @@ private void _filter() 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
Expand Down