Skip to content

Instantly share code, notes, and snippets.

@juriad
Created November 9, 2014 08:02
Show Gist options
  • Select an option

  • Save juriad/d1de8b587cf6f83abc07 to your computer and use it in GitHub Desktop.

Select an option

Save juriad/d1de8b587cf6f83abc07 to your computer and use it in GitHub Desktop.
Propagator for if-then-else construct.
// the propagated constraint is:
// Z = if B then X else Y endif
// propagator for Z:
if (lb(B) < ub(B)) { // B is not fixed
setlb(Z, min(lb(X), lb(Y));
setub(Z, max(ub(X), ub(Y));
} else if (lb(B) == 1) { // B is fixed and B = 1
setlb(Z, lb(X));
setub(Z, ub(X));
} else { // B is fixed and B = 0
setlb(Z, lb(Y));
setub(Z, ub(Y));
}
// propagator for X:
if (lb(B) == up(B) && lb(B) == 1) { // B is fixed and B = 1
// B = 1, we set X according to Z
setlb(X, lb(Z));
setub(X, ub(Z));
} // else nothing is known
// propagator for Y:
if (lb(B) == up(B) && lb(B) == 0) { // B is fixed and B = 0
// B = 0, we set Y according to Z
setlb(Y, lb(Z));
setub(Y, ub(Z));
} // else nothing is known
// propagator for B:
// this one is harder; based on bound of X, Y, Z we want to determine value of B
// these two booleans have meaning if such value 1/0 is possible
// they test if ranges of variables have non-empty intersection
b1 = lb(X) <= ub(Z) && lb(Z) <= ub(X); // X intersects Z
b0 = lb(Y) <= ub(Z) && lb(Z) <= ub(Y); // Y intersects Z
if (b1) {
if(b0) { // both values possible
setlb(B, 0);
setub(B, 1);
} else { // only B=1 possible
setlb(B, 1);
setub(B, 1);
}
} else {
if(b0) { // only B=0 possible
setlb(B, 0);
setub(B, 0);
} else { // no value possible
fail;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment