Belhaver
Belhaver

Reputation: 13

Constraint for column in matrix Choco3

I recently started using Choco3 library for Java for constraint programming and i have a problem which i have no idea how to solve

I've created matrix

IntVar[][] matrix = VariableFactory.boundedMatrix("Matrix", 31, 14, 0, 3, solver);

and I set constraints for rows:

IntVar numberOfDays = VariableFactory.bounded("NumberOfDays", 5, 5, solver);
IntVar numberOfShortDays = VariableFactory.bounded("NumberOfShortDays", 2, 2, solver);
IntVar numberOfNights = VariableFactory.bounded("NumberOfNights", 5, 6, solver);

for (IntVar[] row : matrix ) {
    solver.post(ICF.among(numberOfDays, row, new int[]{1}));
    solver.post(ICF.among(numberOfShortDays, row, new int[]{2}));
    solver.post(ICF.among(numberOfNights, row, new int[]{3}));
}

Now I want to set specific constraints for columns:

IntVar numberOfDayShifts = VariableFactory.bounded("NumberOfDayShifts", 2, 4, solver);
IntVar numberOfNightShifts = VariableFactory.fixed(2, solver);

solver.post(ICF.among(numberOfDayShifts, oneColumn, new int[]{1, 2}));
solver.post(ICF.among(numberOfNightShifts, oneColumn, new int[]{3}));

Do you have any idea how can I achieve that?

EDIT: I've tried to transpose my matrix and put it into another variable by following code:

IntVar[][] transposedMatrix= VariableFactory.boundedMatrix("TransposedMatrix", 14, 31, 0, 3, solver);

for (int i = 0; i < 31; i++) {
    for (int j = 0; j < 14; j++) {
        transposedMatrix[j][i] = matrix[i][j];
    }
}

and set contraints mentioned earlier, but for far less complicated matrix (3x14) time needed to solve it was way bigger I expected (more than 1 hour, then i stopped it), so I don't even imagine how much time will be need to resolve 31x14 problem... Are there any other possible solutions?

Upvotes: 1

Views: 157

Answers (2)

Virgile BRIAN
Virgile BRIAN

Reputation: 33

There is an ever better way to do that with ArrayUtils :

    IntVar[][] transposedMatrix;
    transposedMatrix = matrix.clone();
    org.chocosolver.util.tools.ArrayUtils.transpose(transposedMatrix);

(Since I am a bit late, maybe this function didn't exist back when you asked your question).

Upvotes: 1

Jean-Guillaume
Jean-Guillaume

Reputation: 36

You can use ArrayUtils.getColumn(matrix, colIndex);

Best,

Jean-Guillaume Fages www.cosling.com

Upvotes: 2

Related Questions