簡體   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