2

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);
        }
  • The cl_cap is an array of int.
  • cl_id[i].getValue() is always 0 because it gets the lower bound of the domain and the constraint doesn't apply to cl_id

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

Eike Pierstorff
  • 31,996
  • 4
  • 43
  • 62
  • 1
    It's not clear what the MiniZinc model does since the definition of cl_cap is not shown. But the problem seems to be that you are using .getValue in a constraint which pick a value (here apparently 0). You might have to use the element constraint (http://choco-solver.org/user_guide/5_elements.html#element) to extract the value of cl_cap in the position cl_id[i] (which is a decision variable). These kind of constraints are easier to state in MiniZinc. – hakank Apr 20 '16 at 18:29
  • Solver solver = new Solver(); int [] a = new int []{30,70,50}; int [] b = new int []{50,30,80}; IntVar [] idx = VF.boundedArray("", 3, 0, 2, solver); solver.post(ICF.alldifferent(idx)); for (int i = 0; i < 3; i++) { IntVar x = VF.fixed(b[idx[i].getValue()], solver); solver.post(ICF.arithm(x, ">", a[i])); } solver.findSolution(); for (int i = 0; i < 3; i++) { System.out.println(idx[i].getValue()); } b[idx[i].getValue()] always 0 because isn't initialized yet How i write code to do this. – Abdelhamid Nour Apr 20 '16 at 19:25
  • 1
    Well, the decision variables will not get initialized (or assigned) to any values before the search part of the model. As I mentioned earlier, you should use the "element" constraint to "extract" the value of idx[i] to get the index in "b". (MiniZinc has a much better syntax for the element constraint, so this step might be tricky to state in other CP systems.) Please see the example at the page I linked to earlier. – hakank Apr 20 '16 at 19:37
  • Choco is able to solve problems encoded as FlatZinc files. It might be an option to use the FlatZinc intermediate file of MiniZinc and feed it into Choco. – Axel Kemper Apr 20 '16 at 22:52
  • i have solve my problem , now i have another one when i run my model in minizinc IDE for specific data give a solutions about 10 msec but when i running my model using command line commands it take to long time if it give solutions it different from minizinc IDE solutions or it doesn't give solutions at all my commands i use as follow cd "C:\Program Files\MiniZinc IDE (bundled)" minizinc.exe MinizincCode.mzn – Abdelhamid Nour Apr 22 '16 at 15:30

1 Answers1

2

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).

tobyodavies
  • 27,347
  • 5
  • 42
  • 57