带有枚举的 MiniZinc global_cardinality 函数

MiniZinc global_cardinality function with enums

根据the docs

A key behaviour of enumerated types is that they are automatically coerced to integers when they are used in a position expecting an integer. For example, this allows us to use global constraints defined on integers, such as global_cardinality_low_up

global_cardinality* 家族有两种形式:谓词和函数。虽然在谓词的情况下,枚举项的数组确实会强制转换为整数,但对于函数,强制转换似乎不起作用。 例如,

include "global_cardinality_closed.mzn";

enum MyEnum = {A, B, C};
array[1..2] of MyEnum: toCount = [A, C];

array[1..100] of var MyEnum: values;

%1
constraint let {
    array[int] of var int: counts = global_cardinality_closed(values, toCount);
} in counts[1] > counts[2];

%2
constraint global_cardinality_closed(values, toCount, [5, 6]);

在 MiniZincIDE 中编译上面的代码片段会导致:

MiniZinc: type error: no function or predicate with this signature found: `global_cardinality_closed(array[int] of var MyEnum,array[int] of MyEnum)'
Cannot use the following functions or predicates with the same identifier:
predicate global_cardinality_closed(array[$_] of var int: x,array[$_] of int: cover,array[$_] of var int: counts);
    (requires 3 arguments, but 2 given)

同时%2之后的代码编译正常

我是否遗漏了什么或者我应该提交错误?

要使 %1 正常工作,您可以

include "global_cardinality_closed_fn.mzn";

或者干脆

include "globals.mzn";

函数是利用谓词实现的:

include "global_cardinality_closed.mzn";

/** @group globals.counting
  Returns an array with number of occurrences of \a cover[\p i] in \a x.

  The elements of \a x must take their values from \a cover.
*/
function array[$Y] of var int: global_cardinality_closed(array[$X] of var int: x,
                                                         array[$Y] of int: cover) :: promise_total =
  let { array[int] of int: cover1d = array1d(cover);
        array[index_set(cover1d)] of var 0..length(x): counts;
        constraint global_cardinality_closed(array1d(x),cover1d,counts); } 
  in arrayXd(cover,counts);