Search code examples
arraysmultidimensional-arrayminizinc

MiniZinc - Array of (multidimensional) array


I would like to know if it’s possible to have array of (multidimensional) array in MiniZinc language.

Indeed, I would like to resolve a timetabling problem with workers. My goal is to check if they are available at least 1 day per week. Each worker is indexed by an integer and I have their schedule per week.

For me, an array like : [[number_of_week, weekday]] could be a solution.

For example, worker 1 who is available monday/friday in week 1 and tuesday/thursday in week 2 can be modeled by the following array : (« 1 » means that the worker is available)

[[| 1,0,0,0,1, | 0,1,0,1,0 |], [...], ...]

If it's possible, how to declare this kind of array ? And I’m also open to any advice on modeling this constraint.

Sorry for my imperfect English and thank you in advance, Nicolas.


Solution

  • I'm not sure I understand your question fully, but you can have multi dimensional arrays as follows:

    array[1..3,1..3] of int: a = array2d(1..3,1..3,
        [1,0,0,
         0,1,0,
         1,1,0]);
    

    or using another syntax:

    array[1..3,1..3] of int: a = 
      [|1,0,0
       |0,1,0
       |1,1,0|];
    

    However, you cannot have arrays in arrays, i.e. this is not allowed:

    % This is NOT valid MiniZinc code! 
    array[1..3,1..3] of int: a = 
      [[[1,0,0],[1,0,1]],
       [[0,1,0],[0,1,1]],
       [[1,1,0],[0,0,0]]];
    

    Note also that one cannot have "ragged" arrays: the number of element in each row and each column must be the same.

    Regarding you specific constraint you might - if I understand your description - define a 3D array like this:

    int:_num_workers = 2;
    int: num_weeks = 3;
    int: num_days = 4;
    array[1..num_workers,1..num_weeks,1..num_days] of int: a =
         array3d(1..num_workers,1..num_weeks,1..num_days,
      [
       % worker 1
       % week1   week2     week3
       1,0,0,1,  1,1,0,1,  0,0,1,1,
    
       % worker 2
       % week1   week2      week3
       1,0,0,0,  0,0,0,1,   1,1,1,0
      ]);
    

    Note especially the array3d construct which creates a 3D array. So you can access day d of week e for worker w with a[w,e,d].