简体   繁体   English

为什么 Choco-solver 找不到解决方案?

[英]Why doesn't Choco-solver find a solution?

I'm learning to use Choco-solver .我正在学习使用Choco-solver I found that it fails to find a solution to a very simple problem, which means that I must have misunderstood something...我发现它无法找到一个非常简单的问题的解决方案,这意味着我一定误解了一些东西......

I reduced my code to this:我将我的代码简化为:

    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();

So, three integer variables and one constraint saying that z = x + 2y .因此,三个 integer 变量和一个约束说z = x + 2y Choco-solver responds Complete search - No solution . Choco-solver 响应Complete search - No solution

I find that I get correct solutions if I change the inner part of the constraint from y.mul(2) to y.mul(1) (x = 1, y = 1, z = 2) or to y.add(2) (x = 1, y = 1, z = 4).如果我将约束的内部部分从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)。 I can even set it to y.mul(-2) (x = 1 y = 9 z = -17), but if I use mul with an integer larger than 1 the constraint seems to be unsolveable.我什至可以将其设置为y.mul(-2) (x = 1 y = 9 z = -17),但如果我使用mul的 integer 大于 1,则约束似乎无法解决。

What is going on here?这里发生了什么?

This was indeed a bug in Choco Solver 4.10.7.这确实是 Choco Solver 4.10.7 中的一个错误。 This bug has been fixed since: https://github.com/chocoteam/choco-solver/issues/841此错误已得到修复: https://github.com/chocoteam/choco-solver/issues/841

If you want to use the fixed version, I invite you to download the code from GitHub and compile it using Maven.如果您想使用固定版本,我邀请您从 GitHub 下载代码并使用 Maven 进行编译。

Concerning your constraint, I advise you to post an arithm constraint as following: model.arithm(x, "+", y.mul(2).intVar(), "=", z).post();关于你的约束,我建议你发布一个算术约束如下: model.arithm(x, "+", y.mul(2).intVar(), "=", z).post(); . . This way, you avoid having an intermediate variable corresponding to "x+2*y" that you force to be equal to z.这样,您就避免了一个与“x+2*y”相对应的中间变量,您强制它等于 z。 It does not really matter in such a small example, but it could lead to memory issues on bigger problems while relying on ArExpression (for Arithmetical Expression).在这样一个小例子中并不重要,但它可能导致 memory 在依赖 ArExpression(用于算术表达式)时出现更大的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM