gmnpmkl.h

Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 3 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * Written (W) 2009 Alexander Binder
00008  * Copyright (C) 2009 Fraunhofer Institute FIRST and Max-Planck-Society
00009  */
00010 
00011 #ifndef GMNPMKL_H_
00012 #define GMNPMKL_H_
00013 
00014 #include <vector>
00015 
00016 #include "base/SGObject.h"
00017 #include "lib/ShogunException.h"
00018 #include "classifier/svm/GMNPSVM.h"
00019 #include "kernel/Kernel.h" 
00020 
00021 //for the testing method
00022 #include "kernel/CustomKernel.h" 
00023 #include "kernel/CombinedKernel.h" 
00024 #include "features/DummyFeatures.h"
00025 
00026 
00027 #ifdef USE_GLPK
00028 #include <glpk.h>
00029 
00043 class lpwrapper
00044 {
00045 public:
00046     int32_t lpwrappertype; // 0 -glpk
00047 
00048     lpwrapper();
00049     
00050     virtual ~lpwrapper();
00051     
00052     virtual void setup(const int32_t numkernels); 
00053 
00058     virtual void addconstraint(const ::std::vector<float64_t> & normw2,
00059             const float64_t sumofpositivealphas); 
00060 
00061     virtual void computeweights(std::vector<float64_t> & weights2); 
00062     
00063 };
00064 
00066 class glpkwrapper: public lpwrapper
00067 {
00068 public:
00069     
00070     glpkwrapper();
00071     virtual ~glpkwrapper();
00072     
00073 
00074     
00075 protected:
00076     
00078     glpkwrapper operator=(glpkwrapper & gl);
00079     glpkwrapper(glpkwrapper & gl);
00080     glp_prob* linearproblem;
00081 };
00082 
00083 #else
00084 
00086 class lpwrapper : public CSGObject
00087 {
00088 public:
00089     int32_t lpwrappertype; // 0 -glpk
00090 
00091     lpwrapper();
00092     
00093     virtual ~lpwrapper();
00094     
00095     virtual void setup(const int32_t numkernels); 
00096 
00097     virtual void addconstraint(const ::std::vector<float64_t> & normw2,
00098             const float64_t sumofpositivealphas); 
00099 
00100     virtual void computeweights(std::vector<float64_t> & weights2); 
00101     
00102 };
00103 
00105 class glpkwrapper: public lpwrapper
00106 {
00107 public:
00108     
00109     glpkwrapper();
00110     virtual ~glpkwrapper();
00112     inline virtual const char* get_name() const { return "GLPKWrapper"; }
00113     
00114 
00115     
00116 protected:
00117     
00118     //prohibit copying, copy constructor by declaring protected?
00119     glpkwrapper operator=(glpkwrapper & gl);
00120     glpkwrapper(glpkwrapper & gl);
00121 };
00122 
00123 #endif //USE_GLPK
00124 
00126 class glpkwrapper4CGMNPMKL: public glpkwrapper
00127 {
00128 public:
00129     int32_t numkernels;
00130 
00131     glpkwrapper4CGMNPMKL();
00132     virtual ~glpkwrapper4CGMNPMKL();
00133     
00134     
00135     virtual void setup(const int32_t numkernels2); 
00136 
00137     // takes a set of alpha^t H alpha and -sum alpha and adds constraint32_t to the working set theta <= \beta^ (1) + -sumalpha  
00138     virtual void addconstraint(const ::std::vector<float64_t> & normw2,
00139             const float64_t sumofpositivealphas); 
00140 
00141     virtual void computeweights(std::vector<float64_t> & weights2); 
00142 
00144     inline virtual const char* get_name() const { return "GLPKWrapper4GMNPMKL"; }
00145     
00146 };  
00147 
00150 class CGMNPMKL : public CMultiClassSVM
00151 {
00152 public:
00153     CGMNPMKL();
00154     CGMNPMKL(float64_t C, CKernel* k, CLabels* lab);
00155     
00156     virtual ~CGMNPMKL();
00157 
00158     virtual bool train();
00159 
00164     virtual inline EClassifierType get_classifier_type() { return CT_GMNPMKL; }
00165     
00166     //returns the subkernelweights or NULL if none such have been computed, caller has to delete the returned pointer
00167     float64_t* getsubkernelweights(int32_t & numweights);
00168     
00169     float64_t thresh; // at what l2 norm of sub kernel weights change to quit
00170     int32_t maxiters; // how many iters of silp at max or <0 to ignore this
00171     int32_t lpwrappertype; // what kind of LP solver: 0 - glpk (default)
00172 
00173 
00174     
00175 protected:
00176     
00177     //inits LP
00178     void lpsetup(const int32_t numkernels);
00179     //sets labels for the svm, creates it
00180     void initsvm();
00181     
00182     //uses implicitly Ckernel* kernel
00183     //inits this class
00184     void init();
00185     
00186     
00187     virtual bool evaluatefinishcriterion(const int32_t numberofsilpiterations);
00188     
00189     CGMNPSVM* svm; //the svm in the silp training
00190     
00191     lpwrapper* lpw; // the lp solver wrapper
00192     
00193     void addingweightsstep( const std::vector<float64_t> & curweights);
00194     //the following two is the actual know how in this class :)
00195     // uses the svm
00196     float64_t getsumofsignfreealphas();
00197     // uses the svm and Ckernel * kernel
00198     float64_t getsquarenormofprimalcoefficients(
00199             const int32_t ind);
00200     
00201     
00202     //numberofsilpiterations
00203     
00204     ::std::vector< std::vector< float64_t> > weightshistory;
00205     
00206     
00207     int32_t numdat,numcl,numker;
00208     
00209 };
00210 
00211 #endif // GMNPMKL_H_

SHOGUN Machine Learning Toolbox - Documentation