0

I'm trying to use java choco solver, a CP solver, to color a graph. However i can't seem to get it to work. Even the code in the tutorial doesn't work:

    int n = 8;
    Model model = new Model(n + "-queens problem");
    IntVar[] vars = new IntVar[n];
    for(int q = 0; q < n; q++){
        vars[q] = (IntVar) model.intVar("Q_"+q, 1, n);
    }
    for(int i  = 0; i < n-1; i++){
        for(int j = i + 1; j < n; j++){
            model.arithm(vars[i], "!=",vars[j]).post();
            model.arithm(vars[i], "!=", vars[j], "-", j - i).post();
            model.arithm(vars[i], "!=", vars[j], "+", j - i).post();
        }
    }
    Solution solution = model.getSolver().findSolution();
    if(solution != null){
        System.out.println(solution.toString());
    }

I always get the folowing error:

The method arithm(IntVar, String, int) in the type IIntConstraintFactory is not applicable for the arguments (IntVar, String, IntVar), which i don't understand because vars[j], should be an IntVar.

I hope someone can help!

Kind Regards,

Nicholas

  • ```(IntVar) model.intVar("Q_"+q, 1, n);``` This looks scary. Although i'm not a big java-programmer, you should have a really good reason to use the cast here! I can imagine that this is the source of the problem. Check what java is doing with ```"Q_" + q``` where q is an integer. (Or the more clean approach: do a valid string-building to generate your "Q_"+q string, then remove that cast and try again) – sascha Mar 27 '17 at 11:23

1 Answers1

0

Your code works for me! Maybe your IDE is not well configured. I do not understand the error message because there is a method model.arithm(IntVar, String, IntVar)... Do you see it in the source code when ctrl+click on it? Can you show your imports?

PS: The cast in IntVar is useless (if your IDE asks for it, then you have a configuration issue).