libminizinc icon indicating copy to clipboard operation
libminizinc copied to clipboard

MiniZinc 2.7.0: length of dynamically created array does not work

Open matsc-at-sics-se opened this issue 2 years ago • 3 comments

A colleague found the following. This model is unsat, but IMHO it should not be:

array[1..4] of var 0..1: xs;

var 0..4: n = length([x | x in xs where x=1]);

constraint n = 2;

matsc-at-sics-se avatar Mar 10 '23 07:03 matsc-at-sics-se

This is explained in the documentation under “hidden option types”: https://www.minizinc.org/doc-latest/en/optiontypes.html#hidden-option-types

guidotack avatar Mar 10 '23 07:03 guidotack

Right. Nevertheless, convincing a beginner that the following makes sense is a bit of a challenge:

array[1..4] of bool: xa :: add_to_output = [true, true, false, false];
int: xlen :: add_to_output = length([x | x in xa where x]);

array[1..4] of var bool: ya :: add_to_output;
int: ylen :: add_to_output = length([y | y in ya where y]);

solve :: bool_search(ya[1..2], input_order, indomain_max)
  satisfy;
bash-3.2$ minizinc foo.mzn
xa = [true, true, false, false];
xlen = 2;
ya = [true, true, false, false];
ylen = 4;

matsc-at-sics-se avatar Mar 12 '23 20:03 matsc-at-sics-se

Perhaps a better reference to why this unexpected behavior is as designed is the specification of what array comprehensions mean. The specification of Simple Array Comprehensions says:

Array comprehensions are allowed over a variable set with finite type, the result is an array of optional type, with length equal to the cardinality of the upper bound of the variable set.

PerMildner avatar Mar 15 '23 13:03 PerMildner