CombinedKernel.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) 1999-2008 Soeren Sonnenburg
00008  * Written (W) 1999-2008 Gunnar Raetsch
00009  * Copyright (C) 1999-2008 Fraunhofer Institute FIRST and Max-Planck-Society
00010  */
00011 
00012 #ifndef _COMBINEDKERNEL_H___
00013 #define _COMBINEDKERNEL_H___
00014 
00015 #include "lib/List.h"
00016 #include "lib/io.h"
00017 #include "kernel/Kernel.h"
00018 
00019 #include "features/Features.h"
00020 #include "features/CombinedFeatures.h"
00021 
00038 class CCombinedKernel : public CKernel
00039 {
00040     public:
00047         CCombinedKernel(int32_t size=10, bool append_subkernel_weights=false);
00048 
00056         CCombinedKernel(CCombinedFeatures* l, CCombinedFeatures* r,
00057                 bool append_subkernel_weights=false);
00058 
00059         virtual ~CCombinedKernel();
00060 
00067         virtual bool init(CFeatures* lhs, CFeatures* rhs);
00068 
00070         virtual void cleanup();
00071 
00079         virtual bool load_init(FILE* src) { return false; }
00080 
00088         virtual bool save_init(FILE* dest) { return false; }
00089 
00094         virtual EKernelType get_kernel_type()
00095         {
00096             return K_COMBINED;
00097         }
00098 
00103         virtual EFeatureType get_feature_type()
00104         {
00105             return F_UNKNOWN;
00106         }
00107 
00112         virtual EFeatureClass get_feature_class()
00113         {
00114             return C_COMBINED;
00115         }
00116 
00121         virtual const char* get_name() { return "Combined"; }
00122 
00124         void list_kernels();
00125 
00130         inline CKernel* get_first_kernel()
00131         {
00132             return kernel_list->get_first_element();
00133         }
00134 
00140         inline CKernel* get_first_kernel(CListElement<CKernel*>*&current)
00141         {
00142             return kernel_list->get_first_element(current);
00143         }
00144 
00150         inline CKernel* get_kernel(int32_t idx)
00151         {
00152             CKernel * k = get_first_kernel();
00153             for (int32_t i=1; i<idx; i++)
00154                 k = get_next_kernel(k);
00155             return k;
00156         }
00157 
00162         inline CKernel* get_last_kernel()
00163         {
00164             return kernel_list->get_last_element();
00165         }
00166 
00172         inline CKernel* get_next_kernel(const CKernel* current)
00173         {
00174             ASSERT(kernel_list->get_current_element()==current);
00175             return kernel_list->get_next_element();
00176         }
00177 
00183         inline CKernel* get_next_kernel(CListElement<CKernel*> *&current)
00184         {
00185             return kernel_list->get_next_element(current);
00186         }
00187 
00193         inline bool insert_kernel(CKernel* k)
00194         {
00195             ASSERT(k);
00196             SG_REF(k);
00197 
00198             if (!(k->has_property(KP_LINADD)))
00199                 unset_property(KP_LINADD);
00200 
00201             return kernel_list->insert_element(k);
00202         }
00203 
00209         inline bool append_kernel(CKernel* k)
00210         {
00211             ASSERT(k);
00212             SG_REF(k);
00213 
00214             if (!(k->has_property(KP_LINADD)))
00215                 unset_property(KP_LINADD);
00216 
00217             return kernel_list->append_element(k);
00218         }
00219 
00224         inline bool delete_kernel()
00225         {
00226             CKernel* k=kernel_list->delete_element();
00227             SG_UNREF(k);
00228 
00229             return (k!=NULL);
00230         }
00231 
00236         inline bool get_append_subkernel_weights()
00237         {
00238             return append_subkernel_weights;
00239         }
00240 
00245         inline int32_t get_num_subkernels()
00246         {
00247             if (append_subkernel_weights)
00248             {
00249                 int32_t num_subkernels = 0 ;
00250                 CListElement<CKernel*> *current = NULL ;
00251                 CKernel * kn = get_first_kernel(current) ;
00252                 while(kn)
00253                 {
00254                     num_subkernels += kn->get_num_subkernels() ;
00255                     kn = get_next_kernel(current) ;
00256                 }
00257                 return num_subkernels ;
00258             }
00259             else
00260                 return kernel_list->get_num_elements();
00261         }
00262 
00264         virtual void remove_lhs();
00265 
00267         virtual void remove_rhs();
00268 
00276         virtual bool init_optimization(
00277             int32_t count, int32_t *IDX, float64_t * weights);
00278 
00283         virtual bool delete_optimization();
00284 
00290         virtual float64_t compute_optimized(int32_t idx);
00291 
00298         virtual void compute_batch(
00299             int32_t num_vec, int32_t* vec_idx, float64_t* target,
00300             int32_t num_suppvec, int32_t* IDX, float64_t* alphas,
00301             float64_t factor=1.0);
00302 
00307         static void* compute_optimized_kernel_helper(void* p);
00308 
00313         static void* compute_kernel_helper(void* p);
00314 
00325         void emulate_compute_batch(
00326             CKernel* k, int32_t num_vec, int32_t* vec_idx, float64_t* target,
00327             int32_t num_suppvec, int32_t* IDX, float64_t* weights);
00328 
00334         virtual void add_to_normal(int32_t idx, float64_t weight);
00335 
00337         virtual void clear_normal();
00338 
00344         virtual void compute_by_subkernel(
00345             int32_t idx, float64_t * subkernel_contrib);
00346 
00352         virtual const float64_t* get_subkernel_weights(int32_t& num_weights);
00353 
00359         virtual void set_subkernel_weights(
00360             float64_t* weights, int32_t num_weights);
00361 
00366         virtual void set_optimization_type(EOptimizationType t);
00367 
00369         bool precompute_subkernels();
00370 
00371     protected:
00378         virtual float64_t compute(int32_t x, int32_t y);
00379 
00380     protected:
00382         CList<CKernel*>* kernel_list;
00384         int32_t   sv_count;
00386         int32_t*  sv_idx;
00388         float64_t* sv_weight;
00390         float64_t* subkernel_weights_buffer;
00392         bool append_subkernel_weights;
00393 };
00394 
00395 #endif /* _COMBINEDKERNEL_H__ */

SHOGUN Machine Learning Toolbox - Documentation