Point Cloud Library (PCL)  1.14.0-dev
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 #pragma once
39 
40 #include <pcl/common/common.h>
41 #include <pcl/ml/dt/decision_tree.h>
42 #include <pcl/ml/feature_handler.h>
43 #include <pcl/ml/stats_estimator.h>
44 
45 #include <vector>
46 
47 namespace pcl {
48 
49 template <class FeatureType,
50  class DataSet,
51  class LabelType,
52  class ExampleIndex,
53  class NodeType>
55  DecisionTreeEvaluator() = default;
56 
57 template <class FeatureType,
58  class DataSet,
59  class LabelType,
60  class ExampleIndex,
61  class NodeType>
63  ~DecisionTreeEvaluator() = default;
64 
65 template <class FeatureType,
66  class DataSet,
67  class LabelType,
68  class ExampleIndex,
69  class NodeType>
70 void
75  stats_estimator,
76  DataSet& data_set,
77  std::vector<ExampleIndex>& examples,
78  std::vector<LabelType>& label_data)
79 {
80  const std::size_t num_of_examples = examples.size();
81  label_data.resize(num_of_examples);
82  for (int example_index = 0; example_index < num_of_examples; ++example_index) {
83  NodeType* node = &(tree.getRoot());
84 
85  while (node->sub_nodes.size() != 0) {
86  float feature_result = 0.0f;
87  unsigned char flag = 0;
88  unsigned char branch_index = 0;
89 
90  feature_handler.evaluateFeature(
91  node->feature, data_set, examples[example_index], feature_result, flag);
92  stats_estimator.computeBranchIndex(
93  feature_result, flag, node->threshold, branch_index);
94 
95  node = &(node->sub_nodes[branch_index]);
96  }
97 
98  label_data[example_index] = stats_estimator.getLabelOfNode(*node);
99  }
100 }
101 
102 template <class FeatureType,
103  class DataSet,
104  class LabelType,
105  class ExampleIndex,
106  class NodeType>
107 void
113  stats_estimator,
114  DataSet& data_set,
115  std::vector<ExampleIndex>& examples,
116  std::vector<LabelType>& label_data)
117 {
118  const std::size_t num_of_examples = examples.size();
119  for (int example_index = 0; example_index < num_of_examples; ++example_index) {
120  NodeType* node = &(tree.getRoot());
121 
122  while (node->sub_nodes.size() != 0) {
123  float feature_result = 0.0f;
124  unsigned char flag = 0;
125  unsigned char branch_index = 0;
126 
127  feature_handler.evaluateFeature(
128  node->feature, data_set, examples[example_index], feature_result, flag);
129  stats_estimator.computeBranchIndex(
130  feature_result, flag, node->threshold, branch_index);
131 
132  node = &(node->sub_nodes[branch_index]);
133  }
134 
135  label_data[example_index] += stats_estimator.getLabelOfNode(*node);
136  }
137 }
138 
139 template <class FeatureType,
140  class DataSet,
141  class LabelType,
142  class ExampleIndex,
143  class NodeType>
144 void
149  stats_estimator,
150  DataSet& data_set,
151  ExampleIndex example,
152  NodeType& leave)
153 {
154  NodeType* node = &(tree.getRoot());
155 
156  while (!node->sub_nodes.empty()) {
157  float feature_result = 0.0f;
158  unsigned char flag = 0;
159  unsigned char branch_index = 0;
160 
161  feature_handler.evaluateFeature(
162  node->feature, data_set, example, feature_result, flag);
163  stats_estimator.computeBranchIndex(
164  feature_result, flag, node->threshold, branch_index);
165 
166  node = &(node->sub_nodes[branch_index]);
167  }
168 
169  leave = *node;
170 }
171 
172 template <class FeatureType,
173  class DataSet,
174  class LabelType,
175  class ExampleIndex,
176  class NodeType>
177 void
182  stats_estimator,
183  DataSet& data_set,
184  std::vector<ExampleIndex>& examples,
185  std::vector<NodeType*>& nodes)
186 {
187  const std::size_t num_of_examples = examples.size();
188  for (int example_index = 0; example_index < num_of_examples; ++example_index) {
189  NodeType* node = &(tree.getRoot());
190 
191  while (node->sub_nodes.size() != 0) {
192  float feature_result = 0.0f;
193  unsigned char flag = 0;
194  unsigned char branch_index = 0;
195 
196  feature_handler.evaluateFeature(
197  node->feature, data_set, examples[example_index], feature_result, flag);
198  stats_estimator.computeBranchIndex(
199  feature_result, node->threshold, flag, branch_index);
200 
201  node = &(node->subNodes[branch_index]);
202  }
203 
204  nodes.push_back(node);
205  }
206 }
207 
208 } // namespace pcl
virtual ~DecisionTreeEvaluator()
Destructor.
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.
DecisionTreeEvaluator()
Constructor.
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...
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.
Class representing a decision tree.
Definition: decision_tree.h:49
NodeType & getRoot()
Returns the root node of the tree.
Definition: decision_tree.h:69
Utility class interface which is used for creating and evaluating features.
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 LabelDataType getLabelOfNode(NodeType &node) const =0
Returns the label of the specified node.
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...
Define standard C methods and C++ classes that are common to all methods.