Abdelhamid Nour
Abdelhamid Nour

Reputation: 21

Converting MiniZinc model to choco code

My minizinc model is working fine, but i need to convert it to Java code so i used choco to do it. The problem I am facing right now is the mechanism that minizinc woks with is different form choco. I wrote the constraints i used in minizinc exactly in choco but it didn't work.

Suppose that :

minizinc model is :

array[sub_set] of var cl_set: cl_id;
constraint alldifferent(cl_id);
constraint forall(i in sub_set) ( sub_cap[i] <= cl_cap[cl_id[i]]);

choco code is :

cl_id = VF.boundedArray("", sub_sz, 0, cl_sz - 1, solver);
solver.post(ICF.alldifferent(cl_id));
for (int i = 0; i < sub_sz; i++) {
     Constraint a = ICF.arithm(VF.fixed(cl_cap[cl_id[i].getValue()], solver), ">=", sub_cap[i]);
      solver.post(a);
        }

What should I do to make the choco constraint work the same way as minizinc?

Upvotes: 2

Views: 588

Answers (1)

Jean-Guillaume Fages
Jean-Guillaume Fages

Reputation: 249

As Hakank said, you need the element constraint of Choco Solver. Its syntax is : element(IntVar VALUE, int[] TABLE, IntVar INDEX), meaning VALUE = TABLE[INDEX]

So it gives something like:

for (int i = 0; i < sub_sz; i++) {

    solver.post(ICF.element(VF.bounded(sub_cap[i], cl_sz - 1, solver), cl_cap, cl_id[i]));
}

You cannot use getValue() because at this stage, the problem has not been solved yet (getValue() throws an exception when -ea is passed to the JVM arguments or returns the current variable LOWER BOUND, which is why you get a 0).

Upvotes: 2

Related Questions