/
arg_sort.mzn
56 lines (47 loc) · 2.18 KB
/
arg_sort.mzn
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
include "arg_sort_int.mzn";
include "arg_sort_float.mzn";
include "analyse_all_different.mzn";
/** @group globals.sort
Returns the permutation \a p which causes \a x to be in sorted order hence
\a x[\a p[\p i]] <= \a x[\a p[\p i+1]].
The permutation is the stable sort hence \a x[\a p[\p i]] = \a x[\a p[\p i+1]] \(\rightarrow\) \a p[\p i] < \a p[\p i+1].
*/
function array[int] of var int: arg_sort(array[int] of var int:x) :: promise_total =
if length(x) = 0 then []
else
let { int: l = min(index_set(x));
int: u = max(index_set(x));
array[1..u-l+1] of var l..u: p;
constraint analyse_all_different(p);
constraint arg_sort_int(x,p); }
in p
endif;
/** @group globals.sort
Returns the permutation \a p which causes \a x to be in sorted order hence
\a x[\a p[\p i]] <= \a x[\a p[\p i+1]].
The permutation is the stable sort hence \a x[\a p[\p i]] = \a x[\a p[\p i+1]] \(\rightarrow\) \a p[\p i] < \a p[\p i+1].
*/
function array[int] of var int: arg_sort(array[int] of var float:x) :: promise_total =
if length(x) = 0 then []
else
let { int: l = min(index_set(x));
int: u = max(index_set(x));
array[1..u-l+1] of var l..u: p;
constraint analyse_all_different(p);
constraint arg_sort_float(x,p); }
in p
endif;
/** @group globals.sort
Constrains \a p to be the permutation which causes \a x to be in sorted order hence
\a x[\a p[\p i]] <= \a x[\a p[\p i+1]].
The permutation is the stable sort hence \a x[\a p[\p i]] = \a x[\a p[\p i+1]] \(\rightarrow\) \a p[\p i] < \a p[\p i+1].
*/
predicate arg_sort(array[int] of var int:x,
array[int] of var int:p) = fzn_arg_sort_int(x,p);
/** @group globals.sort
Constrains \a p to be the permutation which causes \a x to be in sorted order hence
\a x[\a p[\p i]] <= \a x[\a p[\p i+1]].
The permutation is the stable sort hence \a x[\a p[\p i]] = \a x[\a p[\p i+1]] \(\rightarrow\) \a p[\p i] < \a p[\p i+1].
*/
predicate arg_sort(array[int] of var float:x,
array[int] of var int:p) = arg_sort_float(x,p);