我正在使用 MiniZinc,想要查找 var int 数组中数字的顺序(从低到高)。
当使用 int 数组时我没有问题。
当使用 var int 数组时,MiniZinc 给出错误:
MiniZinc: type error: no function or predicate with this signature found: arg_sort(array[int] of var int)
,但根据我对文档的理解,它应该可以工作:https://www.minizinc.org/doc-2.6.3/ en/lib-globals-sort.html#mzn-globals-sort-sort(第一部分,3)。
% Create an array of unsorted values
array[1..3] of int: values = [1, 5, 3];
% Apply some transformation that requires using variable values
% (is more complex in real example, here I arbitrarily add 5)
array[1..3] of var int: var_values;
constraint forall (v in 1..3) (var_values[v] = values[v] + 5);
% array[1..3] of int: sorted = sort(values); % Works
% array[1..3] of int: arg_sorted = arg_sort(values); % Works
array[1..3] of var int: arg_var_sorted = arg_sort(var_values); % Does not work
solve satisfy;
我也尝试在这里使用
predicate
。在这里我收到错误:MiniZinc: type error: type-inst must be par set but is 'array[int] of var int'
% Create an array of unsorted values
array[1..3] of int: values = [1, 5, 3];
% Apply some transformation that requires using variable values
% (is more complex in real example, here I arbitrarily add 5)
array[1..3] of var int: var_values;
constraint forall (v in 1..3) (var_values[v] = values[v] + 5);
array[1..3] of var int: order;
predicate arg_sort(var_values, order);
solve satisfy;
我做错了什么?
您的模型缺少将从全局库导入
arg_sort
函数的 include 语句。
添加
include "arg_sort.mzn";
即可正常工作。
请注意,
predicate
项用于定义您自己的函数。您将需要添加类型和函数体才能使它们工作。