繁体   English   中英

Choco-solver :在 intVar 的一部分上定义约束

[英]Choco-solver : define a constraint on a part of an intVar

我们可以使用 choco 求解器检查 2 个部分数字之间的相等性吗?

我有一个二维数组,对于其中的每个元素,它们的域是具有相同形式的不同数字:由 6 位数字组成的数字(元素可以具有的值的一些示例:781010、680101、391111)。 我的问题是,如何只比较一个数字而不是整数?

我必须做一些看起来像这样的事情:

Model model = new Model();
IntVar[][] array = new IntVar[height][width];

....
// getting the domains for each element of array
....

for(int i = 0; i < height; i++) {
    for(int j = 0; j < width; j++) {
       model.arithm(array[i][j]3rdDigit, "=", array[i+1][j]4thDigit);
    }
}

你能帮我吗 ?

您可能会考虑将您的数字分解为一系列数字:对于您当前的每个 IntVar,您可以创建一个 arr 为 6 的数组(因为您的数字总是有 6 位数字)IntVars 并为具有以下约束的值创建另一个 IntVar:

IntVar value = model.intVar(0,999999);
model.scalar(arr, new int[]{100000, 10000, 1000, 100, 10, 1}, "=", value).post();

然后你可以发布你对值的其他约束(你可以将它存储在矩阵中,就像你已经做的那样)。

另一种可能会更慢,可能是发布以下约束(不为数字创建变量):

for(int i = 0; i < height; i++) {
    for(int j = 0; j < width; j++) {
       IntVar thirdDigit = model.intVar(0,9);
       model.mod(array[i][j], 1000, thirdDigit).post();
       IntVar fourthDigit = model.intVar(0,9);
       model.mod(array[i+1][j], 100, fourthDigit).post();
       model.arithm(thirdDigit, "=", fourthDigit).post();
    }
}

暂无
暂无

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

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