minizinc

How to use arg_sort on array[int] of var int in MiniZinc?


I am using MiniZinc and want to find the order (low to high) of numbers in an array of var ints.

When using an array of int I have no issue.

When using an array of var int, MiniZinc gives me the error: MiniZinc: type error: no function or predicate with this signature found: arg_sort(array[int] of var int), yet from my understanding of the documentation, it should work: https://www.minizinc.org/doc-2.6.3/en/lib-globals-sort.html#mzn-globals-sort-sort (first section, 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;

I have also tried using predicate here instead. Here I receive the error: 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;

What am I doing wrong?


Solution

  • Your model is missing an include statements that will import the arg_sort function from the global library.

    Adding include "arg_sort.mzn"; makes it work.


    Note that the predicate items are used to define your own functions. You will need to add types and a function body to make them work.