latest version v1.9 - last update 10 Apr 2010 |
00001 /* 00002 * Copyright (C) 2002, 2003, 2004, 2005, 2006 00003 * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany 00004 * 00005 * This file is part of the LTI-Computer Vision Library (LTI-Lib) 00006 * 00007 * The LTI-Lib is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public License (LGPL) 00009 * as published by the Free Software Foundation; either version 2.1 of 00010 * the License, or (at your option) any later version. 00011 * 00012 * The LTI-Lib is distributed in the hope that it will be 00013 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty 00014 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with the LTI-Lib; see the file LICENSE. If 00019 * not, write to the Free Software Foundation, Inc., 59 Temple Place - 00020 * Suite 330, Boston, MA 02111-1307, USA. 00021 */ 00022 00023 00024 /*-------------------------------------------------------------------- 00025 * project ....: LTI-Lib: Image Processing and Computer Vision Library 00026 * file .......: ltiDecisionTree.h 00027 * authors ....: Peter Doerfler 00028 * organization: LTI, RWTH Aachen 00029 * creation ...: 19.2.2002 00030 * revisions ..: $Id: ltiDecisionTree.h,v 1.6 2006/02/07 18:17:06 ltilib Exp $ 00031 */ 00032 00033 #ifndef _LTI_DECISION_TREE_H_ 00034 #define _LTI_DECISION_TREE_H_ 00035 00036 #include "ltiVector.h" 00037 #include "ltiMatrix.h" 00038 #include "ltiObjectFactory.h" 00039 00040 #include "ltiSupervisedInstanceClassifier.h" 00041 00042 namespace lti { 00043 /** 00044 * This is the base class for all decision trees in the LTI-Lib 00045 */ 00046 class decisionTree : public supervisedInstanceClassifier { 00047 public: 00048 00049 00050 /** 00051 * the parameters for the class decisionTree 00052 */ 00053 class parameters : public supervisedInstanceClassifier::parameters { 00054 public: 00055 /** 00056 * default constructor 00057 */ 00058 parameters(); 00059 00060 /** 00061 * copy constructor 00062 * @param other the parameters object to be copied 00063 */ 00064 parameters(const parameters& other); 00065 00066 /** 00067 * destructor 00068 */ 00069 virtual ~parameters(); 00070 00071 /** 00072 * returns name of this type 00073 */ 00074 const char* getTypeName() const; 00075 00076 /** 00077 * copy the contents of a parameters object 00078 * @param other the parameters object to be copied 00079 * @return a reference to this parameters object 00080 */ 00081 parameters& copy(const parameters& other); 00082 00083 /** 00084 * copy the contents of a parameters object 00085 * @param other the parameters object to be copied 00086 * @return a reference to this parameters object 00087 */ 00088 parameters& operator=(const parameters& other); 00089 00090 00091 /** 00092 * returns a pointer to a clone of the parameters 00093 */ 00094 virtual classifier::parameters* clone() const; 00095 00096 /** 00097 * write the parameters in the given ioHandler 00098 * @param handler the ioHandler to be used 00099 * @param complete if true (the default) the enclosing begin/end will 00100 * be also written, otherwise only the data block will be written. 00101 * @return true if write was successful 00102 */ 00103 virtual bool write(ioHandler& handler,const bool complete=true) const; 00104 00105 /** 00106 * read the parameters from the given ioHandler 00107 * @param handler the ioHandler to be used 00108 * @param complete if true (the default) the enclosing begin/end will 00109 * be also written, otherwise only the data block will be written. 00110 * @return true if write was successful 00111 */ 00112 virtual bool read(ioHandler& handler,const bool complete=true); 00113 00114 # ifdef _LTI_MSC_6 00115 /** 00116 * this function is required by MSVC only, as a workaround for a 00117 * very awful bug, which exists since MSVC V.4.0, and still by 00118 * V.6.0 with all bugfixes (so called "service packs") remains 00119 * there... This method is also public due to another bug, so please 00120 * NEVER EVER call this method directly: use read() instead 00121 */ 00122 bool readMS(ioHandler& handler,const bool complete=true); 00123 00124 /** 00125 * this function is required by MSVC only, as a workaround for a 00126 * very awful bug, which exists since MSVC V.4.0, and still by 00127 * V.6.0 with all bugfixes (so called "service packs") remains 00128 * there... This method is also public due to another bug, so please 00129 * NEVER EVER call this method directly: use write() instead 00130 */ 00131 bool writeMS(ioHandler& handler,const bool complete=true) const; 00132 # endif 00133 00134 // ------------------------------------------------ 00135 // the parameters 00136 // ------------------------------------------------ 00137 00138 //TODO: comment the parameters of your classifier 00139 // If you add more parameters manually, do not forget to do following: 00140 // 1. indicate in the default constructor the default values 00141 // 2. make sure that the copy member also copy your new parameters 00142 // 3. make sure that the read and write members also read and 00143 // write your parameters 00144 00145 00146 }; 00147 00148 /** 00149 * a simple Node for decision trees 00150 */ 00151 class dtNode : public object { 00152 public: 00153 00154 /** 00155 * default constructor 00156 */ 00157 dtNode(); 00158 00159 /** 00160 * Constructor. Sets the id of the node. 00161 * @param theId the nodes id. 00162 */ 00163 dtNode(const int& theId); 00164 00165 /** 00166 * copy constructor 00167 * @param other the dtNode object to be copied 00168 */ 00169 dtNode(const dtNode& other); 00170 00171 /** 00172 * destructor 00173 */ 00174 virtual ~dtNode(); 00175 00176 /** 00177 * returns name of this type 00178 */ 00179 const char* getTypeName() const; 00180 00181 /** 00182 * copy the contents of a dtNode object 00183 * @param other the dtNode object to be copied 00184 * @return a reference to this dtNode object 00185 */ 00186 dtNode& copy(const dtNode& other); 00187 00188 /** 00189 * copy the contents of a dtNode object 00190 * @param other the dtNode object to be copied 00191 * @return a reference to this dtNode object 00192 */ 00193 dtNode& operator=(const dtNode& other); 00194 00195 00196 /** 00197 * returns a pointer to a clone of the dtNode 00198 */ 00199 virtual dtNode* clone() const; 00200 00201 /** 00202 * Sets the left child node and returns the previous 00203 * left child node. 00204 * <p><b>NOTE</b>: No copy or clone is performed, ie left is 00205 * the same pointer as newLeft. This is necessary for building trees. 00206 * @param newLeft new left child node 00207 * @returns the previous left child node 00208 */ 00209 dtNode* setLeftChild(dtNode* newLeft); 00210 00211 /** 00212 * Sets the right child node and returns the previous 00213 * right child node. 00214 * <p><b>NOTE</b>: No copy or clone is performed, ie left is 00215 * the same pointer as newLeft. This is necessary for building trees. 00216 * @param newRight new right child node 00217 * @returns the previous right child node 00218 */ 00219 dtNode* setRightChild(dtNode* newRight); 00220 00221 /** 00222 * Gets the left child node. 00223 */ 00224 const dtNode* getLeftChild() const; 00225 00226 /** 00227 * Gets the right child node. 00228 */ 00229 const dtNode* getRightChild() const; 00230 00231 /** 00232 * Returns true if this node is a leaf, ie it has no children. 00233 */ 00234 bool isLeaf() const; 00235 00236 /** 00237 * Set the nodes id 00238 */ 00239 void setId(const int& theId); 00240 00241 /** 00242 * Get the nodes id 00243 */ 00244 int getId() const; 00245 00246 /** 00247 * write the dtNode in the given ioHandler<p> 00248 * <b>NOTE:</b> Writing a node results in writing of the complete tree 00249 * following this node. 00250 * @param handler the ioHandler to be used 00251 * @param complete if true (the default) the enclosing begin/end will 00252 * be also written, otherwise only the data block will be written. 00253 * @return true if write was successful 00254 */ 00255 virtual bool write(ioHandler& handler,const bool complete=true) const; 00256 00257 /** 00258 * read the dtNode from the given ioHandler 00259 * <b>NOTE:</b> Reading a node results in reading of the complete tree 00260 * following this node. 00261 * @param handler the ioHandler to be used 00262 * @param complete if true (the default) the enclosing begin/end will 00263 * be also written, otherwise only the data block will be written. 00264 * @return true if write was successful 00265 */ 00266 virtual bool read(ioHandler& handler,const bool complete=true); 00267 00268 # ifdef _LTI_MSC_6 00269 /** 00270 * this function is required by MSVC only, as a workaround for a 00271 * very awful bug, which exists since MSVC V.4.0, and still by 00272 * V.6.0 with all bugfixes (so called "service packs") remains 00273 * there... This method is public due to another bug!, so please 00274 * NEVER EVER call this method directly 00275 */ 00276 bool readMS(ioHandler& handler,const bool complete=true); 00277 00278 /** 00279 * this function is required by MSVC only, as a workaround for a 00280 * very awful bug, which exists since MSVC V.4.0, and still by 00281 * V.6.0 with all bugfixes (so called "service packs") remains 00282 * there... This method is public due to another bug!, so please 00283 * NEVER EVER call this method directly 00284 */ 00285 bool writeMS(ioHandler& handler,const bool complete=true) const; 00286 # endif 00287 00288 // ------------------------------------------------ 00289 // the dtNode 00290 // ------------------------------------------------ 00291 00292 protected: 00293 00294 /** 00295 * Assigns ids to the leaf node from left to right starting with 00296 * 0. The ids are equivalent to the positions in the 00297 * decisionTrees outTemplate. The starting point is this 00298 * node. Usually, this method will be called for the root node 00299 * of some decisionTree. The method returns the number of leaf nodes. 00300 */ 00301 int assignIds(); 00302 00303 /** 00304 * Pointer to the left child node 00305 */ 00306 dtNode *left; 00307 00308 /** 00309 * Pointer to the right child node 00310 */ 00311 dtNode *right; 00312 00313 /** 00314 * The nodes id. It is used as a reference to the nodes position in 00315 * the decisionTrees outTemplate. So the ids should always be 00316 * between 0 and n-1 with n being the number of leaf nodes of the 00317 * decision tree. 00318 */ 00319 int id; 00320 00321 }; 00322 00323 00324 /** 00325 * default constructor 00326 */ 00327 decisionTree(); 00328 00329 /** 00330 * copy constructor 00331 * @param other the object to be copied 00332 */ 00333 decisionTree(const decisionTree& other); 00334 00335 /** 00336 * destructor 00337 */ 00338 virtual ~decisionTree(); 00339 00340 /** 00341 * returns the name of this type ("decisionTree") 00342 */ 00343 virtual const char* getTypeName() const; 00344 00345 /** 00346 * copy data of "other" classifier. 00347 * @param other the classifier to be copied 00348 * @return a reference to this classifier object 00349 */ 00350 decisionTree& copy(const decisionTree& other); 00351 00352 /** 00353 * alias for copy member 00354 * @param other the classifier to be copied 00355 * @return a reference to this classifier object 00356 */ 00357 decisionTree& operator=(const decisionTree& other); 00358 00359 /** 00360 * returns used parameters 00361 */ 00362 const parameters& getParameters() const; 00363 00364 /** 00365 * write the parameters in the given ioHandler 00366 * @param handler the ioHandler to be used 00367 * @param complete if true (the default) the enclosing begin/end will 00368 * be also written, otherwise only the data block will be written. 00369 * @return true if write was successful 00370 */ 00371 virtual bool write(ioHandler& handler,const bool complete=true) const; 00372 00373 /** 00374 * read the parameters from the given ioHandler 00375 * @param handler the ioHandler to be used 00376 * @param complete if true (the default) the enclosing begin/end will 00377 * be also written, otherwise only the data block will be written. 00378 * @return true if write was successful 00379 */ 00380 virtual bool read(ioHandler& handler,const bool complete=true); 00381 00382 00383 //TODO: comment the attributes of your classifier 00384 // If you add more attributes manually, do not forget to do following: 00385 // 1. indicate in the default constructor the default values 00386 // 2. make sure that the copy member also copy your new attributes, or 00387 // to ensure there, that these attributes are properly initialized. 00388 00389 protected: 00390 00391 /** 00392 * The array of all dtNodes. 00393 */ 00394 static const dtNode* nodeArray[]; 00395 00396 /** 00397 * The objectFactory for all types of dtNodes. 00398 */ 00399 static const objectFactory<dtNode> nodeFactory; 00400 00401 int numberOfLeafs; 00402 00403 }; 00404 } 00405 00406 #endif