[英]Choco solver old class
我找到了使用choco求解器來解決幻方程序的代碼:
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();
}
但是“問題”類似乎已被“模型”類替換。 使用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的約束。
進一步的閱讀和資源:
在這里,您將找到帶有一些示例代碼的最新教程: choco教程
最后,您還可以在github上查看測試用例: https : //github.com/chocoteam/choco-solver/tree/master/src/test/java/org/chocosolver/solver
特別是對於變量和表達式的測試可能對您來說很有趣。
在這里可以找到更多代碼示例: 更多代碼示例
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.