我找到了用巧克力解算器求解魔方程序的代码:
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();
} 但是类'Problem‘似乎已经被'Model’类取代了。用Problem.makeEnumIntVar代替Model.intVar是正确的吗?当前取代Problem.neq、Problem.eq和Problem.scalar的功能是什么?
发布于 2017-12-02 15:46:16
看起来你有一些不推荐使用的代码。表达式
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的约束。
进一步的阅读和资源:
在这里您可以找到最新的教程,其中包含一些示例代码:choco tutorial
最后,您还可以在github上查看测试用例:https://github.com/chocoteam/choco-solver/tree/master/src/test/java/org/chocosolver/solver
特别是对变量和表达式的测试可能会让您感兴趣。
在这里可以找到更多的代码示例:more code samples
https://stackoverflow.com/questions/47533057
复制相似问题