简体   繁体   English

将 MiniZinc 模型转换为 choco 代码

[英]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.我的 minizinc 模型工作正常,但我需要将其转换为 Java 代码,所以我使用 choco 来完成它。 The problem I am facing right now is the mechanism that minizinc woks with is different form choco.我现在面临的问题是 minizinc 炒锅的机制是不同形式的巧克力。 I wrote the constraints i used in minizinc exactly in choco but it didn't work.我在 choco 中写了我在 minizinc 中使用的约束,但它没有用。

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_cap 是一个 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 cl_id[i].getValue()始终为 0,因为它获取域的下限并且约束不适用于 cl_id

What should I do to make the choco constraint work the same way as minizinc?我应该怎么做才能使 choco 约束的工作方式与 minizinc 相同?

As Hakank said, you need the element constraint of Choco Solver.正如 Hakank 所说,您需要 Choco Solver 的元素约束。 Its syntax is : element(IntVar VALUE, int[] TABLE, IntVar INDEX) , meaning VALUE = TABLE[INDEX]它的语法是: element(IntVar VALUE, int[] TABLE, IntVar INDEX) ,意思是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).你不能使用getValue()因为在这个阶段,问题还没有解决(当 -ea 传递给 JVM 参数或返回当前变量 LOWER BOUND 时, getValue()抛出异常,这就是你得到 0 的原因) .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM