Java 乔科解决旧类
我发现以下代码用于使用choco解算器解算magic square程序:Java 乔科解决旧类,java,solver,choco,Java,Solver,Choco,我发现以下代码用于使用choco解算器解算magic square程序: public static void main(String[] args) { int n = 4; System.out.println("Magic Square Problem with n = " + n); Problem myPb = new Problem(); IntVar[] vars = new IntVar[n * n]; for (int i = 0;
public static void main(String[] args) {
int n = 4;
System.out.println("Magic Square Problem with n = " + n);
Problem myPb = new Problem();
IntVar[] vars = new IntVar[n * n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
vars[i * n + j] = myPb.makeEnumIntVar("C" + i + "_" + j, 1, n * n);
}
IntVar sum = myPb.makeEnumIntVar("S", 1, n * n * (n * n + 1) / 2);
myPb.post(myPb.eq(sum, n * (n*n + 1) / 2));
for (int i = 0; i < n * n; i++)
for (int j = 0; j < i; j++)
myPb.post(myPb.neq(vars[i], vars[j]));
int[] coeffs = new int[n];
for (int i = 0; i < n; i++) {
coeffs[i] = 1;
}
for (int i = 0; i < n; i++) {
IntVar[] col = new IntVar[n];
IntVar[] row = new IntVar[n];
for (int j = 0; j < n; j++) {
col[j] = vars[i * n + j];
row[j] = vars[j * n + i];
}
myPb.post(myPb.eq(myPb.scalar(coeffs, row), sum));
myPb.post(myPb.eq(myPb.scalar(coeffs, col), sum));
myPb.solve();
}
publicstaticvoidmain(字符串[]args){
int n=4;
System.out.println(“n=“+n”)的幻方问题;
问题myPb=新问题();
IntVar[]vars=新的IntVar[n*n];
对于(int i=0;i
但“问题”类似乎已被“模型”类取代。
使用Model.intVar而不是Problem.makeEnumIntVar是否正确?
替换Problem.neq、Problem.eq和Problem.scalar的当前函数是什么?这里似乎有一些不推荐使用的代码。表达式
Problem.scalar and Problem.eq
可以表示为
int capacity = 34; // max capacity
int[] volumes = new int[]{7, 5, 3};
// Problem.scalar
model.scalar(new IntVar[]{obj1, obj2, obj3}, volumes, "=", capacity).post();
// Problem.eq
model.arithm(obj1, "=", obj2).post();
例如,上面的代码表示了标量积等于容量以及obj1必须等于obj2的约束
进一步阅读和参考资料:
在这里,您可以找到最新的教程,其中包含一些示例代码:
最后,您还可以在github上查看测试用例:
特别是变量和表达式的测试可能会让您感兴趣
以下是更多的代码示例: