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.
Tree.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 "Tree.h"
23 
24 GRT_BEGIN_NAMESPACE
25 
26 Tree::Tree(const UINT numSplittingSteps,const UINT minNumSamplesPerNode,const UINT maxDepth,const bool removeFeaturesAtEachSpilt,const UINT trainingMode)
27 {
28  tree = NULL;
29  this->numSplittingSteps = numSplittingSteps;
30  this->minNumSamplesPerNode = minNumSamplesPerNode;
31  this->maxDepth = maxDepth;
32  this->removeFeaturesAtEachSpilt = removeFeaturesAtEachSpilt;
33  this->trainingMode = trainingMode;
34  classType = "Tree";
35  debugLog.setProceedingText("[DEBUG Tree]");
36  errorLog.setProceedingText("[ERROR Tree]");
37  trainingLog.setProceedingText("[TRAINING Tree]");
38  warningLog.setProceedingText("[WARNING Tree]");
39 }
40 
42 {
43  clear();
44 }
45 
46 bool Tree::clear(){
47 
48  if( tree != NULL ){
49  tree->clear();
50  delete tree;
51  tree = NULL;
52  }
53 
54  return true;
55 }
56 
57 bool Tree::print() const{
58  std::ostringstream stream;
59  if( tree != NULL ){
60  if( tree->getModel( stream ) ){
61  std::cout << stream.str();
62  return true;
63  }
64  }
65  return false;
66 }
67 
68 bool Tree::getModel( std::ostream &stream ) const{
69 
70  if( tree != NULL ){
71  if( tree->getModel( stream ) ){
72  return true;
73  }
74  }
75 
76  return false;
77 }
78 
80 
81  if( tree == NULL ){
82  return NULL;
83  }
84 
85  return tree->deepCopyNode();
86 }
87 
88 const Node* Tree::getTree() const{
89  return tree;
90 }
91 
92 UINT Tree::getTrainingMode() const{
93  return trainingMode;
94 }
95 
97  return numSplittingSteps;
98 }
99 
101  return minNumSamplesPerNode;
102 }
103 
104 UINT Tree::getMaxDepth()const{
105  return maxDepth;
106 }
107 
109 
110  if( tree == NULL ){
111  return 0;
112  }
113 
114  return tree->getPredictedNodeID();
115 }
116 
118  return removeFeaturesAtEachSpilt;
119 }
120 
121 bool Tree::setTrainingMode(const UINT trainingMode){
122  if( trainingMode >= BEST_ITERATIVE_SPILT && trainingMode < NUM_TRAINING_MODES ){
123  this->trainingMode = trainingMode;
124  return true;
125  }
126  warningLog << "Unknown trainingMode: " << trainingMode << std::endl;
127  return false;
128 }
129 
130 bool Tree::setNumSplittingSteps(const UINT numSplittingSteps){
131  if( numSplittingSteps > 0 ){
132  this->numSplittingSteps = numSplittingSteps;
133  return true;
134  }
135  warningLog << "setNumSplittingSteps(const UINT numSplittingSteps) - The number of splitting steps must be greater than zero!" << std::endl;
136  return false;
137 }
138 
139 bool Tree::setMinNumSamplesPerNode(const UINT minNumSamplesPerNode){
140  if( minNumSamplesPerNode > 0 ){
141  this->minNumSamplesPerNode = minNumSamplesPerNode;
142  return true;
143  }
144  warningLog << "setMinNumSamplesPerNode(const UINT minNumSamplesPerNode) - The minimum number of samples per node must be greater than zero!" << std::endl;
145  return false;
146 }
147 
148 bool Tree::setMaxDepth(const UINT maxDepth){
149  if( maxDepth > 0 ){
150  this->maxDepth = maxDepth;
151  return true;
152  }
153  warningLog << "setMaxDepth(const UINT maxDepth) - The maximum depth must be greater than zero!" << std::endl;
154  return false;
155 }
156 
157 bool Tree::setRemoveFeaturesAtEachSpilt(const bool removeFeaturesAtEachSpilt){
158  this->removeFeaturesAtEachSpilt = removeFeaturesAtEachSpilt;
159  return true;
160 }
161 
162 GRT_END_NAMESPACE
bool setRemoveFeaturesAtEachSpilt(const bool removeFeaturesAtEachSpilt)
Definition: Tree.cpp:157
virtual bool getModel(std::ostream &stream) const
Definition: Tree.cpp:68
Definition: Node.h:37
bool setMinNumSamplesPerNode(const UINT minNumSamplesPerNode)
Definition: Tree.cpp:139
virtual bool print() const
Definition: Tree.cpp:57
UINT getNumSplittingSteps() const
Definition: Tree.cpp:96
virtual bool getModel(std::ostream &stream) const
Definition: Node.cpp:120
bool setNumSplittingSteps(const UINT numSplittingSteps)
Definition: Tree.cpp:130
bool getRemoveFeaturesAtEachSpilt() const
Definition: Tree.cpp:117
UINT getTrainingMode() const
Definition: Tree.cpp:92
virtual ~Tree(void)
Definition: Tree.cpp:41
virtual Node * deepCopyNode() const
Definition: Node.cpp:276
virtual bool clear()
Definition: Tree.cpp:46
const Node * getTree() const
Definition: Tree.cpp:88
bool setMaxDepth(const UINT maxDepth)
Definition: Tree.cpp:148
virtual bool clear()
Definition: Node.cpp:70
UINT getPredictedNodeID() const
Definition: Node.cpp:316
Tree(const UINT numSplittingSteps=100, const UINT minNumSamplesPerNode=5, const UINT maxDepth=10, const bool removeFeaturesAtEachSpilt=false, const UINT trainingMode=BEST_ITERATIVE_SPILT)
Definition: Tree.cpp:26
UINT getMaxDepth() const
Definition: Tree.cpp:104
This class implements the base class Tree used for the DecisionTree, RegressionTree and ClusterTree...
virtual Node * deepCopyTree() const
Definition: Tree.cpp:79
UINT getMinNumSamplesPerNode() const
Definition: Tree.cpp:100
bool setTrainingMode(const UINT trainingMode)
Definition: Tree.cpp:121
UINT getPredictedNodeID() const
Definition: Tree.cpp:108