PLearn 0.1
ExhaustiveNearestNeighbors.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // ExhaustiveNearestNeighbors.h
00004 //
00005 // Copyright (C) 2004 Nicolas Chapados
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: ExhaustiveNearestNeighbors.h 8184 2007-10-15 20:09:46Z nouiz $ 
00037  ******************************************************* */
00038 
00039 // Authors: Nicolas Chapados
00040 
00041 
00042 #ifndef ExhaustiveNearestNeighbors_INC
00043 #define ExhaustiveNearestNeighbors_INC
00044 
00045 // From PLearn
00046 #include <plearn_learners/nearest_neighbors/GenericNearestNeighbors.h>
00047 #include <plearn/ker/Kernel.h>
00048 
00049 // From C++ stdlib
00050 #include <utility>                           
00051 #include <queue>                             
00052 
00053 namespace PLearn {
00054 
00080 class ExhaustiveNearestNeighbors: public GenericNearestNeighbors
00081 {
00082     typedef GenericNearestNeighbors inherited;
00083 
00084 protected:
00087     static Ker default_kernel;
00088   
00090     // Mat training_mat;
00091 
00093     mutable Vec dummy_vec;
00094 
00096     mutable TVec<int> tmp_indices;
00097 
00099     mutable Vec tmp_distances;
00100 
00102     mutable Mat cached_inputs;
00103 
00105     mutable priority_queue< pair<real,int> > pq;
00106 
00107   
00108 public:
00109     //#####  Public Build Options  ############################################
00110 
00114 
00121     bool kernel_is_pseudo_distance;
00122 
00123 public:
00124     //#####  Object Methods  ##################################################
00125   
00127     ExhaustiveNearestNeighbors(Ker kernel = default_kernel,
00128                                bool kernel_is_pseudo_distance = true);
00129 
00131     virtual void build();
00132 
00134     virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies);
00135 
00136     // Declares other standard object methods.
00137     // If your class is not instantiatable (it has pure virtual methods)
00138     // you should replace this by PLEARN_DECLARE_ABSTRACT_OBJECT.
00139     PLEARN_DECLARE_OBJECT(ExhaustiveNearestNeighbors);
00140 
00141 
00142 public:
00143     //#####  PLearner Methods  ################################################
00144 
00146     virtual void setTrainingSet(VMat training_set, bool call_forget=true);
00147 
00151     virtual void forget();
00152     
00155     virtual void train();
00156 
00158     virtual void computeOutputAndCosts(const Vec& input, const Vec& target,
00159                                        Vec& output, Vec& costs) const;
00160   
00162     virtual void computeOutput(const Vec& input, Vec& output) const;
00163 
00165     virtual void computeCostsFromOutputs(const Vec& input, const Vec& output, 
00166                                          const Vec& target, Vec& costs) const;
00167   
00170     virtual TVec<std::string> getTestCostNames() const;
00171 
00173     virtual int nTestCosts() const;
00174 
00177     virtual TVec<std::string> getTrainCostNames() const;
00178 
00179 private: 
00181     void build_();
00182 
00183 protected: 
00185     static void declareOptions(OptionList& ol);
00186 
00188     void preloadInputCache() const;
00189 
00193     void findNearestNeighbors(const Vec& input, int K, TVec<int>& indices, Vec& distances) const;
00194 };
00195 
00196 // Declares a few other classes and functions related to this class.
00197 DECLARE_OBJECT_PTR(ExhaustiveNearestNeighbors);
00198   
00199 } // end of namespace PLearn
00200 
00201 #endif
00202 
00203 
00204 /*
00205   Local Variables:
00206   mode:c++
00207   c-basic-offset:4
00208   c-file-style:"stroustrup"
00209   c-file-offsets:((innamespace . 0)(inline-open . 0))
00210   indent-tabs-mode:nil
00211   fill-column:79
00212   End:
00213 */
00214 // 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