GestureRecognitionToolkit  Version: 1.0 Revision: 04-03-15
The Gesture Recognition Toolkit (GRT) is a cross-platform, open-source, c++ machine learning library for real-time gesture recognition.
Clusterer.cpp
1 /*
2 GRT MIT License
3 Copyright (c) <2012> <Nicholas Gillian, Media Lab, MIT>
4 
5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
6 and associated documentation files (the "Software"), to deal in the Software without restriction,
7 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
9 subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in all copies or substantial
12 portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
15 LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
18 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 */
20 
21 #include "Clusterer.h"
22 namespace GRT{
23 
24 Clusterer::StringClustererMap* Clusterer::stringClustererMap = NULL;
25 UINT Clusterer::numClustererInstances = 0;
26 
27 Clusterer* Clusterer::createInstanceFromString(string const &clustererType){
28 
29  StringClustererMap::iterator iter = getMap()->find( clustererType );
30  if( iter == getMap()->end() ){
31  return NULL;
32  }
33  return iter->second();
34 }
36  return createInstanceFromString( clustererType );
37 }
38 
40 
41  Clusterer *newInstance = createInstanceFromString( clustererType );
42 
43  if( newInstance == NULL ) return NULL;
44 
45  if( !newInstance->deepCopyFrom( this ) ){
46  delete newInstance;
47  return NULL;
48  }
49  return newInstance;
50 }
51 
53  vector< string > registeredClusterers;
54 
55  StringClustererMap::iterator iter = getMap()->begin();
56  while( iter != getMap()->end() ){
57  registeredClusterers.push_back( iter->first );
58  iter++;
59  }
60  return registeredClusterers;
61 }
62 
64  baseType = MLBase::CLUSTERER;
65  clustererType = "NOT_SET";
66  numClusters = 10;
68  maxLikelihood = 0;
69  bestDistance = 0;
70  minNumEpochs = 1;
71  maxNumEpochs = 1000;
72  minChange = 1.0e-5;
73  converged = false;
74  numClustererInstances++;
75 }
76 
78  if( --numClustererInstances == 0 ){
79  delete stringClustererMap;
80  stringClustererMap = NULL;
81  }
82 }
83 
84 bool Clusterer::copyBaseVariables(const Clusterer *clusterer){
85 
86  if( clusterer == NULL ){
87  errorLog << "copyBaseVariables(const Clusterer *clusterer) - clusterer is NULL!" << endl;
88  return false;
89  }
90 
91  if( !this->copyMLBaseVariables( clusterer ) ){
92  return false;
93  }
94 
95  //Copy the clusterer base variables
96  this->clustererType = clusterer->clustererType;
97  this->numClusters = clusterer->numClusters;
100  this->maxLikelihood = clusterer->maxLikelihood;
101  this->bestDistance = clusterer->bestDistance;
102  this->clusterLikelihoods = clusterer->clusterLikelihoods;
103  this->clusterDistances = clusterer->clusterDistances;
104  this->clusterLabels = clusterer->clusterLabels;
105  this->converged = clusterer->converged;
106  this->ranges = clusterer->ranges;
107 
108  return true;
109 }
110 
111 bool Clusterer::train_(MatrixDouble &trainingData){
112  return false;
113 }
114 
116  MatrixDouble data = trainingData.getDataAsMatrixDouble();
117  return train_( data );
118 }
119 
120 bool Clusterer::train_(UnlabelledData &trainingData){
121  MatrixDouble data = trainingData.getDataAsMatrixDouble();
122  return train_( data );
123 }
124 
126 
127  //Reset the base class
128  MLBase::reset();
129 
131  maxLikelihood = 0;
132  bestDistance = 0;
133  std::fill(clusterLikelihoods.begin(),clusterLikelihoods.end(),0);
134  std::fill(clusterDistances.begin(),clusterDistances.end(),0);
135 
136  return true;
137 }
138 
140 
141  //Clear the MLBase variables
142  MLBase::clear();
143 
145  maxLikelihood = 0;
146  bestDistance = 0;
147  clusterLikelihoods.clear();
148  clusterDistances.clear();
149  clusterLabels.clear();
150 
151  return true;
152 }
153 
154 bool Clusterer::saveClustererSettingsToFile(fstream &file) const{
155 
156  if( !file.is_open() ){
157  errorLog << "saveClustererSettingsToFile(fstream &file) - The file is not open!" << endl;
158  return false;
159  }
160 
161  if( !MLBase::saveBaseSettingsToFile( file ) ) return false;
162 
163  file << "NumClusters: " << numClusters << endl;
164 
165  if( trained ){
166  file << "Ranges: " << endl;
167 
168  for(UINT i=0; i<ranges.size(); i++){
169  file << ranges[i].minValue << "\t" << ranges[i].maxValue << endl;
170  }
171  }
172 
173  return true;
174 }
175 
177 
178  if( !file.is_open() ){
179  errorLog << "loadClustererSettingsFromFile(fstream &file) - The file is not open!" << endl;
180  return false;
181  }
182 
183  //Try and load the base settings from the file
184  if( !MLBase::loadBaseSettingsFromFile( file ) ){
185  return false;
186  }
187 
188  string word;
189 
190  //Load if the number of clusters
191  file >> word;
192  if( word != "NumClusters:" ){
193  errorLog << "loadClustererSettingsFromFile(fstream &file) - Failed to read NumClusters header!" << endl;
194  clear();
195  return false;
196  }
197  file >> numClusters;
198 
199  //Load if the Ranges (if the model has been trained)
200  if( trained ){
201  file >> word;
202  if( word != "Ranges:" ){
203  errorLog << "loadClustererSettingsFromFile(fstream &file) - Failed to read Ranges header!" << endl;
204  clear();
205  return false;
206  }
207  ranges.resize(numInputDimensions);
208 
209  for(UINT i=0; i<ranges.size(); i++){
210  file >> ranges[i].minValue;
211  file >> ranges[i].maxValue;
212  }
213 
214  clusterLabels.resize(numClusters);
215  for(UINT i=0; i<numClusters; i++){
216  clusterLabels[i] = i+1;
217  }
218 
219  clusterLikelihoods.resize(numClusters,0);
220  clusterDistances.resize(numClusters,0);
221 
222  }
223 
224  return true;
225 }
226 
228  if( !trained ) return false;
229  return converged;
230 }
231 
232 UINT Clusterer::getNumClusters() const { return numClusters; }
233 
235 
236 
238  return maxLikelihood;
239 }
240 
242  return bestDistance;
243 }
244 
245 VectorDouble Clusterer::getClusterLikelihoods() const{
246  return clusterLikelihoods;
247 }
248 
249 VectorDouble Clusterer::getClusterDistances() const{
250  return clusterDistances;
251 }
252 
253 vector< UINT > Clusterer::getClusterLabels() const{
254  return clusterLabels;
255 }
256 
257 string Clusterer::getClustererType() const{ return clustererType; }
258 
260  return *this;
261 }
262 
263 bool Clusterer::setNumClusters(const UINT numClusters){
264  if( numClusters == 0 ) return false;
265  clear();
266  this->numClusters = numClusters;
267  return true;
268 }
269 
270 } //End of namespace GRT
271 
double getBestDistance() const
Definition: Clusterer.cpp:241
virtual bool reset()
Definition: MLBase.cpp:116
This is the main base class that all GRT Clustering algorithms should inherit from.
virtual bool clear()
Definition: Clusterer.cpp:139
bool loadClustererSettingsFromFile(fstream &file)
Definition: Clusterer.cpp:176
virtual bool clear()
Definition: MLBase.cpp:118
double getMaximumLikelihood() const
Definition: Clusterer.cpp:237
bool loadBaseSettingsFromFile(fstream &file)
Definition: MLBase.cpp:357
virtual ~Clusterer(void)
Definition: Clusterer.cpp:77
Definition: AdaBoost.cpp:25
bool saveBaseSettingsToFile(fstream &file) const
Definition: MLBase.cpp:334
Clusterer * createNewInstance() const
Definition: Clusterer.cpp:35
virtual bool reset()
Definition: Clusterer.cpp:125
UINT numClusters
Number of clusters in the model.
Definition: Clusterer.h:249
bool copyMLBaseVariables(const MLBase *mlBase)
Definition: MLBase.cpp:46
bool saveClustererSettingsToFile(fstream &file) const
Definition: Clusterer.cpp:154
vector< UINT > getClusterLabels() const
Definition: Clusterer.cpp:253
bool setNumClusters(const UINT numClusters)
Definition: Clusterer.cpp:263
static Clusterer * createInstanceFromString(string const &ClustererType)
Definition: Clusterer.cpp:27
bool copyBaseVariables(const Clusterer *Clusterer)
Definition: Clusterer.cpp:84
bool getConverged() const
Definition: Clusterer.cpp:227
Clusterer * deepCopy() const
Definition: Clusterer.cpp:39
std::map< string, Clusterer *(*)() > StringClustererMap
Definition: Clusterer.h:190
VectorDouble getClusterLikelihoods() const
Definition: Clusterer.cpp:245
string getClustererType() const
Definition: Clusterer.cpp:257
MatrixDouble getDataAsMatrixDouble() const
virtual bool deepCopyFrom(const Clusterer *Clusterer)
Definition: Clusterer.h:58
virtual bool train_(MatrixDouble &trainingData)
Definition: Clusterer.cpp:111
MatrixDouble getDataAsMatrixDouble() const
static vector< string > getRegisteredClusterers()
Definition: Clusterer.cpp:52
const Clusterer & getBaseClusterer() const
Definition: Clusterer.cpp:259
VectorDouble getClusterDistances() const
Definition: Clusterer.cpp:249
UINT getPredictedClusterLabel() const
Definition: Clusterer.cpp:234
UINT getNumClusters() const
Definition: Clusterer.cpp:232
UINT predictedClusterLabel
Stores the predicted cluster label from the most recent predict( )
Definition: Clusterer.h:250