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