PLearn 0.1
RegressionTreeNode.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // RegressionTreeNode.h
00004 // Copyright (c) 1998-2002 Pascal Vincent
00005 // Copyright (C) 1999-2002 Yoshua Bengio and University of Montreal
00006 // Copyright (c) 2002 Jean-Sebastien Senecal, Xavier Saint-Mleux, Rejean Ducharme
00007 //
00008 // Redistribution and use in source and binary forms, with or without
00009 // modification, are permitted provided that the following conditions are met:
00010 // 
00011 //  1. Redistributions of source code must retain the above copyright
00012 //     notice, this list of conditions and the following disclaimer.
00013 // 
00014 //  2. Redistributions in binary form must reproduce the above copyright
00015 //     notice, this list of conditions and the following disclaimer in the
00016 //     documentation and/or other materials provided with the distribution.
00017 // 
00018 //  3. The name of the authors may not be used to endorse or promote
00019 //     products derived from this software without specific prior written
00020 //     permission.
00021 // 
00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00032 // 
00033 // This file is part of the PLearn library. For more information on the PLearn
00034 // library, go to the PLearn Web site at www.plearn.org
00035 
00036 
00037 /* ********************************************************************************    
00038  * $Id: RegressionTreeNode.h, v 1.0 2004/07/19 10:00:00 Bengio/Kegl/Godbout     *
00039  * This file is part of the PLearn library.                                     *
00040  ******************************************************************************** */
00041 
00042 #ifndef RegressionTreeNode_INC
00043 #define RegressionTreeNode_INC
00044 
00045 #include <plearn/base/Object.h>
00046 #include <plearn/base/PP.h>
00047 #include <plearn/math/TVec.h>
00048 #include <boost/tuple/tuple.hpp>
00049 #include "RegressionTreeRegisters.h"
00050 #include "RegressionTree.h"
00051 
00052 namespace PLearn {
00053 using namespace std;
00054 class RegressionTreeRegisters;
00055 class RegressionTreeLeave;
00056 class RegressionTreeNode;
00057 
00058 class RegressionTreeNode: public Object
00059 {
00060     friend class RegressionTree;
00061     typedef Object inherited;
00062   
00063 private:
00064 
00065 /*
00066   Build options: they have to be set before building
00067 */
00068 
00069     int  missing_is_valid;
00070 
00071     PP<RegressionTree> tree;
00072     PP<RegressionTreeLeave> leave;
00073     
00074 /*
00075   Learnt options: they are sized and initialized if need be, in initNode(...)
00076 */
00077  
00078     Vec leave_output;
00079     Vec leave_error;
00080     int split_col;
00081     int split_balance;
00082     real split_feature_value;
00083     real after_split_error;
00084     PP<RegressionTreeNode> missing_node;
00085     PP<RegressionTreeLeave> missing_leave;
00086     PP<RegressionTreeNode> left_node;
00087     PP<RegressionTreeLeave> left_leave;
00088     PP<RegressionTreeNode> right_node;
00089     PP<RegressionTreeLeave> right_leave;
00090     
00091     //only there to reload old version. Put static to use less space.
00092     static int dummy_int;
00093     static Vec tmp_vec;
00094     static PP<RegressionTreeLeave> dummy_leave_template;
00095     static PP<RegressionTreeRegisters> dummy_train_set;
00096 public:  
00097     RegressionTreeNode();
00098     RegressionTreeNode(int missing_is_valid);
00099     virtual              ~RegressionTreeNode();
00100     
00101     PLEARN_DECLARE_OBJECT(RegressionTreeNode);
00102 
00103     static  void         declareOptions(OptionList& ol);
00104     virtual void         makeDeepCopyFromShallowCopy(CopiesMap &copies);
00105     virtual void         build();
00106     void         finalize();
00107     void         initNode(PP<RegressionTree> tree,
00108                           PP<RegressionTreeLeave> leave);
00109     void         lookForBestSplit();
00110     inline void  compareSplit(int col, real left_leave_last_feature,
00111                               real right_leave_first_feature,
00112                               Vec left_error, Vec right_error,
00113                               Vec missing_error);
00114     int          expandNode();
00115     inline int   getSplitBalance()const{
00116         if (split_col < 0) return tree->getSortedTrainingSet()->length();
00117         return split_balance;}
00118     inline real  getErrorImprovment()const{
00119         if (split_col < 0) return -1.0;
00120         real err=leave_error[0] + leave_error[1] - after_split_error;
00121         PLASSERT(is_equal(err,0)||err>0);
00122         return err;
00123     }
00124     inline int          getSplitCol() const{return split_col;}
00125     inline real         getSplitValue() const{return split_feature_value;}
00126     TVec< PP<RegressionTreeNode> >  getNodes();
00127     void         computeOutputAndNodes(const Vec& inputv, Vec& outputv,
00128                                        TVec<PP<RegressionTreeNode> >* nodes=0);
00129     inline void         computeOutput(const Vec& inputv, Vec& outputv)
00130     {computeOutputAndNodes(inputv,outputv);}
00131     inline bool         haveChildrenNode(){return left_node;}
00132     
00133 private:
00134     void         build_();
00135     void         verbose(string msg, int level); 
00136     static tuple<real,real,int> bestSplitInRow(int col, TVec<RTR_type>& candidates,
00137                                                Vec left_error, Vec right_error,
00138                                                const Vec missing_error,
00139                                                PP<RegressionTreeLeave> right_leave,
00140                                                PP<RegressionTreeLeave> left_leave,
00141                                                PP<RegressionTreeRegisters> train_set,
00142                                                Vec values, 
00143                                                TVec<pair<RTR_target_t,RTR_weight_t> > t_w
00144         );
00145 };
00146 
00147 DECLARE_OBJECT_PTR(RegressionTreeNode);
00148 
00149 } // end of namespace PLearn
00150 
00151 #endif
00152 
00153 
00154 /*
00155   Local Variables:
00156   mode:c++
00157   c-basic-offset:4
00158   c-file-style:"stroustrup"
00159   c-file-offsets:((innamespace . 0)(inline-open . 0))
00160   indent-tabs-mode:nil
00161   fill-column:79
00162   End:
00163 */
00164 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines