0

I've found a Choco solver as constraint programming software working with Java. I would like to learn it more. I have done some basic example. But now I would like to try something more complex (Pritsker project scheduling alg) and I need your help. In order to progress I have to understand how to put constraints on rows of matrix variable. Exactly I need to keep a sum of rows equal 1 (task starts only once). I have tried it but unsuccessfuly. Could you help? I do use Choco 2.1.5 My matrix is as follows:

int n = 10; // projects
int m = 12; // time horizon in months
IntegerVariable[][] x = new IntegerVariable[n][m];
int i, j;
for (i = 0; i < n; i++){
    for (j = 0; j < m; j++){
        x[i][j] = Choco.makeIntVar("x_" + i +"_" + j, 0, 1, Options.V_ENUM);
        model.addVariable(x[i][j]);
    }
}
Dan
  • 7,286
  • 6
  • 49
  • 114
JackAW
  • 164
  • 2
  • 14

2 Answers2

1

You should define variables as rows and columns first.

Than, you may use this documentation to proceed. Something like this might be helpful:

IntegerVariable[][] rows;
int n; //number of rows
for(int i=0; i<n; i++)
   model.addConstraint(eq(sum(rows[i], 1));
padawan
  • 1,295
  • 1
  • 17
  • 45
  • thanks a lot. I will redo it and come back. I was trying to sum using 2 indexes and ended up with the constraint for each cell... My second approach was to use scalar method but it didnt work also. – JackAW May 21 '14 at 22:22
  • Ok, It solved my problem. I got another one issue to be solved in order to implement a scheduling alg. What if I would like to have some coefficients for each x[i][j] variable, to be used in a formulation of the goal? When I try something like this: `for (i = 0; i < n; i++){ for (j = 0; j < m; j++){ z[i][j] = Choco.mult(j, x[j][i]); // here I've got error } }` where z is just a temporal variable: `IntegerVariable[][] z = new IntegerVariable[n][m];` I've got the following error: Type mismatch: cannot convert from IntegerExpressionVariable to IntegerVariable. Any idea how to solve it? Thanks. – JackAW May 23 '14 at 08:45
  • 1
    `Choco.mult(j, x[j][i])` returns and `IntegerExpressionVariable`. An `IntegerVariable` is a decision variable, which means an integer to be picked among a domain. But `IntegerExpressionVariable` is a constant variable. So, you need to define `z` as an `IntegerExpressionVariable` – padawan May 23 '14 at 15:22
  • I got the point. For now I did something simple. Just for my 5 projects and horizon of 12 months: `int[] horizon = new int[]{1,2,3,4,5,6,7,8,9,10,11,12}; IntegerVariable goal= Choco.makeIntVar("goal", 1, 1000000, Options.V_BOUND); model.addConstraint(Choco.eq(Choco.sum( Choco.scalar(x[0], horizon), Choco.scalar(x[1], horizon), Choco.scalar(x[2], horizon), Choco.scalar(x[3], horizon), Choco.scalar(x[4], horizon)), goal)); Solver solver = new CPSolver(); solver.read(model); solver.minimize(solver.getVar(goal), false);` I'm sure that the above constraint should be in one line. But how? – JackAW May 24 '14 at 07:49
  • @JAW try this: `eq(plus(sum(x),horizon));` – padawan May 24 '14 at 08:54
  • It requires to create method: `sum(IntegerVariable[][])` – JackAW May 26 '14 at 12:29
0

To add constraint over the rows, you should transpose the matrix and apply the constraint over the lines :

transposed = ArrayUtils.transpose(x);
for(int i=0; i<n; ++i){
    model.sum(transposed[i], "=", 1);
}