GestureRecognitionToolkit  Version: 0.1.0
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 
23 GRT_BEGIN_NAMESPACE
24 
25 Clusterer::StringClustererMap* Clusterer::stringClustererMap = NULL;
26 UINT Clusterer::numClustererInstances = 0;
27 
28 Clusterer* Clusterer::createInstanceFromString( std::string const &clustererType ){
29 
30  StringClustererMap::iterator iter = getMap()->find( clustererType );
31  if( iter == getMap()->end() ){
32  return NULL;
33  }
34  return iter->second();
35 }
37  return createInstanceFromString( clustererType );
38 }
39 
41 
42  Clusterer *newInstance = createInstanceFromString( clustererType );
43 
44  if( newInstance == NULL ) return NULL;
45 
46  if( !newInstance->deepCopyFrom( this ) ){
47  delete newInstance;
48  newInstance = NULL;
49  return NULL;
50  }
51  return newInstance;
52 }
53 
55  Vector< std::string > registeredClusterers;
56 
57  StringClustererMap::iterator iter = getMap()->begin();
58  while( iter != getMap()->end() ){
59  registeredClusterers.push_back( iter->first );
60  iter++;
61  }
62  return registeredClusterers;
63 }
64 
66  baseType = MLBase::CLUSTERER;
67  clustererType = "NOT_SET";
68  numClusters = 10;
70  maxLikelihood = 0;
71  bestDistance = 0;
72  minNumEpochs = 1;
73  maxNumEpochs = 1000;
74  minChange = 1.0e-5;
75  converged = false;
76  numClustererInstances++;
77 }
78 
80  if( --numClustererInstances == 0 ){
81  delete stringClustererMap;
82  stringClustererMap = NULL;
83  }
84 }
85 
86 bool Clusterer::copyBaseVariables(const Clusterer *clusterer){
87 
88  if( clusterer == NULL ){
89  errorLog << "copyBaseVariables(const Clusterer *clusterer) - clusterer is NULL!" << std::endl;
90  return false;
91  }
92 
93  if( !this->copyMLBaseVariables( clusterer ) ){
94  return false;
95  }
96 
97  //Copy the clusterer base variables
98  this->clustererType = clusterer->clustererType;
99  this->numClusters = clusterer->numClusters;
100  this->predictedClusterLabel = clusterer->predictedClusterLabel;
101  this->predictedClusterLabel = clusterer->predictedClusterLabel;
102  this->maxLikelihood = clusterer->maxLikelihood;
103  this->bestDistance = clusterer->bestDistance;
104  this->clusterLikelihoods = clusterer->clusterLikelihoods;
105  this->clusterDistances = clusterer->clusterDistances;
106  this->clusterLabels = clusterer->clusterLabels;
107  this->converged = clusterer->converged;
108  this->ranges = clusterer->ranges;
109 
110  return true;
111 }
112 
113 bool Clusterer::train_(MatrixFloat &trainingData){
114  return false;
115 }
116 
118  MatrixFloat data = trainingData.getDataAsMatrixFloat();
119  return train_( data );
120 }
121 
122 bool Clusterer::train_(UnlabelledData &trainingData){
123  MatrixFloat data = trainingData.getDataAsMatrixFloat();
124  return train_( data );
125 }
126 
128 
129  //Reset the base class
130  MLBase::reset();
131 
133  maxLikelihood = 0;
134  bestDistance = 0;
135  std::fill(clusterLikelihoods.begin(),clusterLikelihoods.end(),0);
136  std::fill(clusterDistances.begin(),clusterDistances.end(),0);
137 
138  return true;
139 }
140 
142 
143  //Clear the MLBase variables
144  MLBase::clear();
145 
147  maxLikelihood = 0;
148  bestDistance = 0;
149  clusterLikelihoods.clear();
150  clusterDistances.clear();
151  clusterLabels.clear();
152 
153  return true;
154 }
155 
156 bool Clusterer::saveClustererSettingsToFile( std::fstream &file ) const{
157 
158  if( !file.is_open() ){
159  errorLog << "saveClustererSettingsToFile(fstream &file) - The file is not open!" << std::endl;
160  return false;
161  }
162 
163  if( !MLBase::saveBaseSettingsToFile( file ) ) return false;
164 
165  file << "NumClusters: " << numClusters << std::endl;
166 
167  if( trained ){
168  file << "Ranges: " << std::endl;
169 
170  for(UINT i=0; i<ranges.size(); i++){
171  file << ranges[i].minValue << "\t" << ranges[i].maxValue << std::endl;
172  }
173  }
174 
175  return true;
176 }
177 
178 bool Clusterer::loadClustererSettingsFromFile( std::fstream &file ){
179 
180  if( !file.is_open() ){
181  errorLog << "loadClustererSettingsFromFile(fstream &file) - The file is not open!" << std::endl;
182  return false;
183  }
184 
185  //Try and load the base settings from the file
186  if( !MLBase::loadBaseSettingsFromFile( file ) ){
187  return false;
188  }
189 
190  std::string word;
191 
192  //Load if the number of clusters
193  file >> word;
194  if( word != "NumClusters:" ){
195  errorLog << "loadClustererSettingsFromFile(fstream &file) - Failed to read NumClusters header!" << std::endl;
196  clear();
197  return false;
198  }
199  file >> numClusters;
200 
201  //Load if the Ranges (if the model has been trained)
202  if( trained ){
203  file >> word;
204  if( word != "Ranges:" ){
205  errorLog << "loadClustererSettingsFromFile(fstream &file) - Failed to read Ranges header!" << std::endl;
206  clear();
207  return false;
208  }
209  ranges.resize(numInputDimensions);
210 
211  for(UINT i=0; i<ranges.size(); i++){
212  file >> ranges[i].minValue;
213  file >> ranges[i].maxValue;
214  }
215 
216  clusterLabels.resize(numClusters);
217  for(UINT i=0; i<numClusters; i++){
218  clusterLabels[i] = i+1;
219  }
220 
221  clusterLikelihoods.resize(numClusters,0);
222  clusterDistances.resize(numClusters,0);
223 
224  }
225 
226  return true;
227 }
228 
230  if( !trained ) return false;
231  return converged;
232 }
233 
234 UINT Clusterer::getNumClusters() const { return numClusters; }
235 
237 
238 
240  return maxLikelihood;
241 }
242 
244  return bestDistance;
245 }
246 
248  return clusterLikelihoods;
249 }
250 
252  return clusterDistances;
253 }
254 
256  return clusterLabels;
257 }
258 
259 std::string Clusterer::getClustererType() const{ return clustererType; }
260 
262  return *this;
263 }
264 
265 bool Clusterer::setNumClusters(const UINT numClusters){
266  if( numClusters == 0 ) return false;
267  clear();
268  this->numClusters = numClusters;
269  return true;
270 }
271 
272 GRT_END_NAMESPACE
273 
bool saveBaseSettingsToFile(std::fstream &file) const
Definition: MLBase.cpp:370
virtual ~Clusterer(void)
Definition: Clusterer.cpp:79
This is the main base class that all GRT Clustering algorithms should inherit from.
virtual bool reset()
Definition: MLBase.cpp:124
VectorFloat getClusterLikelihoods() const
Definition: Clusterer.cpp:247
std::string getClustererType() const
Definition: Clusterer.cpp:259
MatrixFloat getDataAsMatrixFloat() const
virtual bool resize(const unsigned int size)
Definition: Vector.h:133
virtual bool deepCopyFrom(const Clusterer *clusterer)
Definition: Clusterer.h:58
Clusterer(void)
Definition: Clusterer.cpp:65
bool copyBaseVariables(const Clusterer *clusterer)
Definition: Clusterer.cpp:86
bool loadClustererSettingsFromFile(std::fstream &file)
Definition: Clusterer.cpp:178
std::map< std::string, Clusterer *(*)() > StringClustererMap
Definition: Clusterer.h:190
UINT predictedClusterLabel
Stores the predicted cluster label from the most recent predict( )
Definition: Clusterer.h:250
Vector< UINT > getClusterLabels() const
Definition: Clusterer.cpp:255
const Clusterer & getBaseClusterer() const
Definition: Clusterer.cpp:261
Clusterer * deepCopy() const
Definition: Clusterer.cpp:40
bool saveClustererSettingsToFile(std::fstream &file) const
Definition: Clusterer.cpp:156
bool copyMLBaseVariables(const MLBase *mlBase)
Definition: MLBase.cpp:49
virtual bool reset()
Definition: Clusterer.cpp:127
UINT numClusters
Number of clusters in the model.
Definition: Clusterer.h:249
UINT getPredictedClusterLabel() const
Definition: Clusterer.cpp:236
Float getBestDistance() const
Definition: Clusterer.cpp:243
bool loadBaseSettingsFromFile(std::fstream &file)
Definition: MLBase.cpp:393
bool getConverged() const
Definition: Clusterer.cpp:229
virtual bool clear()
Definition: MLBase.cpp:126
UINT getNumClusters() const
Definition: Clusterer.cpp:234
virtual bool train_(MatrixFloat &trainingData)
Definition: Clusterer.cpp:113
MatrixFloat getDataAsMatrixFloat() const
static Vector< std::string > getRegisteredClusterers()
Definition: Clusterer.cpp:54
Definition: Vector.h:41
static Clusterer * createInstanceFromString(std::string const &ClustererType)
Definition: Clusterer.cpp:28
VectorFloat getClusterDistances() const
Definition: Clusterer.cpp:251
virtual bool clear()
Definition: Clusterer.cpp:141
Float getMaximumLikelihood() const
Definition: Clusterer.cpp:239
bool setNumClusters(const UINT numClusters)
Definition: Clusterer.cpp:265
Clusterer * createNewInstance() const
Definition: Clusterer.cpp:36