Choco 求解器 constraint/variable 定义

Choco solver constraint/variable definition

我正在尝试在 choco 中移植一个 minizinc 模型。我知道如何定义变量和其他基本内容,但尽管阅读了教程和一些代码示例,但我在定义一些重要约束时遇到了一些麻烦。 有人可以给我一些建议如何以巧克力求解器样式翻译以下代码(仅 z)吗?

array[1..n,1..n] of int: c;
array[1..n] of var 0..10: next;

var 0..sum(c): z = sum(i in 1..n)(c[i,next[i]]);

谢谢!

我相信你知道如何 post 求和约束,所以非平凡的部分位于 c[i,next[i]] 中,它检索矩阵 c 中第 i 行和第 next[ 列的整数一世]。问题是 next[i] 是一个变量,因此您不能直接使用它来访问 (Java) 数组。

您需要使用元素约束(也存在于 minizinc 中):

/**
 * Creates an element constraint: value = table[index]
 *
 * @param value an integer variable taking its value in table
 * @param table an array of integer values
 * @param index an integer variable representing the value of value in table
 */
default Constraint element(IntVar value, int[] table, IntVar index)

当您使用矩阵时,您需要对每一行执行此操作,然后 post 对它们求和。

另请注意,在 Java 中,数组单元格的访问范围是从 0 到 n-1(在 minizinc 中是从 1 到 n),因此您可能需要相应地更新模型或使用偏移量。

希望对您有所帮助

https://www.cosling.com/