Search code examples
minizinc

MiniZinc: how to sum equal-length subarrays?


The problem I encountered is how to add variables in a[0..9] to b[10..19]. My code is:

array[0..19] of int: a=array1d(0..19,[0,1,2,3,4,5,6,7,8,9,9,8,7,6,5,4,3,2,1,0]);
array[0..19] of int: b=array1d(0..19,[9,8,7,6,5,4,3,2,1,0,0,1,2,3,4,5,6,7,8,9]);
array[0..9] of var int: c;
constraint
    forall(i in 0..9, j in 10..19)
    (
        c[i]=a[i]+b[j]
    );
solve satisfy;
output[show(c[i]) | i in 0..9];

However, MiniZinc gives me the warning "model inconsistency detected, in call 'forall' in array comprehension expressionwith i = 0 with j = 11" and outputs "=====UNSATISFIABLE=====".

How do I get this to work?


Solution

  • (Extracts the answer from my comments.)

    Your forall loop tries to assign c[i] many times with different values, which is not allowed. In MiniZinc, unlike traditional programming languages, a decision variables cannot be reassigned.

    I guess that you mean addition in a parallel loop:

    constraint  
      forall(i in 0..9) ( c[i]=a[i]+b[i+10])
    ;