当前位置: 首页 > 知识库问答 >
问题:

重用choco solver模型进一步约束解决方案

微生昌胤
2023-03-14

我正在使用choco solver库生成一组谜题。我需要运行解算器,检查有多少个解,如果有多个解,添加一个额外的约束。重复这一步会给我一组约束(线索),它们有一个独特的解决方案。

然而,一旦我运行model.get求解器(findAllSolutions()),任何额外的检查都会返回零解。

我猜我需要以某种方式重置模型求解器,但找不到实现这一点的方法——如果必须的话,我宁愿不生成新模型并重新创建现有约束。

原始代码有110 intVar和大量的约束,但我创建了一个小得多的示例。

注意:在实际应用中,我使用模型。getSolver()。findAllSolutions(新的SolutionCounter(模型,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());

共有1个答案

松灿
2023-03-14

对于其他寻找这个的人来说,答案很简单。

model.getSolver().reset();
 类似资料:
  • 我试图在Karaf 3.0.0-RC1中使用H2数据库加载来获取Scala库,但我遇到了这个错误 有人知道我需要在POM和/或功能中添加什么吗。xml来让它工作吗? 谢谢,鲍勃

  • 销关节 cpPinJoint *cpPinJointAlloc(void) cpPinJoint *cpPinJointInit(cpPinJoint *joint, cpBody *a, cpBody *b, cpVect anchr1, cpVect anchr2) cpConstraint *cpPinJointNew(cpBody *a, cpBody *b, cpVect anchr1,

  • 问题 你想定义某些在属性赋值上面有限制的数据结构。 解决方案 在这个问题中,你需要在对某些实例属性赋值时进行检查。 所以你要自定义属性赋值函数,这种情况下最好使用描述器。 下面的代码使用描述器实现了一个系统类型和赋值验证框架: # Base class. Uses a descriptor to set a value class Descriptor: def __init__(self

  • 泛型的类型约束 swapTwoValues(_:_:)函数和Stack类型可以用于任意类型. 但是, 有时在用于泛型函数的类型和泛型类型上, 强制其遵循特定的类型约束很有用. 类型约束指出一个类型形式参数必须继承自特定类, 或者遵循一个特定的协议、组合协议. 例如, Swift的Dictionary类型在可以用于字典中键的类型上设置了一个限制. 如字典中描述的一样,字典键的类型必须是可哈希的. 也

  • 我是相对较新的挥杆,我正试图弄清楚它是如何工作的。告诉我,如果我错了。 我已经大致了解了放置组件所需的gridx和gridy。要创建nxn网格,至少需要n个值(gridx,gridy)。例如,(5,5),(3,3),(4,9),(3,10)将创建一个3x4网格空间(4行,3列),其中使用上述(gridx,gridy)的组件分别放置在单元格(3,2),(1,2),(2,3),(1,4)中。 weig

  • 我试图编写一个通用的getter函数,给定一个键,该键应该只返回类型字符串| boolean | number。因此,默认值应与返回值的类型相同 这是我尝试过的。 我得到这个错误,不确定是什么错误。 什么可能是“字符串|数字|布尔”的子类型让我感到困惑。它们不是已经是基本类型了吗?