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