PLearn 0.1
BinaryKernelDiscrimination.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // BinaryKernelDiscrimination.cc
00004 //
00005 // Copyright (C) 2006 Pierre-Jean L Heureux 
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: .pyskeleton_header 544 2003-09-01 00:05:31Z plearner $ 
00037    ******************************************************* */
00038 
00039 // Authors: Pierre-Jean L Heureux
00040 
00044 #include "BinaryKernelDiscrimination.h"
00045 
00046 namespace PLearn {
00047 using namespace std;
00048 
00049 PLEARN_IMPLEMENT_OBJECT(
00050     BinaryKernelDiscrimination,
00051     "Kernel suitable for a binary vector input",
00052     "Based on Harper,et al. J.Chem.Inf.Comput.Sci. 2001,41,p.1295. \nHELP"
00053     );
00054 
00056 // BinaryKernelDiscrimination //
00058 BinaryKernelDiscrimination::BinaryKernelDiscrimination() :
00059 /* ### Initialize all fields to their default value here */
00060     K(1),
00061     ExpOutput(false)
00062 {
00063     // ...
00064 
00065     // ### You may (or not) want to call build_() to finish building the object
00066     // ### (doing so assumes the parent classes' build_() have been called too
00067     // ### in the parent classes' constructors, something that you must ensure)
00068 }
00069 
00071 // declareOptions //
00073 void BinaryKernelDiscrimination::declareOptions(OptionList& ol)
00074 {
00075     // ### Declare all of this object's options here
00076     // ### For the "flags" of each option, you should typically specify  
00077     // ### one of OptionBase::buildoption, OptionBase::learntoption or 
00078     // ### OptionBase::tuningoption. Another possible flag to be combined with
00079     // ### is OptionBase::nosave
00080 
00081     // ### ex:
00082     declareOption(ol, "lambda", &BinaryKernelDiscrimination::lambda, OptionBase::buildoption,
00083                   "The base of the kernel");
00084     declareOption(ol, "K", &BinaryKernelDiscrimination::K, OptionBase::buildoption,
00085                   "a constant");
00086     declareOption(ol, "ExpOutput", &BinaryKernelDiscrimination::ExpOutput, OptionBase::buildoption,
00087                   "Flag to 1 if you do not want the log of the output. The exponential form will be outputted instead");
00088 
00089     // Now call the parent class' declareOptions
00090     inherited::declareOptions(ol);
00091 }
00092 
00094 // build //
00096 void BinaryKernelDiscrimination::build()
00097 {
00098     // ### Nothing to add here, simply calls build_
00099     inherited::build();
00100     build_();
00101 }
00102 
00104 // build_ //
00106 void BinaryKernelDiscrimination::build_()
00107 {
00108     // ### This method should do the real building of the object,
00109     // ### according to set 'options', in *any* situation. 
00110     // ### Typical situations include:
00111     // ###  - Initial building of an object from a few user-specified options
00112     // ###  - Building of a "reloaded" object: i.e. from the complete set of all serialised options.
00113     // ###  - Updating or "re-building" of an object after a few "tuning" options have been modified.
00114     // ### You should assume that the parent class' build_() has already been called.
00115 }
00116 
00118 // evaluate //
00120 real BinaryKernelDiscrimination::evaluate(const Vec& x1, const Vec& x2) const {
00121     // ### Evaluate the kernel on a pair of points.
00122     int i;
00123 
00124     if (x1.length() != x2.length()){PLERROR("Vector not of same length");}
00125     int N= x1.length();
00126     int dij = 0;
00127     
00128     for (i=0; i < x1.length();i++)
00129     {
00130         if (x1[i] != x2[i]){dij++;};
00131     }
00132     if (ExpOutput)
00133         return pow(lambda,(K-((K*dij)/N)))*pow(1-lambda,K*dij/N);
00134     else
00135         return safeflog(pow(lambda,(K-((K*dij)/N)))*pow(1-lambda,K*dij/N));
00136 }
00137 
00138 /* ### This method will very often be overridden.
00140 // evaluate_i_j //
00142 real BinaryKernelDiscrimination::evaluate_i_j(int i, int j) const {
00143 // ### Evaluate the kernel on a pair of training points.
00144 }
00145 */
00146 
00148 // makeDeepCopyFromShallowCopy //
00150 void BinaryKernelDiscrimination::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00151 {
00152     inherited::makeDeepCopyFromShallowCopy(copies);
00153 
00154     // ### Call deepCopyField on all "pointer-like" fields 
00155     // ### that you wish to be deepCopied rather than 
00156     // ### shallow-copied.
00157     // ### ex:
00158     // deepCopyField(trainvec, copies);
00159 
00160     // ### Remove this line when you have fully implemented this method.
00161     PLERROR("BinaryKernelDiscrimination::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!");
00162 }
00163 
00164 /* ### This method will be overridden if computations need to be done,
00165    ### or to forward the call to another object.
00166    ### In this case, be careful that it may be called BEFORE the build_()
00167    ### method has been called, if the 'specify_dataset' option is used.
00169 // setDataForKernelMatrix //
00171 void BinaryKernelDiscrimination::setDataForKernelMatrix(VMat the_data) {
00172 }
00173 */
00174 
00175 } // end of namespace PLearn
00176 
00177 
00178 /*
00179   Local Variables:
00180   mode:c++
00181   c-basic-offset:4
00182   c-file-style:"stroustrup"
00183   c-file-offsets:((innamespace . 0)(inline-open . 0))
00184   indent-tabs-mode:nil
00185   fill-column:79
00186   End:
00187 */
00188 // 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