PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // MultiToUniInstanceSelectRandomVMatrix.cc 00004 // 00005 // Copyright (C) 2005 Benoit Cromp 00006 // 00007 // Redistribution and use in source and binary forms, with or without 00008 // modification, are permitted provided that the following conditions are met: 00009 // 00010 // 1. Redistributions of source code must retain the above copyright 00011 // notice, this list of conditions and the following disclaimer. 00012 // 00013 // 2. Redistributions in binary form must reproduce the above copyright 00014 // notice, this list of conditions and the following disclaimer in the 00015 // documentation and/or other materials provided with the distribution. 00016 // 00017 // 3. The name of the authors may not be used to endorse or promote 00018 // products derived from this software without specific prior written 00019 // permission. 00020 // 00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00031 // 00032 // This file is part of the PLearn library. For more information on the PLearn 00033 // library, go to the PLearn Web site at www.plearn.org 00034 00035 /* ******************************************************* 00036 * $Id: MultiToUniInstanceSelectRandomVMatrix.cc 8617 2008-03-03 17:45:54Z nouiz $ 00037 ******************************************************* */ 00038 00039 // Authors: Benoit Cromp 00040 00044 #include "MultiToUniInstanceSelectRandomVMatrix.h" 00045 #include <plearn/math/random.h> 00046 #include <plearn/vmat/SubVMatrix.h> 00047 00048 namespace PLearn { 00049 using namespace std; 00050 00051 PLEARN_IMPLEMENT_OBJECT(MultiToUniInstanceSelectRandomVMatrix, 00052 "VMat class that selects randomly one row per bags from a multi instances conforming VMatrix and discard the multi instances bag information column. NOTE: doesn't support weight columns (will be discarded)", 00053 "" 00054 ); 00055 00056 MultiToUniInstanceSelectRandomVMatrix::MultiToUniInstanceSelectRandomVMatrix() 00057 : seed(0) 00058 { 00059 } 00060 00061 void MultiToUniInstanceSelectRandomVMatrix::declareOptions(OptionList& ol) 00062 { 00063 00064 declareOption(ol, "seed", &MultiToUniInstanceSelectRandomVMatrix::seed, OptionBase::buildoption, "Random generator seed (>0) (exceptions : -1 = initialized from clock, 0 = no initialization)."); 00065 00066 inherited::declareOptions(ol); 00067 00068 // Redeclare some options. 00069 redeclareOption(ol, "indices", &MultiToUniInstanceSelectRandomVMatrix::indices, OptionBase::nosave, ""); 00070 redeclareOption(ol, "indices_vmat", &MultiToUniInstanceSelectRandomVMatrix::indices_vmat, OptionBase::nosave, ""); 00071 00072 redeclareOption(ol, "source", &MultiToUniInstanceSelectRandomVMatrix::source_, OptionBase::buildoption, "Multi instances conforming source VMatrix"); 00073 } 00074 00076 // build // 00078 00079 void MultiToUniInstanceSelectRandomVMatrix::build() 00080 { 00081 inherited::build(); 00082 build_(); 00083 } 00084 00085 void MultiToUniInstanceSelectRandomVMatrix::build_() 00086 { 00087 00088 // Seeding the random number generator 00089 if (seed == -1) 00090 PLearn::seed(); 00091 else if (seed > 0) 00092 PLearn::manual_seed(seed); 00093 else if (seed != 0) 00094 PLERROR("In MultiToUniInstanceSelectRandomVMatrix::build_ - The seed must be either -1 or >= 0"); 00095 00096 updateMtime(indices_vmat); 00097 updateMtime(source_); 00098 00099 // Building the source VMatrix (uni instances conforming version of source_) 00100 source = new SubVMatrix(source_, 0, 0, source_->length(), source_->inputsize()+source_->targetsize() - 1); 00101 source->defineSizes(source_->inputsize(), source_->targetsize()-1, 0); 00102 00103 width_ = source->inputsize() + source->targetsize() + source->weightsize(); 00104 inputsize_ = source->inputsize(); 00105 targetsize_ = source->targetsize(); 00106 weightsize_ = source->weightsize(); 00107 00108 // Copy the appropriate fields informations 00109 if (source->getFieldInfos().size() > 0) 00110 { 00111 fieldinfos.resize(width_); 00112 this->setFieldInfos(source->getFieldInfos()); 00113 } 00114 // Building the indices list that correspond to choosing randomly one instance per bag. 00115 /* Notes for this task : 00116 The bag signal values meaning : 00117 1 means the first instance of the bag 00118 2 means the last instance of the bag 00119 3 is for a bag with a single row (= 1+2) 00120 0 is for intermediate instances. 00121 */ 00122 00123 int bag_signal_column = source_->inputsize() + source_->targetsize() - 1; 00124 int first_row = 0; 00125 00126 indices.resize(0); // This get rid of the user's build option value. 00127 for(int row=0; row<source_->length(); row++) 00128 { 00129 switch(int(source_->get(row, bag_signal_column))) 00130 { 00131 case 1: 00132 first_row = row; 00133 break; 00134 case 2: 00135 indices.push_back(first_row+(int)(uniform_sample()*(row-first_row+1))); 00136 break; 00137 case 3: 00138 indices.push_back(row); 00139 break; 00140 }; 00141 } 00142 inherited::build(); 00143 00144 } 00145 00146 } // end of namespace PLearn 00147 00148 00149 /* 00150 Local Variables: 00151 mode:c++ 00152 c-basic-offset:4 00153 c-file-style:"stroustrup" 00154 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00155 indent-tabs-mode:nil 00156 fill-column:79 00157 End: 00158 */ 00159 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :