Go to the documentation of this file.00001
00002 #ifndef clsfy_binary_tree_h_
00003 #define clsfy_binary_tree_h_
00004
00005
00006
00007
00008 #include <clsfy/clsfy_classifier_base.h>
00009 #include <clsfy/clsfy_binary_threshold_1d.h>
00010 #include <vcl_iosfwd.h>
00011
00012
00013
00014
00015
00016
00017
00018 class clsfy_binary_tree_op
00019 {
00020 protected:
00021
00022 int data_index_;
00023 const vnl_vector<double>* data_ptr_;
00024 clsfy_binary_threshold_1d classifier_;
00025
00026 public:
00027
00028 clsfy_binary_tree_op() : data_index_(-1), data_ptr_(0) {}
00029 clsfy_binary_tree_op(const vnl_vector<double>* data_ptr,
00030 int data_index=-1)
00031 : data_index_(data_index), data_ptr_(data_ptr) {}
00032
00033 clsfy_binary_threshold_1d& classifier() {return classifier_;}
00034 unsigned data_index() const {return data_index_;}
00035 void set_data_index(unsigned index) {data_index_=index;}
00036 void set_data_ptr(const vnl_vector<double>* data_ptr) {data_ptr_= data_ptr;}
00037
00038
00039 const vnl_vector<double >& data() const {return *data_ptr_;}
00040
00041 void set_data(const vnl_vector<double >& inputs) {data_ptr_=&inputs;}
00042
00043 double val() const {return (*data_ptr_)[data_index_];}
00044
00045
00046 unsigned classify() {return classifier_.classify(val());}
00047
00048 unsigned ndims() {return data_ptr_ ? data_ptr_->size() : 0;}
00049
00050
00051 void b_write(vsl_b_ostream& bfs) const;
00052
00053
00054 void b_read(vsl_b_istream& bfs);
00055
00056 short version_no() const {return 1;}
00057 };
00058
00059
00060 class clsfy_binary_tree_node
00061 {
00062 int nodeId_;
00063 clsfy_binary_tree_node* parent_;
00064 clsfy_binary_tree_node* left_child_;
00065 clsfy_binary_tree_node* right_child_;
00066 clsfy_binary_tree_op op_;
00067 double prob_;
00068 public:
00069
00070 clsfy_binary_tree_node(clsfy_binary_tree_node* parent,
00071 const clsfy_binary_tree_op& op)
00072 : nodeId_(-1),parent_(parent),left_child_(0),right_child_(0),op_(op),prob_(0.5) {}
00073
00074 virtual clsfy_binary_tree_node* create_child(const clsfy_binary_tree_op& op);
00075 void add_child(const clsfy_binary_tree_op& op,bool bLeft)
00076 {
00077 clsfy_binary_tree_node* child=create_child(op);
00078 if (bLeft)
00079 left_child_=child;
00080 else
00081 right_child_=child;
00082 }
00083
00084
00085
00086 virtual ~clsfy_binary_tree_node() {}
00087
00088 friend class clsfy_binary_tree;
00089 friend class clsfy_binary_tree_builder;
00090 };
00091
00092
00093
00094
00095
00096
00097
00098
00099 class clsfy_binary_tree : public clsfy_classifier_base
00100 {
00101 public:
00102
00103 struct graph_rep
00104 {
00105 int me;
00106 int left_child;
00107 int right_child;
00108 };
00109
00110
00111 clsfy_binary_tree(): root_(0),cache_node_(0) {}
00112
00113 virtual ~clsfy_binary_tree();
00114
00115 clsfy_binary_tree(const clsfy_binary_tree& srcTree);
00116
00117 clsfy_binary_tree& operator=(const clsfy_binary_tree& srcTree);
00118
00119 static void remove_tree(clsfy_binary_tree_node* root);
00120
00121 virtual unsigned classify(const vnl_vector<double> &input) const;
00122
00123
00124
00125 virtual void class_probabilities(vcl_vector<double> &outputs, const vnl_vector<double> &input) const;
00126
00127
00128
00129 virtual double log_l(const vnl_vector<double> &input) const;
00130
00131
00132 virtual unsigned n_classes() const {return 1;}
00133
00134
00135 virtual unsigned n_dims() const;
00136
00137
00138 virtual short version_no() const;
00139
00140
00141 virtual vcl_string is_a() const;
00142
00143
00144 virtual bool is_class(vcl_string const& s) const;
00145
00146
00147 virtual clsfy_classifier_base* clone() const;
00148
00149
00150 virtual void print_summary(vcl_ostream& os) const;
00151
00152
00153 virtual void b_write(vsl_b_ostream& bfs) const;
00154
00155
00156 virtual void b_read(vsl_b_istream& bfs);
00157
00158
00159 void set_root( clsfy_binary_tree_node* root);
00160 private:
00161 clsfy_binary_tree_node* root_;
00162 mutable clsfy_binary_tree_node* cache_node_;
00163 private:
00164 void copy(const clsfy_binary_tree& srcTree);
00165 void copy_children(clsfy_binary_tree_node* pSrcNode,clsfy_binary_tree_node* pNode);
00166 };
00167
00168 #endif // clsfy_binary_tree_h_