-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisodata.C
340 lines (323 loc) · 9.52 KB
/
isodata.C
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/********************************************************
# Author: Yury Puzis
# Started: Spring 2003
# Modified: 2/19/04
********************************************************/
#include "isodata.h"
CLUSTER::CLUSTER(int _size, int _k, float _seed) {
BREAKING_POINT = 10.0;
k = _k;
size = _size;
csize = 0;
values = new CVALUE[_size]; //to be filled in by user
centers = new CCENTER*[_size+1];
add_cluster(_seed);
for (int i = 0; i < size; i++) {
values[i].center = centers[0];
// cout << i << ": " << values[i].center->loc << endl;
}
}
CLUSTER::~CLUSTER() {
delete [] values;
}
float distance(float a, float b) {
if (a > b) return (float)(a - b);
return (float)(b-a);
}
void CLUSTER::print() {
for (int i = 0; i < size; i++) {
// cout << "i: "<< i << " || values[i].loc: " << values[i].loc << " || C:"
// << values[i].center->loc << endl;
}
cout << "-clusters--------------------------------------------" << endl;
for (int v = 0; v < csize; v++) {
cout << v << ": loc: " << centers[v]->loc << " || size: " <<
centers[v]->size << " || deviation: " << centers[v]->deviation << endl;
}
cout << "=====================================================" << endl;
}
void CLUSTER::update_membership() {
for (int i = 0; i < size; i++) {
for (int v = 0; v < csize; v++) {
if (values[i].distance() > ::distance(values[i].loc, centers[v]->loc)) {
// cout << "assigning " << i << " to " << centers[v]->loc << endl;
values[i].center = centers[v];
}
}
}
}
float CLUSTER::update_clusters(){
for (int v = 0; v < csize; v++) {
centers[v]->size = 0;
centers[v]->enumeration = 0;
centers[v]->loc = 0;
centers[v]->distance = 0;
centers[v]->deviation = 0;
}
//cout << " % % % " << endl;
//print ();
//cout << " % % % " << endl;
//incrementaly update distance and deviation
float TOTAL_DIST = 0;
for (int i = 0; i < size; i++) {
values[i].center->loc += values[i].loc;
values[i].center->size++;
values[i].center->distance += values[i].distance();
values[i].center->enumeration++;
//values[i].center->deviation +=
// pow(values[i].center->enumeration - values[i].loc, 2);
TOTAL_DIST += values[i].distance();
}
//recalculate distance to cluster center, and the overall distance (AV_DIST)
int i = 0;
for (int v = 0; v < csize; v++) {
i++;
if (centers[v]->size > 0) {
centers[v]->loc /= centers[v]->size;
centers[v]->distance /= centers[v]->size;
//cout << "DEVIATION: " << centers[v]->deviation << " size " << centers[v]->size << endl;
} else {
//cout << "erasing " << v << " || " << centers[v]->loc << endl;
erase_cluster(v);
v--;
//cout << "shifting " << i << " to " << centers[v]->loc << endl;
//cout << "ZERO! " << endl;
}
}
for (int i = 0; i < size; i++) {
values[i].center->deviation += pow(values[i].loc - values[i].center->loc, 2);
}
for (int v = 0; v < csize; v++) {
centers[v]->deviation = sqrt(centers[v]->deviation / centers[v]->size);
}
return TOTAL_DIST/size;
}
ACT CLUSTER::try_lumping() {
if (last_action != _split) {
if (csize == 0) return _end;
if (csize == 1) return _nothing;
}
update_membership();
cluster_insertion_sort();
for (int v = 0; v < csize; v++) {
centers[v]->enumeration = 0;
}
int MAX_MEMBERS = 0;
for (int v = 0; v < csize-1; v++) {
if (centers[v]->size > MAX_MEMBERS) MAX_MEMBERS = centers[v]->size;
if (::distance(centers[v]->loc, centers[v+1]->loc) < MIN_DIST) {
//cout << "will lump " << v << " and " << v+1 << endl;
centers[v]->enumeration = 1;
v++;
} else {
centers[v]->enumeration = 0;
}
}
//cout << "MAX MEMBERS " << MAX_MEMBERS << endl;
int KS;
if (MAX_MEMBERS < k) KS = MAX_MEMBERS; else KS = k;
if (KS == 0 && last_action == _split) return _nothing;
if (KS == 0 && last_action != _split) return _end;
//cout << "lumping, KS " << KS << endl;
print();
int v = 0;
while (KS > 0 && v < csize-1) {
if (centers[v]->enumeration == 1) {
//cout << "actually lumping " << endl;
KS--;
centers[v]->loc = (centers[v]->loc + centers[v+1]->loc)/2;
//for (int i = 0; i < size; i++)
//if (values[i].center == centers[v]) values[i].center = centers[v+1];
erase_cluster(v+1);
} else {
v++;
}
}
//cout << "after lumping number of clusters:" << csize << endl;
print();
return _lump;
}
void CLUSTER::cluster_iso() {
bool ITER = false;
int ITER_N = 0;
float LO_Q = (k+1)/2;
float HI_Q = 2*k;
//print();
while (ITER_N++ < MAX_ITER) {
//cout << "< Updating membership >" << endl;
update_membership();
//print();
//cout << "< Updating clusters >" << endl;
float AV_DIST = update_clusters();
//print();
if (csize < LO_Q || (csize < HI_Q && ITER)) {
int u = csize;
//cout << "< Number of clusters: " << csize << " >" << endl;
for (int v = 0; u-- > 0; v++) {
if (centers[v]->deviation > BREAKING_POINT) {
if(LO_Q > csize ||
(centers[v]->distance > AV_DIST && centers[v]->size > 2*MIN_SIZE)){
//cout << "splitting " << centers[v]->loc << endl;
centers[add_cluster(centers[v]->loc + 0.01)]->size = 0;
centers[v]->loc -= 0.01;
last_action = _split;
} else {
if (ITER_N > 1) {
if (last_action != _lump) {
last_action = try_lumping();
} else {
if (csize == 0) break; else continue;
}
} else {
last_action = try_lumping();
}
}
}
}
} else {
last_action = try_lumping();
}
ITER = 1 - ITER;
}
}
void CLUSTER::cluster_iso2() {
if (one_to_one()) return;
bool ITER = false;
unsigned int ITER_N = 0;
float LO_Q = (k+1)/2;
float HI_Q = 2*k;
int last = 0;
//int last_csize = -1;
//print();
last_action = _split;
while (last < 4) {
// cout << "LOOP " << ITER_N << endl;
last_action = _nothing;
//cout << "< Updating membership >" << endl;
update_membership();
//cout << "< Updating clusters >" << endl;
float AV_DIST = update_clusters();
//print();
//print();
if (csize < HI_Q) {
int u = csize;
//cout << "< Number of clusters: " << csize << " >" << endl;
for (int v = 0; u-- > 0; v++) {
if (centers[v]->deviation > BREAKING_POINT) {
// if(LO_Q > csize ||
// (centers[v]->distance > AV_DIST && centers[v]->size > 2*MIN_SIZE)){
//cout << "splitting " << centers[v]->loc << " BR " << BREAKING_POINT << endl;
centers[add_cluster(centers[v]->loc + 0.0001)]->size = 0;
centers[v]->loc -= 0.00001;
last = 0;
// }
}
}
}
ITER = 1 - ITER;
last++;
if (last == 4 && csize < k) {
float OBP = BREAKING_POINT;
float DV = 0;
//find average deviation and set BREAKING_POINT to it
for (int v = 0; v < csize; v++) DV += centers[v]->deviation;
DV /= csize;
//make a little smaller then average to be able break a single cluster
BREAKING_POINT = DV-0.0000001;
//cout << "adjusting BREAKING_POINT " << BREAKING_POINT << endl;
//take another shot _only_ if this is the first time this BP is used
if (OBP != BREAKING_POINT) last = 0;
//there are no divisible clusters, quit
if (DV == 0) last = 4;
}
ITER_N++;
// cout << "turn " << ITER_N << endl;
}
if (csize < k) {
//divide largest clusters in 2 ?
}
if (csize > k) {
int EXTRA = csize - k;
while (EXTRA > 0) {
update_membership();
update_clusters();
cluster_insertion_sort();
float s = ::distance(centers[0]->loc, centers[1]->loc); // *
// (centers[0]->size + centers[1]->size);
int l = 0;
for (int v = 1; v < csize-1; v++) {
float m = ::distance(centers[v]->loc, centers[v+1]->loc);
if (m < s) {
s = m;
l = v;
}
}
EXTRA--;
centers[l]->loc = (centers[l]->loc + centers[l+1]->loc)/2;
erase_cluster(l+1);
}
}
update_membership();
update_clusters();
}
void CLUSTER::cluster_k() {
}
bool CLUSTER::one_to_one() {
//check if there are less or same number of values then number of desired cl.
int distinct_values = size;
for (int i = 0; i < size; i++) {
for (int j = i - 1; j >= 0; j--) {
if (values[i].loc == values[j].loc) {
distinct_values--;
break;
}
}
}
//there are more distinct values then desired # of clusters
if (distinct_values > k) return false;
//there are less or equal distinct values then desired # of clusters
bool distinct;
for (int i = 0; i < size; i++) {
distinct = true;
for (int j = i - 1; j >= 0; j--) {
if (values[i].loc == values[j].loc) {
distinct = false;
values[i].center = values[j].center;
break;
}
}
if (distinct) values[i].center = centers[add_cluster(values[i].loc)];
}
return true;
}
void CLUSTER::cluster_insertion_sort() {
int j;
float index;
CCENTER* center;
for (int i = 1; i < csize; i++) {
index = centers[i]->loc;
center = centers[i];
j = i;
while (j > 0 && centers[j-1]->loc > index) centers[j] = centers[--j];
centers[j] = center;
}
}
/* buggy
void CLUSTER::value_insertion_sort() {
int j;
float index;
for (int i = 0; i < size; i++) {
index = values[i];
j = i;
while (j > 0 && values[j-1] > index) values[j] = values[--j];
values[j] = index;
}
}
*/
int CLUSTER::enumerate() {
cluster_insertion_sort();
//assign each cluster serial number
int p = 1;
for (int v = 0; v < csize; v++) centers[v]->enumeration = p++;
return csize;
}