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.
Node.cpp
1 
21 #define GRT_DLL_EXPORTS
22 #include "Node.h"
23 
24 GRT_BEGIN_NAMESPACE
25 
26 Node::StringNodeMap* Node::stringNodeMap = NULL;
27 UINT Node::numNodeInstances = 0;
28 
29 Node* Node::createInstanceFromString( std::string const &nodeType ){
30 
31  StringNodeMap::iterator iter = getMap()->find( nodeType );
32  if( iter == getMap()->end() ){
33  return NULL;
34  }
35 
36  return iter->second();
37 }
38 
40  return createInstanceFromString( nodeType );
41 }
42 
43 Node::Node( const std::string id ) : MLBase( id )
44 {
45  nodeType = id;
46  parent = NULL;
47  leftChild = NULL;
48  rightChild = NULL;
49  clear();
50 }
51 
53  clear();
54 }
55 
57  warningLog << __GRT_LOG__ << " Base class not overwritten!" << std::endl;
58  return false;
59 }
60 
62  warningLog << __GRT_LOG__ << " Base class not overwritten!" << std::endl;
63  return false;
64 }
65 
66 bool Node::clear(){
67 
68  //Set the parent pointer to null, this is safe as the parent pointer does not own the memory
69  parent = NULL;
70 
71  if( leftChild != NULL ){
72  //Recursively clean up the left child
73  leftChild->clear();
74 
75  //Clean up the left child
76  delete leftChild;
77  leftChild = NULL;
78  }
79 
80  if( rightChild != NULL ){
81  //Recursively clean up the right child
82  rightChild->clear();
83 
84  //Clean up the right child
85  delete rightChild;
86  rightChild = NULL;
87  }
88 
89  depth = 0;
90  nodeID = 0;
91  predictedNodeID = 0;
92  isLeafNode = false;
93 
94  return true;
95 }
96 
98  return false; //Base class always return false
99 }
100 
102  return false; //Base class always return false
103 }
104 
105 bool Node::print() const{
106 
107  std::ostringstream stream;
108  if( getModel( stream ) ){
109  std::cout << stream.str();
110  return true;
111  }
112 
113  return false;
114 }
115 
116 bool Node::getModel( std::ostream &stream ) const{
117 
118  std::string tab = "";
119  for(UINT i=0; i<depth; i++) tab += "\t";
120 
121  stream << tab << "depth: " << depth << " isLeafNode: " << isLeafNode << " nodeID: " << nodeID << std::endl;
122 
123  if( leftChild != NULL ){
124  stream << tab << "LeftChild: " << std::endl;
125  leftChild->getModel( stream );
126  }
127 
128  if( rightChild != NULL ){
129  stream << tab << "RightChild: " << std::endl;
130  rightChild->getModel( stream );
131  }
132 
133  return true;
134 }
135 
136 bool Node::save( std::fstream &file ) const{
137 
138  if(!file.is_open())
139  {
140  errorLog << "save(fstream &file) - File is not open!" << std::endl;
141  return false;
142  }
143 
144  file << "NodeType: " << nodeType << std::endl;
145  file << "Depth: " << depth << std::endl;
146  file << "NodeID: " << nodeID << std::endl;
147  file << "IsLeafNode: " << isLeafNode << std::endl;
148  file << "HasLeftChild: " << getHasLeftChild() << std::endl;
149  file << "HasRightChild: " << getHasRightChild() << std::endl;
150 
151  //If there is a left child then load the left child's data
152  if( getHasLeftChild() ){
153  file << "LeftChild\n";
154  if( !leftChild->save( file ) ){
155  errorLog << "save(fstream &file) - Failed to save left child at depth: " << depth << std::endl;
156  return false;
157  }
158  }
159 
160  //If there is a right child then load the right child's data
161  if( getHasRightChild() ){
162  file << "RightChild\n";
163  if( !rightChild->save( file ) ){
164  errorLog << "save(fstream &file) - Failed to save right child at depth: " << depth << std::endl;
165  return false;
166  }
167  }
168 
169  //Save the custom parameters to the file
170  if( !saveParametersToFile( file ) ){
171  errorLog << "save(fstream &file) - Failed to save parameters to file at depth: " << depth << std::endl;
172  return false;
173  }
174 
175  return true;
176 }
177 
178 bool Node::load( std::fstream &file ){
179 
180  //Clear any previous nodes
181  clear();
182 
183  if(!file.is_open())
184  {
185  errorLog << "load(fstream &file) - File is not open!" << std::endl;
186  return false;
187  }
188 
189  std::string word;
190  bool hasLeftChild = false;
191  bool hasRightChild = false;
192 
193  file >> word;
194  if( word != "NodeType:" ){
195  errorLog << "load(fstream &file) - Failed to find Node header!" << std::endl;
196  return false;
197  }
198  file >> nodeType;
199 
200  file >> word;
201  if( word != "Depth:" ){
202  errorLog << "load(fstream &file) - Failed to find Depth header!" << std::endl;
203  return false;
204  }
205  file >> depth;
206 
207  file >> word;
208  if( word != "NodeID:" ){
209  errorLog << "load(fstream &file) - Failed to find NodeID header!" << std::endl;
210  return false;
211  }
212  file >> nodeID;
213 
214  file >> word;
215  if( word != "IsLeafNode:" ){
216  errorLog << "load(fstream &file) - Failed to find IsLeafNode header!" << std::endl;
217  return false;
218  }
219  file >> isLeafNode;
220 
221  file >> word;
222  if( word != "HasLeftChild:" ){
223  errorLog << "load(fstream &file) - Failed to find HasLeftChild header!" << std::endl;
224  return false;
225  }
226  file >> hasLeftChild;
227 
228  file >> word;
229  if( word != "HasRightChild:" ){
230  errorLog << "load(fstream &file) - Failed to find HasRightChild header!" << std::endl;
231  return false;
232  }
233  file >> hasRightChild;
234 
235  if( hasLeftChild ){
236  file >> word;
237  if( word != "LeftChild" ){
238  errorLog << "load(fstream &file) - Failed to find LeftChild header!" << std::endl;
239  return false;
240  }
241  leftChild = createNewInstance();
242  leftChild->setParent( this );
243  if( !leftChild->load(file) ){
244  errorLog << "load(fstream &file) - Failed to load left child at depth: " << depth << std::endl;
245  return false;
246  }
247  }
248 
249  if( hasRightChild ){
250  file >> word;
251  if( word != "RightChild" ){
252  errorLog << "load(fstream &file) - Failed to find RightChild header!" << std::endl;
253  return false;
254  }
255  rightChild = createNewInstance();
256  rightChild->setParent( this );
257  if( !rightChild->load( file ) ){
258  errorLog << "load(fstream &file) - Failed to load right child at depth: " << depth << std::endl;
259  return false;
260  }
261  }
262 
263  //Load the custom parameters from a file
264  if( !loadParametersFromFile( file ) ){
265  errorLog << "loadParametersFromFile(fstream &file) - Failed to load parameters from file at depth: " << depth << std::endl;
266  return false;
267  }
268 
269  return true;
270 }
271 
273 
274  Node *node = createNewInstance();
275 
276  if( node == NULL ){
277  return NULL;
278  }
279 
280  //Copy this node into the node
281  node->setNodeID( nodeID );
282  node->setDepth( depth );
283  node->setIsLeafNode( isLeafNode );
284 
285  //Recursively deep copy the left child
286  if( this->leftChild ){
287  node->setLeftChild( this->leftChild->deepCopy() );
288  node->leftChild->setParent( node );
289  }
290 
291  //Recursively deep copy the right child
292  if( this->rightChild ){
293  node->setRightChild( rightChild->deepCopy() );
294  node->rightChild->setParent( node );
295  }
296 
297  return node;
298 }
299 
300 std::string Node::getNodeType() const{
301  return nodeType;
302 }
303 
304 UINT Node::getDepth() const{
305  return depth;
306 }
307 
308 UINT Node::getNodeID() const{
309  return nodeID;
310 }
311 
313  return predictedNodeID;
314 }
315 
316 UINT Node::getMaxDepth() const {
317 
318  UINT maxDepth = depth;
319 
320  //Search for the maximum depth in the left child
321  if( getHasLeftChild() ){
322  UINT maxLeftDepth = leftChild->getMaxDepth();
323  if( maxLeftDepth > maxDepth ){
324  maxDepth = maxLeftDepth;
325  }
326  }
327 
328  //Search for the maximum depth in the right child
329  if( getHasRightChild() ){
330  UINT maxRightDepth = rightChild->getMaxDepth();
331  if( maxRightDepth > maxDepth ){
332  maxDepth = maxRightDepth;
333  }
334  }
335 
336  return maxDepth;
337 }
338 
339 bool Node::getIsLeafNode() const{
340  return isLeafNode;
341 }
342 
343 bool Node::getHasParent() const{
344  return (parent != NULL);
345 }
346 
347 bool Node::getHasLeftChild() const {
348  return (leftChild != NULL);
349 }
350 
352  return (rightChild != NULL);
353 }
354 
355 bool Node::initNode(Node *parent,const UINT depth,const UINT nodeID,const bool isLeafNode){
356  this->parent = parent;
357  this->depth = depth;
358  this->nodeID = nodeID;
359  this->isLeafNode = isLeafNode;
360  return true;
361 }
362 
363 bool Node::setParent(Node *parent){
364  this->parent = parent;
365  return true;
366 }
367 
368 bool Node::setLeftChild(Node *leftChild){
369  this->leftChild = leftChild;
370  return true;
371 }
372 
373 bool Node::setRightChild(Node *rightChild){
374  this->rightChild = rightChild;
375  return true;
376 }
377 
378 bool Node::setDepth(const UINT depth){
379  this->depth = depth;
380  return true;
381 }
382 
383 bool Node::setNodeID(const UINT nodeID){
384  this->nodeID = nodeID;
385  return true;
386 }
387 
388 bool Node::setIsLeafNode(const bool isLeafNode){
389  this->isLeafNode = isLeafNode;
390  return true;
391 }
392 
393 GRT_END_NAMESPACE
virtual ~Node()
Definition: Node.cpp:52
virtual bool getModel(std::ostream &stream) const override
Definition: Node.cpp:116
Definition: Node.h:37
virtual bool predict_(VectorFloat &x) override
Definition: Node.cpp:56
std::string getNodeType() const
Definition: Node.cpp:300
UINT getDepth() const
Definition: Node.cpp:304
Node(const std::string id="Node")
Definition: Node.cpp:43
bool getIsLeafNode() const
Definition: Node.cpp:339
virtual bool loadParametersFromFile(std::fstream &file)
Definition: Node.h:255
virtual bool saveParametersToFile(std::fstream &file) const
Definition: Node.h:246
This class contains the main Node base class.
bool getHasParent() const
Definition: Node.cpp:343
virtual bool save(std::fstream &file) const override
Definition: Node.cpp:136
virtual bool computeFeatureWeights(VectorFloat &weights) const
Definition: Node.cpp:97
Node * createNewInstance() const
Definition: Node.cpp:39
UINT getPredictedNodeID() const
Definition: Node.cpp:312
virtual bool print() const override
Definition: Node.cpp:105
static Node * createInstanceFromString(std::string const &nodeType)
Definition: Node.cpp:29
virtual bool load(std::fstream &file) override
Definition: Node.cpp:178
virtual bool computeLeafNodeWeights(MatrixFloat &weights) const
Definition: Node.cpp:101
UINT getNodeID() const
Definition: Node.cpp:308
virtual bool clear() override
Definition: Node.cpp:66
This is the main base class that all GRT machine learning algorithms should inherit from...
Definition: MLBase.h:72
std::map< std::string, Node *(*)() > StringNodeMap
Definition: Node.h:215
bool getHasRightChild() const
Definition: Node.cpp:351
bool getHasLeftChild() const
Definition: Node.cpp:347
virtual Node * deepCopy() const
Definition: Node.cpp:272