我正在寻找一种方法来编码在巧克力解算器上的数学方程。我看到有一种对约束进行编码的方法,比如:
X+y<9
但我正在尝试编码如下内容
3x + 4y <9
其中x和y是整型变量。
任何帮助都将不胜感激。
发布于 2016-03-03 00:20:15
我对Choco也是新手,但我可以解决这个问题。
为此,您可以使用约束scalar
(请参阅docs)。
首先,您只需要在两个IntVar
变量中定义x
和y
。您可以使用VariableFactory.bounded
或Variable.enumerated
。当您只想使用具有下限和上限的域时,它们非常相似,但区别在user guide中进行了解释。
然后,您需要使用等式的系数定义一个数组,在本例中为{ 3, 4 }
。
下面是你如何做到这一点:
Solver solver = new Solver();
IntVar x = VariableFactory.bounded("x", 0, 50, solver);
IntVar y = VariableFactory.bounded("y", 0, 50, solver);
int[] coefficients = new int[]{ 3, 4 };
solver.post(IntConstraintFactory.scalar(new IntVar[]{ x, y }, coefficients, "<", VariableFactory.fixed(9, solver)));
if (solver.findSolution()) {
do {
System.out.println("x = " + x.getValue() + ", y = " + y.getValue());
} while (solver.nextSolution());
} else {
System.out.println("The equation has no solutions.");
}
https://stackoverflow.com/questions/35688831
复制相似问题