PLearn 0.1
RankedVMatrix.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // RankedVMatrix.cc
00004 //
00005 // Copyright (C) 2004 Olivier Delalleau
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: RankedVMatrix.cc 5557 2006-05-10 20:36:58Z lamblin $
00037  ******************************************************* */
00038 
00039 // Authors: Olivier Delalleau
00040 
00044 #include "RankedVMatrix.h"
00045 
00046 namespace PLearn {
00047 using namespace std;
00048 
00050 // RankedVMatrix //
00052 RankedVMatrix::RankedVMatrix()
00053 {}
00054 
00055 RankedVMatrix::RankedVMatrix(VMat the_source, PP<RankedVMatrix> the_reference)
00056     : reference(the_reference)
00057 {
00058     source = the_source;
00059     build();
00060 }
00061 
00062 PLEARN_IMPLEMENT_OBJECT(RankedVMatrix,
00063                         "Replaces the target of a source VMat with its rank.",
00064                         "A 'reference' VMat, which is also a RankedVMatrix, can also be provided.\n"
00065                         "In this case, the target will be defined as follows from a target y:\n"
00066                         " 1. Find in the reference's source VMat the target closest to y\n"
00067                         " 2. Use the rank of the input corresponding to this target.\n"
00068                         " 3. If there is no such target, use the maximum rank + 1\n"
00069                         "If no reference is given, then the target is just the rank in the source\n"
00070                         "VMat's sorted targets, starting from 0.\n"
00071     );
00072 
00074 // declareOptions //
00076 void RankedVMatrix::declareOptions(OptionList& ol)
00077 {
00078     declareOption(ol, "reference", &RankedVMatrix::reference, OptionBase::buildoption,
00079                   "An optional reference VMat used to define the targets.\n");
00080 
00081     // Now call the parent class' declareOptions
00082     inherited::declareOptions(ol);
00083 }
00084 
00086 // build //
00088 void RankedVMatrix::build()
00089 {
00090     inherited::build();
00091     build_();
00092 }
00093 
00095 // build_ //
00097 void RankedVMatrix::build_()
00098 {
00099     // ### This method should do the real building of the object,
00100     // ### according to set 'options', in *any* situation.
00101     // ### Typical situations include:
00102     // ###  - Initial building of an object from a few user-specified options
00103     // ###  - Building of a "reloaded" object: i.e. from the complete set of all serialised options.
00104     // ###  - Updating or "re-building" of an object after a few "tuning" options have been modified.
00105     // ### You should assume that the parent class' build_() has already been called.
00106     if (source) {
00107         if (source->targetsize() != 1)
00108             PLERROR("In RankedVMatrix::build_ - The source VMat must have a targetsize equal to 1");
00109         // Get sorted target column.
00110         sorted_targets.resize(source->length(), 2);
00111         sorted_targets.column(0) << source.column(source->inputsize())->toMat();
00112         sorted_targets.column(1) << TVec<int>(0, source->length() - 1, 1);
00113         sortRows(sorted_targets);
00114         index_to_rank.resize(source->length());
00115         if (reference) {
00116             // We define the targets based on the reference rankings.
00117             // First get the sorted target column of the reference.
00118             Mat ref_sorted_targets = reference->getSortedTargets();
00119             // Now find the inverse mapping from index to rank.
00120             int ref_index = 0;
00121             int the_index;
00122             for (int i = 0; i < sorted_targets.length(); i++) {
00123                 while (ref_index < ref_sorted_targets.length() &&
00124                        sorted_targets(i,0) > ref_sorted_targets(ref_index,0))
00125                     ref_index++;
00126                 if (ref_index == 0)
00127                     // The first target higher or equal is the 0-th one.
00128                     the_index = 0;
00129                 else if (ref_index == sorted_targets.length())
00130                     // There is no target higher or equal.
00131                     the_index = sorted_targets.length() - 1;
00132                 else if (fast_exact_is_equal(sorted_targets(i,0),
00133                                              ref_sorted_targets(ref_index, 0)))
00134                     // We have an exact match.
00135                     the_index = ref_index;
00136                 else {
00137                     // General case: we are in-between two targets. We choose the closest
00138                     // one.
00139                     if (fabs(sorted_targets(i,0) - ref_sorted_targets(ref_index,0)) <=
00140                         fabs(sorted_targets(i,0) - ref_sorted_targets(ref_index - 1 ,0)))
00141                         the_index = ref_index;
00142                     else
00143                         the_index = ref_index - 1;
00144                 }
00145                 index_to_rank[(int) sorted_targets(i,1)] = the_index;
00146             }
00147         } else {
00148             // Store the inverse mapping from index to rank.
00149             for (int i = 0; i < source->length(); i++)
00150                 index_to_rank[(int) sorted_targets(i,1)] = i;
00151         }
00152         // Set VMat info.
00153         width_ = source->width() - source->targetsize() + 1;
00154         defineSizes(source->inputsize(), 1, source->weightsize());
00155         setMetaInfoFromSource();
00156     }
00157 }
00158 
00160 // getNewRow //
00162 void RankedVMatrix::getNewRow(int i, const Vec& v) const
00163 {
00164     source->getRow(i, v);
00165     // Replace the target with the rank.
00166     v[inputsize_] = index_to_rank[i];
00167 }
00168 
00170 // makeDeepCopyFromShallowCopy //
00172 void RankedVMatrix::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00173 {
00174     inherited::makeDeepCopyFromShallowCopy(copies);
00175     deepCopyField(index_to_rank, copies);
00176     deepCopyField(sorted_targets, copies);
00177     deepCopyField(reference, copies);
00178 }
00179 
00180 } // end of namespace PLearn
00181 
00182 
00183 /*
00184   Local Variables:
00185   mode:c++
00186   c-basic-offset:4
00187   c-file-style:"stroustrup"
00188   c-file-offsets:((innamespace . 0)(inline-open . 0))
00189   indent-tabs-mode:nil
00190   fill-column:79
00191   End:
00192 */
00193 // 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