Nairo
Nairo

Reputation: 71

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

Can we check an equality between 2 partial number with choco solver ?

I have a 2 dimensional array, and for each element of it, their domain are different number who have the same form : a number composed by 6 digits (some example of values a element can have : 781010, 680101, 391111). My problem is, how to compare only a digit and not the whole number ?

I have to do something who look-like that :

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);
    }
}

can you help me please ?

Upvotes: 1

Views: 445

Answers (1)

Arthur Godet
Arthur Godet

Reputation: 71

You might consider decomposing your numbers in series of digits: for each IntVar you currently, you could create an array arr of 6 (as your numbers always have 6 digits) IntVars and have another IntVar for the value with the following constraint :

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

And then you could post your other constraints on value (which you could store in a matrix, as you already do).

Another possibility, which might be slower, could be to post the following constraints (without creating variables for digits) :

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();
    }
}

Upvotes: 2

Related Questions