-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflame.cpp
More file actions
323 lines (286 loc) · 10.4 KB
/
flame.cpp
File metadata and controls
323 lines (286 loc) · 10.4 KB
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
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
#include"math.h"
#include <utility>
#include"flame.h"
float Flame_Euclidean2( float *x, float *y, int m )
{
float d = 0;
int i;
for(i=0; i<m; i++ ) d += ( x[i] - y[i] ) * ( x[i] - y[i] );
return sqrt( d );
}
Flame::Flame(float **p_data, int p_n, int p_m )
{
m_N = p_n;
m_M = p_m;
// printf( "m_N % i \n", m_N);
m_KMAX = sqrt( m_N ) + 10;
if( m_KMAX >= m_N ) m_KMAX = m_N - 1;
m_indexFloatArray = new std::pair<int, float> [ m_N ];
m_graph = new int*[ m_N ];
m_dists = new float*[ m_N ];
for( int i = 0; i < m_N; i++){
m_graph[ i ] = new int[ m_KMAX ];
m_dists[ i ] = new float[ m_KMAX ];
// weights[i] = new float*[MAX];
for( int j = 0; j < m_N; j++){
m_indexFloatArray[ j ].first = j;
m_indexFloatArray[ j ].second = Flame_Euclidean2( p_data[i], p_data[j], m_M );
}
partialQuickSort( 0, m_N-1, m_KMAX+1 );
/* Store MAX number of nearest neighbors. */
for( int j = 0; j < m_KMAX; j++){
m_graph[ i ][ j ] = m_indexFloatArray[ j+1 ].first;
m_dists[ i ][ j ] = m_indexFloatArray[ j+1 ].second;
}
}
delete[] m_indexFloatArray;
}
Flame::~Flame()
{
for ( int i = 0; i < m_N; i++ ) {
delete[] m_graph[i];
delete[] m_dists[i];
}
delete[] m_graph;
delete[] m_dists;
}
void Flame::partialQuickSort( int first, int last, int part )
{
int lower=first+1, upper=last;
float pivot;
std::pair<int, float> val;
if( first >= last ) return;
val = m_indexFloatArray[first];
m_indexFloatArray[first] = m_indexFloatArray[ (first+last)/2 ];
m_indexFloatArray[ (first+last)/2 ] = val;
pivot = m_indexFloatArray[ first ].second;
while( lower <= upper ){
while( lower <= last && m_indexFloatArray[lower].second < pivot ) lower ++;
while( pivot < m_indexFloatArray[upper].second ) upper --;
if( lower < upper ){
val = m_indexFloatArray[lower];
m_indexFloatArray[lower] = m_indexFloatArray[upper];
m_indexFloatArray[upper] = val;
upper --;
}
lower ++;
}
val = m_indexFloatArray[first];
m_indexFloatArray[first] = m_indexFloatArray[upper];
m_indexFloatArray[upper] = val;
if( first < upper-1 ) partialQuickSort( first, upper-1, part );
if( upper >= part ) return;
if( upper+1 < last ) partialQuickSort( upper+1, last, part );
}
void Flame::defineSupports( int p_knn, float p_thd )
{
float *density = new float[ m_N ];
float d, sum, sum2, fmin, fmax = 0.0;
delete[] m_nncounts;
m_nncounts = new int[ m_N ];
m_weights = new float*[ m_N ];
if( p_knn > m_KMAX ) p_knn = m_KMAX;
m_K = p_knn;
for( int i = 0; i < m_N; i++){
/* To include all the neighbors that have distances equal to the
* distance of the most distant one of the K-Nearest Neighbors */
int k = p_knn;
d = m_dists[i][p_knn-1];
for( int j = p_knn; j < m_KMAX; j++)
if( m_dists[i][j] == d ) k++; else break;
m_nncounts[i] = k;
m_weights[i] = new float[k];
/* The definition of weights in this implementation is
* different from the previous implementations where distances
* or similarities often have to be transformed in some way.
*
* But in this definition, the weights are only dependent on
* the ranking of distances of the neighbors, so it is more
* robust against distance transformations. */
sum = 0.5*k*(k+1.0);
for( int j = 0; j < k; j++)
m_weights[i][j] = (k-j) / sum;
sum = 0.0;
for( int j = 0; j < k; j++) sum += m_dists[i][j];
density[i] = 1.0 / (sum + EPSILON);
}
sum = 0.0;
sum2 = 0.0;
for(int i = 0; i < m_N; i++){
sum += density[i];
sum2 += density[i] * density[i];
}
sum = sum / m_N;
/* Density threshold for possible outliers. */
p_thd = sum + p_thd * sqrt( sum2 / m_N - sum * sum );
if ( m_obtypes != nullptr )
delete m_obtypes;
m_obtypes = new char[m_N];
for( int i = 0; i < m_N; i++)
m_obtypes[i] = OBT_NORMAL;
m_cso_count = 0;
for( int i = 0; i < m_N; i++){
int k = m_nncounts[i];
fmax = 0.0;
fmin = density[i] / density[ m_graph[i][0] ];
for( int j = 1; j < k; j++){
d = density[i] / density[ m_graph[i][j] ];
if( d > fmax ) fmax = d;
if( d < fmin ) fmin = d;
/* To avoid defining neighboring objects or objects close
* to an outlier as CSOs. */
if( m_obtypes[ m_graph[i][j] ] ) fmin = 0.0;
}
// printf(" for el %i we got fmin %f and fmax %f \n", i, fmin, fmax);
if( fmin >= 1.0 ){
m_cso_count ++;
m_obtypes[i] = OBT_SUPPORT;
}else if( fmax <= 1.0 && density[i] < p_thd ){
m_obtypes[i] = OBT_OUTLIER;
}
}
delete[] density;
}
void Flame::localApproximation( int p_steps, float p_epsilon )
{
m_fuzzyships = new float*[ m_N ];
float **fuzzyships2 = new float*[ m_N ];
char even = 0;
double dev;
int k = 0;
for( int i = 0; i < m_N; i++){
m_fuzzyships[i] = new float[ m_cso_count+1 ];
fuzzyships2[i] = new float[ m_cso_count+1 ];
for( int j = 0; j <= m_cso_count; j++)
m_fuzzyships[i][j] = fuzzyships2[i][j] = 0;
if( m_obtypes[i] == OBT_SUPPORT ){
/* Full membership to the cluster represented by itself. */
m_fuzzyships[i][k] = 1.0;
fuzzyships2[i][k] = 1.0;
k ++;
}else if( m_obtypes[i] == OBT_OUTLIER ){
/* Full membership to the outlier group. */
m_fuzzyships[i][m_cso_count] = 1.0;
fuzzyships2[i][m_cso_count] = 1.0;
}else{
/* Equal memberships to all clusters and the outlier group.
* Random initialization does not change the results. */
for( int j = 0; j <= m_cso_count; j++)
m_fuzzyships[i][j] = fuzzyships2[i][j] = 1.0/(m_cso_count+1);
}
}
for( int t = 0; t < p_steps; t++){
printf("iter %i", t);
dev = 0;
for( int i = 0; i < m_N; i++){
int knn = m_nncounts[i];
int *ids = m_graph[i];
float *wt = m_weights[i];
float *fuzzy = m_fuzzyships[i];
float **fuzzy2 = fuzzyships2;
double sum = 0.0;
if( m_obtypes[i] != OBT_NORMAL ) continue;
if( even ){
fuzzy = fuzzyships2[i];
fuzzy2 = m_fuzzyships;
}
/* Update membership of an object by a linear combination of
* the memberships of its nearest neighbors. */
for( int j = 0; j <= m_cso_count; j++){
fuzzy[j] = 0.0;
for(k=0; k<knn; k++) fuzzy[j] += wt[k] * fuzzy2[ ids[k] ][j];
dev += (fuzzy[j] - fuzzy2[i][j]) * (fuzzy[j] - fuzzy2[i][j]);
sum += fuzzy[j];
}
for( int j = 0; j <= m_cso_count; j++) fuzzy[j] = fuzzy[j] / sum;
}
even = ! even;
if( dev < p_epsilon ) break;
}
/* update the membership of all objects to remove clusters
* that contains only the CSO. */
for( int i = 0; i < m_N; i++){
int knn = m_nncounts[i];
int *ids = m_graph[i];
float *wt = m_weights[i];
float *fuzzy = m_fuzzyships[i];
float **fuzzy2 = fuzzyships2;
for( int j = 0; j <= m_cso_count; j++){
fuzzy[j] = 0.0;
for(k=0; k<knn; k++) fuzzy[j] += wt[k] * fuzzy2[ ids[k] ][j];
dev += (fuzzy[j] - fuzzy2[i][j]) * (fuzzy[j] - fuzzy2[i][j]);
}
}
for( int i = 0; i < m_N; i++) delete[] fuzzyships2[i];
delete[] fuzzyships2;
}
void Flame::makeClusters( float p_thd )
{
int imax;
int C = m_cso_count+1;
float fmax;
// IntArray *clust;
if ( m_clusters != nullptr ) delete[] m_clusters;
m_clusters = new int[ m_N ];
m_indexFloatArray = new std::pair<int, float>[ m_N ];
/* Sort objects based on the "entropy" of fuzzy memberships. */
for( int i = 0; i < m_N; i++){
m_indexFloatArray[i].first = i;
m_indexFloatArray[i].second = 0.0;
for( int j = 0; j < C; j++){
float fs = m_fuzzyships[i][j];
if( fs > EPSILON ) m_indexFloatArray[i].second -= fs * log( fs );
}
}
partialQuickSort( 0, m_N-1, m_N );
// self->clusters = (IntArray*) calloc( C, sizeof(IntArray) );
if( p_thd <0 || p_thd > 1.0 ){
/* Assign each object to the cluster
* in which it has the highest membership. */
for( int i = 0; i < m_N; i++ ){
int id = m_indexFloatArray[i].first;
fmax = 0;
imax = -1;
for( int j = 0; j < C; j++ ){
if( m_fuzzyships[id][j] > fmax ){
imax = j;
fmax = m_fuzzyships[id][j];
}
}
m_clusters[i] = imax;
}
// }else{
// /* Assign each object to all the clusters
// * in which it has membership higher than thd,
// * otherwise, assign it to the outlier group.*/
// for(i=0; i<m_N; i++){
// int id = m_indexFloatArray[i].first;
// imax = -1;
// for(j=0; j<C; j++){
// if( m_fuzzyships[id][j] > p_thd || ( j == C-1 && imax <0 ) ){
// imax = j;
// clust = self->clusters + j;
// IntArray_Push( self->clusters + j, id );
// }
// }
// }
}
/* removing empty clusters */
// C = 0;
// for( int i = 0; i < m_cso_count; i++){
// if( self->clusters[i].size >0 ){
// self->clusters[C] = self->clusters[i];
// C ++;
// }
// }
/* keep the outlier group, even if its empty */
// self->clusters[C] = self->clusters[m_cso_count];
// C ++;
// for( int i = C; i < m_cso_count+1; i++)
// memset( self->clusters+i, 0, sizeof(IntArray) );
// m_count = C;
delete[] m_indexFloatArray;
}