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.
DecisionTreeNode.cpp
1 
2 #include "DecisionTreeNode.h"
3 
4 using namespace GRT;
5 
6 //Register the DecisionTreeNode with the Node base class
7 RegisterNode< DecisionTreeNode > DecisionTreeNode::registerModule("DecisionTreeNode");
8 
10  nodeType = "DecisionTreeNode";
11  parent = NULL;
12  leftChild = NULL;
13  rightChild = NULL;
14  clear();
15 }
16 
18  clear();
19 }
20 
21 bool DecisionTreeNode::predict(const VectorFloat &x,VectorFloat &classLikelihoods){
22 
23  predictedNodeID = 0;
24 
25  if( isLeafNode ){
26  classLikelihoods = classProbabilities;
27  predictedNodeID = nodeID;
28  return true;
29  }
30 
31  if( leftChild == NULL && rightChild == NULL )
32  return false;
33 
34  if( predict( x ) ){
35  if( rightChild ){
36  if( rightChild->predict( x, classLikelihoods ) ){
37  predictedNodeID = rightChild->getPredictedNodeID();
38  return true;
39  }
40  warningLog << "predict(const VectorFloat &x,VectorFloat &classLikelihoods) - Right child failed prediction!" << std::endl;
41  return false;
42  }
43  }else{
44  if( leftChild ){
45  if( leftChild->predict( x, classLikelihoods ) ){
46  predictedNodeID = leftChild->getPredictedNodeID();
47  return true;
48  }
49  warningLog << "predict(const VectorFloat &x,VectorFloat &classLikelihoods) - Left child failed prediction!" << std::endl;
50  return false;
51  }
52  }
53 
54  return false;
55 }
56 
57 bool DecisionTreeNode::computeBestSpilt( const UINT &trainingMode, const UINT &numSplittingSteps,const ClassificationData &trainingData, const Vector< UINT > &features, const Vector< UINT > &classLabels, UINT &featureIndex, Float &minError ){
58 
59  switch( trainingMode ){
60  case Tree::BEST_ITERATIVE_SPILT:
61  return computeBestSpiltBestIterativeSpilt( numSplittingSteps, trainingData, features, classLabels, featureIndex, minError );
62  break;
63  case Tree::BEST_RANDOM_SPLIT:
64  return computeBestSpiltBestRandomSpilt( numSplittingSteps, trainingData, features, classLabels, featureIndex, minError );
65  break;
66  default:
67  errorLog << "computeBestSpilt(...) - Uknown trainingMode!" << std::endl;
68  return false;
69  break;
70  }
71 
72  return false;
73 }
74 
76 
77  //Call the base class clear function
78  Node::clear();
79 
80  nodeSize = 0;
81  classProbabilities.clear();
82 
83  return true;
84 }
85 
86 bool DecisionTreeNode::getModel( std::ostream &stream ) const{
87 
88  std::string tab = "";
89  for(UINT i=0; i<depth; i++) tab += "\t";
90 
91  stream << tab << "depth: " << depth << " nodeSize: " << nodeSize << " isLeafNode: " << isLeafNode << std::endl;
92  stream << tab << "ClassProbabilities: ";
93  for(UINT i=0; i<classProbabilities.size(); i++){
94  stream << classProbabilities[i] << "\t";
95  }
96  stream << std::endl;
97 
98  if( leftChild != NULL ){
99  stream << tab << "LeftChild: " << std::endl;
100  leftChild->getModel( stream );
101  }
102 
103  if( rightChild != NULL ){
104  stream << tab << "RightChild: " << std::endl;
105  rightChild->getModel( stream );
106  }
107 
108  return true;
109 }
110 
112 
113  DecisionTreeNode *node = dynamic_cast< DecisionTreeNode* >( DecisionTreeNode::createInstanceFromString( nodeType ) );
114 
115  if( node == NULL ){
116  return NULL;
117  }
118 
119  //Copy this node into the node
120  node->depth = depth;
121  node->isLeafNode = isLeafNode;
122  node->nodeID = nodeID;
123  node->predictedNodeID = predictedNodeID;
124  node->nodeSize = nodeSize;
125  node->classProbabilities = classProbabilities;
126 
127  //Recursively deep copy the left child
128  if( leftChild ){
129  node->leftChild = leftChild->deepCopyNode();
130  node->leftChild->setParent( node );
131  }
132 
133  //Recursively deep copy the right child
134  if( rightChild ){
135  node->rightChild = rightChild->deepCopyNode();
136  node->rightChild->setParent( node );
137  }
138 
139  return node;
140 }
141 
143  return dynamic_cast< DecisionTreeNode* >( deepCopyNode() );
144 }
145 
147  return nodeSize;
148 }
149 
151  return (UINT)classProbabilities.size();
152 }
153 
155  return classProbabilities;
156 }
157 
158 bool DecisionTreeNode::setLeafNode( const UINT nodeSize, const VectorFloat &classProbabilities ){
159  this->nodeSize = nodeSize;
160  this->classProbabilities = classProbabilities;
161  this->isLeafNode = true;
162  return true;
163 }
164 
165 bool DecisionTreeNode::setNodeSize(const UINT nodeSize){
166  this->nodeSize = nodeSize;
167  return true;
168 }
169 
170 bool DecisionTreeNode::setClassProbabilities(const VectorFloat &classProbabilities){
171  this->classProbabilities = classProbabilities;
172  return true;
173 }
174 
175 UINT DecisionTreeNode::getClassLabelIndexValue(UINT classLabel,const Vector< UINT > &classLabels){
176  const UINT N = classLabels.getSize();
177  for(UINT i=0; i<N; i++){
178  if( classLabel == classLabels[i] )
179  return i;
180  }
181  return 0;
182 }
virtual bool computeBestSpilt(const UINT &trainingMode, const UINT &numSplittingSteps, const ClassificationData &trainingData, const Vector< UINT > &features, const Vector< UINT > &classLabels, UINT &featureIndex, Float &minError)
virtual bool clear()
Definition: Node.h:37
Definition: DebugLog.cpp:23
bool setClassProbabilities(const VectorFloat &classProbabilities)
UINT getNumClasses() const
unsigned int getSize() const
Definition: Vector.h:193
virtual Node * deepCopyNode() const
Definition: Node.cpp:275
This file implements a DecisionTreeNode, which is a specific base node used for a DecisionTree...
virtual bool clear()
Definition: Node.cpp:69
virtual bool predict(const VectorFloat &x, VectorFloat &classLikelihoods)
bool setLeafNode(const UINT nodeSize, const VectorFloat &classProbabilities)
DecisionTreeNode * deepCopy() const
static Node * createInstanceFromString(std::string const &nodeType)
Definition: Node.cpp:28
UINT getNodeSize() const
virtual Node * deepCopyNode() const
virtual ~DecisionTreeNode()
virtual bool getModel(std::ostream &stream) const
VectorFloat getClassProbabilities() const
bool setNodeSize(const UINT nodeSize)