clueless-kmean now takes as parameter in which mode to work, and test.sh generate...
[clueless-kmeans.git] / clusterer.cc
1 /*
2  *  clueless-kmean is a variant of k-mean which enforces balanced
3  *  distribution of classes in every cluster
4  *
5  *  Copyright (c) 2013 Idiap Research Institute, http://www.idiap.ch/
6  *  Written by Francois Fleuret <francois.fleuret@idiap.ch>
7  *
8  *  This file is part of clueless-kmean.
9  *
10  *  clueless-kmean is free software: you can redistribute it and/or
11  *  modify it under the terms of the GNU General Public License
12  *  version 3 as published by the Free Software Foundation.
13  *
14  *  clueless-kmean is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  *  General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with selector.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  */
23
24 #include "clusterer.h"
25 #include <glpk.h>
26
27 Clusterer::Clusterer() {
28   _cluster_means = 0;
29   _cluster_var = 0;
30 }
31
32 Clusterer::~Clusterer() {
33   deallocate_array<scalar_t>(_cluster_means);
34   deallocate_array<scalar_t>(_cluster_var);
35 }
36
37 scalar_t Clusterer::distance_to_centroid(scalar_t *x, int k) {
38   scalar_t dist = 0;
39   for(int d = 0; d < _dim; d++) {
40     dist += sq(_cluster_means[k][d] - x[d]) / (2 * _cluster_var[k][d]);
41     dist += 0.5 * log(_cluster_var[k][d]);
42     ASSERT(!isnan(dist) && !isinf(dist));
43   }
44   return dist;
45 }
46
47 void Clusterer::initialize_clusters(int nb_points, scalar_t **points) {
48   int *used = new int[nb_points];
49   for(int k = 0; k < nb_points; k++) { used[k] = 0; }
50   for(int k = 0; k < _nb_clusters; k++) {
51     int l;
52     do { l = int(drand48() * nb_points); } while(used[l]);
53     for(int d = 0; d < _dim; d++) {
54       _cluster_means[k][d] = points[l][d];
55       _cluster_var[k][d] = 1.0;
56     }
57     used[l] = 1;
58   }
59   delete[] used;
60 }
61
62 scalar_t Clusterer::baseline_cluster_association(int nb_points, scalar_t **points,
63                                                  int nb_classes, int *labels,
64                                                  scalar_t **gamma)  {
65   int *associated_clusters = new int[nb_points];
66   scalar_t total_dist = 0;
67
68   for(int n = 0; n < nb_points; n++) {
69     scalar_t lowest_dist = 0;
70     for(int k = 0; k < _nb_clusters; k++) {
71       scalar_t dist = distance_to_centroid(points[n], k);
72       if(k == 0 || dist <= lowest_dist) {
73         lowest_dist = dist;
74         associated_clusters[n] = k;
75       }
76     }
77
78     total_dist += lowest_dist;
79   }
80
81   for(int n = 0; n < nb_points; n++) {
82     for(int k = 0; k < _nb_clusters; k++) {
83       gamma[n][k] = 0.0;
84     }
85     gamma[n][associated_clusters[n]] = 1.0;
86   }
87
88   delete[] associated_clusters;
89
90   return total_dist;
91 }
92
93 scalar_t Clusterer::baseline_lp_cluster_association(int nb_points, scalar_t **points,
94                                                     int nb_classes, int *labels,
95                                                     scalar_t **gamma)  {
96   glp_prob *lp;
97
98   int *coeff_row = new int[nb_points * _nb_clusters + 1];
99   int *coeff_col = new int[nb_points * _nb_clusters + 1];
100   scalar_t *coeff_wgt = new scalar_t[nb_points * _nb_clusters + 1];
101
102   lp = glp_create_prob();
103
104   glp_set_prob_name(lp, "baseline_lp_cluster_association");
105   glp_set_obj_dir(lp, GLP_MIN);
106
107   glp_add_rows(lp, nb_points);
108
109   for(int n = 1; n <= nb_points; n++) {
110     glp_set_row_bnds(lp, n, GLP_FX, 1.0, 1.0);
111   }
112
113   glp_add_cols(lp, nb_points * _nb_clusters);
114   for(int k = 1; k <= _nb_clusters; k++) {
115     for(int n = 1; n <= nb_points; n++) {
116       int i = n + nb_points * (k - 1);
117       glp_set_obj_coef(lp, i, distance_to_centroid(points[n-1], k-1));
118       glp_set_col_bnds(lp, i, GLP_DB, 0.0, 1.0);
119     }
120   }
121
122   int n_coeff = 1;
123
124   for(int n = 1; n <= nb_points; n++) {
125     for(int k = 1; k <= _nb_clusters; k++) {
126       coeff_row[n_coeff] = n;
127       coeff_col[n_coeff] = n + nb_points * (k - 1);
128       coeff_wgt[n_coeff] = 1.0;
129       n_coeff++;
130     }
131   }
132
133   glp_load_matrix(lp, nb_points * _nb_clusters, coeff_row, coeff_col, coeff_wgt);
134
135   glp_simplex(lp, NULL);
136
137   scalar_t total_dist = glp_get_obj_val(lp);
138
139   for(int k = 1; k <= _nb_clusters; k++) {
140     for(int n = 1; n <= nb_points; n++) {
141       int i = n + nb_points * (k - 1);
142       gamma[n-1][k-1] = glp_get_col_prim(lp, i);
143     }
144   }
145
146   delete[] coeff_row;
147   delete[] coeff_col;
148   delete[] coeff_wgt;
149
150   glp_delete_prob(lp);
151
152   return total_dist;
153 }
154
155 scalar_t Clusterer::uninformative_lp_cluster_association(int nb_points, scalar_t **points,
156                                                          int nb_classes, int *labels,
157                                                          scalar_t **gamma)  {
158   // N points
159   // K clusters
160   // dist(n,k) distance of samples n to cluster k
161   //
162   // We want to optimize the
163   //
164   // \gamma(n,k) is the association of point n to cluster k
165   //
166   // to minimize
167   //
168   // \sum_{n,k} \gamma(n,k) dist(n,k)
169   //
170   // under
171   //
172   // (A) \forall n, k, \gamma(n, k) >= 0
173   // (B) \forall n, \sum_k \gamma(n,k) = 1
174   // (C) \forall k, \sum_n \gamma(n,k) = N/K
175
176   glp_prob *lp;
177
178   // The coefficients for the constraints are passed to the glpk
179   // functions with a sparse representation.
180
181   // ** GLPK USES INDEXES STARTING AT 1, NOT 0. **
182
183   int nb_coeffs = nb_points * _nb_clusters + nb_points * _nb_clusters;
184
185   int *coeff_row = new int[nb_coeffs + 1];
186   int *coeff_col = new int[nb_coeffs + 1];
187   scalar_t *coeff_wgt = new scalar_t[nb_coeffs + 1];
188
189   int n_coeff = 1;
190
191   scalar_t *nb_samples_per_class = new scalar_t[nb_classes];
192   for(int c = 0; c < nb_classes; c++) {
193     nb_samples_per_class[c] = 0.0;
194   }
195
196   for(int n = 0; n < nb_points; n++) {
197     nb_samples_per_class[labels[n]] += 1.0;
198   }
199
200   lp = glp_create_prob();
201
202   glp_set_prob_name(lp, "uninformative_lp_cluster_association");
203   glp_set_obj_dir(lp, GLP_MIN);
204
205   // We have one column per coefficient gamma
206
207   glp_add_cols(lp, nb_points * _nb_clusters);
208
209   // The column for gamma[n][k] point 1<=n<=nb_points and cluster
210   // 1<=k<=_nb_clusters is nb_points * (k - 1) + n;
211
212   // The constraints (A) will be expressed by putting directly bounds
213   // on the variables (i.e. one per column). So we need one row per
214   // (B) constraint, and one per (C) constraint.
215
216   glp_add_rows(lp, nb_points + _nb_clusters * nb_classes);
217
218   // First, we set the weights for the objective function, and the (A)
219   // constraints on the individual gammas
220
221   for(int k = 1; k <= _nb_clusters; k++) {
222     for(int n = 1; n <= nb_points; n++) {
223       int col = n + nb_points * (k - 1);
224
225       // The LP weight on the gammas for the global loss is the
226       // normalized distance of that sample to the centroid of that
227       // cluster
228
229       glp_set_obj_coef(lp, col, distance_to_centroid(points[n-1], k-1));
230
231       // The (A) constraints: Each column correspond to one of the
232       // gamma, and it has to be in [0,1]
233
234       glp_set_col_bnds(lp, col, GLP_DB, 0.0, 1.0);
235     }
236   }
237
238   // The (B) constraints: for each point, the sum of its gamma is
239   // equal to 1.0
240
241   for(int n = 1; n <= nb_points; n++) {
242     int row = n;
243     glp_set_row_bnds(lp, row, GLP_FX, 1.0, 1.0);
244     for(int k = 1; k <= _nb_clusters; k++) {
245       coeff_row[n_coeff] = row;
246       coeff_col[n_coeff] = nb_points * (k - 1) + n;
247       coeff_wgt[n_coeff] = 1.0;
248       n_coeff++;
249     }
250   }
251
252   // The (C) constraints: For each pair cluster/class, the sum of the
253   // gammas for this cluster and this class is equal to the number of
254   // sample of that class, divided by the number of clusters
255
256   for(int k = 1; k <= _nb_clusters; k++) {
257     for(int c = 1; c <= nb_classes; c++) {
258       int row = nb_points + (k - 1) * nb_classes + c;
259       scalar_t tau = nb_samples_per_class[c-1] / scalar_t(_nb_clusters);
260       glp_set_row_bnds(lp, row, GLP_FX, tau, tau);
261       for(int n = 1; n <= nb_points; n++) {
262         if(labels[n-1] == c - 1) {
263           coeff_row[n_coeff] = row;
264           coeff_col[n_coeff] = (k-1)  * nb_points + n;
265           coeff_wgt[n_coeff] = 1.0;
266           n_coeff++;
267         }
268       }
269     }
270   }
271
272   ASSERT(n_coeff == nb_coeffs + 1);
273
274   glp_load_matrix(lp, nb_coeffs, coeff_row, coeff_col, coeff_wgt);
275
276   // Now a miracle occurs
277
278   glp_simplex(lp, NULL);
279
280   // We retrieve the result
281
282   scalar_t total_dist = glp_get_obj_val(lp);
283
284   for(int k = 1; k <= _nb_clusters; k++) {
285     for(int n = 1; n <= nb_points; n++) {
286       int i = n + nb_points * (k - 1);
287       gamma[n-1][k-1] = glp_get_col_prim(lp, i);
288     }
289   }
290
291   delete[] nb_samples_per_class;
292   delete[] coeff_row;
293   delete[] coeff_col;
294   delete[] coeff_wgt;
295   glp_delete_prob(lp);
296
297   return total_dist;
298 }
299
300 void Clusterer::update_clusters(int nb_points, scalar_t **points, scalar_t **gamma)  {
301   for(int k = 0; k < _nb_clusters; k++) {
302
303     for(int d = 0; d < _dim; d++) {
304       _cluster_means[k][d] = 0.0;
305       _cluster_var[k][d] = 0.0;
306     }
307
308     scalar_t sum_gamma = 0;
309     for(int n = 0; n < nb_points; n++) {
310       sum_gamma += gamma[n][k];
311       for(int d = 0; d < _dim; d++) {
312         _cluster_means[k][d] += gamma[n][k] * points[n][d];
313         _cluster_var[k][d] += gamma[n][k] * sq(points[n][d]);
314       }
315     }
316
317     ASSERT(sum_gamma >= 1);
318
319     for(int d = 0; d < _dim; d++) {
320       if(sum_gamma >= 2) {
321         _cluster_var[k][d] =
322           (_cluster_var[k][d] - sq(_cluster_means[k][d]) / sum_gamma) / (sum_gamma - 1);
323         _cluster_var[k][d] = max(scalar_t(min_cluster_variance), _cluster_var[k][d]);
324       } else {
325         _cluster_var[k][d] = 1;
326       }
327
328       _cluster_means[k][d] /= sum_gamma;
329     }
330   }
331 }
332
333 void Clusterer::train(int mode,
334                       int nb_clusters, int dim,
335                       int nb_points, scalar_t **points,
336                       int nb_classes, int *labels,
337                       int *cluster_associations) {
338   deallocate_array<scalar_t>(_cluster_means);
339   deallocate_array<scalar_t>(_cluster_var);
340   _nb_clusters = nb_clusters;
341   _dim = dim;
342   _cluster_means = allocate_array<scalar_t>(_nb_clusters, _dim);
343   _cluster_var = allocate_array<scalar_t>(_nb_clusters, _dim);
344
345   scalar_t **gammas = allocate_array<scalar_t>(nb_points, _nb_clusters);
346
347   if(nb_clusters > nb_points) abort();
348
349   initialize_clusters(nb_points, points);
350
351   scalar_t pred_total_distance, total_distance = FLT_MAX;
352   int nb_rounds = 0;
353
354   do {
355     pred_total_distance = total_distance;
356
357     switch(mode) {
358
359     case STANDARD_ASSOCIATION:
360     total_distance =
361       baseline_cluster_association(nb_points, points, nb_classes, labels, gammas);
362       break;
363
364     case STANDARD_LP_ASSOCIATION:
365     total_distance =
366       baseline_lp_cluster_association(nb_points, points, nb_classes, labels, gammas);
367       break;
368
369     case UNINFORMATIVE_LP_ASSOCIATION:
370     total_distance =
371       uninformative_lp_cluster_association(nb_points, points, nb_classes, labels, gammas);
372       break;
373
374     default:
375       cerr << "Unknown sample-cluster association mode." << endl;
376       abort();
377     }
378
379     cout << "TRAIN " << nb_rounds << " " << total_distance << endl;
380     update_clusters(nb_points, points, gammas);
381     nb_rounds++;
382   } while(total_distance < min_iteration_improvement * pred_total_distance &&
383           nb_rounds < max_nb_iterations);
384
385   if(cluster_associations) {
386     for(int n = 0; n < nb_points; n++) {
387       for(int k = 0; k < _nb_clusters; k++) {
388         if(k == 0 || gammas[n][k] > gammas[n][cluster_associations[n]]) {
389           cluster_associations[n] = k;
390         }
391       }
392     }
393   }
394
395   deallocate_array<scalar_t>(gammas);
396 }
397
398 int Clusterer::cluster(scalar_t *point) {
399   scalar_t lowest_dist = 0;
400   int associated_cluster = -1;
401
402   for(int k = 0; k < _nb_clusters; k++) {
403     scalar_t dist = 0;
404
405     for(int d = 0; d < _dim; d++) {
406       dist += sq(_cluster_means[k][d] - point[d]) / (2 * _cluster_var[k][d]);
407       dist += 0.5 * log(_cluster_var[k][d]);
408       ASSERT(!isnan(dist) && !isinf(dist));
409     }
410
411     if(k == 0 || dist <= lowest_dist) {
412       lowest_dist = dist;
413       associated_cluster = k;
414     }
415   }
416
417   ASSERT(associated_cluster >= 0);
418
419   return associated_cluster;
420 }