PLearn 0.1
RegressionTreeRegisters.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // RegressionTreeRegisters.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: RegressionTreeRegisters.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 RegressionTreeRegisters_INC
00043 #define RegressionTreeRegisters_INC
00044 
00045 #include <plearn/vmat/VMatrix.h>
00046 #include <plearn/base/stringutils.h>
00047 #include <plearn/math/TMat.h>
00048 #include <plearn/vmat/VMat.h>
00049 
00052 #ifndef RTR_type
00053 #define RTR_type uint32_t
00054 #endif
00055 
00056 #ifndef RTR_type_id
00057 #define RTR_type_id int16_t
00058 #endif
00059 
00060 #ifndef RTR_target_t
00061 #define RTR_target_t real
00062 #endif
00063 
00064 #ifndef RTR_weight_t
00065 #define RTR_weight_t real
00066 #endif
00067 
00068 //if you are sur the their is no missing value in the training set
00069 //you can set its value to false for some speed up.
00070 #ifndef RTR_HAVE_MISSING
00071 #define RTR_HAVE_MISSING true
00072 #endif
00073 
00074 namespace PLearn {
00075 using namespace std;
00076 
00077 class RegressionTreeLeave;
00078 class RegressionTreeRegisters: public VMatrix
00079 {
00080     typedef VMatrix inherited;
00081     
00082 private:
00083 
00084 /*
00085   Build options: they have to be set before training
00086 */
00087 
00088     int  report_progress;
00089     int  verbosity;
00090   
00091 /*
00092   Learnt options: they are sized and initialized if need be, at build() or reinitRegisters()
00093 */
00094 
00095     int       next_id;
00096 
00097     TMat<RTR_type> tsorted_row;
00098     TVec<RTR_type_id> leave_register;
00099     VMat tsource;
00100     Mat tsource_mat;
00101     //we put it in pair instead of two vector to speed up
00102     //the getAllRegisteredRow(leave_id, col, reg, target, weight, value) fct
00103     TVec<pair<RTR_target_t,RTR_weight_t> > target_weight;
00104     VMat source;
00105 
00106     bool do_sort_rows;
00107     bool mem_tsource;
00108     bool have_missing;
00109 
00110     mutable vector<bool> compact_reg;
00111     mutable int compact_reg_leave;
00112 
00114     mutable PP<RegressionTreeLeave> tmp_leave;
00116     mutable Vec tmp_vec;
00117 
00118 public:
00119 
00120     RegressionTreeRegisters();
00121     RegressionTreeRegisters(VMat source_, bool report_progress_ = false,
00122                             bool vebosity_ = false, bool do_sort_rows = true,
00123                             bool mem_tsource_ = true);
00124     RegressionTreeRegisters(VMat source_, TMat<RTR_type> tsorted_row_,
00125                             VMat tsource_, bool report_progress_ = false,
00126                             bool vebosity_ = false, bool do_sort_rows = true,
00127                             bool mem_tsource_ = true);
00128     virtual              ~RegressionTreeRegisters();
00129     
00130     PLEARN_DECLARE_OBJECT(RegressionTreeRegisters);
00131 
00132     static  void         declareOptions(OptionList& ol);
00133     virtual void         makeDeepCopyFromShallowCopy(CopiesMap &copies);
00134     virtual void         build();
00135     void         reinitRegisters();
00136     inline void         registerLeave(RTR_type_id leave_id, int row)
00137     { leave_register[row] = leave_id;    }
00138     inline virtual real get(int i, int j) const{
00139         if(j<inputsize())return tsource->get(j,i);
00140         if(j==inputsize())return target_weight[i].first;
00141         else  return target_weight[i].second;
00142     }
00143     inline real         getTarget(int row)const
00144     {return target_weight[row].first;}
00145     inline real         getWeight(int row)const{
00146         return target_weight[row].second;
00147     }
00148     inline void         setWeight(int row,real val){
00149         target_weight[row].second = val;
00150     }
00151     inline bool         haveMissing()const{return have_missing;}
00152     inline RTR_type_id     getNextId(){
00153         PLCHECK(next_id<std::numeric_limits<RTR_type_id>::max());
00154         next_id += 1;return next_id;}
00155     void         getAllRegisteredRow(RTR_type_id leave_id, TVec<RTR_type> &reg)const;
00156     void         getAllRegisteredRow(RTR_type_id leave_id, int col, TVec<RTR_type> &reg)const;
00157     void         getAllRegisteredRow(RTR_type_id leave_id, int col, TVec<RTR_type> &reg,
00158                                      TVec<pair<RTR_target_t,RTR_weight_t> > &t_w, Vec &value)const;
00159    void          getAllRegisteredRowLeave(
00160         RTR_type_id leave_id, int col, TVec<RTR_type> &reg,
00161         TVec<pair<RTR_target_t,RTR_weight_t> > &t_w, Vec &value,
00162         PP<RegressionTreeLeave> missing_leave,
00163         PP<RegressionTreeLeave> left_leave,
00164         PP<RegressionTreeLeave> right_leave,
00165         TVec<RTR_type> &candidate)const;
00166     tuple<real,real,int> bestSplitInRow(
00167         RTR_type_id leave_id, int col, TVec<RTR_type> &reg,
00168         PP<RegressionTreeLeave> left_leave,
00169         PP<RegressionTreeLeave> right_leave,
00170         Vec left_error, Vec right_error)const;
00171     void         printRegisters();
00172     void         getExample(int i, Vec& input, Vec& target, real& weight);
00173     inline virtual void put(int i, int j, real value)
00174     {
00175         PLASSERT(inputsize()>0&&targetsize()>0);
00176         if(j!=inputsize()+targetsize())
00177             PLERROR("In RegressionTreeRegisters::put - implemented the put of "
00178                     "the weightsize only");
00179         setWeight(i,value);
00180     }
00181     
00183     inline TMat<RTR_type> getTSortedRow(){return tsorted_row;}
00184     inline VMat  getTSource(){return tsource;}
00185     virtual void finalize(){tsorted_row = TMat<RTR_type>();}
00186 
00187 private:
00188     void         build_();
00189     void         sortRows();
00190     void         sortEachDim(int dim);
00191     void         verbose(string msg, int level);
00192     void         checkMissing();
00193 
00194 };
00195 
00196 DECLARE_OBJECT_PTR(RegressionTreeRegisters);
00197 
00198 } // end of namespace PLearn
00199 
00200 #endif
00201 
00202 
00203 /*
00204   Local Variables:
00205   mode:c++
00206   c-basic-offset:4
00207   c-file-style:"stroustrup"
00208   c-file-offsets:((innamespace . 0)(inline-open . 0))
00209   indent-tabs-mode:nil
00210   fill-column:79
00211   End:
00212 */
00213 // 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