unexpected behavior of "ift" (IfArExpression)

20 views
Skip to first unread message

Chris Houser

unread,
Oct 4, 2022, 4:34:26 PM10/4/22
to choco-solver
The docs for ift say that it returns "if(b, y1, y2)", so I was surprised to see that it apparently also disallows y1 from being equal to y2. I would have expected that if b were true, y2 would be unconstrained -- is this not the normal meaning of "if"?

Below is a demonstration of what I mean. It sets up a constraint like "n = if(p, a, b)", and propagates two time, once starting with p unconstrained ("p = [0, 1]") and once with p set to true ("p = 1"). In both cases a starts as "a = 2" and b starts as "b = {1..3}", and I would expect it to remain that way after propagation. However when p = 1, b is changed to exclude 2 (that is, "b = {1, 3}") -- why should this be?

import org.chocosolver.solver.Model;
import org.chocosolver.solver.exception.ContradictionException;
import org.chocosolver.solver.variables.BoolVar;
import org.chocosolver.solver.variables.IntVar;

public class IftExample {
    public static void iftExample(Model m, BoolVar p) {
        IntVar  n = m.intVar ("n", -10, 10);
        IntVar  a = m.intVar ("a", new int[]{2});
        IntVar  b = m.intVar ("b", new int[]{1, 2, 3});

        n.eq(p.ift(a, b)).post();

        try {
            m.getSolver().propagate();
            System.out.println(m.toString());
        }
        catch(ContradictionException ex) {
            System.out.println("contradiction!");
        }
    }

    public static void main(String args[]) {
        // First with an unconstrained predicate
        Model mA = new Model();
        BoolVar pA = mA.boolVar("pA");
        iftExample(mA, pA);

        // Now with predicate set to true
        Model mB = new Model();
        BoolVar pB = mB.boolVar("pB", true);
        iftExample(mB, pB);
    }
}

If this is intended behavior, perhaps the docs for ift could be clarified?

Thanks,
--Chouser

cpru...@gmail.com

unread,
Oct 5, 2022, 4:54:30 AM10/5/22
to choco-solver
Hi

You are right, the decomposition of the expression is not correct.
I'll fix that asap.

Best regards,
Charles

cpru...@gmail.com

unread,
Oct 5, 2022, 5:02:07 AM10/5/22
to choco-solver

Chris Houser

unread,
Oct 5, 2022, 8:28:54 AM10/5/22
to choco-solver
Excellent, thanks for the prompt response!
--Chouser
Reply all
Reply to author
Forward
0 new messages