Added distance_to_centroid.
[clueless-kmeans.git] / clusterer.h
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 #ifndef CLUSTERER_H
25 #define CLUSTERER_H
26
27 #include "misc.h"
28 #include "arrays.h"
29
30 class Clusterer {
31 public:
32
33   enum { STANDARD_ASSOCIATION, STANDARD_LP_ASSOCIATION, UNINFORMATIVE_LP_ASSOCIATION };
34
35   const static int max_nb_iterations = 10;
36   const static scalar_t min_iteration_improvement = 0.999;
37
38   int _nb_clusters;
39   int _dim;
40   scalar_t **_cluster_means, **_cluster_var;
41
42   scalar_t distance_to_centroid(scalar_t *x, int k);
43
44   void initialize_clusters(int nb_points, scalar_t **points);
45
46   // Does the standard hard k-mean association
47
48   scalar_t baseline_cluster_association(int nb_points, scalar_t **points,
49                                         int nb_classes, int *labels,
50                                         scalar_t **gamma);
51
52   // Does the same with an LP formulation, as a sanity check
53
54   scalar_t baseline_lp_cluster_association(int nb_points, scalar_t **points,
55                                            int nb_classes, int *labels,
56                                            scalar_t **gamma);
57
58   // Does the association under constraints that each cluster gets
59   // associated clusters with the same class proportion as the overall
60   // training set
61
62   scalar_t uninformative_lp_cluster_association(int nb_points, scalar_t **points,
63                                                 int nb_classes, int *labels,
64                                                 scalar_t **gamma);
65
66   void update_clusters(int nb_points, scalar_t **points, scalar_t **gamma);
67
68 public:
69   Clusterer();
70   ~Clusterer();
71
72   void train(int mode,
73              int nb_clusters, int dim,
74              int nb_points, scalar_t **points,
75              int nb_classes, int *labels,
76              // This last array returns for each sample to what
77              // cluster it was associated. It can be null.
78              int *cluster_associations);
79
80   int cluster(scalar_t *point);
81 };
82
83 #endif