-
Notifications
You must be signed in to change notification settings - Fork 1
/
kmeans.pl
186 lines (154 loc) · 4.88 KB
/
kmeans.pl
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
:- module(kmeans,
[ k_means/4 % :Map, +Count, +Objects, -Clusters
]).
:- use_module(library(debug)).
:- use_module(library(apply)).
:- use_module(library(lists)).
:- use_module(library(random)).
:- set_prolog_flag(optimise, true).
:- meta_predicate
k_means(2, +, +, -),
k_dist(2, +, +, -),
k_mean(2, +, -).
/** <module> K-Means for clustering rectangles and points
*/
%% k_means(:Map, +Count, +Objects, -Clusters) is det.
k_means(Map, Count, Objects, Clusters) :-
length(Objects, Len),
( Count >= Len
-> maplist(one_element_list, Objects, Clusters)
; randset(Count, Len, SelectionIndices),
n_select(SelectionIndices, Objects, Selection),
maplist(center(Map), Selection, KM1),
MaxIter is max(10, log(Len)*5),
k_iterate(Map, 1, MaxIter, KM1, Objects, [], Clusters)
).
k_iterate(Map, Iteration, MaxIter, Centroites, Objects, Old, Clusters) :-
maplist(length, Old, OldS),
debug(kmean, 'Clustering ~d ... ~p', [Iteration, OldS]),
k_cluster(Map, Centroites, Objects, Clusters0),
( Clusters0 == Old
-> Clusters = Old
; length(Centroites, Needed),
length(Clusters0, CCount),
( CCount < Needed
-> Extra is Needed-CCount,
fill_empty(Extra, Clusters0, Clusters1)
; Clusters1 = Clusters0
),
Iteration2 is Iteration+1,
( Iteration2 < MaxIter
-> maplist(k_mean(Map), Clusters1, NewCentroites),
k_iterate(Map, Iteration2, MaxIter, NewCentroites,
Objects, Clusters1, Clusters)
; Clusters = Clusters1
)
).
%% fill_empty(+Empty, +NonEmpty, -Clusters)
%
% If we end up with empty clusters, take some random element
% from the remaining clusters to fill them up.
fill_empty(0, Clusters, Clusters).
fill_empty(N, Clusters0, Clusters) :-
repeat,
random_select(Cluster, Clusters0, Clusters1),
Cluster = [_,_|_], !,
random_select(Obj, Cluster, RestCluster),
succ(N2, N),
fill_empty(N2, [[Obj],RestCluster|Clusters1], Clusters).
%% k_cluster(:Map, +Centroites, +Objects, -Clusters) is det.
k_cluster(Map, Centroites, Objects, Clusters) :-
CTerm =.. [c|Centroites],
maplist(closest_centroit_pair(Map, CTerm), Objects, Pairs),
keysort(Pairs, Sorted),
group_pairs_by_key(Sorted, Grouped),
pairs_values(Grouped, Clusters).
closest_centroit_pair(Map, CTerm, Object, Centroit-Object) :-
center(Map, Object, Center),
closest_centroit(Center, CTerm, Centroit).
closest_centroit(C0, Centroites, I) :-
functor(Centroites, _, Arity),
arg(Arity, Centroites, CBest),
pt_distance(CBest, C0, DBest),
A2 is Arity-1,
closest_centroit(C0, A2, Centroites, DBest, Arity, I).
closest_centroit(_, 0, _, _, I, I) :- !.
closest_centroit(C0, A, Centroites, DBest, I0, I) :-
arg(A, Centroites, C),
pt_distance(C0, C, D),
( D < DBest
-> I1 = A,
DBest1 = D
; I1 = I0,
DBest1 = DBest
),
A2 is A - 1,
closest_centroit(C0, A2, Centroites, DBest1, I1, I).
pt_distance(point(X1,Y1), point(X2,Y2), D) :-
D is sqrt((X2-X1)**2+(Y2-Y1)**2).
one_element_list(Obj, [Obj]).
n_select(Indices, Set, Selection) :-
n_select(Indices, 1, Set, Selection).
n_select([], _, _, []) :- !.
n_select([I|IT], I, [H|T0], [H|T]) :- !,
I2 is I+1,
n_select(IT, I2, T0, T).
n_select(IL, I0, Set0, Set) :-
I1 is I0+1,
n_select(IL, I1, Set0, Set).
center(Map, Obj, point(X,Y)) :-
rect(Map, Obj, rect(Xs,Ys, Xe,Ye)),
X is (Xe+Xs)/2,
Y is (Ye+Ys)/2.
%% k_dist(+Map, +Object1, +Object2, -Distance) is det.
%
% True when Distance is the Euclidean distance between Object1 and
% Object2. This is defined as the length of the diagonal of the
% minimum bounding box then contains both objects.
k_dist(Map, O1, O2, D) :-
rect(Map, O1, R1),
rect(Map, O2, R2),
rect_union(R1, R2, rect(Xs,Ys, Xe,Ye)),
D is sqrt((Xe-Xs)**2+(Ye-Ys)**2).
rect_union(rect(Xas,Yas, Xae,Yae),
rect(Xbs,Ybs, Xbe,Ybe),
rect(Xs,Ys, Xe,Ye)) :-
Xs is min(Xas,Xbs),
Xe is max(Xae,Xbe),
Ys is min(Yas,Ybs),
Ye is max(Yae,Ybe).
rect_union_list([H|T], Union) :- !,
rect_union_list(T, H, Union).
rect_union_list([], Union, Union).
rect_union_list([H|T], Union0, Union) :-
rect_union(H, Union0, Union1),
rect_union_list(T, Union1, Union).
%% k_mean(:Map, +Objects, -Mean) is det.
%
% Is true if mean is the centrum of gravity of the objects in
% List.
%
% @param Mean is a term point(MX,MY).
k_mean(Map, Objects, point(X,Y)) :-
assertion(Objects \== []),
maplist(rect(Map), Objects, Rects),
maplist(area, Rects, Areas),
sum_xy(Rects, Areas, XSum, YSum),
sum_list(Areas, Den),
X is XSum/Den,
Y is YSum/Den.
sum_xy(Rects, Areas, XSum, YSum) :-
sum_xy(Rects, Areas, 0, XSum, 0, YSum).
sum_xy([], [], XSum, XSum, YSum, YSum).
sum_xy([rect(Xs,Ys, Xe,Ye)|RT], [A|AT], XSum0, XSum, YSum0, YSum) :-
XSum1 is XSum0 + A*((Xs+Xe)/2),
YSum1 is YSum0 + A*((Ys+Ye)/2),
sum_xy(RT, AT, XSum1, XSum, YSum1, YSum).
area(rect(Xs,Ys, Xe,Ye), A) :-
A is Xe-Xs*Ye-Ys.
%% rect(:Map, +Object, -Rect)
%
% Rect is the bounding box of Object. Rect is a term
% rect(Xs,Ys, Xe,Ye).
rect(Map, O, Rect) :-
call(Map, O, Rect).