Constraint Failure

In reply to MICRO_91:

Your code has several problems

  • You are not allowed to refer to a random array as a whole variable in a constraint. You must have a constant select, or use an iterative constraint like foreach or array.sum that gets unrolled unto a constant select.
  • A user defined function call in a constraint breaks the solution space in half: first, the random variables used by the function inputs, and second, the set random variables assigned by the output. Once the set of input variables get solved, they become state variables. (they cannot change to solve constraints for the output)
  • You have a cyclic dependency on the inputs and outputs to your function. The solver does not look into your function to break the cycle.

You can fix this by using the built-in iterative sum() method.

constraint VALUES { 
                       foreach(a[i])
                          if ( i == 0 )
                              a[i] == 0 ;
                          else if ( i == 1 )
                              a[i] == 1 ;
                          else 
                              a[i] == a.sum() with (item.index<i ? item:0) ;
                   }

This gets unrolled into the following constraints

a[0] == 0;
a[1] == 1;
a[2] == a[0] + a[1] + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0;
a[3] == a[0] + a[1] + a[2] + 0 + 0 + 0 + 0 + 0 + 0 + 0;
...
a[9] == a[0] + a[1] + a[2] + a[3] + a[4] + a[5] + a[6] + a[7] + a[8] + 0;