Saumya Bhatnagar
Saumya Bhatnagar

Reputation: 13

MiniZinc Array Declaration

How can I declare an array in MiniZinc, similar to the below Java code where the size of second dimension of the 2D array is not same for all?

int numV=5
int[] numActs=new int[numV];
double[][] min=new double[numV][];

for(int i=0;i<numV;i++){
   for(int j=0;j<numActs[i];j++){
      min[i][j]=<some value>;
    }
}

Upvotes: 1

Views: 3555

Answers (1)

hakank
hakank

Reputation: 6854

Short answer: MiniZinc don't support "jagged" arrays, i.e. rows with unequal length (and no columns with unequal length) . All rows must have the same length (as do columns), and must be declared with the length in flattening time.

What you can do is to define the data matrix (here called "m") with maximum row length * maximum column length, and fill out the shorter rows with some dummy values, e.g. 0:

Example: Rows with unequal lengths:

      [1,2,3,4]
      [5,6],
      [7,8,9],
      [10]
      [11,12,13]

The corresponding MiniZinc declaration would be:

     int: rows = 5;
     int: cols = 4;
     array[1..rows,1..cols] of int: m =
           array2d(1..rows,1..cols,
               [
                    1,2,3,4,
                     5,6,0,0,
                     7,8,9,0,
                    10,0,0,0,
                    11,12,13,0,
               ]);

You will have to handle these dummy values, either by ignoring the dummy values in your model, or use a length array which contains the length of each row, e.g.

  array[1..rows] of int: lengths = [4,2,3,1,3];

Upvotes: 3

Related Questions