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