Point Cloud Library (PCL)  1.8.1
decision_tree_evaluator.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2011, Willow Garage, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of Willow Garage, Inc. nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  */
37 
38 #ifndef PCL_ML_DT_DECISION_TREE_EVALUATOR_HPP_
39 #define PCL_ML_DT_DECISION_TREE_EVALUATOR_HPP_
40 
41 #include <pcl/common/common.h>
42 
43 #include <pcl/ml/dt/decision_tree.h>
44 #include <pcl/ml/feature_handler.h>
45 #include <pcl/ml/stats_estimator.h>
46 
47 #include <vector>
48 
49 
50 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
51 template <class FeatureType, class DataSet, class LabelType, class ExampleIndex, class NodeType>
53 {
54 }
55 
56 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
57 template <class FeatureType, class DataSet, class LabelType, class ExampleIndex, class NodeType>
59 {
60 }
61 
62 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
63 template <class FeatureType, class DataSet, class LabelType, class ExampleIndex, class NodeType>
64 void
69  DataSet & data_set,
70  std::vector<ExampleIndex> & examples,
71  std::vector<LabelType> & label_data)
72 {
73  const size_t num_of_examples = examples.size ();
74  label_data.resize (num_of_examples);
75  for (int example_index = 0; example_index < num_of_examples; ++example_index)
76  {
77  NodeType * node = &(tree.getRoot ());
78 
79  while (node->sub_nodes.size () != 0)
80  {
81  float feature_result = 0.0f;
82  unsigned char flag = 0;
83  unsigned char branch_index = 0;
84 
85  feature_handler.evaluateFeature (node->feature, data_set, examples[example_index], feature_result, flag);
86  stats_estimator.computeBranchIndex (feature_result, flag, node->threshold, branch_index);
87 
88  node = &(node->sub_nodes[branch_index]);
89  }
90 
91  label_data[example_index] = stats_estimator.getLabelOfNode (*node);
92  }
93 }
94 
95 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
96 template <class FeatureType, class DataSet, class LabelType, class ExampleIndex, class NodeType>
97 void
102  DataSet & data_set,
103  std::vector<ExampleIndex> & examples,
104  std::vector<LabelType> & label_data)
105 {
106  const size_t num_of_examples = examples.size ();
107  for (int example_index = 0; example_index < num_of_examples; ++example_index)
108  {
109  NodeType * node = &(tree.getRoot ());
110 
111  while (node->sub_nodes.size () != 0)
112  {
113  float feature_result = 0.0f;
114  unsigned char flag = 0;
115  unsigned char branch_index = 0;
116 
117  feature_handler.evaluateFeature (node->feature, data_set, examples[example_index], feature_result, flag);
118  stats_estimator.computeBranchIndex (feature_result, flag, node->threshold, branch_index);
119 
120  node = &(node->sub_nodes[branch_index]);
121  }
122 
123  label_data[example_index] += stats_estimator.getLabelOfNode (*node);
124  }
125 }
126 
127 template <class FeatureType, class DataSet, class LabelType, class ExampleIndex, class NodeType>
128 void
132  DataSet & data_set,
133  ExampleIndex example,
134  NodeType & leave)
135 {
136 
137  NodeType * node = &(tree.getRoot ());
138 
139  while (node->sub_nodes.size () != 0)
140  {
141  float feature_result = 0.0f;
142  unsigned char flag = 0;
143  unsigned char branch_index = 0;
144 
145  feature_handler.evaluateFeature (node->feature, data_set, example, feature_result, flag);
146  stats_estimator.computeBranchIndex (feature_result, flag, node->threshold, branch_index);
147 
148  node = &(node->sub_nodes[branch_index]);
149  }
150 
151  leave = *node;
152 
153 }
154 
155 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
156 template <class FeatureType, class DataSet, class LabelType, class ExampleIndex, class NodeType>
157 void
162  DataSet & data_set,
163  std::vector<ExampleIndex> & examples,
164  std::vector<NodeType*> & nodes)
165 {
166  const size_t num_of_examples = examples.size ();
167  for (int example_index = 0; example_index < num_of_examples; ++example_index)
168  {
169  NodeType * node = &(tree.getRoot ());
170 
171  while (node->sub_nodes.size () != 0)
172  {
173  float feature_result = 0.0f;
174  unsigned char flag = 0;
175  unsigned char branch_index = 0;
176 
177  feature_handler.evaluateFeature (node->feature, data_set, examples[example_index], feature_result, flag);
178  stats_estimator.computeBranchIndex (feature_result, node->threshold, flag, branch_index);
179 
180  node = &(node->subNodes[branch_index]);
181  }
182 
183  nodes.push_back(node);
184  }
185 }
186 
187 #endif
Class representing a decision tree.
Definition: decision_tree.h:51
void evaluateAndAdd(pcl::DecisionTree< NodeType > &tree, pcl::FeatureHandler< FeatureType, DataSet, ExampleIndex > &feature_handler, pcl::StatsEstimator< LabelType, NodeType, DataSet, ExampleIndex > &stats_estimator, DataSet &data_set, std::vector< ExampleIndex > &examples, std::vector< LabelType > &label_data)
Evaluates the specified examples using the supplied tree and adds the results to the supplied results...
virtual void evaluateFeature(const FeatureType &feature, DataSet &data_set, std::vector< ExampleIndex > &examples, std::vector< float > &results, std::vector< unsigned char > &flags) const =0
Evaluates a feature on the specified data.
virtual ~DecisionTreeEvaluator()
Destructor.
Define standard C methods and C++ classes that are common to all methods.
NodeType & getRoot()
Returns the root node of the tree.
Definition: decision_tree.h:73
void getNodes(pcl::DecisionTree< NodeType > &tree, pcl::FeatureHandler< FeatureType, DataSet, ExampleIndex > &feature_handler, pcl::StatsEstimator< LabelType, NodeType, DataSet, ExampleIndex > &stats_estimator, DataSet &data_set, std::vector< ExampleIndex > &examples, std::vector< NodeType * > &nodes)
Evaluates the specified examples using the supplied tree.
virtual void computeBranchIndex(const float result, const unsigned char flag, const float threshold, unsigned char &branch_index) const =0
Computes the branch indices obtained by the specified threshold on the supplied feature evaluation re...
virtual LabelDataType getLabelOfNode(NodeType &node) const =0
Returns the label of the specified node.
void evaluate(pcl::DecisionTree< NodeType > &tree, pcl::FeatureHandler< FeatureType, DataSet, ExampleIndex > &feature_handler, pcl::StatsEstimator< LabelType, NodeType, DataSet, ExampleIndex > &stats_estimator, DataSet &data_set, std::vector< ExampleIndex > &examples, std::vector< LabelType > &label_data)
Evaluates the specified examples using the supplied tree.
Utility class interface which is used for creating and evaluating features.