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