重用choco求解器模型来进一步约束解决方案

问题描述 投票:0回答:1

我正在使用choco求解器库生成一组拼图。我需要运行求解器,检查有多少个解决方案,如果有多个,则添加一个额外的约束。重复此操作将为我提供一组具有独特解决方案的约束(线索)。

但是一旦我运行model.getSolver(findAllSolutions()),任何其他检查都会返回零解决方案。

我想我需要以某种方式重置模型求解器,但找不到实现此目的的方法-如果需要,我宁愿不生成新模型并重新创建现有约束。

原始代码有110个IntVar,并且有很多约束,但是我创建了一个小得多的示例。

注意:在实际的应用程序中,我使用model.getSolver()。findAllSolutions(new SolutionCounter(model,2))加快了速度,但在此省略了该步骤。

Model model = new Model();
// setup two doors A and B, one has the value 0 the other 1
IntVar doorA = model.intVar("Door A", 0, 1);
IntVar doorB = model.intVar("Door B", 0, 1);
model.allDifferent(new IntVar[]{doorA, doorB}).post();
// setup two windows A and B, one has the value 0 the other 1
IntVar windowA = model.intVar("Window A", 0, 1);
IntVar windowB = model.intVar("Window B", 0, 1);
model.allDifferent(new IntVar[]{windowA, windowB}).post();
// assign the first constraint and count the solutions
model.arithm(doorA,"=",0).post();
// this should force door B to be 1 - there are two remaining solutions
List<Solution> solutions = model.getSolver().findAllSolutions();
System.out.println("results after first clue");
for (Solution s : solutions) {
     System.out.println(">"+s.toString());
}
assertEquals("First clue leaves two solutions",2,solutions.size());
// add second clue
model.arithm(windowA,"=",1).post();
// this should force window B to by 0 - only one valid solution
List<Solution> solutions2 = model.getSolver().findAllSolutions();
System.out.println("results after second clue");
for (Solution s : solutions2) {
   System.out.println(">"+s.toString());
}
assertEquals("Second clue leaves one solution",1,solutions2.size());
java constraint-programming choco
1个回答
0
投票

对于寻找此内容的其他人,答案很简单。

model.getSolver().reset();
© www.soinside.com 2019 - 2024. All rights reserved.