[英]Why doesn't Choco-solver find a solution?
我正在学习使用Choco-solver 。 我发现它无法找到一个非常简单的问题的解决方案,这意味着我一定误解了一些东西......
我将我的代码简化为:
Model model = new Model("Minimum");
IntVar x = model.intVar("x", 1, 9, false);
IntVar y = model.intVar("y", 1, 9, false);
IntVar z = model.intVar("z", -1000, 1000, false);
z.eq(x.add(y.mul(2))).post();
Solver solver = model.getSolver();
solver.showStatistics();
solver.showSolutions();
solver.findSolution();
因此,三个 integer 变量和一个约束说z = x + 2y
。 Choco-solver 响应Complete search - No solution
。
如果我将约束的内部部分从y.mul(2)
更改为y.mul(1)
(x = 1, y = 1, z = 2) 或y.add(2)
2),我发现我得到了正确的解决方案y.add(2)
(x = 1, y = 1, z = 4)。 我什至可以将其设置为y.mul(-2)
(x = 1 y = 9 z = -17),但如果我使用mul
的 integer 大于 1,则约束似乎无法解决。
这里发生了什么?
这确实是 Choco Solver 4.10.7 中的一个错误。 此错误已得到修复: https://github.com/chocoteam/choco-solver/issues/841
如果您想使用固定版本,我邀请您从 GitHub 下载代码并使用 Maven 进行编译。
关于你的约束,我建议你发布一个算术约束如下: model.arithm(x, "+", y.mul(2).intVar(), "=", z).post();
. 这样,您就避免了一个与“x+2*y”相对应的中间变量,您强制它等于 z。 在这样一个小例子中并不重要,但它可能导致 memory 在依赖 ArExpression(用于算术表达式)时出现更大的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.