/
all_differ_from_at_least_k_pos.pi
86 lines (61 loc) · 1.63 KB
/
all_differ_from_at_least_k_pos.pi
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
Global constraint all_differ_from_at_least_k_pos in Picat.
From Global Constraint Catalog
http://www.emn.fr/x-info/sdemasse/gccat/Call_different_from_at_least_k_pos.html
"""
Enforce all pairs of distinct vectors of the VECTORS collection to differ
from at least K positions.
Example
(
2, <
vec-<2, 5, 2, 0>,
vec-<3, 6, 2, 1>,
vec-<3, 6, 1, 0>
>
)
The all_differ_from_at_least_k_pos constraint holds since:
* The first and second vectors differ from 3 positions, which is
greater than or equal to K=2.
* The first and third vectors differ from 3 positions, which is greater
than or equal to K=2.
* The second and third vectors differ from 2 positions, which is greater
than or equal to K=2.
"""
Model created by Hakan Kjellerstrand, hakank@gmail.com
See also my Picat page: http://www.hakank.org/picat/
*/
import util.
import cp.
main => go.
go =>
Rows = 3,
Cols = 4,
X = new_array(Rows,Cols).array_matrix_to_list_matrix(),
XList = vars(X),
XList :: 0..6,
K :: 0..Cols,
K #= 2,
% the example above
% X = [[2,5,2,0],
% [3,6,2,1],
% [3,6,1,0]],
X = [[2,5,2,_],
[3,6,2,1],
[3,6,1,0]],
% fails
% X = [[2,5,2,0],
% [2,5,2,1],
% [3,6,1,0]],
all_differ_from_at_least_k_pos(K,X),
Vars = XList ++ [K],
solve([],Vars),
write(k=K),nl,
foreach(Row in X) writeln(Row) end,
nl.
all_differ_from_at_least_k_pos(K,X) =>
Len = X.length(),
foreach(I in 1..Len, J in 1..Len)
if I != J then
sum([(R1 #!= R2) : {R1,R2} in zip(X[I],X[J])]) #>= K
end
end.