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
00073 public:
00074
00075 clsfy_binary_tree_builder();
00076
00077
00078
00079 virtual clsfy_classifier_base* new_classifier() const;
00080
00081
00082
00083 virtual double build(clsfy_classifier_base& classifier,
00084 mbl_data_wrapper<vnl_vector<double> >& inputs,
00085 unsigned nClasses,
00086 const vcl_vector<unsigned> &outputs) const;
00087
00088
00089 virtual vcl_string is_a() const;
00090
00091
00092 virtual bool is_class(vcl_string const& s) const;
00093
00094
00095 short version_no() const;
00096
00097
00098 virtual clsfy_builder_base* clone() const;
00099
00100
00101 virtual void print_summary(vcl_ostream& os) const;
00102
00103
00104 virtual void b_write(vsl_b_ostream& bfs) const;
00105
00106
00107 virtual void b_read(vsl_b_istream& bfs);
00108
00109
00110 int max_depth() const {return max_depth_;};
00111
00112
00113
00114
00115
00116 void set_max_depth(int max_depth) {max_depth_=max_depth;}
00117
00118 int min_node_size() const {return min_node_size_;}
00119
00120
00121
00122
00123
00124 void set_min_node_size(int min_node_size) {min_node_size_=min_node_size;}
00125
00126
00127
00128
00129 void set_nbranch_params(int nbranch_params) {nbranch_params_ = nbranch_params;}
00130
00131
00132
00133
00134 void set_calc_test_error(bool on) {calc_test_error_=on;}
00135
00136
00137 void seed_sampler(unsigned long seed);
00138 protected:
00139
00140
00141
00142 virtual void randomise_parameters(unsigned ndimsUsed,
00143 vcl_vector<unsigned >& param_indices) const;
00144
00145 mutable vnl_random random_sampler_;
00146
00147
00148 private:
00149 void build_children(
00150 const vcl_vector<vnl_vector<double> >& vin,
00151 const vcl_vector<unsigned>& outputs,
00152 clsfy_binary_tree_bnode* parent,bool left) const;
00153
00154 void copy_children(clsfy_binary_tree_bnode* pBuilderNode,clsfy_binary_tree_node* pNode) const;
00155
00156 void set_node_prob(clsfy_binary_tree_node* pNode,
00157 clsfy_binary_tree_bnode* pBuilderNode) const ;
00158
00159 void build_a_node(
00160 const vcl_vector<vnl_vector<double> >& vin,
00161 const vcl_vector<unsigned>& outputs,
00162 const vcl_set<unsigned >& subIndices,
00163 clsfy_binary_tree_bnode* pNode) const;
00164
00165 bool isNodePure(const vcl_set<unsigned >& subIndices,
00166 const vcl_vector<unsigned>& outputs) const;
00167
00168 void add_terminator(
00169 const vcl_vector<vnl_vector<double> >& vin,
00170 const vcl_vector<unsigned>& outputs,
00171 clsfy_binary_tree_bnode* parent,
00172 bool left, bool pure) const;
00173
00174 bool calc_test_error_;
00175
00176 };
00177
00178
00179 #endif // clsfy_binary_tree_builder_h_