Go to the documentation of this file.00001
00002 #ifndef clsfy_binary_tree_builder_h_
00003 #define clsfy_binary_tree_builder_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007
00008
00009
00010
00011
00012 #include <clsfy/clsfy_builder_base.h>
00013 #include <clsfy/clsfy_binary_tree.h>
00014 #include <vcl_vector.h>
00015 #include <vcl_set.h>
00016 #include <vcl_string.h>
00017 #include <vcl_iosfwd.h>
00018 #include <mbl/mbl_data_wrapper.h>
00019 #include <vnl/vnl_vector.h>
00020 #include <vnl/vnl_random.h>
00021
00022
00023 class clsfy_binary_tree_bnode :public clsfy_binary_tree_node
00024 {
00025
00026
00027 vcl_set<unsigned> subIndicesL;
00028 vcl_set<unsigned> subIndicesR;
00029
00030
00031 clsfy_binary_tree_bnode(clsfy_binary_tree_node* parent,
00032 const clsfy_binary_tree_op& op):
00033 clsfy_binary_tree_node(parent,op) {}
00034
00035 virtual clsfy_binary_tree_node* create_child(const clsfy_binary_tree_op& op);
00036
00037
00038
00039 virtual ~clsfy_binary_tree_bnode();
00040
00041 friend class clsfy_binary_tree_builder;
00042 };
00043
00044
00045
00046
00047
00048
00049
00050
00051 class clsfy_binary_tree_builder : public clsfy_builder_base
00052 {
00053
00054
00055
00056 int max_depth_;
00057
00058
00059
00060
00061
00062 int min_node_size_;
00063
00064
00065
00066
00067 int nbranch_params_;
00068
00069
00070 mutable vcl_vector<unsigned > base_indices_;
00071
00072 public:
00073
00074 clsfy_binary_tree_builder();
00075
00076
00077
00078 virtual clsfy_classifier_base* new_classifier() const;
00079
00080
00081
00082 virtual double build(clsfy_classifier_base& classifier,
00083 mbl_data_wrapper<vnl_vector<double> >& inputs,
00084 unsigned nClasses,
00085 const vcl_vector<unsigned> &outputs) const;
00086
00087
00088 virtual vcl_string is_a() const;
00089
00090
00091 virtual bool is_class(vcl_string const& s) const;
00092
00093
00094 short version_no() const;
00095
00096
00097 virtual clsfy_builder_base* clone() const;
00098
00099
00100 virtual void print_summary(vcl_ostream& os) const;
00101
00102
00103 virtual void b_write(vsl_b_ostream& bfs) const;
00104
00105
00106 virtual void b_read(vsl_b_istream& bfs);
00107
00108
00109 int max_depth() const {return max_depth_;}
00110
00111
00112
00113
00114
00115 void set_max_depth(int max_depth) {max_depth_=max_depth;}
00116
00117 int min_node_size() const {return min_node_size_;}
00118
00119
00120
00121
00122
00123 void set_min_node_size(int min_node_size) {min_node_size_=min_node_size;}
00124
00125
00126
00127
00128 void set_nbranch_params(int nbranch_params) {nbranch_params_ = nbranch_params;}
00129
00130
00131
00132
00133 void set_calc_test_error(bool on) {calc_test_error_=on;}
00134
00135
00136 void seed_sampler(unsigned long seed);
00137 protected:
00138
00139
00140
00141 virtual void randomise_parameters(unsigned ndimsUsed,
00142 vcl_vector<unsigned >& param_indices) const;
00143
00144 mutable vnl_random random_sampler_;
00145
00146
00147 private:
00148 void build_children(
00149 const vcl_vector<vnl_vector<double> >& vin,
00150 const vcl_vector<unsigned>& outputs,
00151 clsfy_binary_tree_bnode* parent, bool to_left) const;
00152
00153 void copy_children(clsfy_binary_tree_bnode* pBuilderNode,clsfy_binary_tree_node* pNode) const;
00154
00155 void set_node_prob(clsfy_binary_tree_node* pNode,
00156 clsfy_binary_tree_bnode* pBuilderNode) const ;
00157
00158 void build_a_node(
00159 const vcl_vector<vnl_vector<double> >& vin,
00160 const vcl_vector<unsigned>& outputs,
00161 const vcl_set<unsigned >& subIndices,
00162 clsfy_binary_tree_bnode* pNode) const;
00163
00164 bool isNodePure(const vcl_set<unsigned >& subIndices,
00165 const vcl_vector<unsigned>& outputs) const;
00166
00167 void add_terminator(
00168 const vcl_vector<vnl_vector<double> >& vin,
00169 const vcl_vector<unsigned>& outputs,
00170 clsfy_binary_tree_bnode* parent,
00171 bool to_left, bool pure) const;
00172
00173 bool calc_test_error_;
00174 };
00175
00176
00177 #endif // clsfy_binary_tree_builder_h_