I am using choco API to solve a problem. What I need is coding a constraint that make the sum of all my variables equal to 1. This code keeps a sum of rows equal 1:
IntegerVariable[][] rows;
int n; //number of rows
for(int i=0; i<n; i++)
model.addConstraint(eq(sum(rows[i], 1));
But what I need is programming a code that keeps the sum of all my element's matrix (sum of rows) to be equaled to 1 and not the sum of each row = 1.
If I understand you correctly you want to ensure that the sums of the full matrix is equal to 1.
Then you can use an ArrayList ("all") to collect all the IntegerVariables into one list and then add a constraint to "all". Your example is not complete, e.g. the number of columns, so I assume that there are n columns and that it is a 0/1 matrix. Here's an example:
// ...
ArrayList<IntegerVariable> all = new ArrayList<IntegerVariable>();
int n = 5; // number of rows and columns
IntegerVariable[][] rows = new IntegerVariable[n][n];
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
rows[i][j] = makeIntVar("rows["+i+","+j+"]", 0, 1);
all.add(rows[i][j]);
}
}
// convert ArrayList all to an array
model.addConstraint(eq(sum(all.toArray(new IntegerVariable[1])),1));
// ...